Skip to content

Instantly share code, notes, and snippets.

@maruel
Last active August 22, 2024 17:38
Show Gist options
  • Save maruel/ffbcd83f204561346bbe3cc51682e7af to your computer and use it in GitHub Desktop.
Save maruel/ffbcd83f204561346bbe3cc51682e7af to your computer and use it in GitHub Desktop.
Merge all tesla sentry videos with timestamp overlay
#!/bin/bash
# Copyright 2024 Marc-Antoine Ruel
# Original at https://gist.github.com/maruel/ffbcd83f204561346bbe3cc51682e7af
#
# Merges mp4 from Tesla Sentry into 4 videos files: front.mp4, back.mp4,
# left_repeater.mp4, right_repeater.mp4. Overlay the timestamp on the video.
#
# Usage:
# - Install ffmpeg.
# - Copy all the files from your Tesla Sentry USB key in a directory.
# - Run this script from this directory.
set -eu
# Get the first file name to determine the start time.
EPOCH="$(python3 -c "import datetime,glob;d=datetime.datetime;print(round((d.strptime(sorted(glob.glob('*-front.mp4'))[0][:19],'%Y-%m-%d_%H-%M-%S')-d(1970,1,1)).total_seconds()))")"
FONTFILE="/usr/share/fonts/truetype/noto/NotoSansMono-Regular.ttf"
CONTENT="text='%{pts\:gmtime\:$EPOCH\:%Y-%m-%d %T}': "
ASPECT="fontsize=48: fontcolor=white: box=1: boxcolor=black@0.5:"
LOCATION="x=(w-text_w-10): y=(h-text_h-10):"
SETTINGS="drawtext=fontfile=$FONTFILE: $CONTENT $LOCATION $ASPECT"
for side in front back left_repeater right_repeater; do
NAME="${side}.mp4"
echo "- Generating $NAME"
rm -f inputs.txt
for i in $(ls *-${side}.mp4); do echo "file '$i'" >> inputs.txt; done
# Options to make it smaller:
# -r 1
# -crf 37
ffmpeg -hide_banner -loglevel error -f concat -safe 0 -i inputs.txt -r 10 \
-vf "$SETTINGS" -movflags +faststart -preset veryslow $NAME
rm inputs.txt
done
#!/bin/bash
# Copyright 2024 Marc-Antoine Ruel
# Original at https://gist.github.com/maruel/ffbcd83f204561346bbe3cc51682e7af
#
# Generates a film strip from each videos.
#
# Assumes merge_tesla_sentry_videos.sh was run before.
#
# Adapt this code to your specific needs.
set -eu
function gen_strip {
side=$1
offset=$2
fps=$3
duration=$4
tile=$5
out=$6
rm -f frame_*.png
echo "Doing $out"
ffmpeg -y -hide_banner -loglevel error -i ${side}.mp4 \
-ss $offset -vf "fps=$fps" -t $duration frame_%03d.png
ffmpeg -y -hide_banner -loglevel error -i frame_%03d.png \
-filter_complex "tile=$tile" $out
rm frame_*.png
}
#for side in front back left_repeater right_repeater; do
# gen_strip $side 00:00:00 1/5 50 2x5 ${side}.jpg
#done
# Adapt to your needs:
gen_strip front 00:00:00 1/2 20 2x5 front1.jpg
gen_strip front 00:09:00 1/2 80 5x8 front2.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment