ffchoppa, for quick ffmpeg edits

       
       
       
       
SCRIPTS
  Links
    To
      Create?
#!/bin/sh
#
# commands for editing multimedia files using ffmpeg
#
# NOTE: using -c copy to preserve quality, so start times may not be exact.
#
# TODO: add re-encode support with quality options
#       maybe add [hh:mm:ss] time codes in addition to seconds
#

set -e

CMD=$1
INPUT=$2
START=$3
FINISH=$4
OUTFILE=$5
CLIPLEN=$FINISH

usage() {
   echo ""
   echo "usage: ffchoppa.sh chop  [input-file] [start] [finish]  [output-filename]"
   echo "                   pluck [input-file] [start] [cliplen] [output-filename]"
   echo ""
   echo "example: ffchoppa pluck coolvideo.mp4 120 30 coolclip"
   echo "         extract 00:02:00 .. 00:02:30 from coolvideo.mp4"
   echo ""
   echo "time values are in seconds."
   echo "supports .mp4 .webm .m4a"
   echo "if you can, use a tmpfs or ramfs; this script uses temporary files."
   echo ""
   exit
}
if [ -z "$INPUT" ]; then
   echo "missing input parameter"
   usage
fi

EXT=error
FILTER_COMPLEX=error

case "$INPUT" in
   *.webm)
      EXT=webm
      FILTER_COMPLEX="[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1"
   ;;
   *.mp4)
      EXT=mp4
      FILTER_COMPLEX="[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1"
   ;;
   *.m4a) # no video
      EXT=m4a
      FILTER_COMPLEX="[0:a][1:a]concat=n=2:v=0:a=1"
   ;;
   *)
      echo "could not determine file extension / file format"
      usage;
esac
echo "ext is==$EXT";

if [ -z "$START" ]; then
   echo "missing start parameter"
   usage
fi
if [ -z "$FINISH" ]; then
   echo "missing finish parameter"
   usage
fi
if [ -z "$OUTFILE" ]; then
   OUTPUT=fftemp_$INPUT
else
   OUTPUT=$OUTFILE.$EXT
fi

case "$CMD" in
   # delete a section from start to finish
   chop)
      if [ "$START" = "0" ]; then # remove beginning
         ffmpeg -ss $FINISH -i $INPUT -c copy $OUTPUT
      elif [ "$FINISH" = "0" ]; then # remove end
         ffmpeg -i $INPUT -to $START -c copy $OUTPUT
      else
         ffmpeg -i $INPUT -to $START -c copy ffchoppa_tmp_a.$EXT # first half
         ffmpeg -ss $FINISH -i $INPUT -c copy ffchoppa_tmp_b.$EXT # second half
         ffmpeg -i ffchoppa_tmp_a.$EXT -i ffchoppa_tmp_b.$EXT \
            -filter_complex "$FILTER_COMPLEX" -vsync vfr $OUTPUT # stitch
      fi
   ;;

   # delete everything except cliplen from start
   pluck)
      ffmpeg -ss $START -i $INPUT -to $CLIPLEN -c copy  $OUTPUT #second half
   ;;
   *)
      echo "only supported commands are {chop, pluck}"
      usage
      exit
   ;;
esac
#
# other misc options,   -c:v libvpx   selects video encoder (if you remove -c copy)
#                       -b:v 1024K    selects it's bitrate
#