0

I'm trying to concatenate multiple short .mp4 video clips from a security camera. The camera records short clips, with a few seconds on either end of a timespan when motion is detected. For example, two minutes of video will often be broken up into four ~35 second clips, with the first/last few seconds of each clip being duplicative of the last/first few seconds of the previous/next clip.

I simply concatenate the clips together using the ffmpeg concat demuxer, as described here: How to concatenate two MP4 files using FFmpeg?, with

(echo file 'first file.mp4' & echo file 'second file.mp4' )>list.txt
ffmpeg -safe 0 -f concat -i list.txt -c copy output.mp4

Or else I transcode them into intermediate MPEG-2 transport streams, which I can then concatenate with the file-level concat protocol, as described here: https://trac.ffmpeg.org/wiki/Concatenate#protocol, with

ffmpeg -i "first file.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
ffmpeg -i "second file.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc output.mp4

But either way, the resulting video (output.mp4) jumps backward in time a few seconds every half-minute or so because of the duplicated frames.

I want to throw out the duplicate frames, and tie the clips together based on timestamps to achieve smooth playback of the concatenated full-length video. I'd strongly prefer to do this on Windows with ffmpeg if possible. Surely this has been done before, right? Are there timestamps in the .mp4 files that I can use to determine how much overlap there is, and then splice at the proper point-in-time? And if so, how do I read them, how do I splice at an exact point in time, and how do I get around the KeyFrames issue if I can splice at the exact point in time?

Jimbo1987
  • 111
  • 1
  • 16
  • Check [this answer](https://stackoverflow.com/a/31542673/2738151) to obtain the `creation_time` tag of a video (some videos do not have it, you have to check yours) with `ffprobe`. The same answer suggests considering ExifTool and MediaInfo. [This answer](https://stackoverflow.com/a/22243834/2738151) explains how to get the duration of a video. With that data (if available and accurate) you could be able to figure out the overlapping fragments of the videos. The [Seeking](https://trac.ffmpeg.org/wiki/Seeking) page in the FFmpeg wiki is a good starting point to cut sections of a video. – Hernán Alarcón Dec 17 '20 at 03:45
  • Thanks, Hernan, for the starting point. ffprobe revealed a creation_time that was actually the time the video was downloaded from the surveillance DVR, rounded to the nearest second. So I SSH'd into the DVR to see if the original file had useful attributes. I found something much better, that I assume all such systems probably have -- a metadata file associated with each .mp4 clip. That file gives me a *nix timestamp in milliseconds for the start and end of the clip. Now the long pole in the tent is to figure out how to find the KeyFrames and set the trimming points there. – Jimbo1987 Dec 18 '20 at 14:56
  • Related: https://video.stackexchange.com/questions/24681/concat-overlapping-videos-with-ffmpeg-using-python3/24689 – mrienstra Mar 17 '23 at 23:24

0 Answers0