From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Mon, 14 Oct 2024 18:41:38 +0000 (-0400) Subject: abstract the ffmpeg command X-Git-Url: http://git.skullheadx.com/tech/openbsd_html_css/static/gitweb.css?a=commitdiff_plain;h=6251508a88fd41a43a948e66f73f694ca44d121b;p=youtube-downloader.git abstract the ffmpeg command --- diff --git a/README.md b/README.md index ff1d3c4..4117e44 100644 --- 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 diff --git a/ytdl/__pycache__/funcmodule.cpython-312.pyc b/ytdl/__pycache__/funcmodule.cpython-312.pyc index 465528f..1852a60 100644 Binary files a/ytdl/__pycache__/funcmodule.cpython-312.pyc and b/ytdl/__pycache__/funcmodule.cpython-312.pyc differ diff --git a/ytdl/funcmodule.py b/ytdl/funcmodule.py index 155396e..acd2018 100644 --- a/ytdl/funcmodule.py +++ b/ytdl/funcmodule.py @@ -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")