From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Mon, 14 Oct 2024 03:33:26 +0000 (-0400) Subject: thing for everything X-Git-Url: http://git.skullheadx.com/nixos/static/gitweb.css?a=commitdiff_plain;h=dc884c0826329c35c92541ee283eaf2ce0a663ef;p=youtube-downloader.git thing for everything --- diff --git a/ytdl/__main__.py b/ytdl/__main__.py index 7a60290..aa323d0 100644 --- a/ytdl/__main__.py +++ b/ytdl/__main__.py @@ -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": diff --git a/ytdl/funcmodule.py b/ytdl/funcmodule.py index 02fe6bd..18c837e 100644 --- a/ytdl/funcmodule.py +++ b/ytdl/funcmodule.py @@ -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