diff options
Diffstat (limited to 'src/model/youload_playlist.py')
-rw-r--r-- | src/model/youload_playlist.py | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/src/model/youload_playlist.py b/src/model/youload_playlist.py index a021b83..51f8758 100644 --- a/src/model/youload_playlist.py +++ b/src/model/youload_playlist.py @@ -1,4 +1,5 @@ from pytube import Playlist +from moviepy.editor import VideoFileClip import os def make_alpha_numeric(string: str) -> str: @@ -7,8 +8,8 @@ def make_alpha_numeric(string: str) -> str: class YouLoadPlayList: """A class for download and handling youtube playlists""" - DOWNLOAD_TYPES = ["highest", "lowest", "audio", "720p", "480p", "360p", "240p", "144p"] - DEFAULT_DOWNLOAD_TYPE = "highest" + DOWNLOAD_TYPES = ["default", "highest", "lowest", "mp3", "720p", "480p", "360p", "240p", "144p"] + DEFAULT_DOWNLOAD_TYPE = "default" def __init__(self, link: str): self.yt_playlist = Playlist(link) @@ -34,7 +35,6 @@ class YouLoadPlayList: def download_video(self, video_num: int, use_prefix: bool) -> str: """Download video at 'video_num'""" video = self.yt_playlist.videos[video_num] - video_size = video.streams.get_highest_resolution().filesize # Create prefix. filename_prefix = "" @@ -43,14 +43,25 @@ class YouLoadPlayList: filename_prefix = str(video_num + 1) + " " # Download this fucker. - if self.download_type == "highest": + if self.download_type == "default": + video.streams.first().download(output_path=self.folder_name, filename_prefix=filename_prefix) + elif self.download_type == "highest": video.streams.get_highest_resolution().download(output_path=self.folder_name, filename_prefix=filename_prefix) elif self.download_type == "lowest": video.streams.get_lowest_resolution().download(output_path=self.folder_name, filename_prefix=filename_prefix) - elif self.download_type == "audio": - video.streams.get_audio_only().download(output_path=self.folder_name, filename_prefix=filename_prefix) + elif self.download_type == "mp3": + file_path = video.streams.first().download(output_path=self.folder_name, filename_prefix=filename_prefix) + + # Get video from mp4. + video_audio = VideoFileClip(file_path) + + # Get mp3. + file_path_mp3 = file_path[:-4] + ".mp3" + video_audio.audio.write_audiofile(file_path_mp3) + + os.remove(file_path) else: video.streams.get_by_resolution(self.download_type).download(output_path=self.folder_name, filename_prefix=filename_prefix) - return f"Title: {video.title}, Size: {video_size // (1024 ** 2)} MB" + return f"Title: {video.title}" |