0
点赞
收藏
分享

微信扫一扫

golang将pcm格式音频转为mp3格式

路西法阁下 03-16 19:30 阅读 2

文章目录


前言

在使用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指定输出文件的路径
举报

相关推荐

0 条评论