Video assembly with ffmpeg

I recently took my car to a racetrack, covered with cameras. I wanted to post these on youtube, but encountered a few issues:

  1. Dashcam saves video in 5-minute chunks

  2. Front and Rear videos need to be combined

  3. I don’t know anything about video editing

  4. I didn’t have a working video editor

  5. Fedora doesn’t seem to ship ffmpeg, and rpmfusion doesn’t support Fedora 22 yet

The last point was somewhat resolved by a binary build of ffmpeg.

Dashcam saves video in 5-minute chunks

There is no gap or overlap with the mini0805, which makes clips easy to combine with ffmpeg’s concat filter.

I used an input file to list all the components:

$ cat Front-Run2.list

file 'Front/DCIM/100MEDIA/AMBA1299.MOV'
file 'Front/DCIM/100MEDIA/AMBA1300.MOV'
file 'Front/DCIM/100MEDIA/AMBA1301.MOV'
file 'Front/DCIM/100MEDIA/AMBA1302.MOV'
file 'Front/DCIM/100MEDIA/AMBA1303.MOV' 

Then provide that to ffmpeg:

$ ffmpeg -f concat -i Front-Run2.list -c copy Front-Run2.mov

Using the ‘copy’ codec avoids re-encoding the video, which makes this a quick operation.

This is likely a good time to trim the start and end of the video. I did this on the compiled version:

$ ffmpeg -i Front-Run2.mov -ss 0:45 -t 12:30 -c copy Front-Run2.trim.mov

After reviewing that output, I replace the previous version:

$ mv Front-Run2.trim.mov Front-Run2.mov

Again, the copy filter makes this a quick operation. -ss is the start offset (so the new video will start 0:45 seconds in). -t however is the duration, so it will end at the 13:15 mark of the original video. There is also a to option to set a stop time, but I missed that in the man page until writing this article.

This step was needed for all the videos I wanted to use.

Front and Rear videos need to be combined

This step unfortunately involved a small amount of trial-and-error.

First, I wanted to overlay the rear camera footage in the bottom corner of the front camera. This required some additional information:

  • I’m combining Front-Run2.mov and Rear-Run2.mov
  • Videos are 1920x1080. I want the rear video at 25%. That’s 480x270
  • I want the overlay to be in from the edge. Lets say by 70 pixels.
  • There may be a better overlay method, but this works.

Now, in a perfect world, you can just compile the “finished” version. However, both of my dash cams start recording with a variance of 1-3 seconds, so the videos don’t line up exactly (even though I used the same trimming). My video has my car start moving at 1:25, so I made a 30 second video starting at 1:15.

Note that we can’t use the ‘copy’ codec, as we’re actually modifying the video at this point. This makes the 30 second clips a massive time saver.

$ ffmpeg -i Front-Run2.mov -vf "movie=Rear-Run2.mov, scale=480:-1 [inner]; [in][inner] overlay=70:740 [out]" -ss 1:15 -t 30 Sample-Run2.mov

I then watched the video to time the difference between when each car started moving. I found the rear video was about 1 second behind. I trimmed it using -ss 0:01, then recreated my 30 second sample. Once I was satisfied, I generated the entire video.

$ ffmpeg -i Front-Run2.mov -vf "movie=Rear-Run2.mov, scale=480:-1 [inner]; [in][inner] overlay=70:740 [out]" Combined-Run2.mov

Slight sound censoring

I was discussing a few things I’d rather not have in the video. This means I needed to mute three sections of the audio.

I watched the video, taking note of the ranges I wanted to mute:

  • 7:14-7:19
  • 8:47-9:22
  • 15:02-15:34

Unfortunately, the volume filter I was using seemed to only take seconds… So:

  • 434-439
  • 526-562
  • 902-934

Now that I’ll be building my “final” video, I converted it to a “faststart” mp4 based on youtube’s video recommendations. Also, we’re not modifying the video, so we can use -vcodec copy. The audio will be re-encoded, due to the filters.

$ ffmpeg -i Combined-Run2.mov -af "volume=enable='between(t,434,439)':volume=0, volume=enable='between(t,526,562)':volume=0, volume=enable='between(t,902,934)':volume=0" -vcodec copy -movflags faststart Final-Run2.mp4

Final result

You can see the resulting video on youtube.

Outstanding issues

So all videos were 1920x1080, but youtube only offers “720p”. I don’t know why.