Files
banban/talkingq-url/test/wav_2_mp3.py
2026-03-24 15:04:36 +08:00

50 lines
1.6 KiB
Python
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.

import shutil
from pathlib import Path
from pydub import AudioSegment
def check_dependencies():
dependencies = ["ffmpeg", "ffprobe"]
missing = []
for dep in dependencies:
if not shutil.which(dep):
missing.append(dep)
if missing:
print(f"缺少必要依赖: {', '.join(missing)}")
print("请安装缺失的依赖项。在Ubuntu上可以使用: sudo apt-get install ffmpeg")
return False
return True
def convert_wav_to_mp3(source_dir):
source_path = Path(source_dir)
if not source_path.exists() or not source_path.is_dir():
print(f"源目录不存在或不是一个目录: {source_dir}")
return False
wav_files = list(source_path.glob("**/*.wav"))
if not wav_files:
print(f"在目录 {source_dir} 中没有找到WAV文件")
return False
print(f"找到 {len(wav_files)} 个WAV文件开始转换...")
for wav_file in wav_files:
try:
mp3_file = wav_file.with_suffix(".mp3")
sound = AudioSegment.from_wav(str(wav_file))
sound = sound.set_frame_rate(16000).set_sample_width(2).set_channels(1)
sound.export(str(mp3_file), format="mp3", bitrate="16k")
print(f"已转换: {wav_file} -> {mp3_file}")
except Exception as e:
print(f"转换文件 {wav_file} 时出错: {str(e)}")
print("转换完成!")
return True
if __name__ == "__main__":
if not check_dependencies():
print("缺少必要依赖,程序退出")
exit(1)
source_directory = "/home/ubuntu/TalkingQ_URL/assets/roles/zhuchi"
convert_wav_to_mp3(source_directory)