cleanup: remove dead code and duplicate docs
- Remove session-ses_2f27.md (161KB raw session log) - Remove 49 ROOT_* duplicate files across REFERENCE/ - Remove 14 duplicate files between REFERENCE/ root and history/ - Remove asr_legacy.rs (dead code, replaced by asr.rs) - Remove src/core/worker/ (duplicate JobWorker) - Remove src/core/layers/ (empty directory) - Remove 4 .bak files in src/ - Remove 7 dead private methods in worker/processor.rs - Remove backup directory from git tracking
This commit is contained in:
@@ -18,12 +18,10 @@ Configuration:
|
||||
import sys
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import argparse
|
||||
import signal
|
||||
import subprocess
|
||||
import tempfile
|
||||
from datetime import datetime
|
||||
from faster_whisper import WhisperModel
|
||||
|
||||
PROCESSOR_VERSION = "2.1"
|
||||
@@ -164,44 +162,127 @@ def run_asr(video_path, output_path, uuid: str = ""):
|
||||
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...")
|
||||
|
||||
# Use small model with CPU (MPS not supported by faster_whisper)
|
||||
# small 模型在準確率和速度間取得最佳平衡
|
||||
model = WhisperModel("small", device="cpu", compute_type="int8")
|
||||
model = WhisperModel(MODEL_SIZE, device="cpu", compute_type="int8")
|
||||
|
||||
if publisher:
|
||||
publisher.info("asr", f"Transcribing: {video_path}")
|
||||
|
||||
# Transcribe with VAD filter for better accuracy, with PyAV fallback
|
||||
segments, info = transcribe_with_fallback(model, video_path, publisher)
|
||||
|
||||
if publisher:
|
||||
publisher.info("asr", f"ASR_LANGUAGE:{info.language}")
|
||||
|
||||
results = []
|
||||
total_segments = 0
|
||||
|
||||
for segment in segments:
|
||||
results.append(
|
||||
{"start": segment.start, "end": segment.end, "text": segment.text.strip()}
|
||||
)
|
||||
total_segments += 1
|
||||
if total_segments % 100 == 0:
|
||||
if publisher:
|
||||
publisher.progress(
|
||||
"asr", total_segments, 0, f"Segment {total_segments}"
|
||||
if cut_scenes:
|
||||
# 分段處理:對每個場景萃取音訊並轉錄
|
||||
import subprocess
|
||||
import tempfile
|
||||
import json
|
||||
temp_dir = tempfile.mkdtemp(prefix="asr_cut_")
|
||||
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"
|
||||
all_segments = []
|
||||
|
||||
for idx, (start_t, end_t) in enumerate(cut_scenes):
|
||||
seg_wav = os.path.join(temp_dir, f"seg_{idx:04d}.wav")
|
||||
# 用 ffmpeg 萃取出該段音訊
|
||||
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)
|
||||
|
||||
if not os.path.exists(seg_wav) or os.path.getsize(seg_wav) < 100:
|
||||
continue # 跳過空音訊
|
||||
|
||||
try:
|
||||
seg_result, seg_info = model.transcribe(
|
||||
seg_wav, beam_size=5,
|
||||
vad_filter=True,
|
||||
vad_parameters=dict(min_silence_duration_ms=500, speech_pad_ms=200),
|
||||
)
|
||||
if transcript_language is None:
|
||||
transcript_language = seg_info.language
|
||||
|
||||
output = {
|
||||
"language": info.language,
|
||||
"language_probability": info.language_probability,
|
||||
"segments": results,
|
||||
}
|
||||
scene_segments = []
|
||||
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": seg_start,
|
||||
"end": seg_end,
|
||||
"text": segment.text.strip(),
|
||||
"scene_number": scene_idx + 1,
|
||||
})
|
||||
total_segments += 1
|
||||
|
||||
with open(output_path, "w") as f:
|
||||
json.dump(output, f, indent=2)
|
||||
# 當前 scene 結果寫入 .asr.tmp
|
||||
all_segments.extend(scene_segments)
|
||||
with open(tmp_path, "w") as f:
|
||||
json.dump({"language": transcript_language or "", "segments": all_segments}, f)
|
||||
|
||||
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
|
||||
|
||||
info_language = transcript_language or "unknown"
|
||||
print(f"[ASR] Segmented transcription complete: {total_segments} segments", file=sys.stderr)
|
||||
else:
|
||||
# 無 CUT 資料,直接轉錄(原有流程)
|
||||
segments, info = transcribe_with_fallback(model, video_path, publisher)
|
||||
info_language = info.language
|
||||
|
||||
tmp_path = output_path + ".tmp"
|
||||
all_segments = []
|
||||
for segment in segments:
|
||||
all_segments.append({
|
||||
"start": segment.start, "end": segment.end,
|
||||
"text": segment.text.strip(),
|
||||
})
|
||||
total_segments += 1
|
||||
if total_segments % 100 == 0:
|
||||
if publisher:
|
||||
publisher.progress("asr", total_segments, 0, f"Segment {total_segments}")
|
||||
with open(tmp_path, "w") as f:
|
||||
json.dump({"language": info_language, "segments": all_segments}, f)
|
||||
|
||||
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"{len(results)} segments")
|
||||
|
||||
Reference in New Issue
Block a user