I am having difficulty with a side project I am working on. The goal of the program is to be able to do a few functions with a youtube video or playlist link.
- download a youtube video and audio and merge them.
- download a youtube playlist's video and audio and merge them.
- download a youtube video's audio only.
- download a youtube playlist's audio only.
My main issues arise with the 2. function, downloading a playlist with video and audio and merging them. The program is able to complete function 1 successfully (download a video and audio and merge to one file), and function 3 successfully (download video and output just audio file) Function 4 hasn't been implemented at all yet.
The program is broken into 2 main files: youtube_downloader.py and main.pyThe errors I receive from output of running function 2 are listed after the code below
main.py
from youtube_downloader import download_playlist,input_links,download_video,convert_to_mp3,convert_to_mp4from moviepy.editor import VideoFileClip, AudioFileClip, concatenate_videoclipsimport osprint("Load Complete\n")print('''What would you like to do?(1) Download a YouTube video and audio(2) Download a YouTube Playlist (video and audio)(3) Download a YouTube Video's audio only(4) Download a Youtube Playlist's audio only(q) Quit''')done = Falsewhile done == False: #ask user for choice choice = input("Choice: ") if choice == "1" or choice == "2": # Sets Quality Option of video(s) quality = input("Please choose a quality (low or 0, medium or 1, high or 2, very high or 3):") #download videos manually if choice == "1": links = input_links() print('Download has been started') for link in links: filename = download_video(link, quality) convert_to_mp4(filename) print("Download finished!") #download a playlist if choice == "2": link = input("Enter the link to the playlist: ") print("Downloading playlist...") filenames = download_playlist(link, quality) print("Download finished! Beginning conversion...") **################################################################# for file in os.listdir('./Downloaded/'): convert_to_mp4(filenames) #################################################################** print("Conversion finished!") elif choice == "3": links = input_links() for link in links: print("Downloading...") filename = download_video(link, 'low') print("Converting...") convert_to_mp3(filename) os.remove(filename) elif choice == "4": pass #TODO: add option 4 code elif choice == "q" or choice == "Q": done = True print("Goodbye") else: print("Invalid input!")youtube_downloader.py
import pytubefrom pytube import YouTube, Playlistfrom pytube.cli import on_progressfrom moviepy.editor import VideoFileClip, AudioFileClipimport os"""Downloads video to a 'Downloaded' folder in the same dir as the program."""def download_video(url, resolution): itag = choose_resolution(resolution) video = YouTube(url,on_progress_callback=on_progress) stream = video.streams.get_by_itag(itag) try: os.mkdir('./Downloaded/') except: pass stream.download(output_path='./Downloaded/') return f'./Downloaded/{stream.default_filename}'def download_videos(urls, resolution): for url in urls: download_video(url, resolution)def download_playlist(url, resolution): playlist = Playlist(url) download_videos(playlist.video_urls, resolution)def choose_resolution(resolution): if resolution in ["low", "360", "360p","0"]: itag = 18 elif resolution in ["medium", "720", "720p", "hd","1"]: itag = 22 elif resolution in ["high", "1080", "1080p", "fullhd", "full_hd", "full hd","2"]: itag = 137 elif resolution in ["very high", "2160", "2160p", "4K", "4k","3"]: itag = 313 else: itag = 18 return itagdef input_links(): print("Enter the links of the videos (end by entering 'stop' or 0):") links = [] link = "" while link != "0" and link.lower() != "stop": link = input("video_url or \"stop\": ") links.append(link) if len(links)==1: print("No links were inputed") exit() links.pop() return linksdef convert_to_mp3(filename): clip = VideoFileClip(filename) clip.audio.write_audiofile(filename[:-3] +"mp3") clip.close()def convert_to_mp4(filename): video_clip = VideoFileClip(filename) audio_clip = AudioFileClip(filename) final_clip = video_clip.set_audio(audio_clip) final_clip.write_videofile(filename[:-3] +"mp4") final_clip.close()Error output
Traceback (most recent call last): File "C:\Users\ptcoo\Documents\youtube-downloader-converter\main.py", line 44, in <module> convert_to_mp4(filenames) File "C:\Users\ptcoo\Documents\youtube-downloader-converter\youtube_downloader.py", line 69, in convert_to_mp4 video_clip = VideoFileClip(filename) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ptcoo\AppData\Roaming\Python\Python312\site-packages\moviepy\video\io\VideoFileClip.py", line 88, in __init__ self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ptcoo\AppData\Roaming\Python\Python312\site-packages\moviepy\video\io\ffmpeg_reader.py", line 35, in __init__ infos = ffmpeg_parse_infos(filename, print_infos, check_duration, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ptcoo\AppData\Roaming\Python\Python312\site-packages\moviepy\video\io\ffmpeg_reader.py", line 244, in ffmpeg_parse_infos is_GIF = filename.endswith('.gif') ^^^^^^^^^^^^^^^^^AttributeError: 'NoneType' object has no attribute 'endswith'