python通过FFmpeg对wav音频转码成MP3
时间: 2023-10-13 11:29:09 浏览: 325
你可以使用 Python 中的 subprocess 模块来调用 FFmpeg 进行音频转码,具体步骤如下:
1. 安装 FFmpeg
首先需要在系统中安装 FFmpeg。如果你使用的是 Linux 或 macOS 系统,可以通过终端命令安装,例如在 Ubuntu 中可以使用以下命令安装:
```bash
sudo apt-get install ffmpeg
```
如果你使用的是 Windows 系统,则需要从 FFmpeg 官网下载安装包进行安装。
2. 编写 Python 代码
接下来可以使用以下代码实现音频转码:
```python
import subprocess
def convert_to_mp3(input_file, output_file):
# 设置 FFmpeg 的命令行参数
command = ['ffmpeg', '-i', input_file, '-f', 'mp3', '-ab', '192000', '-vn', output_file]
# 调用 FFmpeg 进行转码
subprocess.call(command)
# 示例:将 input.wav 转码成 output.mp3
convert_to_mp3('input.wav', 'output.mp3')
```
上述代码中,`convert_to_mp3` 函数接收两个参数,分别是需要转码的文件路径和输出文件路径。在函数内部,使用 subprocess 模块调用 FFmpeg,设置命令行参数并进行转码。
其中,`-i` 参数指定输入文件路径,`-f mp3` 参数指定输出格式为 MP3,`-ab 192000` 参数指定比特率为 192 Kbps,`-vn` 参数禁用视频流。
3. 运行代码
保存上述代码为 Python 文件,然后在终端中运行:
```bash
python filename.py
```
即可将指定的音频文件转码成 MP3 格式,并输出到指定的文件路径。
阅读全文
相关推荐















