]> Skullheadx's Git Forge - youtube-downloader.git/commitdiff
i dont know
authorSkullheadx <admonty1@gmail.com>
Sun, 13 Oct 2024 21:43:55 +0000 (17:43 -0400)
committerSkullheadx <admonty1@gmail.com>
Sun, 13 Oct 2024 21:43:55 +0000 (17:43 -0400)
main.py

diff --git a/main.py b/main.py
index c20b94f7af2b9f7ea50d044236e1b0b7cc09bb8b..5f5b3a1847afb40feaddd530ca936cd006d2d72f 100644 (file)
--- a/main.py
+++ b/main.py
@@ -1,4 +1,5 @@
 from pytubefix import YouTube, Playlist
+from pytubefix.exceptions import VideoUnavailable
 import ffmpeg
 
 
@@ -7,6 +8,8 @@ ABR = ["160kbps", "128kbps", "70kbps", "50kbps", "48kbps"]
 target_res = 0
 target_abr=0
 
+failed_download = set()
+
 if __name__ == "__main__":
 
     # get list of links from file
@@ -28,46 +31,57 @@ if __name__ == "__main__":
     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)