Skip to content

Instantly share code, notes, and snippets.

@geerlingguy
Last active August 10, 2023 06:06
Show Gist options
  • Save geerlingguy/d515b8e85242b1787a4bbdc21c037495 to your computer and use it in GitHub Desktop.
Save geerlingguy/d515b8e85242b1787a4bbdc21c037495 to your computer and use it in GitHub Desktop.
Create a time lapse video from a set of real-time dash cam clips.
#!/bin/bash
#
# Batch Time-Lapse creation script.
#
# This script can be used to speed up, trim, and finally concatenate tens or
# even hundreds of video clips, e.g. from a dash cam. You can do other things,
# too, but the main things this script does include:
#
# 1. Copy across and speed up video clips from an input dir to an output dir.
# 2. Trim off the first x frames of each of the copied/sped up clips.
# 3. Concatenate (join together) all the clips into one final sped up clip.
#
# Assumptions:
#
# - `ffmpeg` is installed (`brew install ffmpeg` on macOS).
# - All video clips are the same format, and from the same long time-based-
# split recording from one dash cam or other recording device.
#
# @author Jeff Geerling, 2016.
# @see http://www.jeffgeerling.com/blog/2016/converting-batch-dashcam-videos-one-much-smaller-timelapse
# Video file options and locations.
INPUT_DIR="/Volumes/MOBIUS/DCIM/100HDDVR"
OUTPUT_DIR="/Users/jgeerling/Desktop/RoadTrip"
OUTPUT_FILE_NAME="timelapse.mov"
VIDEO_EXTENSION="MOV"
# How much to speed up the clips. Quick reference:
# - 0.50 = 2x
# - 0.10 = 10x
# - 0.05 = 50x
# - 0.01 = 100x
SPEEDUP="0.05"
# How much to trim off each sped-up clip. One frame @ 30fps = "00.03".
TRIM_AMOUNT="00.12"
# Create a directory in which processed files will be stored.
mkdir -p "$OUTPUT_DIR"
# Enter the input directory (where all the clips are stored).
cd "$INPUT_DIR"
# Loop through all the files, creating sped-up versions of each in the dest dir.
for FILE in *.$VIDEO_EXTENSION; do
VIDEO_NAME="${FILE%.*}"
# e.g. `ffmpeg -i video.mov -v "setpts=0.50*PTS" -an /path/to/speedy.mov`
ffmpeg -i $FILE -vf "setpts=$SPEEDUP*PTS" -an "$OUTPUT_DIR/$VIDEO_NAME-speedy.$VIDEO_EXTENSION"
done
# Enter the output directory (where the sped-up versions are stored).
cd "$OUTPUT_DIR"
# Create a directory in which cropped/final length sped-up clips will be stored.
mkdir -p cropped
# Trim off the first x frames of each of the processed clips.
for FILE in *.$VIDEO_EXTENSION; do
VIDEO_NAME="${FILE%.*}"
# e.g. `ffmpeg -i video.mov -v "setpts=0.50*PTS" -an /path/to/speedy.mov`
ffmpeg -i $FILE -ss $TRIM_AMOUNT -an "$OUTPUT_DIR/cropped/$VIDEO_NAME.$VIDEO_EXTENSION"
done
# Enter the cropped output directory (where the trimmed versions are stored).
cd "$OUTPUT_DIR/cropped"
# Create a file listing all the videos in the current directory.
printf "file '%s'\n" *.$VIDEO_EXTENSION > concat.txt
# Concatenate all the videos using the file listing.
ffmpeg -f concat -i concat.txt -c copy $OUTPUT_FILE_NAME
@camilleg
Copy link

Is it intentional that $OUTPUT_FILE_NAME is created in $OUTPUT_DIR/cropped instead of just $OUTPUT_DIR?

@yeled
Copy link

yeled commented Dec 5, 2018

Thanks, was very useful starter script to time-lapse my Unifi security cameras.

@geerlingguy
Copy link
Author

Just updated the script to wrap $INPUT_DIR and $OUTPUT_DIR in double quotes so paths with spaces in file/folder names would work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment