Skip to content

Instantly share code, notes, and snippets.

@Argus-Khan
Created November 1, 2021 02:40
Show Gist options
  • Save Argus-Khan/33bd76607fd267c818ca5663ad834c5f to your computer and use it in GitHub Desktop.
Save Argus-Khan/33bd76607fd267c818ca5663ad834c5f to your computer and use it in GitHub Desktop.
# A small bash script to spit videos in smaller parts. # Author: Argus Khan # Dependencies: ffmpeg,mideainfo
# A small bash script to spit videos in smaller parts.
# Author: Argus Khan
# Dependencies: ffmpeg,mideainfo
#!/usr/bin/env bash
function usage {
echo "Please use the following formatting:"
echo "Usage: $(basename $0) [-o OUTPUT_NAME] [-t CLIP_LENGTH] <INPUT_VIDEO_FILE>" 2>&1
echo "[Options]:"
echo ' -t CLIP_LENGTH The length of each clip in seconds.'
echo ' -o OUTPUT_NAME Custom Name for the clips.'
exit 1
}
function secToStamp {
Hr=0;Mn=0;Sc=0
Hr=$(expr $1 / 3600)
if [[ $(expr $1 % 3600) != 0 ]]; then
Mn=$(expr $(expr $1 % 3600) / 60)
if [[ $(expr $(expr $1 % 3600) % 60) != 0 ]]; then
Sc=$(expr $(expr $1 % 3600) % 60)
fi
fi
timeStamp="$Hr:$Mn:$Sc"
}
if [[ $# == 0 ]]; then
echo "[Syntax Error] No arguments found!"
echo
usage
fi
for f in $@; do :; done
vidFile=$f
interval=30
outputName=$vidFile
if ! [[ "$vidFile" =~ [a-z]\.(mp4|mkv|mov|webm|avi|ts|wmv|ogg|ogv|3gp|gifv) ]]
then
echo "[Error] Invalid file format!"
echo "Please only use the following video formats:"
echo "[mp4|mkv|mov|webm|avi|ts|wmv|ogg|ogv|3gp|gifv]"
echo
usage
fi
optstring="t:o:"
while getopts ${optstring} arg; do
case ${arg} in
t)
interval="${OPTARG}" ;;
o)
outputName="${OPTARG}" ;;
?)
echo "Invalid option: -${OPTARG}."
echo
usage
;;
esac
done
dur=$(mediainfo --Output='General;%Duration%' $vidFile)
dur=$(( $dur / 1000))
looop=$(($dur / $interval))
if [[ $(expr $dur % $interval) != 0 ]]; then
looop=$(($looop + 1))
fi
# echo "Total video duration(secs): " $dur
# echo "Total number of clips: " $looop
# echo "Input file:" $vidFile
# echo "Clip Length:" $interval
# echo "Output file:" $outputName
timePassed=0
timeStamp=""
for (( i = 1; i <= $looop ; i++ )); do
secToStamp $timePassed
# echo $timeStamp
ffmpeg -nostats -loglevel 0 -ss $timeStamp -i $vidFile -t $interval -c:v copy -c:a copy "$i"_$outputName
timePassed=$(($timePassed + $interval))
# echo "Time past: " $timePassed
done
echo "Done clipping!!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment