Skip to content

Instantly share code, notes, and snippets.

@nbrew
Created April 25, 2016 19:47
Show Gist options
  • Save nbrew/4b45517150ffe3a0caec971a768f3163 to your computer and use it in GitHub Desktop.
Save nbrew/4b45517150ffe3a0caec971a768f3163 to your computer and use it in GitHub Desktop.
Loop an Existing Video Using FFMPEG

Loop Video Bash Script

Copys (loops) an existing mp4 file x times into a new file. Useful for taking short source video files and looping many times for use in BrightSign player.

#!/bin/bash
#
# USAGE: loop-video fileInThisDirectory howManyLoops outputFile
#
# Example:
# $ cd ~/Documents/Videos
# $ ls
# TEN-CU-E03-Vid03_1280x800.mp4 TEN-CU-E03-Vid05_1280x800.mp4
# TEN-CU-E03-Vid04_1280x800.mp4
# $ for f in *.mp4; do ~/bin/loop-video.sh "$f" 8; done
# ...
# Finished
# $ ls
# TEN-CU-E03-Vid03_1280x800.mp4 TEN-CU-E03-Vid04_1280x800_8_loops.mp4
# TEN-CU-E03-Vid03_1280x800_8_loops.mp4 TEN-CU-E03-Vid05_1280x800.mp4
# TEN-CU-E03-Vid04_1280x800.mp4 TEN-CU-E03-Vid05_1280x800_8_loops.mp4
#
# Example:
# $ ~/bin/loop-video.sh TEN-CU-E03-Vid03_1280x800.mp4
# USAGE /Users/nhyde/bin/loop-video.sh inputFile numloops [outputFile]
#
# TEN-CU-E03-Vid03_1280x800.mp4 can be looped 4 times per GB
# 4GB 8GB
# ----- -----
# 16 times 32 times
#
input="${1}"
loops="${2}"
output="${3}"
tmpfile=$(uuidgen).txt
function printLoopsPerGb {
filename="${1}"
if [ "x${filename}" != "x" ]; then
kb=$(du -k "${filename}" | cut -f1)
gb=1000000
loops_per_gb=$(expr $gb / $kb)
echo "${filename} can be looped ${loops_per_gb} times per GB"
echo " 4GB 8GB"
echo " ----- -----"
echo " $(expr $loops_per_gb \* 4) times $(expr $loops_per_gb \* 8) times"
fi
}
if [ "x${input}" == "x" ]; then
echo "USAGE: ${0} inputFile numloops [outputFile]"
exit
fi
if [ "x${loops}" == "x" ]; then
echo "USAGE: ${0} inputFile numloops [outputFile]"
echo
printLoopsPerGb ${1}
exit
fi
if [ "x${output}" == "x" ]; then
output="${input%.mp4}_${loops}_loops.mp4"
fi
echo "Looping ${input} ${loops} times into ${output}"
# try/catch-esque block
{
for i in `seq 1 $loops`; do printf "file '%s'\n" "${input}" >> $tmpfile; done
ffmpeg -f concat -i "${tmpfile}" -c copy "${output}"
rm $tmpfile
} || {
rm $tmpfile
}
echo
echo Finished
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment