]> Skullheadx's Git Forge - youtube-downloader.git/commitdiff
thing for everything
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 14 Oct 2024 03:33:26 +0000 (23:33 -0400)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 14 Oct 2024 03:33:26 +0000 (23:33 -0400)
ytdl/__main__.py
ytdl/funcmodule.py

index 7a602908deee9d6c80132c252261a201e83d78c4..aa323d0dfc3edc7138370b40df38070ea1a2a6a4 100644 (file)
@@ -1,5 +1,5 @@
 import sys
-from .funcmodule import check_playlist, get_audio_metadata_stream, download_audio_stream
+from .funcmodule import check_playlist, get_audio_metadata_stream, download_audio_stream, get_and_download
 import concurrent.futures
 
 
@@ -46,16 +46,8 @@ def main():
         # Use ThreadPoolExecutor to run downloads concurrently
         with concurrent.futures.ThreadPoolExecutor() as executor:
             # Schedule the download_audio_stream function for each audio stream
-            futures = {executor.submit(download_audio_stream, stream, metadata): stream for stream, metadata in
-                       zip(audio_streams, metadata_list)}
+            futures = {executor.submit(get_and_download, link): link for link in links}
 
-            # Optionally, you can wait for completion and handle exceptions
-            for future in concurrent.futures.as_completed(futures):
-                stream = futures[future]
-                try:
-                    future.result()  # This will raise an exception if the function raised one
-                except Exception as e:
-                    print(f"Error downloading {stream.title}: {e}")
     elif mode == "-v":
         pass
     elif mode == "-av":
index 02fe6bddd2b4a6abd38dbb163feb821d0e89cd52..18c837edd4105e02c99e40c30ceb32f124be0ec0 100644 (file)
@@ -64,3 +64,46 @@ def download_audio_stream(audio_stream, metadata):
     # clean up tmp files
     os.remove(thumbnail_filename)
     os.remove(audio_stream.default_filename)
+
+
+def get_and_download(link):
+    yt = YouTube(link)
+    yt.check_availability()
+    print(f"Fetching stream for {yt.title}")
+    assert len(yt.streams.filter(only_audio=True)) > 0, "No available audio streams"
+    audio_stream = yt.streams.filter(only_audio=True).order_by("abr").last()
+    metadata = {
+        "title": yt.title,
+        "artist": yt.author,
+        "thumbnail_url": yt.thumbnail_url,
+        "publish_date": yt.publish_date,
+        "views": yt.views
+    }
+
+    print(f"Downloading audio stream for {audio_stream.title}")
+    audio_stream.download()
+
+    # create thumbnail file
+    data = requests.get(metadata["thumbnail_url"]).content
+    thumbnail_filename = f'{audio_stream.title}.jpg'
+    with open(thumbnail_filename, 'wb') as f:
+        f.write(data)
+
+    command = [
+        'ffmpeg',
+        '-i', audio_stream.default_filename,
+        '-i', thumbnail_filename,
+        '-map', '0',
+        '-map', '1',
+        '-metadata', f'title={audio_stream.title}',
+        '-metadata', f'artist={metadata["artist"]}',
+        '-metadata', f'date={metadata["publish_date"]}',
+        '-metadata', f'comment={big_num_format(metadata["views"]) + " views"}',
+        audio_stream.title + ".mp4",
+        '-y'
+    ]
+    subprocess.run(command)
+
+    # clean up tmp files
+    os.remove(thumbnail_filename)
+    os.remove(audio_stream.default_filename)
\ No newline at end of file