#!/bin/sh ts2sec() { IFS=:; set -- $1 echo $((${1#0}*60*60 + ${2#0}*60 + ${3#0})) } SRC="$1" START="$2" STOP="$3" # Calculate the segment time (in seconds) SPAN="$(($(ts2sec "$STOP") - $(ts2sec "$START")))" # Generate an output filename OUT="$(basename "$SRC" .mkv)-cut.mkv" # Cut exactly the segment requested into $OUT. ffmpeg -i "$SRC" -ss "$START" -t "$SPAN" -c copy \ -c:v libx265 -preset slow -crf 23 \ -c:a libopus -b:a 96k -ac 2 -ar 48000 -y -- "$OUT" ls -lh "$SRC" "$OUT"