Rafael
Rafael

Reputation: 388

ffmpeg leaves audio gap when concatening videos

I am trying to cut a video in 2 parts then reassembling with ffmpeg but the final output has a small audio glitch right where the segments meet. I am using the following command to split the video 1.mp4 in 2 parts:

ffmpeg -i 1.mp4 -ss 00:00:00 -t 00:00:02 -async 1 1-1.mp4

and

ffmpeg -i 1.mp4 -ss 00:00:02 -t 00:00:02 -async 1 1-2.mp4

Once I have the 2 parts I am concatening them back together with:

ffmpeg -f concat -i files.txt -c copy output.mp4

files.txt is correctly listing both files. Can anyone point me to where the problem might be?

Thanks

Upvotes: 5

Views: 2928

Answers (3)

Davide
Davide

Reputation: 17788

I run into this myself and the problem was that one audio was at 44kHz and the other at 48kHz. Once I made both at the same sample rate, the problem was solved

Upvotes: 0

shakram02
shakram02

Reputation: 11826

I had the same problem for about 3 weeks. just merge the mp3 files using sox

sox in1.mp3 in2.mp3 in3.mp3 out.mp3

When I used concat with FFMPEG it made 12.5ms (I saw them on using Audacity) audio gaps. (I don't know why)

Maybe for your case it'll be better to extract the audio and video to two separate files using ffmpeg, merge them (video using FFMPEG and audio using sox) then put the files together into one container (mp4) file

Upvotes: 0

Gyan
Gyan

Reputation: 92928

The glitch is likely due to the audio priming sample showing up in between.

Since you're re-encoding the segments, you can do this in one command:

ffmpeg -i 1.mp4 -filter_complex 
                "[0]trim=duration=2[v1];[0]trim=2:4,setpts=PTS-STARTPTS[v2];
                 [0]atrim=duration=2[a1];[0]atrim=2:4,asetpts=PTS-STARTPTS[a2];
                 [v1][a1][v2][a2]concat=n=2:v=1:a=1[v][a]"
       -map "[v]" -map "[a]" output.mp4

Upvotes: 3

Related Questions