From e1e36604c26784ee772a48b32f7655c13cf53591 Mon Sep 17 00:00:00 2001 From: Vadim Date: Thu, 26 Dec 2024 00:24:17 +0100 Subject: [PATCH] Check if ffmpeg is available Added option for user defined ffmpeg --- README.md | 3 +- ReleaseNotes.md | 1 + src/Settings.py | 2 + src/UltraSinger.py | 37 ++++++++++++++----- .../DeviceDetection/device_detection.py | 4 +- src/modules/common_print.py | 4 +- src/modules/ffmpeg_helper.py | 31 ++++++++++++++++ 7 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 src/modules/ffmpeg_helper.py diff --git a/README.md b/README.md index 4b667b4..0baf3d2 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ This will help me a lot to keep this project alive and improve it. ### Installation * Install Python 3.10 **(older and newer versions has some breaking changes)**. [Download](https://www.python.org/downloads/) -* Also install ffmpeg separately with PATH. [Download](https://www.ffmpeg.org/download.html) +* Also download or install ffmpeg with PATH. [Download](https://www.ffmpeg.org/download.html) * Go to folder `install` and run install script for your OS. * Choose `GPU` if you have an nvidia CUDA GPU. * Choose `CPU` if you don't have an nvidia CUDA GPU. @@ -133,6 +133,7 @@ _Not all options working now!_ --format_version 0.3.0|1.0.0|1.1.0|1.2.0 >> ((default) is 1.2.0) --musescore_path path to MuseScore executable --keep_numbers Transcribe numbers as digits and not words + --ffmpeg Path to ffmpeg and ffprobe executable [yt-dlp] --cookiefile File name where cookies should be read from diff --git a/ReleaseNotes.md b/ReleaseNotes.md index 2a7e9eb..11687c7 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -7,6 +7,7 @@ Date: 2024.12.22 - Optimized whisper language check with confidence - Improved arguments, so you dont need to use true or false - Added interactive mode + - Use user defined ffmpeg path # Version: 0.0.12 Date: 2024.12.19 diff --git a/src/Settings.py b/src/Settings.py index 6772d3c..f10350a 100644 --- a/src/Settings.py +++ b/src/Settings.py @@ -10,6 +10,7 @@ from modules.Ultrastar.ultrastar_txt import FormatVersion @dataclass_json @dataclass class Settings: + APP_VERSION = "0.0.13-dev2" CONFIDENCE_THRESHOLD = 0.6 CONFIDENCE_PROMPT_TIMEOUT = 4 @@ -23,6 +24,7 @@ class Settings: ignore_audio = False input_file_is_ultrastar_txt = False # todo: to process_data keep_cache = False + user_ffmpeg_path = "" # Process data Paths input_file_path = "" diff --git a/src/UltraSinger.py b/src/UltraSinger.py index b82744f..824591b 100644 --- a/src/UltraSinger.py +++ b/src/UltraSinger.py @@ -18,11 +18,11 @@ from modules.Audio.vocal_chunks import ( from modules.Audio.silence_processing import remove_silence_from_transcription_data, mute_no_singing_parts from modules.Speech_Recognition.Whisper import WhisperModel from modules.Audio.separation import DemucsModel - from modules.Audio.convert_audio import convert_audio_to_mono_wav, convert_wav_to_mp3 from modules.Audio.youtube import ( download_from_youtube, ) +from modules.Audio.bpm import get_bpm_from_file from modules.console_colors import ( ULTRASINGER_HEAD, @@ -66,8 +66,8 @@ from modules.musicbrainz_client import search_musicbrainz from modules.sheet import create_sheet from modules.ProcessData import ProcessData, ProcessDataPaths, MediaInfo from modules.DeviceDetection.device_detection import check_gpu_support -from modules.Audio.bpm import get_bpm_from_file from modules.Image.image_helper import save_image +from modules.ffmpeg_helper import is_ffmpeg_available, get_ffmpeg_and_ffprobe_paths from Settings import Settings @@ -152,13 +152,13 @@ def run() -> tuple[str, Score, Score]: print(f"{ULTRASINGER_HEAD} {bright_green_highlighted('Option:')} {cyan_highlighted('Hyphenation will not be applied')}") process_data = InitProcessData() - + process_data.process_data_paths.cache_folder_path = ( os.path.join(settings.output_folder_path, "cache") if settings.cache_override_path is None else settings.cache_override_path ) - + # Create process audio process_data.process_data_paths.processing_audio_path = CreateProcessAudio(process_data) @@ -212,7 +212,7 @@ def run() -> tuple[str, Score, Score]: # Cleanup if not settings.keep_cache: remove_cache_folder(process_data.process_data_paths.cache_folder_path) - + # Print Support print_support() return ultrastar_file_output, simple_score, accurate_score @@ -585,10 +585,27 @@ def main(argv: list[str]) -> None: """Main function""" print_version(settings.APP_VERSION) init_settings(argv) + check_requirements() run() sys.exit() +def check_requirements() -> None: + if not settings.force_cpu: + settings.tensorflow_device, settings.pytorch_device = check_gpu_support() + print(f"{ULTRASINGER_HEAD} ----------------------") + + if not is_ffmpeg_available(settings.user_ffmpeg_path): + print( + f"{ULTRASINGER_HEAD} {red_highlighted('Error:')} {blue_highlighted('FFmpeg')} {red_highlighted('is not available. Provide --ffmpeg ‘path’ or install FFmpeg with PATH')}") + sys.exit(1) + else: + ffmpeg_path, ffprobe_path = get_ffmpeg_and_ffprobe_paths() + print(f"{ULTRASINGER_HEAD} {blue_highlighted('FFmpeg')} - using {red_highlighted(ffmpeg_path)}") + print(f"{ULTRASINGER_HEAD} {blue_highlighted('FFprobe')} - using {red_highlighted(ffprobe_path)}") + + print(f"{ULTRASINGER_HEAD} ----------------------") + def remove_cache_folder(cache_folder_path: str) -> None: """Remove cache folder""" os_helper.remove_folder(cache_folder_path) @@ -668,7 +685,7 @@ def init_settings(argv: list[str]) -> Settings: f"{ULTRASINGER_HEAD} {red_highlighted('Error: Format version')} {blue_highlighted(arg)} {red_highlighted('is not supported.')}" ) sys.exit(1) - elif opt in ("--keep_cache"): + elif opt in ("--keep_cache"): settings.keep_cache = True elif opt in ("--musescore_path"): settings.musescore_path = arg @@ -681,6 +698,8 @@ def init_settings(argv: list[str]) -> Settings: sys.exit() elif opt in ("--cookiefile"): settings.cookiefile = arg + elif opt in ("--ffmpeg"): + settings.user_ffmpeg_path = arg if settings.output_folder_path == "": if settings.input_file_path.startswith("https:"): dirname = os.getcwd() @@ -688,9 +707,6 @@ def init_settings(argv: list[str]) -> Settings: dirname = os.path.dirname(settings.input_file_path) settings.output_folder_path = os.path.join(dirname, "output") - if not settings.force_cpu: - settings.tensorflow_device, settings.pytorch_device = check_gpu_support() - return settings @@ -722,7 +738,8 @@ def arg_options(): "keep_cache=", "musescore_path=", "keep_numbers", - "cookiefile=" + "cookiefile=", + "ffmpeg=" ] return long, short diff --git a/src/modules/DeviceDetection/device_detection.py b/src/modules/DeviceDetection/device_detection.py index 092f295..fcfe0d2 100644 --- a/src/modules/DeviceDetection/device_detection.py +++ b/src/modules/DeviceDetection/device_detection.py @@ -12,11 +12,9 @@ pytorch_gpu_supported = False def check_gpu_support() -> tuple[bool, bool]: """Check worker device (e.g cuda or cpu) supported by tensorflow and pytorch""" - print(f"{ULTRASINGER_HEAD} Checking GPU support for {blue_highlighted('tensorflow')} and {blue_highlighted('pytorch')}.") + print(f"{ULTRASINGER_HEAD} Checking GPU support.") tensorflow_gpu_supported = False - pytorch_gpu_supported = False - gpus = tf.config.list_physical_devices('GPU') if gpus: tensorflow_gpu_supported = True diff --git a/src/modules/common_print.py b/src/modules/common_print.py index f2546c9..5003c9a 100644 --- a/src/modules/common_print.py +++ b/src/modules/common_print.py @@ -55,7 +55,9 @@ def print_help() -> None: --plot Enable creation of plots. Plots are disabled by default. --format_version 0.3.0|1.0.0|1.1.0|1.2.0 >> ((default) is 1.2.0) --musescore_path path to MuseScore executable - + --keep_numbers Transcribe numbers as digits and not words + --ffmpeg Path to ffmpeg and ffprobe executable + [yt-dlp] --cookiefile File name where cookies should be read from and dumped to. diff --git a/src/modules/ffmpeg_helper.py b/src/modules/ffmpeg_helper.py new file mode 100644 index 0000000..19f28a7 --- /dev/null +++ b/src/modules/ffmpeg_helper.py @@ -0,0 +1,31 @@ +"""FFmpeg helper module.""" + +import os +import shutil + + +def __add_ffmpeg_to_path(ffmpeg_path) -> None: + os.environ["PATH"] = os.pathsep.join([os.environ["PATH"], ffmpeg_path]) + + +def get_ffmpeg_and_ffprobe_paths(): + ffmpeg_path = shutil.which("ffmpeg") + ffprobe_path = shutil.which("ffprobe") + + if not ffmpeg_path: + raise FileNotFoundError("FFmpeg executable not found in the system path.") + if not ffprobe_path: + raise FileNotFoundError("FFprobe executable not found in the system path.") + + return ffmpeg_path, ffprobe_path + + +def is_ffmpeg_available(user_path: str = "") -> bool: + if shutil.which("ffmpeg"): + return True + if (path := user_path) and path not in os.environ["PATH"]: + __add_ffmpeg_to_path(path) + if shutil.which("ffmpeg"): + return True + return False +