from pytubefix import YouTube, Playlist
+from pytubefix.exceptions import VideoUnavailable
import ffmpeg
target_res = 0
target_abr=0
+failed_download = set()
+
if __name__ == "__main__":
# get list of links from file
for link in links:
target_res = 0
target_abr = 0
- success = True
- yt = YouTube(link)
- video_streams = []
- while len(video_streams) == 0:
- video_streams = yt.streams.filter(file_extension='mp4', res=RES[target_res]) # find available streams
- if target_res + 1 < len(RES):
- target_res = target_res + 1
- else:
- success = False
- break
- if not success:
- print(f"Unable to find video stream for {yt.title}")
- break
- vstream = video_streams[0]
-
-
- # audio
- audio_streams = []
- while len(audio_streams) == 0:
- audio_streams = yt.streams.filter(only_audio=True, abr=ABR[target_abr]) # find available streams
+ video_success = True
+ audio_success = True
+
+ try:
+ yt = YouTube(link)
+ yt.streams
+
+ except VideoUnavailable:
+ print(f'Video {link} is unavaialable, skipping.')
+ failed_download.add(((yt.title, link)))
+ else:
+ video_streams = []
+ while len(video_streams) == 0:
+ video_streams = yt.streams.filter(file_extension='mp4', res=RES[target_res]) # find available streams
+ if target_res + 1 < len(RES):
+ target_res = target_res + 1
+ else:
+ video_success = False
+ break
+ if not video_success:
+ print(f"Unable to find video stream for {yt.title}")
+ failed_download.add(((yt.title, link)))
- if target_abr + 1 < len(RES):
- target_abr = target_abr + 1
- else:
- success = False
break
- if not success:
- print(f"Unable to find audio stream for {yt.title}")
- break
- astream = audio_streams[0]
+ vstream = video_streams[0]
+
+ # audio
+ audio_streams = []
+ while len(audio_streams) == 0:
+ audio_streams = yt.streams.filter(only_audio=True, abr=ABR[target_abr]) # find available streams
+
+ if target_abr + 1 < len(RES):
+ target_abr = target_abr + 1
+ else:
+ audio_success = False
+ break
+ if not audio_success:
+ print(f"Unable to find audio stream for {yt.title}")
+ failed_download.add(((yt.title, link)))
+ break
+ astream = audio_streams[0]
- vstream.download(output_path="downloaded/video_only")
- astream.download(output_path="downloaded/audio_only")
-
- input_video = ffmpeg.input(f"downloaded/video_only/{vstream.default_filename}")
- input_audio = ffmpeg.input(f"downloaded/audio_only/{astream.default_filename}")
-
- ffmpeg.concat(input_video, input_audio, v=1, a=1).output(f'downloaded/{yt.title}.mp4').run()
+ vstream.download(output_path="downloaded/video_only")
+ astream.download(output_path="downloaded/audio_only")
+ input_video = ffmpeg.input(f"downloaded/video_only/{vstream.default_filename}")
+ input_audio = ffmpeg.input(f"downloaded/audio_only/{astream.default_filename}")
+ ffmpeg.concat(input_video, input_audio, v=1, a=1).output(f'downloaded/{yt.title}.mp4').run()
+print("Failed Downloading:")
+print(failed_download)