Skip to content

Instantly share code, notes, and snippets.

@ahdinosaur
Created June 18, 2024 02:51
Show Gist options
  • Save ahdinosaur/8dd10a8604ec9b44bf1b726d146aab14 to your computer and use it in GitHub Desktop.
Save ahdinosaur/8dd10a8604ec9b44bf1b726d146aab14 to your computer and use it in GitHub Desktop.
Use ffmpeg to create a supercut from a directory of videos
#!/bin/bash
set -euo pipefail
mkdir -p ./cuts
if [[ $(ls -A ./cuts) ]]
then
rm ./cuts/*
fi
if [[ -f ./cuts.txt ]]
then
rm ./cuts.txt
fi
if [[ -f ./supercut.mp4 ]]
then
rm ./supercut.mp4
fi
files="$(ls -1 | egrep '\.mp4$')"
num_files="$(ls -1 | egrep '\.mp4$' | wc -l)"
video_index=0
video_start=0
seconds_per_video=1
total_seconds_per_video=10
frames_per_second=24
for file in ${files}
do
# TODO handle when video_start + seconds_per_video > total_seconds_per_video
echo "video index" $video_index
echo "video_start" $video_start
ffmpeg -hide_banner -loglevel error -ss "${video_start}" -i "${file}" -t "${seconds_per_video}" -c copy "./cuts/${video_index}.mp4"
echo "file cuts/${video_index}.mp4" >> "./cuts.txt"
video_index=$((${video_index} + 1))
video_start=$(((${video_start} + ${seconds_per_video}) % ${total_seconds_per_video}))
done
ffmpeg -hide_banner -loglevel error -f concat -i ./cuts.txt -c copy ./supercut.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment