kh_split.sh

Browser view only. Use the download link if you want to save the file.

Back to Toolbox
#!/bin/bash

version="1.1"

if [ -z "$1" ] || [ -z "$2" ]; then
  echo "Usage: $(basename "$0") [video file to split] [split point]"
  echo
  echo "      Split point is in form h:m:s, such as 1:18:03"
  echo
  echo "      Output is out1.ext and out2.ext, where ext is"
  echo "      the same extension as the video filename."
  echo
  echo "      You should choose the split point as the 1st second of"
  echo "      the second video. Better to see a 1/4 second switch on"
  echo "      the very last second of the first, than the very first"
  echo "      second of the second video."
  exit 1
fi

if [ ! -f "$1" ]; then
  echo "File $1 not found here"
  exit 2
fi

if ! command -v ffmpeg &> /dev/null; then
  echo "FFMPEG not found here"
  exit 3
fi

outfile1="out1.${1##*.}"
outfile2="out2.${1##*.}"

if [ -n "$3" ]; then
  outfile1="$3.${1##*.}"
fi

if [ -n "$4" ]; then
  outfile2="$4.${1##*.}"
fi

echo
echo "$(basename "$0") $version"
echo
echo "$1 -> $outfile1 / $outfile2  (cut at $2)"
echo

## frame accurate, requires reencode
ffmpeg -y -i "$1" -c:v copy -t "$2" "$outfile1"
ffmpeg -y -i "$1" -c:v copy -ss "$2" "$outfile2"

## lands on nearest i-frame
##ffmpeg -y -i "$1" -c:v copy -t "$2" "$outfile1"
##ffmpeg -y -ss "$2" -i "$1" -c:v copy "$outfile2"

exit 0