Skip to content

Instantly share code, notes, and snippets.

@GideonPARANOID
Last active August 29, 2015 14:27
Show Gist options
  • Save GideonPARANOID/4b4864ef375ba59c9315 to your computer and use it in GitHub Desktop.
Save GideonPARANOID/4b4864ef375ba59c9315 to your computer and use it in GitHub Desktop.
Builds a montage from a video file, of a set length using equally spaced 1 second clips
#!/bin/bash
# functions
function get_file_extension() {
local filename=$(basename $1)
echo ${filename##*.}
}
function float_to_int() {
echo ${1%.*}
}
function build_time() {
local hours=$(expr $1 / 3600)
local mins=$(expr $1 / 60)
local secs=$(expr $1 % 60)
echo "$hours:$mins:$secs"
}
# validation
if [ -z "$1" ]; then
echo "$0 <input_file> <output_length> <output_file>"
exit
fi
input_file=$1
output_length=$2
output_file=$3
file_type=$(get_file_extension $input_file)
input_length=$(float_to_int $(ffprobe -i $input_file -show_entries format=duration -v quiet -of csv="p=0"))
if [ $(expr $input_length '>' $output_length) -eq 0 ]; then
echo 'output length greater than input length'
exit
fi
# setup
workspace_name="workspace$RANDOM"
mkdir $workspace_name
# the time between each clip
time_fraction=$(expr $(expr $input_length - 1) / $output_length)
full_path=$(realpath $workspace_name)
# generating clips
for (( i=0; i<$output_length; i++ )); do
time=$(build_time $(expr $time_fraction '*' $i))
ffmpeg -loglevel panic -ss $time -i $input_file -t 00:00:01 -c:v copy -c:a copy $workspace_name/$i.$file_type
echo "file '$full_path/$i.$file_type'" >> file_list
done
# concatenating clips
ffmpeg -loglevel panic -f concat -i file_list -c copy $output_file
# tear down
rm -r $workspace_name file_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment