From d0c32c84778b77036c8d5fb2050411c542eeff05 Mon Sep 17 00:00:00 2001 From: Benedikt Wagener Date: Fri, 13 Dec 2024 11:47:26 +0100 Subject: [PATCH] add cookiefile option and pass it to yt-dlp --- README.md | 8 ++++++++ src/Settings.py | 3 +++ src/UltraSinger.py | 7 +++++-- src/modules/Audio/youtube.py | 23 ++++++++++++++--------- src/modules/common_print.py | 3 +++ 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d7b6595..ea31291 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,9 @@ _Not all options working now!_ --plot True|False >> ((default) is False) --format_version 0.3.0|1.0.0|1.1.0 >> ((default) is 1.0.0) --musescore_path path to MuseScore executable + + [yt-dlp] + --cookiefile File name where cookies should be read from [device] --force_cpu True|False >> ((default) is False) All steps will be forced to cpu @@ -157,6 +160,11 @@ For standard use, you only need to use [opt]. All other options are optional. -i https://www.youtube.com/watch?v=BaW_jenozKc ``` +Note that if you run into a yt-dlp error such as `Sign in to confirm you’re not a bot. This helps protect our community` ([yt-dlp issue](https://github.com/yt-dlp/yt-dlp/issues/10128)) you can follow these steps: + +* generate a cookies.txt file with [yt-dlp](https://github.com/yt-dlp/yt-dlp/wiki/Installation) `yt-dlp --cookies cookies.txt --cookies-from-browser firefox` +* then pass the cookies.txt to UltraSinger `--cookiefile cookies.txt` + #### UltraStar (re-pitch) This re-pitch the audio and creates a new txt file. diff --git a/src/Settings.py b/src/Settings.py index 16d3602..e0121bb 100644 --- a/src/Settings.py +++ b/src/Settings.py @@ -54,6 +54,9 @@ class Settings: # MuseScore musescore_path = None + # yt-dlp + cookiefile = None + # UltraSinger Evaluation Configuration test_songs_input_folder = None cache_override_path = None diff --git a/src/UltraSinger.py b/src/UltraSinger.py index 9d8e4d0..01f189a 100644 --- a/src/UltraSinger.py +++ b/src/UltraSinger.py @@ -335,7 +335,7 @@ def InitProcessData(): settings.output_folder_path, process_data.process_data_paths.audio_output_file_path, process_data.media_info, - ) = download_from_youtube(settings.input_file_path, settings.output_folder_path) + ) = download_from_youtube(settings.input_file_path, settings.output_folder_path, settings.cookiefile) else: # Audio File print(f"{ULTRASINGER_HEAD} {gold_highlighted('full automatic mode')}") @@ -647,6 +647,8 @@ def init_settings(argv: list[str]) -> Settings: settings.keep_cache = arg elif opt in ("--musescore_path"): settings.musescore_path = arg + elif opt in ("--cookiefile"): + settings.cookiefile = arg if settings.output_folder_path == "": if settings.input_file_path.startswith("https:"): dirname = os.getcwd() @@ -684,7 +686,8 @@ def arg_options(): "force_crepe_cpu=", "format_version=", "keep_cache", - "musescore_path=" + "musescore_path=", + "cookiefile=", ] return long, short diff --git a/src/modules/Audio/youtube.py b/src/modules/Audio/youtube.py index 5d959ab..50a29b5 100644 --- a/src/modules/Audio/youtube.py +++ b/src/modules/Audio/youtube.py @@ -15,10 +15,12 @@ from modules.Image.image_helper import crop_image_to_square from modules.musicbrainz_client import get_music_infos -def get_youtube_title(url: str) -> tuple[str, str]: +def get_youtube_title(url: str, cookiefile: str = None) -> tuple[str, str]: """Get the title of the YouTube video""" - ydl_opts = {} + ydl_opts = { + "cookiefile": cookiefile, + } with yt_dlp.YoutubeDL(ydl_opts) as ydl: result = ydl.extract_info( url, download=False # We just want to extract the info @@ -31,7 +33,7 @@ def get_youtube_title(url: str) -> tuple[str, str]: return result["channel"].strip(), result["title"].strip() -def __download_youtube_audio(url: str, clear_filename: str, output_path: str): +def __download_youtube_audio(url: str, clear_filename: str, output_path: str, cookiefile: str = None): """Download audio from YouTube""" print(f"{ULTRASINGER_HEAD} Downloading Audio") @@ -41,18 +43,20 @@ def __download_youtube_audio(url: str, clear_filename: str, output_path: str): "postprocessors": [ {"key": "FFmpegExtractAudio", "preferredcodec": "mp3"} ], + "cookiefile": cookiefile, } __start_download(ydl_opts, url) -def __download_youtube_thumbnail(url: str, clear_filename: str, output_path: str) -> str: +def __download_youtube_thumbnail(url: str, clear_filename: str, output_path: str, cookiefile: str = None) -> str: """Download thumbnail from YouTube""" print(f"{ULTRASINGER_HEAD} Downloading thumbnail") ydl_opts = { "skip_download": True, "writethumbnail": True, + "cookiefile": cookiefile, } thumbnail_url = download_and_convert_thumbnail(ydl_opts, url, clear_filename, output_path) @@ -78,13 +82,14 @@ def download_and_convert_thumbnail(ydl_opts, url: str, clear_filename: str, outp return "" -def __download_youtube_video(url: str, clear_filename: str, output_path: str) -> None: +def __download_youtube_video(url: str, clear_filename: str, output_path: str, cookiefile: str = None) -> None: """Download video from YouTube""" print(f"{ULTRASINGER_HEAD} Downloading Video") ydl_opts = { "format": "bestvideo[ext=mp4]/mp4", "outtmpl": output_path + "/" + clear_filename + ".mp4", + "cookiefile": cookiefile, } __start_download(ydl_opts, url) @@ -98,9 +103,9 @@ def __start_download(ydl_opts, url: str) -> None: raise Exception("Download failed with error: " + str(errors)) -def download_from_youtube(input_file_path: str, output_folder_path: str) -> tuple[str, str, str, MediaInfo]: +def download_from_youtube(input_file_path: str, output_folder_path: str, cookiefile: str = None) -> tuple[str, str, str, MediaInfo]: """Download from YouTube""" - (artist, title) = get_youtube_title(input_file_path) + (artist, title) = get_youtube_title(input_file_path, cookiefile) # Get additional data for song (title_info, artist_info, year_info, genre_info) = get_music_infos( @@ -116,8 +121,8 @@ def download_from_youtube(input_file_path: str, output_folder_path: str) -> tupl song_output = os.path.join(output_folder_path, basename_without_ext) song_output = get_unused_song_output_dir(song_output) os_helper.create_folder(song_output) - __download_youtube_audio(input_file_path, basename_without_ext, song_output) - __download_youtube_video(input_file_path, basename_without_ext, song_output) + __download_youtube_audio(input_file_path, basename_without_ext, song_output, cookiefile) + __download_youtube_video(input_file_path, basename_without_ext, song_output, cookiefile) thumbnail_url = __download_youtube_thumbnail( input_file_path, basename_without_ext, song_output ) diff --git a/src/modules/common_print.py b/src/modules/common_print.py index 6e25136..32f1623 100644 --- a/src/modules/common_print.py +++ b/src/modules/common_print.py @@ -55,6 +55,9 @@ def print_help() -> None: --format_version 0.3.0|1.0.0|1.1.0 >> ((default) is 1.0.0) --musescore_path path to MuseScore executable + [yt-dlp] + --cookiefile File name where cookies should be read from and dumped to. + [device] --force_cpu True|False >> ((default) is False) All steps will be forced to cpu --force_whisper_cpu True|False >> ((default) is False) Only whisper will be forced to cpu