我尝试了以下命令从视频中提取音频:

ffmpeg -i Sample.avi -vn -ar 44100 -ac 2 -ab 192k -f mp3 Sample.mp3

但我得到了如下的输出

libavutil     50.15. 1 / 50.15. 1
libavcodec    52.72. 2 / 52.72. 2
libavformat   52.64. 2 / 52.64. 2
libavdevice   52. 2. 0 / 52. 2. 0
libavfilter    1.19. 0 /  1.19. 0
libswscale     0.11. 0 /  0.11. 0
libpostproc   51. 2. 0 / 51. 2. 0
SamplE.avi: Invalid data found when processing input

有人能帮忙吗?


当前回答

这是我刚才用的:

ffmpeg -i my.mkv -map 0:3 -vn -b:a 320k my.mp3

选项的解释:

my.mkv is a source video file, you can use other formats as well -map 0:3 means I want 3rd stream from video file. Put your N there - video files often has multiple audio streams; you can omit it or use -map 0:a to take the default audio stream. Run ffprobe my.mkv to see what streams does the video file have. my.mp3 is a target audio filename, and ffmpeg figures out I want an MP3 from its extension. In my case the source audio stream is ac3 DTS and just copying wasn't what I wanted 320k is a desired target bitrate -vn means I don't want video in target file

其他回答

对于寻找更简单的方法从视频文件中提取音频,同时保留原始视频文件的参数的人,您可以使用:

ffmpeg -i <video_file_name.extension> <audio_file_name.extension>

例如,运行:

ffmpeg -i screencap.mov screencap.mp3

从mov视频文件中提取mp3音频文件。

为了提取而不进行转换,我使用上下文菜单项-作为Linux中的文件管理器自定义操作-来运行以下(在检查了视频包含的音频类型之后;例如,视频包含ogg音频):

bash -c 'ffmpeg -i "$0" -map 0:a -c:a copy "${0%%.*}".ogg' %f 

这是基于ffmpeg命令ffmpeg -i INPUT -map 0:a -c:a copy OUTPUT。

我使用-map 0:1没有问题,但是,正如@ lord necbeard的评论所说,“流0:1不能保证总是音频。使用-map 0:a代替-map 0:1可以避免歧义。”

使用-b:a代替-ab,因为-ab现在已经过时了,还要确保输入的文件路径是正确的。

从视频中提取音频,我已经使用下面的命令和它的工作良好。

String[] complexCommand = {"-y", "-i", inputFileAbsolutePath, "-vn", "-ar", "44100", "-ac", "2", "-b:a", "256k", "-f", "mp3", outputFileAbsolutePath};

在这里,

-y -覆盖输出文件而不询问。 -i - FFmpeg从-i选项指定的任意数量的输入“文件”中读取 -vn -关闭录像功能 -ar -设置音频流编码时的采样率 -ac -设置音频通道数。 -b:a—设置音频比特率 -f - format

在GitHub上查看我完整的FFmpeg android项目示例。

从几个视频剪辑创建一个音频书

首先,从一堆h264文件中提取音频(如' .m4a):

for f in *.mp4; do ffmpeg -i "$f" -vn -c:a copy "$(basename "$f" .mp4).m4a"; done

vn output选项禁用视频输出(自动选择或映射任何视频流)。要完全手动控制,请参阅-map选项。

可选

如果有一个40秒的介绍,你可以用-ss参数跳过它:

for f in *.m4a; do ffmpeg -i "$f" -ss 00:00:40  -c copy crop/"$f"; done

将所有文件合并为一个:

ffmpeg -f concat -safe 0 -i <(for f in ./*.m4a; do echo "file '$PWD/$f'"; done) -c copy output.m4a

要编码mp3音频,ffmpeg.org显示了以下示例:

ffmpeg -i input.wav -codec:a libmp3lame -qscale:a 2 output.mp3

我只是通过将input.wav替换为视频文件名来从视频中提取音频。2表示190 kb/秒。您可以在上面的链接中看到其他质量级别。