比如今天遇到一个问题,合并后的视频没有声音。
第一步,很快搜到 Concatenating videos with ffmpeg produces silent video when the first video has no audio track 这个答案。按照里面的说法,因为第一个视频(即封面视频)没有声音,所以后面就都没有声音。解决方案是,给第一个视频添加一条空白音轨,这样就不影响其它视频的声音了。
答案里的方法是:
ffmpeg -i INPUT -f lavfi -i aevalsrc=0 -shortest -y OUTPUT
试了一下不好使,而且合并时大量报错:
[aac @ 0x55e495967e80] decode_band_types: Input buffer exhausted before END element found Error while decoding stream #0:1: Invalid data found when processing input
仔细阅读答案的评论,有位同学说:
For the silent audio make sure to match the channel layout and sample rate of the other inputs. For example, 44.1kHz stereo audio:
-i anullsrc=cl=stereo:r=44100
生成空白音轨的时候,要确保声道和采样率与其它输入视频一致。比如,44.1KHz 立体声:
-i anullsrc=cl=stereo:r=44100
似乎有点关系。我立刻使用 ffprobe 查看源视频的信息:
ffprobe -i input.mp4 -show_streams -select_streams a -loglevel error
得到:
sample_rate=24000 channels=2 channel_layout=stereo
于是,我修改前面的空白音轨生成方式,这次终于成功了:
ffmpeg -i input.mp4 -f lavfi -i anullsrc=cl=stereo:r=24000 -shortest -y input-new.mp4
FFMPEG 会以第一个视频为基底,往上合并其它视频,所以第一个视频的音轨就很重要
合并视频前最好先进行转码,然后 -c copy
就好了。