Skip to content

Instantly share code, notes, and snippets.

@DusteDdk
Created July 23, 2020 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DusteDdk/447850b1737550bb46be8efbed35ff0a to your computer and use it in GitHub Desktop.
Save DusteDdk/447850b1737550bb46be8efbed35ff0a to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ -z "$2" ]
then
echo "Generate script that cuts longvideos into shorter clips."
echo "- Useful for converting old recordings into video-clips."
echo
echo "Usage: $0 CUTS OUTSCRIPT"
echo "CUTS - File describing what to do."
echo "OUTSCRIPT - Name of the script to generate."
echo
echo "The CUTS file must have the following format:"
echo "begin"
echo "inputfile1.mp4"
echo "START STOP CLIPNAME1.mp4"
echo "START STOP CLIPNAME2.mp4"
echo "START STOP CLIPNAMEn.mp4"
echo "end"
echo
echo "There can be as many clips per input files as needed."
echo "There can be as many inputfiles as needed, one per begin/end block."
echo "The START and STOP times are of format HH:MM:SS.MS"
echo
echo "Example file:"
echo "begin"
echo "tape1.mp4"
echo "00:00:00.00 00:12:45.00 dusteds_12th_bithday.mp4"
echo "00:12.45.10 00:20:00.00 xmas-1998.mp4"
echo "00:21 00:30:54.00 skitrip-1999.mp4"
echo "end"
echo
echo "begin"
echo "tape2.mp4"
echo "00:01:00.00 00:20:30.00 dusteds_13th_bithday.mp4"
echo "00:20.35.00 00:30:00.00 xmas-2000.mp4"
echo "end"
echo
echo "This script will output the lines of the cuts file as it reads them."
echo
exit 1
fi
CUTS="$1"
SCRIPT="$2"
if [ -x "$SCRIPT" ]
then
echo "Error, output script '$SCRIPT' already exist, will not overwrite."
exit 1
fi
STATE="lfb" # looking for begin
function lfb {
if [ "$1" == "begin" ]
then
STATE="rfn"
echo "begin"
fi
}
SOURCE_FILE=""
function rfn {
SOURCE_FILE="$1"
STATE="rcl"
echo "$SOURCE_FILE"
}
NUMCMDS=0
function rcl {
LINE=`echo "$1" | tr -s " "`
if [ "$LINE" == "end" ]
then
STATE="lfb"
echo "end"
echo
else
START_TIME=`echo $LINE | cut -d ' ' -f 1`
STOP_TIME=`echo $LINE | cut -d ' ' -f 2`
DEST_FILE=`echo $LINE | cut -d ' ' -f 3`
echo "$START_TIME $STOP_TIME $DEST_FILE"
CMD="ffmpeg -ss $START_TIME -i \"$SOURCE_FILE\" -c copy -to $STOP_TIME $DEST_FILE"
echo "$CMD" >> "$SCRIPT"
((NUMCMDS++))
fi
}
while IFS= read -r LINE
do
case $STATE in
("lfb") lfb "$LINE" ;;
("rfn") rfn "$LINE" ;;
("rcl") rcl "$LINE" ;;
esac
done < "$CUTS"
echo "Wrote $NUMCMDS lines to $SCRIPT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment