Files
momentry_core/scripts/asr_processor.py
T
Accusys a3c3ed8dc1 refactor: eliminate all CARGO_MANIFEST_DIR compile-time dependencies (A-4)
- Add src/core/path_utils.rs with runtime path resolution helpers
- Replace all env!("CARGO_MANIFEST_DIR") with runtime resolution
- Priority: env var -> current_exe sibling -> dev fallback

Changes:
- executor.rs: scripts_dir(), cli_dir() (4 locations)
- face.rs: swift_processor_path()
- tool_selector.rs: swift_binary_path()
- thumbnail/mod.rs: script_path()

Issue: CARGO_MANIFEST_DIR expands to build machine path at compile time,
       breaking on binary-only release hosts.

Solution: Resolve paths at runtime in this order:
1. Explicit environment variable (MOMENTRY_SCRIPTS_DIR, MOMENTRY_CLI_DIR)
2. Sibling of current_exe (same directory as running binary)
3. Relative path fallback (dev machine)

Verified: cargo check --lib passes
2026-10-04 13:44:40 +08:00

691 lines
26 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/opt/homebrew/bin/python3.11
"""
ASR Processor - faster-whisper small model (Production)
Version: 2.1
Model: small (int8 quantization, CPU)
Reason: small 模型在準確率和速度間取得最佳平衡
經實驗驗證,最少要使用 small 才可以較好的處理多語種及台灣腔國語
Configuration:
- Model: faster-whisper/small
- Device: CPU (MPS not supported by faster_whisper)
- Compute: int8
- Beam size: 5
- VAD filter: enabled (min_silence=500ms, speech_pad=200ms)
- Audio fallback: ffmpeg extraction for PyAV-incompatible streams (v2.1)
"""
import sys
import json
import os
import math
import time
import argparse
import signal
import subprocess
import tempfile
from faster_whisper import WhisperModel
PROCESSOR_VERSION = "2.2"
MODEL_SIZE = "large-v3" # Upgraded from small to large-v3 for better language detection
DEVICE = "cpu"
# Common languages to try when auto-detection fails
COMMON_LANGUAGES = ["en", "zh", "ja"] # English, Chinese (auto繁简), Japanese
def segment_confidence(segment) -> float:
"""Derive a 0-1 confidence from faster-whisper's avg_logprob.
faster-whisper segments do NOT expose a `confidence` attribute, so the old
getattr(segment, "confidence", 0.0) always returned 0. avg_logprob is the
average log-probability per token (negative); exp() maps it to (0, 1].
"""
logprob = getattr(segment, "avg_logprob", -0.1)
return min(1.0, math.exp(logprob))
def calculate_transcription_quality(segments, info) -> dict:
"""Calculate transcription quality metrics.
Returns:
dict with:
- avg_logprob: Average log probability (higher is better)
- avg_no_speech_prob: Average no_speech_prob (lower is better)
- segment_count: Number of segments
- quality_score: Combined quality score (higher is better)
"""
# Convert generator to list if needed
if not isinstance(segments, list):
segments = list(segments)
if not segments:
return {
"avg_logprob": -1.0,
"avg_no_speech_prob": 1.0,
"segment_count": 0,
"quality_score": 0.0,
}
total_logprob = sum(getattr(s, "avg_logprob", -0.5) for s in segments)
total_no_speech = sum(getattr(s, "no_speech_prob", 0.5) for s in segments)
avg_logprob = total_logprob / len(segments)
avg_no_speech_prob = total_no_speech / len(segments)
# Quality score: combination of metrics
# Higher logprob is better, lower no_speech_prob is better
quality_score = (
(1 + avg_logprob) * # Normalize logprob to positive
(1 - avg_no_speech_prob) * # Lower no_speech is better
min(len(segments) / 100, 1.0) # Prefer more segments (cap at 100)
)
return {
"avg_logprob": avg_logprob,
"avg_no_speech_prob": avg_no_speech_prob,
"segment_count": len(segments),
"quality_score": quality_score,
}
def transcribe_with_common_languages(model, video_path, publisher=None):
"""Transcribe video using common languages when auto-detection fails.
Strategy:
1. Try auto-detection first
2. If detected language is in COMMON_LANGUAGES, use it
3. If not, or quality is poor, try common languages
4. Return the best result
Args:
model: WhisperModel instance
video_path: Path to video or audio file
publisher: RedisPublisher for progress updates
Returns:
Tuple of (segments_list, info)
"""
# Step 1: Try auto-detection
msg = "[ASR] Trying auto language detection..."
sys.stderr.write(msg + "\n")
sys.stderr.flush()
if publisher:
publisher.info("asr", "Trying auto language detection...")
segments_gen, info = transcribe_with_fallback(model, video_path, publisher, language=None)
segments = list(segments_gen) # Convert generator to list
detected_lang = info.language
quality = calculate_transcription_quality(segments, info)
msg = f"[ASR] Auto-detection: {detected_lang}, quality: {quality['quality_score']:.2f}"
sys.stderr.write(msg + "\n")
sys.stderr.flush()
if publisher:
publisher.info("asr", f"Auto-detection: {detected_lang}, quality: {quality['quality_score']:.2f}")
# Step 2: Check if detected language is in common languages
if detected_lang in COMMON_LANGUAGES:
# Good detection, return it
msg = f"[ASR] Using auto-detected language: {detected_lang}"
sys.stderr.write(msg + "\n")
sys.stderr.flush()
if publisher:
publisher.info("asr", f"Using auto-detected language: {detected_lang}")
return segments, info
# Step 3: If quality is good enough, accept auto-detection even if not common
if quality["quality_score"] > 0.5 and quality["segment_count"] > 10:
msg = f"[ASR] Auto-detected {detected_lang} has good quality, using it"
sys.stderr.write(msg + "\n")
sys.stderr.flush()
if publisher:
publisher.info("asr", f"Auto-detected {detected_lang} has good quality, using it")
return segments, info
# Step 4: Try common languages
msg = "[ASR] Auto-detection poor, trying common languages..."
sys.stderr.write(msg + "\n")
sys.stderr.flush()
if publisher:
publisher.info("asr", "Auto-detection poor, trying common languages...")
best_result = (segments, info)
best_quality = quality
for lang in COMMON_LANGUAGES:
if lang == detected_lang:
continue # Already tried
msg = f"[ASR] Trying language: {lang}"
sys.stderr.write(msg + "\n")
sys.stderr.flush()
if publisher:
publisher.info("asr", f"Trying language: {lang}")
try:
seg_gen, inf = transcribe_with_fallback(model, video_path, publisher, language=lang)
seg = list(seg_gen) # Convert generator to list
q = calculate_transcription_quality(seg, inf)
msg = f"[ASR] Language {lang}: quality {q['quality_score']:.2f}"
sys.stderr.write(msg + "\n")
sys.stderr.flush()
if publisher:
publisher.info("asr", f"Language {lang}: quality {q['quality_score']:.2f}")
# Compare quality
if q["quality_score"] > best_quality["quality_score"]:
best_quality = q
best_result = (seg, inf)
except Exception as e:
sys.stderr.write(f"ASR: Failed to transcribe with {lang}: {e}\n")
sys.stderr.flush()
continue
# Step 5: Return best result
final_lang = best_result[1].language
msg = f"[ASR] Best language: {final_lang}, quality: {best_quality['quality_score']:.2f}"
sys.stderr.write(msg + "\n")
sys.stderr.flush()
if publisher:
publisher.info("asr", f"Best language: {final_lang}, quality: {best_quality['quality_score']:.2f}")
return best_result
def round_json_numbers(obj):
"""Recursively round all float values to 2 decimal places (JSON output only)."""
if isinstance(obj, float):
return round(obj, 2)
if isinstance(obj, dict):
return {k: round_json_numbers(v) for k, v in obj.items()}
if isinstance(obj, list):
return [round_json_numbers(v) for v in obj]
return obj
COMPUTE_TYPE = "int8"
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from redis_publisher import RedisPublisher
def signal_handler(signum, frame):
print(f"ASR: Received signal {signum}, exiting...")
sys.exit(1)
def has_audio_stream(video_path):
"""Check if video file has audio stream using ffprobe."""
try:
cmd = [
"ffprobe",
"-v",
"error",
"-select_streams",
"a",
"-show_entries",
"stream=codec_type",
"-of",
"csv=p=0",
video_path,
]
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
return bool(result.stdout.strip())
except subprocess.CalledProcessError:
return False
except FileNotFoundError:
print("WARNING: ffprobe not found, assuming audio exists")
return True
def extract_audio_with_ffmpeg(video_path):
"""Extract audio from video to WAV using ffmpeg.
Returns path to temporary WAV file. Caller is responsible for cleanup.
"""
wav_path = tempfile.mktemp(suffix=".wav", prefix="asr_audio_")
cmd = [
"ffmpeg",
"-y",
"-i", video_path,
"-vn",
"-acodec", "pcm_s16le",
"-ar", "16000",
"-ac", "1",
wav_path,
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
sys.stderr.write(f"ASR: ffmpeg extraction failed: {result.stderr}\n")
sys.stderr.flush()
return None
return wav_path
def transcribe_with_fallback(model, video_path, publisher=None, language=None):
"""Transcribe video with fallback to ffmpeg-extracted WAV.
First tries direct transcription (PyAV). If PyAV fails to decode,
falls back to ffmpeg audio extraction then transcription.
Args:
model: WhisperModel instance
video_path: Path to video or audio file
publisher: RedisPublisher for progress updates
language: Force language (e.g., "ja", "en", None for auto-detect)
"""
# Try direct transcription first
try:
if publisher:
publisher.info("asr", "Direct transcription attempt...")
# Build transcription kwargs
transcribe_kwargs = {
"beam_size": 5,
"vad_filter": True,
"vad_parameters": dict(min_silence_duration_ms=500, speech_pad_ms=200),
}
# Add language if specified
if language:
transcribe_kwargs["language"] = language
if publisher:
publisher.info("asr", f"Forcing language: {language}")
return model.transcribe(video_path, **transcribe_kwargs)
except Exception as e:
error_str = str(e)
# Check if it's a PyAV/av decoding error
is_pyav_error = any(
keyword in error_str.lower()
for keyword in ["av.error", "avcodec", "decode", "packet"]
)
if not is_pyav_error:
raise # Re-raise non-PyAV errors
if publisher:
publisher.info("asr", "PyAV decode failed, falling back to ffmpeg extraction...")
sys.stderr.write("ASR: PyAV decode error detected, falling back to ffmpeg extraction\n")
sys.stderr.flush()
wav_path = extract_audio_with_ffmpeg(video_path)
if wav_path is None:
raise RuntimeError("Failed to extract audio with ffmpeg")
try:
if publisher:
publisher.info("asr", "Transcribing extracted WAV audio...")
# Build transcription kwargs for fallback
fallback_kwargs = {
"beam_size": 5,
"vad_filter": True,
"vad_parameters": dict(min_silence_duration_ms=500, speech_pad_ms=200),
}
# Add language if specified
if language:
fallback_kwargs["language"] = language
segments, info = model.transcribe(wav_path, **fallback_kwargs)
return segments, info
finally:
# Clean up temporary WAV file
try:
os.remove(wav_path)
except OSError:
pass
def get_fps_from_cut(cut_path):
"""從 CUT 資料獲取 FPS"""
if os.path.exists(cut_path):
try:
with open(cut_path) as f:
cut_data = json.load(f)
fps = cut_data.get("fps")
if fps and fps > 0:
return fps
except Exception as e:
print(f"[ASR] Failed to load CUT FPS: {e}", file=sys.stderr)
return None
def get_fps_from_ffprobe(video_path):
"""從影片獲取 FPS (ffprobe)"""
try:
cmd = ["ffprobe", "-v", "error",
"-select_streams", "v:0",
"-show_entries", "stream=r_frame_rate",
"-of", "csv=p=0", video_path]
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
# ffprobe 的 csv 輸出可能帶多餘欄位或重複行(如 "60000/1001," 或
# "60000/1001\n\n60000/1001"),需逐行取第一個可解析且 >0 的值。
for line in result.stdout.splitlines():
for token in line.split(","):
token = token.strip()
if not token:
continue
try:
if "/" in token:
num, den = token.split("/", 1)
fps = float(num) / float(den)
else:
fps = float(token)
except (ValueError, ZeroDivisionError):
continue
if fps > 0:
return fps
return None
except Exception:
return None
def run_asr(video_path, output_path, uuid: str = "", fps: float = None, language: str = None, separate_vocals: bool = False, try_common_languages: bool = False):
# Set up signal handlers
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
# Vocal separation (optional preprocessing)
if separate_vocals:
sys.stderr.write("[ASR] Separating vocals using demucs...\n")
sys.stderr.flush()
from preprocess_audio import preprocess_audio_for_asr
processed_audio = preprocess_audio_for_asr(video_path, separate=True)
if processed_audio:
video_path = processed_audio # Use separated vocals
sys.stderr.write(f"[ASR] Using separated vocals: {processed_audio}\n")
else:
sys.stderr.write("[ASR] Vocal separation failed, using original audio\n")
sys.stderr.flush()
# If try_common_languages is enabled and no language specified, use smart detection
if try_common_languages and not language:
sys.stderr.write("[ASR] Smart language detection enabled (common languages)\n")
sys.stderr.flush()
# FPS detection chain: CLI → CUT → ffprobe → FAIL
if fps is not None:
print(f"[ASR] Using CLI-provided FPS: {fps}", file=sys.stderr)
else:
# Use uuid to construct CUT path (output_path may have .tmp suffix from executor)
output_dir = os.path.dirname(output_path)
if uuid and output_dir:
cut_path_check = os.path.join(output_dir, f"{uuid}.cut.json")
else:
cut_path_check = output_path.replace(".asr.json", ".cut.json")
fps = get_fps_from_cut(cut_path_check)
if fps:
print(f"[ASR] FPS from CUT: {fps}", file=sys.stderr)
if fps is None:
fps = get_fps_from_ffprobe(video_path)
if fps:
print(f"[ASR] FPS from ffprobe: {fps}", file=sys.stderr)
if fps is None:
print("[ASR] ERROR: Cannot determine FPS (no CUT data, ffprobe failed). Aborting.", file=sys.stderr)
sys.exit(1)
publisher = RedisPublisher(uuid) if uuid else None
if publisher:
publisher.info("asr", "ASR_START")
# Check for audio stream
if not has_audio_stream(video_path):
if publisher:
publisher.info("asr", "No audio stream detected, skipping transcription")
output = {
"status": "no_audio_track",
"language": "",
"language_probability": 0.0,
"segments": []
}
with open(output_path, "w") as f:
json.dump(round_json_numbers(output), f, indent=2, ensure_ascii=False)
if publisher:
publisher.complete("asr", "0 segments (no audio)")
sys.stderr.write("ASR: No audio stream, skipping transcription\n")
sys.stderr.flush()
sys.exit(0)
# 嘗試以 CUT 場景分段處理(降低長片記憶體使用)
cut_scenes = []
cut_path = output_path.replace(".asr.json", ".cut.json")
if os.path.exists(cut_path):
try:
with open(cut_path) as f:
cut_data = json.load(f)
scenes = cut_data.get("scenes", [])
if scenes:
cut_scenes = [(s["start_time"], s["end_time"]) for s in scenes]
print(f"[ASR] Loaded {len(cut_scenes)} cut scenes for segmented transcription", file=sys.stderr)
except Exception as e:
print(f"[ASR] Failed to load cut scenes: {e}", file=sys.stderr)
if publisher:
publisher.info("asr", "Loading Whisper model...")
sys.stderr.write(f"[ASR] Loading Whisper model {MODEL_SIZE}...\n")
sys.stderr.flush()
model = WhisperModel(MODEL_SIZE, device="cpu", compute_type="int8")
sys.stderr.write(f"[ASR] Model loaded\n")
sys.stderr.flush()
if publisher:
publisher.info("asr", f"Transcribing: {video_path}")
results = []
total_segments = 0
if cut_scenes:
# 分段處理:對每個場景萃取音訊並轉錄
sys.stderr.write(f"[ASR] Starting segmented transcription for {len(cut_scenes)} scenes\n")
sys.stderr.flush()
import subprocess
import tempfile
temp_dir = tempfile.mkdtemp(prefix="asr_cut_")
sys.stderr.write(f"[ASR] Temp dir: {temp_dir}\n")
sys.stderr.flush()
transcript_language = None
# 建立 scene lookup: 給定時間點,找是哪個 scene
import bisect
scene_starts = [s[0] for s in cut_scenes]
def find_scene_idx(t):
i = bisect.bisect_right(scene_starts, t) - 1
return max(0, i)
# 逐段處理,每段結果即時寫入 .asr.tmp
tmp_path = output_path + ".tmp"
err_path = output_path + ".err"
all_segments = []
# Resume: 若 executor 將 .tmp rename 成 .err,先救回
if not os.path.exists(tmp_path) and os.path.exists(err_path) and os.path.getsize(err_path) > 10:
try:
os.rename(err_path, tmp_path)
sys.stderr.write(f"[ASR] Recovered .err → .tmp for resume ({os.path.getsize(tmp_path)} bytes)\n")
sys.stderr.flush()
except Exception as e:
sys.stderr.write(f"[ASR] Failed to recover .err: {e}\n")
sys.stderr.flush()
# Resume: 若已有 .asr.tmp,載入已完成的 segments 並跳過已處理的 scenes
resume_from_scene = 0
if os.path.exists(tmp_path) and os.path.getsize(tmp_path) > 10:
try:
with open(tmp_path) as f:
existing = json.load(f)
all_segments = existing.get("segments", [])
if all_segments:
# 找出最後一個 segment 的 end_time,決定 resume 起點
last_end = max(s.get("end", 0) for s in all_segments)
# 找出最後完成的 scene_idx(場景 end_time > last_end)
for i, (st, et) in enumerate(cut_scenes):
if et > last_end:
resume_from_scene = i
break
else:
resume_from_scene = len(cut_scenes) # 全部完成
# 繼承 language
if existing.get("language"):
transcript_language = existing["language"]
sys.stderr.write(f"[ASR] Resume from scene {resume_from_scene}/{len(cut_scenes)} "
f"(last segment end={last_end:.1f}s, {len(all_segments)} existing segments)\n")
sys.stderr.flush()
except Exception as e:
sys.stderr.write(f"[ASR] Failed to load tmp for resume: {e}, starting fresh\n")
sys.stderr.flush()
all_segments = []
for idx, (start_t, end_t) in enumerate(cut_scenes):
if idx < resume_from_scene:
continue # 跳過已處理的 scenes
seg_wav = os.path.join(temp_dir, f"seg_{idx:04d}.wav")
sys.stderr.write(f"[ASR] Scene {idx}: {start_t:.1f}-{end_t:.1f}s\n")
sys.stderr.flush()
# 用 ffmpeg 萃取出該段音訊
t0 = time.time()
cmd = ["ffmpeg", "-y", "-v", "quiet", "-i", video_path,
"-ss", str(start_t), "-to", str(end_t),
"-ar", "16000", "-ac", "1", seg_wav]
subprocess.run(cmd, check=False, capture_output=True)
sys.stderr.write(f"[ASR] Scene {idx}: ffmpeg took {time.time()-t0:.1f}s\n")
sys.stderr.flush()
if not os.path.exists(seg_wav) or os.path.getsize(seg_wav) < 100:
sys.stderr.write(f"[ASR] Scene {idx}: empty audio, skipping\n")
sys.stderr.flush()
continue
try:
t1 = time.time()
# Build transcription kwargs for segmented processing
seg_transcribe_kwargs = {
"beam_size": 5,
"vad_filter": True,
"vad_parameters": dict(min_silence_duration_ms=500, speech_pad_ms=200),
}
# Add language if specified
if language:
seg_transcribe_kwargs["language"] = language
seg_result, seg_info = model.transcribe(seg_wav, **seg_transcribe_kwargs)
sys.stderr.write(f"[ASR] Scene {idx}: transcribe took {time.time()-t1:.1f}s, language={seg_info.language}\n")
sys.stderr.flush()
scene_segments = []
seg_language = seg_info.language if seg_info else transcript_language
for segment in seg_result:
seg_start = start_t + segment.start
seg_end = start_t + segment.end
scene_idx = find_scene_idx((seg_start + seg_end) / 2)
scene_segments.append({
"start_time": round(seg_start, 2),
"end_time": round(seg_end, 2),
"start_frame": int(round(seg_start * fps)),
"end_frame": int(round(seg_end * fps)),
"text": segment.text.strip(),
"scene_number": scene_idx + 1,
"language": seg_language,
"confidence": round(segment_confidence(segment), 2),
})
total_segments += 1
# 當前 scene 結果寫入 .asr.tmp
all_segments.extend(scene_segments)
with open(tmp_path, "w") as f:
json.dump(round_json_numbers({"language": transcript_language or "", "segments": all_segments}), f, ensure_ascii=False)
if total_segments % 100 == 0:
if publisher:
publisher.progress("asr", total_segments, 0, f"Segment {total_segments}")
except Exception as e:
print(f"[ASR] Segment {idx} failed: {e}", file=sys.stderr)
# 清理暫存 WAV
try: os.remove(seg_wav)
except: pass
try: os.rmdir(temp_dir)
except: pass
# Determine status for cut_scenes branch
if total_segments > 0:
status = "has_transcript"
else:
status = "silent_audio"
info_language = transcript_language or "unknown"
print(f"[ASR] Segmented transcription complete: {total_segments} segments, status={status}", file=sys.stderr)
# Write final output with status
with open(tmp_path, "w") as f:
json.dump(round_json_numbers({"status": status, "language": info_language, "segments": all_segments}), f, ensure_ascii=False)
else:
# 無 CUT 資料,直接轉錄(原有流程)
if try_common_languages and not language:
# Use smart language detection
segments, info = transcribe_with_common_languages(model, video_path, publisher)
else:
segments, info = transcribe_with_fallback(model, video_path, publisher, language=language)
info_language = info.language
tmp_path = output_path + ".tmp"
all_segments = []
for segment in segments:
all_segments.append({
"start_time": round(segment.start, 2),
"end_time": round(segment.end, 2),
"start_frame": int(round(segment.start * fps)),
"end_frame": int(round(segment.end * fps)),
"text": segment.text.strip(),
"confidence": round(segment_confidence(segment), 2),
})
total_segments += 1
if total_segments % 100 == 0:
if publisher:
publisher.progress("asr", total_segments, 0, f"Segment {total_segments}")
# Determine status for direct transcription branch
if total_segments > 0:
status = "has_transcript"
else:
status = "silent_audio"
with open(tmp_path, "w") as f:
json.dump(round_json_numbers({"status": status, "language": info_language, "segments": all_segments}), f, ensure_ascii=False)
if publisher:
publisher.info("asr", f"ASR_LANGUAGE:{info_language}")
# rename .tmp → .json
os.rename(tmp_path, output_path)
if publisher:
publisher.complete("asr", f"{total_segments} segments")
sys.stderr.write(
f"ASR: Transcription complete, {total_segments} segments written to {output_path}\n"
)
sys.stderr.flush()
sys.exit(0)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="ASR Transcription")
parser.add_argument("video_path", help="Path to video file")
parser.add_argument("output_path", help="Output JSON path")
parser.add_argument("--uuid", "-u", help="UUID for Redis progress", default="")
parser.add_argument("--fps", type=float, help="Override FPS (default: auto-detect)")
parser.add_argument("--language", "-l", help="Force language (e.g., ja for Japanese, en for English)", default=None)
parser.add_argument("--separate-vocals", action="store_true", help="Separate vocals using demucs before transcription")
parser.add_argument("--try-common-languages", action="store_true", help="Try common languages (en, zh, ja) if auto-detection fails or produces garbled output")
args = parser.parse_args()
run_asr(args.video_path, args.output_path, args.uuid, fps=args.fps, language=args.language, separate_vocals=args.separate_vocals, try_common_languages=args.try_common_languages)