Download yt video/audio only once

This commit is contained in:
Vadim committed 2026-02-08 22:33:31 +01:00
1 parent b758d08d14
commit 0a1d140b91
4 files changed
+84 -30

No files matched your search

+3 -2
View File
@@ -1,5 +1,5 @@
# Version: 0.0.13dev6
Date: 2024.12.26
# Version: 0.0.13dev10
Date: 2026.02.08
- Changes:
- Download Cover from MusicBrainz
- Fix selection of whisper model
@@ -10,6 +10,7 @@ Date: 2024.12.26
- Use user defined ffmpeg path
- Show GPU Name and VRAM
- No linebreak before the last 'E' word for UltraStar txt files
- Download yt video/audio only once
# Version: 0.0.12
Date: 2024.12.19
+1 -1
View File
@@ -11,7 +11,7 @@ from modules.Ultrastar.ultrastar_txt import FormatVersion
@dataclass
class Settings:
APP_VERSION = "0.0.13-dev9"
APP_VERSION = "0.0.13-dev10"
CONFIDENCE_THRESHOLD = 0.6
CONFIDENCE_PROMPT_TIMEOUT = 4
+43 -27
View File
@@ -10,6 +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
def get_youtube_title(url: str, cookiefile: str = None) -> tuple[str, str]:
@@ -30,20 +31,18 @@ def get_youtube_title(url: str, cookiefile: str = None) -> tuple[str, str]:
return result["channel"].strip(), result["title"].strip()
def __download_youtube_audio(url: str, clear_filename: str, output_path: str, cookiefile: str = None):
"""Download audio from YouTube"""
def __download_youtube_video_with_audio(url: str, clear_filename: str, output_path: str, cookiefile: str = None) -> str:
"""Download video with audio from YouTube and return the file extension"""
print(f"{ULTRASINGER_HEAD} Downloading Audio")
print(f"{ULTRASINGER_HEAD} Downloading Video with Audio")
ydl_opts = {
"format": "bestaudio/best",
"outtmpl": output_path + "/" + clear_filename,
"postprocessors": [
{"key": "FFmpegExtractAudio", "preferredcodec": "mp3"}
],
"format": "bestvideo[ext=mp4]+bestaudio/best",
"outtmpl": output_path + "/" + clear_filename + ".%(ext)s",
"merge_output_format": "mp4",
"cookiefile": cookiefile,
}
__start_download(ydl_opts, url)
return "mp4"
def __download_youtube_thumbnail(url: str, clear_filename: str, output_path: str, cookiefile: str = None) -> str:
@@ -75,16 +74,6 @@ 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, 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)
def __start_download(ydl_opts, url: str) -> None:
@@ -104,26 +93,53 @@ def download_from_youtube(input_url: str, output_folder_path: str, cookiefile: s
song_info = search_musicbrainz(title, artist)
basename_without_ext = sanitize_filename(f"{song_info.artist} - {song_info.title}")
basename = basename_without_ext + ".mp3"
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_url, basename_without_ext, song_output, cookiefile)
__download_youtube_video(input_url, basename_without_ext, song_output, cookiefile)
print(f"{ULTRASINGER_HEAD} Downloading from YouTube")
video_ext = __download_youtube_video_with_audio(
input_url, basename_without_ext, song_output, cookiefile
)
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)
if song_info.cover_url is not None and song_info.cover_image_data is not None:
cover_url = song_info.cover_url
save_image(song_info.cover_image_data, basename_without_ext, song_output)
else:
cover_url = __download_youtube_thumbnail(
input_url, basename_without_ext, song_output
)
audio_file_path = os.path.join(song_output, basename)
input_url, basename_without_ext, song_output, cookiefile
)
real_bpm = get_bpm_from_file(audio_file_path)
return (
basename_without_ext,
song_output,
audio_file_path,
MediaInfo(artist=song_info.artist, title=song_info.title, year=song_info.year, genre=song_info.genres, bpm=real_bpm,
cover_url=cover_url, video_url=input_url),
MediaInfo(
artist=song_info.artist,
title=song_info.title,
year=song_info.year,
genre=song_info.genres,
bpm=real_bpm,
cover_url=cover_url,
video_url=input_url,
),
)
+37
View File
@@ -2,6 +2,7 @@
import os
import shutil
import subprocess
def __add_ffmpeg_to_path(ffmpeg_path) -> None:
@@ -29,3 +30,39 @@ def is_ffmpeg_available(user_path: str = "") -> bool:
return True
return False
def extract_audio(input_video_path: str, output_audio_path: str, ffmpeg_path: str) -> None:
"""Extract audio from video file in original format without re-encoding"""
cmd = [
ffmpeg_path,
"-i",
input_video_path,
"-vn", # No video
"-acodec",
"copy", # Copy audio codec without re-encoding
"-y", # Overwrite output file if exists
output_audio_path,
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
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:
"""Remove audio from video file without re-encoding video"""
cmd = [
ffmpeg_path,
"-i",
input_video_path,
"-an", # No audio
"-vcodec",
"copy", # Copy video codec without re-encoding
"-y", # Overwrite output file if exists
output_video_path,
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
raise Exception(f"FFmpeg audio removal failed: {result.stderr}")