#!/bin/bash

set -e

if [ -z "$3" ]; then
	cat <<- EOF
    Usage: trim.sh /path/to/file.mp4 HH:MM:SS[.ms] HH:MM:SS[.ms] [/path/to/output.mp4]
    NOTE:
        .ms (miliseconds) are optional and default .000
    Alternate flags ( environment variables ):
        REMOVE_AUDIO
            When this is set audio output is disabled
	EOF
    exit 0
fi

input="$1"
start="$2"
end="$3"

# Figure out the output file name
out="$(dirname $input)/$(basename -s .mp4 $input).slow.mp4"
if [ ! -z "$4" ]; then
    out="$4"
fi

# Figure out the copy options for audio
if [ ! -z "$REMOVE_AUDIO" ]; then
	echo "Audio: DISABLED"
	COPY_OPT="-c copy -an"
else
	echo "Audio: ENABLED"
	COPY_OPT="-c:v copy"
fi

LOG="-loglevel error"

echo Trimming video
ffmpeg $LOG -y -i "$input" -ss "$start" -to "$end" $COPY_OPT "$out"

echo File here $(pwd)
ls -la "$out"