ba18f4c403
- Upgrade Whisper model: small -> large-v3 - Add --language parameter for forced language detection - Add --separate-vocals for demucs vocal separation - Add preprocess_audio.py utility - Fix Japanese detection failure issue - Improve transcription accuracy significantly Usage: python3 scripts/asr_processor.py video.mp4 output.json --language ja --separate-vocals
147 lines
3.7 KiB
Python
147 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
音頻預處理工具
|
|
使用 demucs 分離人聲
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
|
|
def separate_vocals(audio_path, output_dir=None):
|
|
"""
|
|
使用 demucs 分離人聲軌道
|
|
|
|
Args:
|
|
audio_path: 音頻文件路徑
|
|
output_dir: 輸出目錄(默認為音頻文件所在目錄)
|
|
|
|
Returns:
|
|
vocals_path: 分離後的人聲文件路徑
|
|
"""
|
|
|
|
if output_dir is None:
|
|
output_dir = os.path.dirname(audio_path)
|
|
|
|
print(f"[VOCAL_SEP] 分離人聲: {audio_path}")
|
|
|
|
# 使用 demucs 分離
|
|
cmd = [
|
|
"python3", "-m", "demucs",
|
|
"--two-stems=vocals", # 只分離人聲和伴奏
|
|
"-o", output_dir,
|
|
audio_path
|
|
]
|
|
|
|
try:
|
|
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
|
|
|
# demucs 的輸出路徑
|
|
audio_name = Path(audio_path).stem
|
|
vocals_path = os.path.join(
|
|
output_dir,
|
|
"htdemucs", # 默認模型名
|
|
audio_name,
|
|
"vocals.wav"
|
|
)
|
|
|
|
if os.path.exists(vocals_path):
|
|
print(f"[VOCAL_SEP] 成功: {vocals_path}")
|
|
return vocals_path
|
|
else:
|
|
print(f"[VOCAL_SEP] 錯誤: 找不到輸出文件")
|
|
return None
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"[VOCAL_SEP] 失敗: {e.stderr}")
|
|
return None
|
|
except FileNotFoundError:
|
|
print("[VOCAL_SEP] 錯誤: demucs 未安裝")
|
|
print("安裝命令: pip3 install demucs --break-system-packages")
|
|
return None
|
|
|
|
|
|
def extract_audio_from_video(video_path, audio_path=None):
|
|
"""
|
|
從視頻中提取音頻
|
|
|
|
Args:
|
|
video_path: 視頻文件路徑
|
|
audio_path: 輸出音頻路徑(默認為視頻同目錄)
|
|
|
|
Returns:
|
|
audio_path: 提取的音頻路徑
|
|
"""
|
|
|
|
if audio_path is None:
|
|
audio_path = video_path.rsplit(".", 1)[0] + ".wav"
|
|
|
|
print(f"[AUDIO_EXTRACT] 提取音頻: {video_path}")
|
|
|
|
cmd = [
|
|
"ffmpeg", "-y",
|
|
"-i", video_path,
|
|
"-vn", # 不包含視頻
|
|
"-acodec", "pcm_s16le", # 16-bit PCM
|
|
"-ar", "16000", # 16kHz (Whisper 優化)
|
|
"-ac", "1", # 單聲道
|
|
audio_path
|
|
]
|
|
|
|
try:
|
|
subprocess.run(cmd, capture_output=True, check=True)
|
|
print(f"[AUDIO_EXTRACT] 成功: {audio_path}")
|
|
return audio_path
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"[AUDIO_EXTRACT] 失敗: {e.stderr.decode()}")
|
|
return None
|
|
|
|
|
|
def preprocess_audio_for_asr(video_path, output_dir=None, separate=True):
|
|
"""
|
|
ASR 音頻預處理
|
|
|
|
Args:
|
|
video_path: 視頻文件路徑
|
|
output_dir: 輸出目錄
|
|
separate: 是否分離人聲
|
|
|
|
Returns:
|
|
處理後的音頻路徑
|
|
"""
|
|
|
|
print(f"[PREPROCESS] 預處理音頻: {video_path}")
|
|
|
|
# 1. 提取音頻
|
|
audio_path = extract_audio_from_video(video_path)
|
|
if not audio_path:
|
|
return None
|
|
|
|
# 2. 分離人聲(可選)
|
|
if separate:
|
|
vocals_path = separate_vocals(audio_path, output_dir)
|
|
if vocals_path:
|
|
# 刪除原始音頻
|
|
os.remove(audio_path)
|
|
return vocals_path
|
|
|
|
return audio_path
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 2:
|
|
print("用法: python preprocess_audio.py <video_or_audio_path> [--no-separate]")
|
|
sys.exit(1)
|
|
|
|
input_path = sys.argv[1]
|
|
separate = "--no-separate" not in sys.argv
|
|
|
|
result = preprocess_audio_for_asr(input_path, separate=separate)
|
|
|
|
if result:
|
|
print(f"\n✅ 成功: {result}")
|
|
else:
|
|
print("\n❌ 失敗")
|
|
sys.exit(1) |