4

I have the following command working in ffmpeg, which adds 1 second of a black frame to the beginning of the video. However, I lose the audio from the original video in the output video. How can I adjust the command to make sure the original audio stays with the final output, or better yet, there is 1 second of "blank" audio at the beginning so it matches the new output video.

ffmpeg -i originalvideo -f lavfi -i color=c=black:s=1920x1080:r=25:sar=1/1 -filter_complex "[0:v] setpts=PTS-STARTPTS [main]; [1:v] trim=end=1,setpts=PTS-STARTPTS [pre]; [pre][main] concat=n=2:v=1:a=0 [out]" -map "[out]" finaloutputvideo.mp4
llogan
  • 121,796
  • 28
  • 232
  • 243
user4889724
  • 121
  • 1
  • 12

1 Answers1

2

You need to generate or add audio to be associated with the black video because each concatenated section needs the same number of video and audio streams.

ffmpeg \
-i originalvideo \
-f lavfi -i color=c=black:s=1920x1080:r=25:sar=1/1:d=1 \
-t 1 -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-filter_complex \
"[0:v]setpts=PTS-STARTPTS[mainv]; \
 [0:a]asetpts=PTS-STARTPTS[maina]; \
 [1:v][2:a][mainv][maina]concat=n=2:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" -movflags +faststart output.mp4
  • You can eliminate the (a)setpts filters for the color and anullsrc source filters since their timestamps start at 0.

  • You can avoid the trim filter because you can set the duration for each filter.

  • The -t value for the anullsrc duration only needs to be less than or equal to the duration of the black video: the concat filter will automatically pad the rest with silence.

  • With anullsrc ensure that you match the proper channel layout and audio sample rate of your main video; otherwise concat may automatically choose a "common" layout and rate that may not be what you want.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • 1
    For what it's worth, I found it way easier to [add empty audio](https://stackoverflow.com/questions/12368151/adding-silent-audio-in-ffmpeg) to my file and do the simple `concat` vs. typing out something like this. – Hendy May 04 '18 at 04:51
  • 2
    @Hendy That's also a valid method especially if no filtering is needed and the user just wants to concatenate using the concat demuxer and avoid re-encoding. However, if additional filtering is needed for all segments then re-encoding is required and everything can be done in one command using the concat filter. Just depends on the users needs. Lots of examples using both methods on this site. – llogan May 04 '18 at 18:30