21

I'm trying to concat 4 mp4 files. I'm using the command below but not able to concat

ffmpeg -i new1.mp4 -i new2.mp4 -i new3.mp4 -i new4.mp4 -filter_complex concat=n=4:v=1:a=1 output.mp4

Getting this error :

Input link in1:v0 parameters (size 640x640, SAR 16:9) do not match the corresponding output link in0:v0 parameters (640x640, SAR 427:240)

All four vides has the same codec and same size (640x640) and the same bitrate (30)

What I am doing wrong?

3 Answers 3

28

The inputs don't have identical sample aspect ratios. Try

ffmpeg -i new1.mp4 -i new2.mp4 -i new3.mp4 -i new4.mp4 -filter_complex \
        "[0]setdar=16/9[a];[1]setdar=16/9[b];[2]setdar=16/9[c];[3]setdar=16/9[d]; \
         [a][b][c][d]concat=n=4:v=1:a=1" output.mp4
Sign up to request clarification or add additional context in comments.

17 Comments

It works great thank you. But please can you give me an advice how to set scale in the same code here is the code not working : ffmpeg -i new1.mp4 -i new2.mp4 -i new3.mp4 -i new4.mp4 -filter_complex "[0]setdar=16/9:scale=640x640[a];[1]setdar=16/9:scale=640x640[b];[2]setdar=16/9:scale=640x640[c];[3]setdar=16/9:scale=640x640[d]; [a][b][c][d] concat=n=4:v=1" output.mp4
it should be scale=640x640,setdar=16/9 -- reverse order and comma, not colon.
No. You need to feed the audio streams to the concat: [a][0:a][b][1:a][c][2:a][d][3:a] concat=n=4:v=1:a=1[v][a] and then map the output -map "[v]" -map "[a]"
Yes, technically, the SARs need to be equal. Equal DAR will lead to equal SAR only when the resolutions all match.
ffmpeg was stuck in a loop and outputting a garbage video, due to the different input video framerates, but noting the "Frame rate very high for a muxer not efficiently supporting it." and taking its suggestion to add "-vsync 2" did the trick to produce a nice final video!
|
11

Here is one more simple way https://stackoverflow.com/a/48853654/6465520

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v]scale=1024:576:force_original_aspect_ratio=1[v0]; [1:v]scale=1024:576:force_original_aspect_ratio=1[v1]; [v0][0:a][v1][1:a]concat=n=2:v=1:a=1[v][a]" -map [v] -map [a] output.mp4

So, first you scale all input videos to the same resolution and then concatenate them.

UPD

The answer above does not work for videos with different aspect ratio. I constructed another filter, which first scales the videos to the desired resolution, adding a black padding if necessary, and then concats them:

ffmpeg -i 1.mp4 -i 2.mp4 -n -filter_complex "[0:v]scale=1280:720:force_original_aspect_ratio=decrease:eval=frame,pad=1280:720:-1:-1:color=black[v0]; [1:v]scale=1280:720:force_original_aspect_ratio=decrease:eval=frame,pad=1280:720:-1:-1:color=black[v1]; [v0][0:a][v1][1:a] concat=n=2:v=1:a=1 [v] [a]" -map [v] -map [a] -s hd720 -vcodec libx264 result1.mp4

3 Comments

I had it first like that but even if the scales are shown as equal the SAR values can mismatch. I got an error "Input link in0:v0 parameters (size 1280x720, SAR 9:8) do not match the corresponding output link in0:v0 parameters (1280x720, SAR 5751:5120)". The answer of Gyan above with both settings "scale=1280x720,setdar=1280/720" solved it.
can I get the scale=1280:720 parameter from the input videos? e.g., use the largest input video?
When I use your second solution, I get this error: Input link in0:v0 parameters (size 1920x1080, SAR 27000:27007) do not match the corresponding output link in0:v0 parameters (1920x1080, SAR 1:1)
1

@maksim-vorontsov's answer for different aspect ratios was so good (and I needed to do this to a lot of videos with arbitrary inputs) that I made a bash script out of it to allow concatenation and resizing of arbitrary set of input videos into a final output:

https://gist.github.com/mkatychev/c1edf136ad36bc137134d098198903b3

the core ffmpeg argbuilder is the function below:

join_files() {
  local input_n=()
  local scale_to_n=("$SEP")
  local concat_n=()
  for i in $(seq $#); do
    input_n+=("-i ${!i}")
    ((n = i - 1))

    concat_n+=("[v${n}][${n}:a]")

    local scale_to=""
    scale_to+="[${n}:v]scale=$SCALE:force_original_aspect_ratio=decrease:eval=frame,"
    scale_to+="pad=$SCALE:-1:-1:color=black[v${n}];$SEP"
    scale_to_n+=("$scale_to")
  done
  local filter_complex=""
  filter_complex+="${scale_to_n[*]}"
  filter_complex+="${concat_n[*]}"
  filter_complex+=" concat=n=$#:v=1:a=1 [v] [a]"
  # shellcheck disable=SC2068
  command $FFMPEG ${input_n[@]} -n -filter_complex "$filter_complex" \
    -map "[v]" -map "[a]" -s hd480 -vcodec libx264 "$OUT_FILE"
}

Running with --debug flag:

$ ./ffmpeg_concat_and_scale.sh video_1.mp4 video_2.flv video_3.mp4 video_4.flv --scale 640:480 --debug --out video_1_to_4.mkv

ffmpeg -i video_1.mp4 -i video_2.flv -i video_3.mp4 -i video_4.flv -n -filter_complex
 [0:v]scale=640:480:force_original_aspect_ratio=decrease:eval=frame,pad=640:480:-1:-1:color=black[v0];
 [1:v]scale=640:480:force_original_aspect_ratio=decrease:eval=frame,pad=640:480:-1:-1:color=black[v1];
 [2:v]scale=640:480:force_original_aspect_ratio=decrease:eval=frame,pad=640:480:-1:-1:color=black[v2];
 [3:v]scale=640:480:force_original_aspect_ratio=decrease:eval=frame,pad=640:480:-1:-1:color=black[v3];
[v0][0:a] [v1][1:a] [v2][2:a] [v3][3:a] concat=n=4:v=1:a=1 [v] [a] -map [v] -map [a] -s hd480 -vcodec libx264 video_1_to_4.mkv

Remove --debug to create video_1_to_4.mkv:

./ffmpeg_concat_and_scale.sh video_1.mp4 video_2.flv video_3.mp4 video_4.flv --scale 640:480 --out video_1_to_4.mkv

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.