ffmpeg merge dash cam mp4 videos with last second trimmed?

I have a dashcam (made in Taiwan), it generates videos by exactly 5 minutes and 1 second, the last 1 second is duplicate of next video first 1 second.

I wanna cut the last second of each video, then merge them them all, and downsampling the 1080p to 720p with optimized compression.

How can I do this with ffmpeg? thanks in advance!


Step 1:

Chop off last second from a file exactly 5m 1s long.

ffmpeg -i "input.mp4" -t 300 -c copy "input-5m.mp4"

Step 2:

Merge files and downscale to 720p

1)Create a text file containing the list of all trimmed files, in sequence, like this

file 'input1-5m.mp4'
file 'input2-5m.mp4'
file 'input3-5m.mp4'
...
file 'inputn-5m.mp4'

2)Merge and compress

ffmpeg -f concat -i "list.txt" -vf scale=1280x720,setsar=1 -sws_flags lanczos -c:v libx264 -crf 23 -c:a aac "merged-720p.mp4"

If you have an older build of ffmpeg, you may have to insert -strict -2 to skip the inferior VisualOn AAC encoder.


If you want to skip Step 1, then you can use the command below for part 2 of Step 2:

ffmpeg -f concat -i "list.txt" -vf "select='(gte(mod((t\),301),0)*lt(mod((t\),301),300))',scale=1280x720,setsar=1,setpts=N/TB/FRAME_RATE" -af "aselect='(gte(mod((t\),301),0)*lt(mod((t\),301),300))',asetpts=N/SR/TB" -sws_flags lanczos -c:v libx264 -crf 23 -c:a aac "merged-720p.mp4"

But this won't work well if any of the input files timestamps have an irregular pattern.


With the new concatdec_select flag, this can also be done as follows

1)Generate the text file, as above, but with in/out points specified:

file 'input1.mp4'
outpoint 300
file 'input2.mp4'
outpoint 300
file 'input3.mp4'
outpoint 300
...
file 'inputn.mp4'
outpoint 300   

2)Merge and compress

ffmpeg -f concat -i "list.txt" -vf "select=concatdec_select,scale=1280x720,setsar=1,setpts=N/TB/FRAME_RATE" -af "aselect=concatdec_select,asetpts=N/SR/TB" -sws_flags lanczos -c:v libx264 -crf 23 -c:a aac "merged-720p.mp4"

The wildcard with this method is that the selection cut-offs may not be precise, except in intra-coded streams.


Jeff Geerling's script automates most of Mulvya's answer. Disable the script's time compression by setting SPEEDUP="1.0", and skip exactly 1 second of overlap between clips by settting TRIM_AMOUNT="01.00". Add options like -vf scale=1280x720 to the ffmpeg command on the last line.