]> Skullheadx's Git Forge - youtube-downloader.git/commitdiff
abstract the ffmpeg command
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 14 Oct 2024 18:41:38 +0000 (14:41 -0400)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Mon, 14 Oct 2024 18:41:38 +0000 (14:41 -0400)
README.md
ytdl/__pycache__/funcmodule.cpython-312.pyc
ytdl/funcmodule.py

index ff1d3c40ad1ddeb92bb70dbca302ad44084bd3da..4117e4416e7352fd8ef7ffef29730338b575bf79 100644 (file)
--- a/README.md
+++ b/README.md
@@ -12,9 +12,5 @@ downloads the audio and video and stitches it together in the current directory.
 - `-f` - force replace if file exists
 
 # TODO:
-- [x] add video only
-- [x] add audio + video separate
-- [x] add stitched together
-- [x] add force replace
 - [ ] add album name
-- [x] add force replace check for audio and video only
\ No newline at end of file
+- [ ] figure out why -av takes so long compared to -a and -v
\ No newline at end of file
index 465528fb0122506ee47a790457731db06b157330..1852a604a6d2653655055b823cec7fd62ec731fc 100644 (file)
Binary files a/ytdl/__pycache__/funcmodule.cpython-312.pyc and b/ytdl/__pycache__/funcmodule.cpython-312.pyc differ
index 155396e67b21e96eaf7fd5857f6c682959c44ac8..acd2018b8240ac2ed6b6b01690d76cc13fc831e3 100644 (file)
@@ -61,6 +61,25 @@ def download(link, mode, force=False):
         download_double_stream(yt, filename, thumbnail_filename, mode)
 
 
+def convert_add_metadata(input1, input2, output, yt, m1=1, m2=0):
+    command = [
+        'ffmpeg',
+        '-i', input1,
+        '-i', input2,
+        '-map', f'{m1}',
+        '-map', f'{m2}',
+        '-c', 'copy',
+        f'-disposition:v:{m2}', 'attached_pic',
+        '-metadata', f'title={yt.title}',
+        '-metadata', f'artist={yt.author}',
+        '-metadata', f'comment={big_num_format(yt.views) + " views"}',
+        '-metadata', f'date={yt.publish_date}',
+        output + ".mp4",
+        '-y'
+    ]
+    subprocess.run(command)
+
+
 def download_single_stream(yt, filename, thumbnail_filename, mode):
     print(f"Fetching stream for {yt.title}")
     stream = None
@@ -76,24 +95,10 @@ def download_single_stream(yt, filename, thumbnail_filename, mode):
     default_filename = "default " + fix_filename(stream.default_filename)
     stream.download(filename=default_filename, skip_existing=True)
 
-    command = [
-        'ffmpeg',
-        '-i', default_filename,
-        '-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 + ".mp4",
-        '-y'
-    ]
-    subprocess.run(command)
+    print(f"Adding metadata to {yt.title}")
+    convert_add_metadata(default_filename, thumbnail_filename, filename, yt)
 
-    # clean up tmp files
+    print("Removing temporary files")
     os.remove(thumbnail_filename)
     os.remove(default_filename)
 
@@ -112,58 +117,17 @@ def download_double_stream(yt, filename, thumbnail_filename, mode):
     audio_stream.download(filename=audio_default_filename, skip_existing=True)
     video_stream.download(filename=video_default_filename, skip_existing=True)
 
+    print(f"Adding metadata to {yt.title}")
     if mode == '-av':
         for suffix, stream in [(" (audio only)", audio_default_filename), (" (video only)", video_default_filename)]:
-            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)
+            convert_add_metadata(stream, thumbnail_filename, filename + suffix, yt)
     elif mode == '-d':
-        command = [
-            'ffmpeg',
-            '-i', audio_default_filename,
-            '-i', video_default_filename,
-            '-map', '0',
-            '-map', '1',
-            '-c', 'copy',
-            '-disposition:v:1', '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 + "tmp" + ".mp4",
-            '-y'
-        ]
-        subprocess.run(command)
-
-        command = [
-            'ffmpeg',
-            '-i', filename + "tmp" + ".mp4",
-            '-i', thumbnail_filename,
-            '-map', '0',
-            '-map', '1',
-            '-c', 'copy',
-            '-disposition:v:1', 'attached_pic',
-                  filename + ".mp4",
-            '-y'
-        ]
-        subprocess.run(command)
+        convert_add_metadata(audio_default_filename, video_default_filename, filename + "tmp", yt, m1=0, m2=1)
+        convert_add_metadata(filename + "tmp.mp4", thumbnail_filename, filename, yt)
 
-        os.remove(filename + "tmp" + ".mp4")
-
-    # clean up tmp files
+    print("Removing temporary files")
     os.remove(thumbnail_filename)
     os.remove(audio_default_filename)
     os.remove(video_default_filename)
+    if mode == '-d':
+        os.remove(filename + "tmp" + ".mp4")