]> Skullheadx's Git Forge - youtube-downloader.git/commitdiff
`-av' works now
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 14 Oct 2024 18:00:18 +0000 (14:00 -0400)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 14 Oct 2024 18:00:18 +0000 (14:00 -0400)
ytdl/__main__.py
ytdl/__pycache__/__main__.cpython-312.pyc
ytdl/__pycache__/funcmodule.cpython-312.pyc
ytdl/funcmodule.py

index 8c7b198d648ff7c6c8cea0274f8397ce7d59b3bc..bc863695410b9382658edabe5a7233ead2b6abda 100644 (file)
@@ -1,5 +1,5 @@
 import sys
-from .funcmodule import check_playlist, get_and_download
+from .funcmodule import check_playlist, download
 import concurrent.futures
 
 
@@ -32,7 +32,7 @@ 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(get_and_download, link, mode, force): link for link in links}
+        futures = {executor.submit(download, link, mode, force): link for link in links}
 
 
 if __name__ == '__main__':
index 500e48998f4c7f9e96dca406be80ceaf58a9cef1..0a297c4dd31c12fa39efaaabe4bbf139438379f6 100644 (file)
Binary files a/ytdl/__pycache__/__main__.cpython-312.pyc and b/ytdl/__pycache__/__main__.cpython-312.pyc differ
index 8d42a165efac31556f9e183f8406eb9620c507fb..58dc6300ea2845b9197da7e2fb4f9647fbdbdb5f 100644 (file)
Binary files a/ytdl/__pycache__/funcmodule.cpython-312.pyc and b/ytdl/__pycache__/funcmodule.cpython-312.pyc differ
index bc52f5e6624f702d054a13a4897c86aee5265ceb..8135523a9775defa2e09f741cb6a452ad6696f42 100644 (file)
@@ -35,7 +35,8 @@ def download_thumbnail(thumbnail_url, thumbnail_filename):
     with open(thumbnail_filename, 'wb') as f:
         f.write(data)
 
-def get_and_download(link, mode, force=False):
+
+def download(link, mode, force=False):
     yt = YouTube(link)
     filename = fix_filename(yt.title)
     if (filename + '.mp4' in glob.glob("*.mp4")) and not force:
@@ -43,6 +44,16 @@ def get_and_download(link, mode, force=False):
         return
     yt.check_availability()
 
+    thumbnail_filename = f'{filename}.jpg'
+    download_thumbnail(yt.thumbnail_url, thumbnail_filename)
+
+    if mode == '-a' or mode == '-v':
+        download_single_stream(yt, filename,thumbnail_filename, mode)
+    elif mode == '-av' or mode == '-d':
+        download_double_stream(yt, filename, thumbnail_filename, mode)
+
+
+def download_single_stream(yt, filename, thumbnail_filename, mode):
     print(f"Fetching stream for {yt.title}")
     stream = None
     if mode == "-a":
@@ -57,9 +68,6 @@ def get_and_download(link, mode, force=False):
     default_filename = "default " + fix_filename(stream.default_filename)
     stream.download(filename=default_filename, skip_existing=True)
 
-    thumbnail_filename = f'{filename}.jpg'
-    download_thumbnail(yt.thumbnail_url, thumbnail_filename)
-
     command = [
         'ffmpeg',
         '-i', default_filename,
@@ -80,3 +88,45 @@ def get_and_download(link, mode, force=False):
     # clean up tmp files
     os.remove(thumbnail_filename)
     os.remove(default_filename)
+
+
+def download_double_stream(yt, filename,thumbnail_filename, mode):
+    print(f"Fetching streams 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()
+    assert len(yt.streams.filter(only_video=True)) > 0, "No available video streams"
+    video_stream = yt.streams.filter(only_video=True).order_by("resolution").last()
+
+    print(f"Downloading streams for {yt.title}")
+    audio_default_filename = "default audio " + fix_filename(audio_stream.default_filename)
+    video_default_filename = "default video " + fix_filename(video_stream.default_filename)
+
+    audio_stream.download(filename=audio_default_filename, skip_existing=True)
+    video_stream.download(filename=video_default_filename, skip_existing=True)
+
+    if mode == '-av':
+        for suffix, stream in [(" (audio only)", audio_default_filename), (" (video only)", video_default_filename)]:
+            print(suffix, stream, "jahdnfjlkahwejklfaw")
+
+            command = [
+                'ffmpeg',
+                '-i', stream,
+                '-i', thumbnail_filename,
+                '-map', '1',
+                '-map', '0',
+                '-c', 'copy',
+                '-disposition:v:0', 'attached_pic',
+                '-metadata', f'title={filename}',
+                '-metadata', f'artist={yt.author}',
+                '-metadata', f'comment={big_num_format(yt.views) + " views"}',
+                '-metadata', f'date={yt.publish_date}',
+                filename + suffix + ".mp4",
+                '-y'
+            ]
+            subprocess.run(command)
+
+    # clean up tmp files
+    os.remove(thumbnail_filename)
+    os.remove(audio_default_filename)
+    os.remove(video_default_filename)
+