Compare commits

...
3 Commits
4 changed files with 94 additions and 61 deletions

No files matched your search

+6 -1
View File
@@ -52,7 +52,12 @@ options:
--use_coreml Optional: use Apple Silicon GPU with CoreML for separation (default: False). Example: --use_coreml=true
--denoise DENOISE Optional: enable or disable denoising during separation (default: True). Example: --denoise=False
--normalize NORMALIZE Optional: enable or disable normalization during separation (default: True). Example: --normalize=False
--no_track_subfolders Optional:do NOT create a named subfolder for each track. Example: --no_track_subfolders
--no_track_subfolders Optional: do NOT create a named subfolder for each track. Example: --no_track_subfolders
--intro_background_color INTRO_BACKGROUND_COLOR Optional: Background color for intro video (default: black). Example: --intro_background_color=#123456
--intro_background_image INTRO_BACKGROUND_IMAGE Optional: Path to background image for intro video. Overrides background color if provided. Example: --intro_background_image=path/to/image.jpg
--intro_font INTRO_FONT Optional: Font file for intro video (default: Avenir-Next-Bold). Example: --intro_font=AvenirNext-Bold.ttf
--intro_artist_color INTRO_ARTIST_COLOR Optional: Font color for intro video artist text (default: #ff7acc). Example: --intro_artist_color=#123456
--intro_title_color INTRO_TITLE_COLOR Optional: Font color for intro video title text (default: #ffdf6b). Example: --intro_title_color=#123456
```
+83 -55
View File
@@ -29,8 +29,8 @@ class KaraokePrep:
intro_background_color="#000000",
intro_background_image=None,
intro_font="AvenirNext-Bold.ttf",
intro_artist_color="#ff7acc",
intro_title_color="#ffdf6b",
intro_artist_color="#ffffff",
intro_title_color="#ff7acc",
):
self.logger = logging.getLogger(__name__)
self.logger.setLevel(log_level)
@@ -60,11 +60,15 @@ class KaraokePrep:
self.normalization_enabled = normalization_enabled
self.denoise_enabled = denoise_enabled
self.create_track_subfolders = create_track_subfolders
self.intro_background_color = intro_background_color
self.intro_background_image = intro_background_image
self.intro_font = intro_font
self.intro_artist_color = intro_artist_color
self.intro_title_color = intro_title_color
self.title_format = {
"background_color": intro_background_color,
"background_image": intro_background_image,
"artist_font": intro_font,
"title_font": intro_font,
"artist_color": intro_artist_color,
"title_color": intro_title_color,
}
self.logger.debug(f"KaraokePrep output_format: {self.output_format}")
@@ -82,6 +86,8 @@ class KaraokePrep:
with yt_dlp.YoutubeDL({"quiet": True}) as ydl:
info = ydl.extract_info(self.url, download=False)
self.artist, self.title = self.parse_metadata(info)
if self.persistent_artist:
self.artist = self.persistent_artist
if self.artist and self.title:
self.logger.info(f"Extracted artist: {self.artist}, title: {self.title}")
else:
@@ -228,58 +234,78 @@ class KaraokePrep:
return track_output_dir, artist_title
def create_intro_video(self, artist, title, output_filename):
def calculate_text_size_and_position(self, draw, text, font_path, start_size, resolution, padding):
font_size = start_size
font = ImageFont.truetype(font_path, size=font_size) if os.path.exists(font_path) else ImageFont.load_default()
# Initial position for calculating the text bounding box
temp_position = (padding, padding)
bbox = draw.textbbox(temp_position, text, font=font)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
while text_width + 2 * padding > resolution[0] or text_height + 2 * padding > resolution[1]:
font_size -= 10
if font_size <= 0:
raise ValueError("Cannot fit text within screen bounds.")
font = ImageFont.truetype(font_path, size=font_size) if os.path.exists(font_path) else ImageFont.load_default()
bbox = draw.textbbox(temp_position, text, font=font)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
text_position = ((resolution[0] - text_width) // 2, (resolution[1] - text_height) // 2)
return font, text_position
def calculate_text_position(self, draw, text, font, resolution, vertical_offset):
bbox = draw.textbbox((0, 0), text, font=font)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
text_x = (resolution[0] - text_width) // 2
text_y = vertical_offset
return (text_x, text_y), text_height
def create_intro_video(self, artist, title, format, output_image_filepath, output_video_filepath):
duration = 5 # Duration in seconds
resolution = (3840, 2160) # 4K resolution
font_size = 400 # Initial font size
line_spacing = 50 # Adjust as needed
padding = 300 # Padding from edges
# Load or create background image
if self.intro_background_image:
background = Image.open(self.intro_background_image)
if format["background_image"] and os.path.exists(format["background_image"]):
self.logger.debug(f"Title screen background image file found: {format['background_image']}")
background = Image.open(format["background_image"])
else:
background = Image.new("RGB", resolution, color=self.hex_to_rgb(self.intro_background_color))
background = Image.new("RGB", resolution, color=self.hex_to_rgb(format["background_color"]))
# Resize background to match resolution
background = background.resize(resolution)
# Create an ImageDraw instance
title = title.upper()
artist = artist.upper()
initial_font_size = 500
top_padding = 950
title_padding = 400
artist_padding = 700
fixed_gap = 150
draw = ImageDraw.Draw(background)
# Function to calculate text width
def calculate_text_width(font):
artist_text_width = draw.textlength(artist, font=font)
title_text_width = draw.textlength(title, font=font)
return max(artist_text_width, title_text_width)
# Calculate positions and sizes for title and artist
title_font, _ = self.calculate_text_size_and_position(
draw, title, format["title_font"], initial_font_size, resolution, title_padding
)
artist_font, _ = self.calculate_text_size_and_position(
draw, artist, format["artist_font"], initial_font_size, resolution, artist_padding
)
# Calculate vertical positions with consistent gap
title_text_position, title_height = self.calculate_text_position(draw, title, title_font, resolution, top_padding)
artist_text_position, _ = self.calculate_text_position(draw, artist, artist_font, resolution, title_text_position[1] + title_height + fixed_gap)
# Adjust font size if necessary
while True:
if os.path.exists(self.intro_font):
font = ImageFont.truetype(self.intro_font, size=font_size)
else:
self.logger.warning(f"Font file not found: {self.intro_font}. Using default font.")
font = ImageFont.load_default(size=font_size)
draw.text(title_text_position, title, fill=format["title_color"], font=title_font)
draw.text(artist_text_position, artist, fill=format["artist_color"], font=artist_font)
max_text_width = calculate_text_width(font)
if max_text_width + padding * 2 > resolution[0]:
font_size -= 10 # Reduce font size
if font_size <= 0:
raise ValueError("Cannot fit text within screen bounds.")
else:
break
# Calculate text positions
artist_text_position = ((resolution[0] - calculate_text_width(font)) // 2, resolution[1] // 2 - font_size - line_spacing)
title_text_position = ((resolution[0] - calculate_text_width(font)) // 2, resolution[1] // 2 + line_spacing)
# Draw text
draw.text(artist_text_position, artist, fill=self.intro_artist_color, font=font)
draw.text(title_text_position, title, fill=self.intro_title_color, font=font)
# Save to a temporary image file
temp_image_path = "/tmp/temp_background.png"
background.save(temp_image_path)
# Save static background image
background.save(output_image_filepath)
# Use ffmpeg to create video
ffmpeg_command = [
@@ -288,7 +314,7 @@ class KaraokePrep:
"-loop",
"1",
"-i",
temp_image_path,
output_image_filepath,
"-c:v",
"libx264",
"-t",
@@ -297,11 +323,10 @@ class KaraokePrep:
"yuv420p",
"-vf",
f"scale={resolution[0]}:{resolution[1]}",
output_filename,
output_video_filepath,
]
subprocess.run(ffmpeg_command)
os.remove(temp_image_path)
def hex_to_rgb(self, hex_color):
"""Convert hex color to RGB tuple."""
@@ -324,6 +349,10 @@ class KaraokePrep:
"title": title,
}
processed_track["title_image"] = os.path.join(track_output_dir, f"{artist_title} (Title).png")
processed_track["title_video"] = os.path.join(track_output_dir, f"{artist_title} (Title).mov")
self.create_intro_video(artist, title, self.title_format, processed_track["title_image"], processed_track["title_video"])
yt_webm_filename_pattern = os.path.join(track_output_dir, f"{artist_title} (YouTube *.webm")
yt_webm_glob = glob.glob(yt_webm_filename_pattern)
@@ -377,9 +406,6 @@ class KaraokePrep:
processed_track["lyrics"] = lyrics_file
intro_filename = os.path.join(track_output_dir, f"{artist_title} (Intro).mp4")
self.create_intro_video(artist, title, intro_filename)
self.logger.info(f"Separating audio twice for track: {title} by {artist}")
instrumental_path = os.path.join(track_output_dir, f"{artist_title} (Instrumental {self.model_name}).{self.output_format}")
@@ -421,7 +447,6 @@ class KaraokePrep:
Processes all videos in a YouTube playlist.
"""
self.logger.debug(f"Querying playlist metadata from YouTube, assuming consistent artist {self.artist}...")
persistent_artist = self.artist
with yt_dlp.YoutubeDL({"quiet": True}) as ydl:
result = ydl.extract_info(self.url, download=False)
if "entries" in result:
@@ -432,13 +457,16 @@ class KaraokePrep:
self.logger.info(f"Processing video: {video_url}")
self.url = video_url
track_results.append(self.prep_single_track())
self.artist = persistent_artist
self.artist = self.persistent_artist
self.title = None
return track_results
def process(self):
if self.is_playlist_url():
self.logger.info(f"Provided YouTube URL is a playlist, beginning batch operation")
self.persistent_artist = self.artist
self.logger.info(
f"Provided YouTube URL is a playlist, beginning batch operation with persistent artist: {self.persistent_artist}"
)
return self.process_playlist()
else:
self.logger.info(f"Provided YouTube URL is NOT a playlist, processing single track")
+4 -4
View File
@@ -112,14 +112,14 @@ def main():
parser.add_argument(
"--intro_artist_color",
default="#ff7acc",
help="Optional: Font color for intro video artist text (default: #ff7acc). Example: --intro_artist_color=#123456",
default="#ffdf6b",
help="Optional: Font color for intro video artist text (default: #ffdf6b). Example: --intro_artist_color=#123456",
)
parser.add_argument(
"--intro_title_color",
default="#ffdf6b",
help="Optional: Font color for intro video title text (default: #ffdf6b). Example: --intro_title_color=#123456",
default="#ffffff",
help="Optional: Font color for intro video title text (default: #ffffff). Example: --intro_title_color=#123456",
)
args = parser.parse_args()
+1 -1
View File
@@ -1,6 +1,6 @@
[tool.poetry]
name = "karaoke-prep"
version = "0.4.5"
version = "0.4.8"
description = "Prepare for karaoke video creation, by downloading audio and lyrics for a specified song or youtube playlist and separatung audio stems."
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
license = "MIT"