Skip to content

Instantly share code, notes, and snippets.

@olexs
Created January 24, 2019 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olexs/2570fc03e4d2200c2257eda3f3c48fe5 to your computer and use it in GitHub Desktop.
Save olexs/2570fc03e4d2200c2257eda3f3c48fe5 to your computer and use it in GitHub Desktop.
#!/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
#
# Modified by Olex as follows:
# - cut off the beginning of each clip and concatenate before speeding the up
# - use nvidia hw codecs for re-encode when speeding up
# Video file options and locations.
INPUT_DIR="/d/Roadtrip/07-29 Day 0 Austria - Slovenia - Ivans/Dashcam"
OUTPUT_DIR="/d/Roadtrip/07-29 Day 0 Austria - Slovenia - Ivans/Hyperlapse/Output"
CONCAT_FILE_NAME="$OUTPUT_DIR/concat.mp4"
OUTPUT_FILE_NAME="$OUTPUT_DIR/timelapse.mp4"
VIDEO_EXTENSION="MP4"
# How much to speed up the clips. Quick reference:
# - 0.50 = 2x
# - 0.10 = 10x
# - 0.02 = 50x
# - 0.01 = 100x
SPEEDUP="0.033333"
# How much to trim off each sped-up clip.
TRIM_AMOUNT="01.00"
# Create a directory in which processed files will be stored.
mkdir -p "$OUTPUT_DIR"
mkdir -p "$OUTPUT_DIR/trimmed"
# Enter the input directory (where all the clips are stored).
cd "$INPUT_DIR"
# Loop through all the files, creating trimmed versions via stream copy
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 -n -threads 8 -hwaccel cuvid -c:v h264_cuvid -ss 1 -i "$FILE" -vf "setpts=$SPEEDUP*PTS" -c:v h264_nvenc -rc constqp -global_quality 20 -preset hq -rc-lookahead 32 -g 300 -an "$OUTPUT_DIR/$VIDEO_NAME-speedy.$VIDEO_EXTENSION"
#ffmpeg -y -threads 8 -hwaccel cuvid -c:v h264_cuvid -ss 1 -i "$FILE" -c:v h264_nvenc -rc constqp -global_quality 19 -preset hq -rc-lookahead 32 -g 300 -an "$OUTPUT_DIR/$VIDEO_NAME-speedy.$VIDEO_EXTENSION"
ffmpeg -n -ss $TRIM_AMOUNT -i "$FILE" -c:v copy -an "$OUTPUT_DIR/trimmed/$VIDEO_NAME-trimmed.$VIDEO_EXTENSION"
done
cd "$OUTPUT_DIR/trimmed"
# 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 -y -f concat -i concat.txt -c copy "$CONCAT_FILE_NAME"
# Enter the output directory (where the sped-up versions are stored).
cd "$OUTPUT_DIR"
# Timelapse the concatenated file
ffmpeg -y -threads 8 -hwaccel cuvid -c:v h264_cuvid -i "$CONCAT_FILE_NAME" -vf "setpts=$SPEEDUP*PTS" -c:v h264_nvenc -rc constqp -global_quality 18 -preset hq -rc-lookahead 32 -g 300 -an "$OUTPUT_FILE_NAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment