3

I'm trying to merge some videos but I'm getting timestamp errors.

I tried to make them all equal with the same dimensions, frame rate, sample rate and also by adding an audio track when there's none.

ffmpeg -i input1.mp4 -y -i audio1.mp3 -c:v copy -c:a aac -shortest output1.mp4
ffmpeg -i input2.mp4 -y -i audio2.mp3 -c:v copy -c:a aac -shortest output2.mp4
ffmpeg -y -safe 0 -f concat -i list.txt -c copy output.mp4

The error message:

Non-monotonous DTS in output stream 0:0; previous: 8052684, current: 4127401; changing to 8052685. This may result in incorrect timestamps in the output file.
llogan
  • 121,796
  • 28
  • 232
  • 243

1 Answers1

20

Since I'm assuming your inputs are going to be arbitrary I recommend using the concat filter instead of the concat demuxer because you're going to need to perform filtering anyway to conform everything into a common set of parameters and you can do everything in one command.

Make all videos 1280x720, 1:1 SAR, 30 fps, yuv420p

Using scale (width x height / resolution), setsar (aspect ratio), fps (frame rate), format (chroma subsampling), and concat (concatenation/joining) filters.

ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex \
"[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v0];
 [1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v1];
 [2:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v2];
 [v0][0:a][v1][1:a][v2][2:a]concat=n=3:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart output.mp4

Same as above but also processes audio to be stereo with 48000 sample rate

Added the aformat (sample rate and channel layout) filter.

ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex \
"[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v0];
 [1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v1];
 [2:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v2];
 [0:a]aformat=sample_rates=48000:channel_layouts=stereo[a0];
 [1:a]aformat=sample_rates=48000:channel_layouts=stereo[a1];
 [2:a]aformat=sample_rates=48000:channel_layouts=stereo[a2];
 [v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart output.mp4

With watermark

ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -i logo.png -filter_complex \
"[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v0];
 [1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v1];
 [2:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v2];
 [0:a]aformat=sample_rates=48000:channel_layouts=stereo[a0];
 [1:a]aformat=sample_rates=48000:channel_layouts=stereo[a1];
 [2:a]aformat=sample_rates=48000:channel_layouts=stereo[a2];
 [v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[vid][a];[vid][3]overlay=W-w-5:H-h-5[v]" \
-map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart output.mp4

For more info see overlay filter documentation and How to add and position watermark with ffmpeg?

Adding silent dummy audio for an input that does not have audio

The anullsrc filter is used to provide silent dummy audio if one of your inputs does not contain audio. This may be required because all segments to be concatenated must have the same number and type of streams. In other words, you can't concat a video without audio to a video with audio. So silent audio can be added as in this example:

ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -t 0.1 -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=48000 -filter_complex \
"[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v0];
 [1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:-1:-1,setsar=1,fps=30,format=yuv420p[v1];
 [2:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720::-1:-1,setsar=1,fps=30,format=yuv420p[v2];
 [0:a]aformat=sample_rates=48000:channel_layouts=stereo[a0];
 [2:a]aformat=sample_rates=48000:channel_layouts=stereo[a2];
 [v0][a0][v1][3:a][v2][a2]concat=n=3:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart output.mp4

Note: Leave -t 0.1 as is: the duration of anullsrc only needs to be shorter than the duration of the associated video input(s). The concat filter will automatically extend the silent audio to match the length of the associated video input.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • Hi @llogan, if I needed to add a fourth video to the above what changes with: [v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[v][a]" ? Many thanks! – MetaCoder Aug 07 '20 at 00:49
  • @Philban `[v0][a0][v1][a1][v2][a2][v3][a3]concat=n=4:v=1:a=1[v][a]` – llogan Aug 07 '20 at 03:25
  • Thank you for the help! I'm getting a error now about not matching streams. I'll open a separate question so i can paste in the command and error log. – MetaCoder Aug 07 '20 at 11:16
  • this is really helpful! however, when i try to add silent dummy audio, I got following Error: ```[pad @ 0x7f861d50f200] Cannot find color '-1' [pad @ 0x7f861d50f200] Unable to parse option value "-1" as color [pad @ 0x7f861d50f200] Cannot find color '-1' [pad @ 0x7f861d50f200] Unable to parse option value "-1" as color [pad @ 0x7f861d50f200] Error setting option color to value -1. [Parsed_pad_11 @ 0x7f861d50f140] Error applying options to the filter. [AVFilterGraph @ 0x7f861d517500] Error initializing filter 'pad' with args '1280:720::-1:-1'``` any idea @llogan ? – Anton R Sep 20 '21 at 11:35
  • @llogan im sorry, turns out it was unrelated error. it works fine now! – Anton R Sep 20 '21 at 16:38
  • @llogan can you help out this: https://stackoverflow.com/questions/71217250/track-and-add-silent-audio-in-video-using-ffmpeg-and-ffprobe-combined-single-com – Kishan Bhatiya Feb 22 '22 at 08:22
  • @llogan Is it possible that we don't reencode the middle video, get resolutions and fps from the middle video and apply those data in 1st and last video only to reduce process time? – Om Infowave Developers Apr 29 '22 at 09:31
  • I have 2 identical videos and can't concat. They have only different bitrate - is it important? And both have no audio. – Peter.k Feb 22 '23 at 19:55