跳转至

Openlrc

基础环境安装Mac版

# 需要Rust編程語言環境(Rust和Cargo)支持
brew install rust
rustc --version
cargo --version
# 清理本地pip緩存後重試
pip cache purge
# 1.This project can be installed from PyPI:
pip install openlrc
# OR
pip install git+https://github.com/zh-plus/openlrc
# 2.Install latest fast-whisper from source:
pip install "faster-whisper @ https://github.com/SYSTRAN/faster-whisper/archive/8327d8cc647266ed66f6cd878cf97eccface7351.tar.gz"
# 3.Install PyTorch:
pip install torch torchvision torchaudio
# 4.Fix the typing-extensions issue:
pip install typing-extensions -U

执行脚本

from faster_whisper import WhisperModel
import os

def transcribe_and_save_lrc(audio_path):
    model = WhisperModel('large-v2', device='cpu', compute_type='float32')
    segments, _ = model.transcribe(audio_path, language='zh', task='transcribe')

    lrc_lines = []
    for segment in segments:
        # 格式化时间戳 [mm:ss.xx]
        start_min = int(segment.start // 60)
        start_sec = segment.start % 60
        timestamp = f"[{start_min:02d}:{start_sec:05.2f}]"
        line = f"{timestamp}{segment.text}"
        lrc_lines.append(line)

    lrc_text = "\n".join(lrc_lines)

    # 生成.lrc文件路径
    base_path, _ = os.path.splitext(audio_path)
    lrc_path = base_path + ".lrc"

    with open(lrc_path, "w", encoding="utf-8") as f:
        f.write(lrc_text)

    print(f"LRC文件已保存: {lrc_path}")

if __name__ == '__main__':
    transcribe_and_save_lrc('/Volumes/06. 陳昊宇 - 吃掉的幸福.mp3')