dots

git clone git://mattcarlson.org/repos/dots.git
Log | Files | Refs

trim.sh (1152B)


      1 #!/bin/sh
      2 #
      3 # trim
      4 
      5 # get dir in which the script was executed
      6 DIR="$(dirname "${0}")"
      7 
      8 # amount of time to trim files by
      9 TRIM_BY=${2}
     10 
     11 main() {
     12     # loop over all files of certain type
     13     for f in "${DIR}"/*."${1}"; do
     14         # get base and ext
     15         base="${f%.*}"
     16         ext="${f##*.}"
     17 
     18         # get file duration in minutes and seconds
     19         time="$(ffmpeg -i "${f}" 2>&1 | grep 'Duration:' | awk -F ',' '{print $1}' | cut -f 1 -d '.' | awk -F ':' '{print $3 " " $4}')"
     20         min=$(printf '%s\n' "${time}" | awk '{print $1}')
     21         sec=$(printf '%s\n' "${time}" | awk '{print $2}')
     22 
     23         # convert minutes to seconds
     24         min_to_sec=$((min * 60))
     25 
     26         # length in pure seconds
     27         length=$((min_to_sec + sec))
     28 
     29         # length after trim
     30         final_length=$((length - TRIM_BY))
     31 
     32         printf '%s\n' "Trimming ${base}.${ext} by ${TRIM_BY} seconds..."
     33 
     34         # agree to trim file
     35         yes | ffmpeg -i "${f}" -ss 00 -t "${final_length}" -c copy "${base}-new.${ext}" 2>/dev/null
     36 
     37         # create backup just in case
     38         mv "${f}" "${f}.bak" && mv "${base}-new.${ext}" "${base}.${ext}"
     39     done
     40 }
     41 
     42 main "${@}"