Dash Cam Time Lapse With Map

The steps needed to create a time lapse video using ffmpeg are fairly easy. I’ve used the gpx-animator to create map animation for use in my hiking videos. And ffmpeg can do a video within video. Might be interesting to see if all that can be combined.

Create Time Lapse

Create a time lapse video:

for f in /RawCamVideos/*.MP4 ; do echo "file '$f'" >> mylist.txt; done
ffmpeg -f concat -safe 0 -i mylist.txt \
    -vf "setpts=0.01*PTS,crop=in_w:in_w*9/16,scale=1920:1080" \
    -vcodec libx264 -b:v 10000k -r 30 -pix_fmt yuv420p -an \
    dashcam_timelapse.mp4

Extract GPX from videos

Two steps.

  • First, extract GPX from individual videos.
  • Second, merge the GPX files into a single file.
for f in RawCamVideos/*.MP4 ; do ~/bin/nvtk_mp42gpx.py -i${f} -o${f}.gpx -f ; done
cd RawCamVideos/
~/bin/gpx_merge

The nvtk_mp42gpx.py script written by Sergei Franco makes quick work of extracting gpx data from the proprietary stream embedded in Viofo dashcam MP4 files.

The gpx_merge script is pretty simple and writen in bash. It basically builds a list of gpx files in a directory then calls gpsbabel to merge them into one file.

Create GPX map animation

Get current gpx-animator app and create a map animation with the same speed up as the time lapse video.

I think there might be a way to call gpx-amimator from the command line but it is very easy to do what we want via the GUI. The final result we will put into gpx-animation.mp4

I should look into the gpx-animator code and see what the problem with the speed display is. In this video it is off by about 50%.

Inset Time Lapse into Map Animation

ffmpeg -i dashcam_timelapse.mp4 -i gpx-animation.mp4 \
    -filter_complex "[0]scale=iw/2:ih/2 [pip]; [1][pip] overlay=main_w-overlay_w-10:10" \
    -vcodec libx264 -b:v 10000k -r 30 -pix_fmt yuv420p -an \
    output.mp4

The above will scale the time lapse by half (50%) and put it in the upper right corner of the map animation. Lots of options here. For example:

ffmpeg -i gpx-animation.mp4 -i dashcam_timelapse.mp4 \
    -filter_complex "[0]scale=iw/3:ih/3 [pip]; [1][pip] overlay=main_w-overlay_w-10:main_h-overlay_h-10" \
    -vcodec libx264 -b:v 10000k -r 30 -pix_fmt yuv420p -an \
    output.mp4

Will scale the map animation to 1/3 and put it in the lower right corner of the time lapse.

Create Title Cards

Adding a sound track will make a video like this more appealing. But music has licensing and the free music available to us requires acknowledging where we got it. If we are making title cards then we may as will do one for the beginning of the video too.

Title and music “cards” are created in two steps:

  • Create a PNG from some text
  • Convert the PNG into a short video clip
cat music.txt  | convert -gravity Center -size 1920x1080 -font "Calibri" -style Normal -background black -undercolor black -fill white label:@- music.png
cat title.txt  | convert -gravity Center -size 1920x1080 -font "Calibri" -style Normal -background black -undercolor black -fill white label:@- title.png
ffmpeg -loop 1 -i music.png -tune film -t 3 -r 30.0 -pix_fmt yuv420p music.mp4
ffmpeg -loop 1 -i title.png -tune film -t 3 -r 30.0 -pix_fmt yuv420p title.mp4

At this point we have three video clips we want to join together. Create a shot list file for `ffmpeg to work with the file contains:

file 'title.mp4'
file 'output.mp4'
file 'music.mp4'

Then use ffmpeg to merge the clips together.

ffmpeg -safe 0 -f concat -i shotlist.txt -c copy silent-version.mp4

Add A Soundtrack

The only step left is to add the sound track to our video.

ffmpeg -i silent-version.mp4 -i music-for-video.mp3 -acodec aac -vcodec copy -shortest dashcam-video.mp4

The Final Result

You can view the final result on a Peertube instance here.