文章目录
- 前言
- 一、安装ffmpeg
- 二、代码
前言
在使用ffmpeg之前也使用了很多库,但是都没有成功。
https://github.com/viert/go-lame
github.com/hajimehoshi/go-mp3
github.com/go-audio/audio/mp3
golang.org/x/mobile/exp/audio/mp3
所以本文使用ffmpeg来将pcm文件转为mp3
一、安装ffmpeg
本文选用ffmpeg,安装方式见:https://blog.csdn.net/weixin_49832841/article/details/136651985
二、代码
func PcmToMp3(inputPath string, outputPath string) (string, error) {_, err := exec.Command("ffmpeg", "-y", "-f", "s16le", "-ar", "16k", "-ac", "1", "-i", inputPath, outputPath).Output()if err != nil {fmt.Errorf("PcmToMp3 Failed,errormsg:%s", err)return "", err}return outputPath, nil
}
参数 | 说明 |
---|---|
ffmpeg | 执行的命令是FFmpeg |
-y | 覆盖目标文件而不询问 |
“-f”, “s16le” | 指定输入文件的格式为有符号16位小端字节序 |
“-ar”, “16k” | 指定音频采样率为16kHz |
“-ac”, “1” | 指定声道数为1(单声道) |
“-i”, inputPath | 指定输入文件的路径 |
outputPath | 指定输出文件的路径 |