From e05c93bc4d3066817ff997d592c108a53121cfb8 Mon Sep 17 00:00:00 2001 From: Vadim Date: Sun, 8 Feb 2026 23:15:11 +0100 Subject: [PATCH] Support video as input --- ReleaseNotes.md | 3 +- src/Settings.py | 2 +- src/UltraSinger.py | 58 ++++++++++++++++++++++++++---------- src/modules/Audio/youtube.py | 22 ++++---------- src/modules/ffmpeg_helper.py | 56 ++++++++++++++++++++++++++++++++-- 5 files changed, 105 insertions(+), 36 deletions(-) diff --git a/ReleaseNotes.md b/ReleaseNotes.md index 8082d8d..a5cc8da 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -1,4 +1,4 @@ -# Version: 0.0.13dev10 +# Version: 0.0.13dev11 Date: 2026.02.08 - Changes: - Download Cover from MusicBrainz @@ -11,6 +11,7 @@ Date: 2026.02.08 - Show GPU Name and VRAM - No linebreak before the last 'E' word for UltraStar txt files - Download yt video/audio only once + - Support for video as input # Version: 0.0.12 Date: 2024.12.19 diff --git a/src/Settings.py b/src/Settings.py index 31a78bb..c7a8bc0 100644 --- a/src/Settings.py +++ b/src/Settings.py @@ -11,7 +11,7 @@ from modules.Ultrastar.ultrastar_txt import FormatVersion @dataclass class Settings: - APP_VERSION = "0.0.13-dev10" + APP_VERSION = "0.0.13-dev11" CONFIDENCE_THRESHOLD = 0.6 CONFIDENCE_PROMPT_TIMEOUT = 4 diff --git a/src/UltraSinger.py b/src/UltraSinger.py index 8c4e120..67da004 100644 --- a/src/UltraSinger.py +++ b/src/UltraSinger.py @@ -67,7 +67,12 @@ from modules.sheet import create_sheet from modules.ProcessData import ProcessData, ProcessDataPaths, MediaInfo from modules.DeviceDetection.device_detection import check_gpu_support from modules.Image.image_helper import save_image -from modules.ffmpeg_helper import is_ffmpeg_available, get_ffmpeg_and_ffprobe_paths +from modules.ffmpeg_helper import ( + is_ffmpeg_available, + get_ffmpeg_and_ffprobe_paths, + is_video_file, + separate_audio_video, +) from Settings import Settings @@ -359,7 +364,7 @@ def InitProcessData(): process_data.media_info ) = download_from_youtube(settings.input_file_path, settings.output_folder_path, settings.cookiefile) else: - # Audio File + # Audio/Video File print(f"{ULTRASINGER_HEAD} {gold_highlighted('Full Automatic Mode')}") process_data = ProcessData() ( @@ -367,7 +372,7 @@ def InitProcessData(): settings.output_folder_path, process_data.process_data_paths.audio_output_file_path, process_data.media_info, - ) = infos_from_audio_input_file() + ) = infos_from_audio_video_input_file() return process_data @@ -517,8 +522,8 @@ def transcribe_audio(cache_folder_path: str, processing_audio_path: str) -> Tran return transcription_result -def infos_from_audio_input_file() -> tuple[str, str, str, MediaInfo]: - """Infos from audio input file""" +def infos_from_audio_video_input_file() -> tuple[str, str, str, MediaInfo]: + """Infos from audio/video input file""" basename = os.path.basename(settings.input_file_path) basename_without_ext = os.path.splitext(basename)[0] @@ -530,27 +535,50 @@ def infos_from_audio_input_file() -> tuple[str, str, str, MediaInfo]: song_info = search_musicbrainz(title, artist) basename_without_ext = f"{song_info.artist} - {song_info.title}" - extension = os.path.splitext(basename)[1] - basename = f"{basename_without_ext}{extension}" song_folder_output_path = os.path.join(settings.output_folder_path, basename_without_ext) song_folder_output_path = get_unused_song_output_dir(song_folder_output_path) os_helper.create_folder(song_folder_output_path) - os_helper.copy(settings.input_file_path, song_folder_output_path) - os_helper.rename( - os.path.join(song_folder_output_path, os.path.basename(settings.input_file_path)), - os.path.join(song_folder_output_path, basename), - ) + + extension = os.path.splitext(basename)[1] + if is_video_file(settings.input_file_path): + print(f"{ULTRASINGER_HEAD} {gold_highlighted('Video file detected - separating audio and video')}") + + video_with_audio_basename = f"{basename_without_ext}{extension}" + video_with_audio_path = os.path.join(song_folder_output_path, video_with_audio_basename) + os_helper.copy(settings.input_file_path, video_with_audio_path) + + # Separate audio and video + ultrastar_audio_input_path, final_video_path = separate_audio_video( + video_with_audio_path, basename_without_ext, song_folder_output_path + ) + else: + # Audio file + basename_with_ext = f"{basename_without_ext}{extension}" + os_helper.copy(settings.input_file_path, song_folder_output_path) + os_helper.rename( + os.path.join(song_folder_output_path, os.path.basename(settings.input_file_path)), + os.path.join(song_folder_output_path, basename_with_ext), + ) + ultrastar_audio_input_path = os.path.join(song_folder_output_path, basename_with_ext) + # Todo: Read ID3 tags if song_info.cover_image_data is not None: save_image(song_info.cover_image_data, basename_without_ext, song_folder_output_path) - ultrastar_audio_input_path = os.path.join(song_folder_output_path, basename) - real_bpm = get_bpm_from_file(settings.input_file_path) + + real_bpm = get_bpm_from_file(ultrastar_audio_input_path) return ( basename_without_ext, song_folder_output_path, ultrastar_audio_input_path, - MediaInfo(artist=song_info.artist, title=song_info.title, year=song_info.year, genre=song_info.genres, bpm=real_bpm, cover_url=song_info.cover_url), + MediaInfo( + artist=song_info.artist, + title=song_info.title, + year=song_info.year, + genre=song_info.genres, + bpm=real_bpm, + cover_url=song_info.cover_url, + ), ) diff --git a/src/modules/Audio/youtube.py b/src/modules/Audio/youtube.py index 9cbfbb4..8e91980 100644 --- a/src/modules/Audio/youtube.py +++ b/src/modules/Audio/youtube.py @@ -10,7 +10,7 @@ from modules.Audio.bpm import get_bpm_from_file from modules.console_colors import ULTRASINGER_HEAD from modules.Image.image_helper import save_image from modules.musicbrainz_client import search_musicbrainz -from modules.ffmpeg_helper import get_ffmpeg_and_ffprobe_paths, extract_audio, remove_audio_from_video +from modules.ffmpeg_helper import separate_audio_video def get_youtube_title(url: str, cookiefile: str = None) -> tuple[str, str]: @@ -103,22 +103,10 @@ def download_from_youtube(input_url: str, output_folder_path: str, cookiefile: s ) video_with_audio_path = os.path.join(song_output, f"{basename_without_ext}.{video_ext}") - ffmpeg_path, _ = get_ffmpeg_and_ffprobe_paths() - - print(f"{ULTRASINGER_HEAD} Extracting audio from video") - audio_file_path = os.path.join(song_output, f"{basename_without_ext}.m4a") - extract_audio(video_with_audio_path, audio_file_path, ffmpeg_path) - - print(f"{ULTRASINGER_HEAD} Creating video without audio") - video_only_path = os.path.join(song_output, f"{basename_without_ext}_video.mp4") - remove_audio_from_video(video_with_audio_path, video_only_path, ffmpeg_path) - - # Remove original video with audio - os.remove(video_with_audio_path) - - # Rename video without audio to original name - final_video_path = os.path.join(song_output, f"{basename_without_ext}.mp4") - os.rename(video_only_path, final_video_path) + # Separate audio and video + audio_file_path, final_video_path = separate_audio_video( + video_with_audio_path, basename_without_ext, song_output + ) if song_info.cover_url is not None and song_info.cover_image_data is not None: cover_url = song_info.cover_url diff --git a/src/modules/ffmpeg_helper.py b/src/modules/ffmpeg_helper.py index 4eea311..420bcfe 100644 --- a/src/modules/ffmpeg_helper.py +++ b/src/modules/ffmpeg_helper.py @@ -31,8 +31,10 @@ def is_ffmpeg_available(user_path: str = "") -> bool: return False -def extract_audio(input_video_path: str, output_audio_path: str, ffmpeg_path: str) -> None: +def extract_audio(input_video_path: str, output_audio_path: str) -> None: """Extract audio from video file in original format without re-encoding""" + ffmpeg_path, _ = get_ffmpeg_and_ffprobe_paths() + cmd = [ ffmpeg_path, "-i", @@ -49,8 +51,9 @@ def extract_audio(input_video_path: str, output_audio_path: str, ffmpeg_path: st raise Exception(f"FFmpeg audio extraction failed: {result.stderr}") -def remove_audio_from_video(input_video_path: str, output_video_path: str, ffmpeg_path: str) -> None: +def remove_audio_from_video(input_video_path: str, output_video_path: str) -> None: """Remove audio from video file without re-encoding video""" + ffmpeg_path, _ = get_ffmpeg_and_ffprobe_paths() cmd = [ ffmpeg_path, @@ -66,3 +69,52 @@ def remove_audio_from_video(input_video_path: str, output_video_path: str, ffmpe result = subprocess.run(cmd, capture_output=True, text=True) if result.returncode != 0: raise Exception(f"FFmpeg audio removal failed: {result.stderr}") + + +def is_video_file(file_path: str) -> bool: + """Check if file contains video streams using ffprobe""" + try: + _, ffprobe_path = get_ffmpeg_and_ffprobe_paths() + + cmd = [ + ffprobe_path, + "-v", "error", + "-select_streams", "v:0", + "-show_entries", "stream=codec_type", + "-of", "default=noprint_wrappers=1:nokey=1", + file_path + ] + + result = subprocess.run(cmd, capture_output=True, text=True) + return result.returncode == 0 and result.stdout.strip() == "video" + except Exception: + return False + + +def separate_audio_video(video_with_audio_path: str, basename_without_ext: str, output_folder: str) -> tuple[str, str]: + """ + Separate audio and video from a video file. + Returns tuple of (audio_file_path, video_file_path) + """ + from modules.console_colors import ULTRASINGER_HEAD + + # Get original video file extension + _, video_ext = os.path.splitext(video_with_audio_path) + + print(f"{ULTRASINGER_HEAD} Extracting audio from video") + audio_file_path = os.path.join(output_folder, f"{basename_without_ext}.m4a") + extract_audio(video_with_audio_path, audio_file_path) + + print(f"{ULTRASINGER_HEAD} Creating video without audio") + video_only_path = os.path.join(output_folder, f"{basename_without_ext}_video{video_ext}") + remove_audio_from_video(video_with_audio_path, video_only_path) + + # Remove original video with audio + os.remove(video_with_audio_path) + + # Rename video without audio to final name + final_video_path = os.path.join(output_folder, f"{basename_without_ext}{video_ext}") + os.rename(video_only_path, final_video_path) + + return audio_file_path, final_video_path +