Quick shell script to trim a video clip with avconv

This script is a shorter version of the shell script to trim a video clip with avconv, ffmpeg or mencoder.

Main difference is that instead of start/stop times in HH:MM:SS format, which are tedious to type, it just needs start time, defaulting to 8 sec duration (optional script arg allows custom duration).

The reason for this version is that, when making clips out of wakeboard videos, I typically just to need to extract the trick, which, from the cut in, execution, landing, and cut out, lasts about 8 sec. Less typing, means I can make clips faster. In some cases, I want something other than 8 sec, so the script allows custom duration specified as an optional last arg.

Here's the script:

#!/bin/bash

Default_Duration=8

Usage()
{
 cat << EOF

 Description
 -----------

 Simpler version of trim.vid. Only uses avconv. Instead of duration calc,
 just trims provided start time + around $Default_Duration sec, unless another value
 is provided as third script arg.

 Examples
 --------

  1. Trim 10 sec starting at 45 sec (i.e. clip will be 10 sec long, containing
     footage from 45 to 55 sec of the input video.mp4).

    $(basename $0) video.mp4 00.00.45 10

  2. Trim starting at 30 min using default duration ($Default_Duration sec):

    $(basename $0) video.mp4 00.30.00

EOF
}

if [ "$#" -lt "2" ]; then
  Usage
  exit 1
 else
  InFile=$1
  StartTime=$(sed 's/\./:/g' <<<"$2")
  if [ -n "$3" ]; then
    echo "DEBUG: setting 'Duration' to '$3' (from supplied arg)"
    Duration="$3"
  else
    Duration=$Default_Duration
  fi
fi

CheckInput()
{
 if ! [ -r $InFile ]; then
   echo "ERROR: input file \"$InFile\" not readable"
   exit 1
 fi

 if [ "$(grep -Eo '(:|\.)' <<<$StartTime | wc -l)" -ne "2" ]; then
   echo "ERROR: check StartTime format, expecting cols or dots e.g.:"
   echo "00:20:00 or 00.20.00"
   exit 1
 fi

 if [ "$(sed -r 's/(:|\.)//g' <<<$StartTime | wc -c)" -ne "7" ]; then
   echo "ERROR: check StartTime format, expecting six numeric digits, e.g.:"
   echo "00:20:00 or 00.20.00"
   exit 1
 fi  

 start=$(sed -r 's/(:|\.)//g' <<<$StartTime)
}

ConstructFileName()
{
 ext=$(egrep -io "\.[a-z0-9]{2,4}$" <<<$InFile)
 main=$(sed "s/$ext$//" <<<$InFile)
 OutFile=${main}.trim.${start}+${Duration}${ext}
}

aTrim()
{
 avconv=$(which avconv)
 if [ -x "$avconv" ]; then
   echo "INFO: using '$avconv'.."
   $avconv -i $InFile -ss $StartTime -t $Duration -codec copy -threads auto $OutFile
   echo "=================================================="
   echo "command used was:"
   echo "$avconv -i $InFile -ss $StartTime -t $Duration -codec copy -threads auto $OutFile"
   exit 0
 else
   echo "ERROR: avconv not available.."
   exit 1
 fi
}

echo
echo "checking input.."
CheckInput

echo "constructing file name for output.."
ConstructFileName

echo "trimmed video will be saved as $OutFile"

echo
echo "trimming video.."
echo "=================================================="

aTrim

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).