#!/bin/bash

if [ "$#" -lt 1 -o "$#" -gt 3 ]; then
    echo "Usage: $0 <input-file> [start-time] [end-time]"
    exit 1
fi

input_file="$1"
start_time="$2"
end_time="$3"
input_file_base="$(basename "$input_file")"
input_file_noext="${input_file_base%.*}"
if [ "$input_file_base" = "$input_file_noext.mkv" ]; then
    output_file="$(dirname "$input_file")/${input_file_noext}_processed.mkv"
else
    output_file="$(dirname "$input_file")/$input_file_noext.mkv"
fi
opts=""
if [ ! -e "$input_file" ]; then
    echo "Input file $input_file does not exist!"
    exit 1
fi

# Compand to correct mic
opts="$opts -af compand='0:1:-90/-900|-70/-70|-30/-9|0/-3:6:0:0:0'"
# Set codecs
opts="$opts -vcodec copy"
opts="$opts -acodec mp3"
# Set time limits if appropriate
if [ -n "$start_time" ]; then
    opts="$opts -ss $start_time"
fi
if [ -n "$end_time" ]; then
    opts="$opts -to $end_time"
fi

ffmpeg \
    -i "$input_file" \
    $opts \
    "$output_file"

