123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import subprocess
- import os
- import subprocess
- class AudioProcessor:
- def __init__(self, default_sample_rate=8000, default_channels=1):
- """
- 初始化音频处理器,并设定默认采样率和声道数。
- 参数:
- default_sample_rate (int): 默认采样率,以赫兹(Hz)计。
- default_channels (int): 默认音频通道数。
- """
- self.default_sample_rate = default_sample_rate
- self.default_channels = default_channels
- def convert_audio(self, input_path, output_path):
- """
- 使用FFmpeg进行音频文件转换。
- 参数:
- input_path (str): 输入音频文件的路径。
- output_path (str): 输出音频文件的路径。
- """
- # 确定输入和输出文件类型
- input_ext = input_path.split('.')[-1]
- output_ext = output_path.split('.')[-1]
- # 构建FFmpeg命令
- ffmpeg_cmd = ['ffmpeg', '-y']
- # 设置输入格式参数
- if input_ext == 'g711a':
- ffmpeg_cmd.extend(['-f', 'alaw', '-ar', '8000', '-ac', '1'])
- ffmpeg_cmd.extend(['-i', input_path])
- # 设置输出格式参数
- if output_ext == 'pcm':
- ffmpeg_cmd.extend(['-acodec', 'pcm_s16le', '-f', 's16le'])
- elif output_ext == 'g711a':
- ffmpeg_cmd.extend(['-ar', '8000', '-ac', '1', '-acodec', 'pcm_alaw', '-f', 'alaw'])
- ffmpeg_cmd.append(output_path)
- # 执行FFmpeg命令
- command = ' '.join(ffmpeg_cmd)
- print(command) # 输出命令用于调试
- subprocess.run(command, shell=True, check=True)
|