Skip to content

Instantly share code, notes, and snippets.

@lukas2511
Created January 8, 2021 18:59
Show Gist options
  • Save lukas2511/f9a01ee22675be568f18ec4084af7707 to your computer and use it in GitHub Desktop.
Save lukas2511/f9a01ee22675be568f18ec4084af7707 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
INFILE="rc3-mcr-11512-eng-deu-CIA_vs_Wikileaks_hd.mp4"
OUTDIR="output/"
FRAMES=1000
tmpdir="$(mktemp -d)"
framecount=$(ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 "${INFILE}")
nthframe=$((framecount/FRAMES))
duration=$(echo "$(ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=nokey=1:noprint_wrappers=1 "${INFILE}") * 1000" | bc -l | cut -d'.' -f1)
framedur=$((duration/FRAMES))
ffmpeg -i "${INFILE}" -filter_complex "[v] select=not(mod(n\,${nthframe})) [frames]; [frames] scale=160:-1 [scaled]" -map '[scaled]' -vsync vfr -compression_algo raw -pix_fmt rgb24 -vframes 1000 "${tmpdir}/%04d.tiff"
width=$(identify -format "%w" "${tmpdir}/0001.tiff")
height=$(identify -format "%h" "${tmpdir}/0001.tiff")
montage "${tmpdir}"/*.tiff -tile 6x11 -geometry '+0x+0' "${OUTDIR}/frames-%d.jpg"
montage "${tmpdir}"/*.tiff -tile x1 -geometry 1x90'!' "${OUTDIR}/timeline.jpg"
print_timestamp() {
minutes=$(((${1}/1000)/60))
seconds=$(((${1}/1000)%60))
hundreds=$((${1}%1000))
printf "%02d:%02d.%03d" ${minutes} ${seconds} ${hundreds}
}
(
echo "WEBVTT"
echo ""
lasttimestamp=0
for i in $(seq 0 $((FRAMES-1))); do
row=$(((i%66)/6))
col=$(((i%66)%6))
timestamp=$((lasttimestamp+framedur))
echo "$(print_timestamp ${lasttimestamp}) --> $(print_timestamp ${timestamp})"
echo "frames-$((i/66)).jpg?xywh=$((width*col)),$((height*row)),${width},${height}"
echo ""
lasttimestamp=$timestamp
done
)> "${OUTDIR}/timelens.vtt"
rm -r "${tmpdir}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment