Re-encode passes (vertical render, hooks, subtitles, AI effects) ran at CRF 22-23, stacking visible generation loss across steps (#13). Bump all of them to CRF 18; post-process steps also move preset fast -> medium (the realtime reframe pipe keeps fast to not slow the streamed encode). OpenCV resizes now use INTER_LANCZOS4 for sharper scaling. Adopted from PR #14 review (the good parts, rebased on current main).
231 lines
7.8 KiB
Python
231 lines
7.8 KiB
Python
import os
|
|
import subprocess
|
|
|
|
|
|
def transcribe_audio(video_path):
|
|
"""
|
|
Transcribe audio from a video file using faster-whisper.
|
|
Returns transcript in the same format as main.py for compatibility.
|
|
"""
|
|
from faster_whisper import WhisperModel
|
|
|
|
print(f"🎙️ Transcribing audio from: {video_path}")
|
|
|
|
# Run on CPU with INT8 quantization for speed
|
|
model = WhisperModel("base", device="cpu", compute_type="int8")
|
|
|
|
segments, info = model.transcribe(video_path, word_timestamps=True)
|
|
|
|
transcript = {
|
|
"segments": [],
|
|
"language": info.language
|
|
}
|
|
|
|
for segment in segments:
|
|
seg_data = {
|
|
"start": segment.start,
|
|
"end": segment.end,
|
|
"text": segment.text,
|
|
"words": []
|
|
}
|
|
if segment.words:
|
|
for word in segment.words:
|
|
seg_data["words"].append({
|
|
"word": word.word.strip(),
|
|
"start": word.start,
|
|
"end": word.end
|
|
})
|
|
transcript["segments"].append(seg_data)
|
|
|
|
print(f"✅ Transcription complete. Language: {info.language}")
|
|
return transcript
|
|
|
|
|
|
def generate_srt_from_video(video_path, output_path, max_chars=20, max_duration=2.0):
|
|
"""
|
|
Transcribe a video and generate SRT directly.
|
|
Used for dubbed videos that don't have a pre-existing transcript.
|
|
"""
|
|
transcript = transcribe_audio(video_path)
|
|
|
|
# Get video duration to use as clip_end
|
|
import cv2
|
|
cap = cv2.VideoCapture(video_path)
|
|
fps = cap.get(cv2.CAP_PROP_FPS)
|
|
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
duration = frame_count / fps if fps else 0
|
|
cap.release()
|
|
|
|
return generate_srt(transcript, 0, duration, output_path, max_chars, max_duration)
|
|
|
|
|
|
def generate_srt(transcript, clip_start, clip_end, output_path, max_chars=20, max_duration=2.0):
|
|
"""
|
|
Generates an SRT file from the transcript for a specific time range.
|
|
Groups words into short lines suitable for vertical video.
|
|
"""
|
|
|
|
words = []
|
|
# 1. Extract and flatten words within range
|
|
for segment in transcript.get('segments', []):
|
|
for word_info in segment.get('words', []):
|
|
# Check overlap
|
|
if word_info['end'] > clip_start and word_info['start'] < clip_end:
|
|
words.append(word_info)
|
|
|
|
if not words:
|
|
return False
|
|
|
|
srt_content = ""
|
|
index = 1
|
|
|
|
current_block = []
|
|
block_start = None
|
|
|
|
for i, word in enumerate(words):
|
|
# Adjust times relative to clip
|
|
start = max(0, word['start'] - clip_start)
|
|
end = max(0, word['end'] - clip_start)
|
|
|
|
# Clip to video duration logic handled by ffmpeg usually, but good to be safe
|
|
|
|
if not current_block:
|
|
current_block.append(word)
|
|
block_start = start
|
|
else:
|
|
# Decide whether to close block
|
|
current_text_len = sum(len(w['word']) + 1 for w in current_block)
|
|
duration = end - block_start
|
|
|
|
if current_text_len + len(word['word']) > max_chars or duration > max_duration:
|
|
# Finalize current block
|
|
# End time of block is start of this word (gap) or end of last word?
|
|
# Usually end of last word.
|
|
block_end = current_block[-1]['end'] - clip_start
|
|
|
|
text = " ".join([w['word'] for w in current_block]).strip()
|
|
srt_content += format_srt_block(index, block_start, block_end, text)
|
|
index += 1
|
|
|
|
current_block = [word]
|
|
block_start = start
|
|
else:
|
|
current_block.append(word)
|
|
|
|
# Final block
|
|
if current_block:
|
|
block_end = current_block[-1]['end'] - clip_start
|
|
text = " ".join([w['word'] for w in current_block]).strip()
|
|
srt_content += format_srt_block(index, block_start, block_end, text)
|
|
|
|
with open(output_path, 'w', encoding='utf-8') as f:
|
|
f.write(srt_content)
|
|
|
|
return True
|
|
|
|
def format_srt_block(index, start, end, text):
|
|
def format_time(seconds):
|
|
hours = int(seconds // 3600)
|
|
minutes = int((seconds % 3600) // 60)
|
|
secs = int(seconds % 60)
|
|
millis = int((seconds - int(seconds)) * 1000)
|
|
return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"
|
|
|
|
return f"{index}\n{format_time(start)} --> {format_time(end)}\n{text}\n\n"
|
|
|
|
def hex_to_ass_color(hex_color, opacity=1.0):
|
|
"""Convert #RRGGBB to ASS &HAABBGGRR format. opacity: 0.0=transparent, 1.0=opaque"""
|
|
hex_color = hex_color.lstrip('#')
|
|
if len(hex_color) != 6:
|
|
hex_color = "FFFFFF"
|
|
r = int(hex_color[0:2], 16)
|
|
g = int(hex_color[2:4], 16)
|
|
b = int(hex_color[4:6], 16)
|
|
alpha = round((1.0 - opacity) * 255)
|
|
return f"&H{alpha:02X}{b:02X}{g:02X}{r:02X}"
|
|
|
|
|
|
def burn_subtitles(video_path, srt_path, output_path, alignment=2, fontsize=16,
|
|
font_name="Verdana", font_color="#FFFFFF",
|
|
border_color="#000000", border_width=2,
|
|
bg_color="#000000", bg_opacity=0.0):
|
|
"""
|
|
Burns subtitles into the video using FFmpeg.
|
|
Supports two modes:
|
|
- Outline mode (bg_opacity=0): Text with colored outline/border
|
|
- Box mode (bg_opacity>0): Text with semi-transparent background box
|
|
"""
|
|
# Sanitize the client-supplied font name before it goes into the FFmpeg
|
|
# force_style string. A quote/comma/colon/backslash would break out of the
|
|
# quoted style and inject extra libavfilter directives (filtergraph
|
|
# injection / DoS). Keep only safe font-name characters.
|
|
import re as _re
|
|
font_name = _re.sub(r"[^A-Za-z0-9 _\-]", "", str(font_name))[:64].strip() or "Verdana"
|
|
|
|
# Position mapping
|
|
ass_alignment = 2
|
|
align_lower = str(alignment).lower()
|
|
if align_lower == 'top':
|
|
ass_alignment = 6
|
|
elif align_lower == 'middle':
|
|
ass_alignment = 10
|
|
elif align_lower == 'bottom':
|
|
ass_alignment = 2
|
|
|
|
# Font size scaling for ASS virtual resolution (PlayResY=288 default)
|
|
# For vertical 1080x1920 video, we need larger text for readability
|
|
final_fontsize = int(fontsize * 0.85)
|
|
if final_fontsize < 10:
|
|
final_fontsize = 10
|
|
|
|
# Path handling for FFmpeg filter syntax
|
|
safe_srt_path = srt_path.replace('\\', '/').replace(':', '\\:')
|
|
|
|
# Convert colors to ASS format and build style
|
|
primary_colour = hex_to_ass_color(font_color, 1.0)
|
|
|
|
if bg_opacity > 0:
|
|
# Box mode: opaque background box
|
|
border_style = 3
|
|
outline_colour = hex_to_ass_color(bg_color, bg_opacity)
|
|
outline_width = 1
|
|
else:
|
|
# Outline mode: text border/outline
|
|
border_style = 1
|
|
outline_colour = hex_to_ass_color(border_color, 1.0)
|
|
outline_width = max(1, border_width)
|
|
|
|
back_colour = hex_to_ass_color("#000000", 0.0)
|
|
|
|
style_string = (
|
|
f"Alignment={ass_alignment},"
|
|
f"Fontname={font_name},"
|
|
f"Fontsize={final_fontsize},"
|
|
f"PrimaryColour={primary_colour},"
|
|
f"OutlineColour={outline_colour},"
|
|
f"BackColour={back_colour},"
|
|
f"BorderStyle={border_style},"
|
|
f"Outline={outline_width},"
|
|
f"Shadow=0,"
|
|
f"MarginV=25,"
|
|
f"Bold=1"
|
|
)
|
|
|
|
cmd = [
|
|
'ffmpeg', '-y',
|
|
'-i', video_path,
|
|
'-vf', f"subtitles='{safe_srt_path}':force_style='{style_string}'",
|
|
'-c:a', 'copy',
|
|
'-c:v', 'libx264', '-preset', 'medium', '-crf', '18',
|
|
output_path
|
|
]
|
|
|
|
print(f"🎬 Burning subtitles: {' '.join(cmd)}")
|
|
result = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
|
|
|
|
if result.returncode != 0:
|
|
print(f"❌ FFmpeg Subtitle Error: {result.stderr.decode()}")
|
|
raise Exception(f"FFmpeg failed: {result.stderr.decode()}")
|
|
|
|
return True
|
|
|