Script to calculate clip duration from start/stop times for trimming with mencoder or ffmpeg
To trim a video clip, mencoder and ffmpeg expect a start time in hh:mm:ss, but instead of end time, want duration:
$ mencoder -ss 00:02:55 -endpos 00:07:15 -oac copy -ovc copy in.avi -o out.avi $ ffmpeg -ss 00:02:55 -t 00:07:15 -vcodec copy -acodec copy -i in.avi out.avi
Video players make it easy to identify start/stop times, but difficult to figure out duration, so I wrote a little shell script that will calculate duration from start/stop times.
Usage example:
$ calc-duration 00:02:55 00:10:10 Start: 00 hrs 02 min 55 sec Stop: 00 hrs 10 min 10 sec +----------------------+ | duration is 00:07:15 | +----------------------+
Script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #!/bin/bash #============================================================# # Calculates duration between start time and stop time # USAGE: calc-duration 00:02:00 00:10:10 # Fix: # 1. Currently expects start hour to be less than stop hour, # so won't work for a clip that's longer than 24 hrs, not an # issue for me as I don't have any of those... ;-) #============================================================# starthh=$(echo $1 | awk -F: '{print$1}') startmm=$(echo $1 | awk -F: '{print$2}') startss=$(echo $1 | awk -F: '{print$3}') stophh=$(echo $2 | awk -F: '{print$1}') stopmm=$(echo $2 | awk -F: '{print$2}') stopss=$(echo $2 | awk -F: '{print$3}') echo echo " Start:" $starthh hrs $startmm min $startss sec echo " Stop: " $stophh hrs $stopmm min $stopss sec echo if [[ $((10#$startmm)) -gt $((10#$stopmm)) && $((10#$startss)) -gt $((10#$stopss)) ]]; then # echo start mm and ss are greater than stop # start mm is greater than stop mm, so we add 60 to stop mm and # substract 1 from stop hh newstopmm1=$((10#$stopmm+10#60)) newstophh=$((10#$stophh-10#1)) # start ss is greater than stop ss, so we add 60 to stop ss and # substract 1 from stop mm newstopss=$((10#$stopss+10#60)) newstopmm=$((10#$newstopmm1-10#1)) # calculate differences diffhh=$((10#$newstophh-10#$starthh)) diffmm=$((10#$newstopmm-10#$startmm)) diffss=$((10#$newstopss-10#$startss)) elif [[ $((10#$startmm)) -gt $((10#$stopmm)) ]]; then # echo startmm is greater than stopmm # start mm is greater than stop mm, so we add 60 to stop mm and # substract 1 from stop hh newstopmm=$((10#$stopmm+10#60)) newstophh=$((10#$stophh-10#1)) # calculate differences diffhh=$((10#$newstophh-10#$starthh)) diffmm=$((10#$newstopmm-10#$startmm)) diffss=$((10#$stopss-10#$startss)) elif [[ $((10#$startss)) -gt $((10#$stopss)) ]]; then # echo startss is greater than stopss # start ss is greater than stop ss, so we add 60 to stop ss and # substract 1 from stop mm newstopss=$((10#$stopss+10#60)) newstopmm=$((10#$stopmm-10#1)) # calculate differences diffhh=$((10#$stophh-10#$starthh)) diffmm=$((10#$newstopmm-10#$startmm)) diffss=$((10#$newstopss-10#$startss)) else # echo all stop values are greater than start values # calculate differences diffhh=$((10#$stophh-10#$starthh)) diffmm=$((10#$stopmm-10#$startmm)) diffss=$((10#$stopss-10#$startss)) fi # pad single digit results with a zero (if + signs contain a single digit, add a zero, then remove + signs) pdiffhh=$(echo +$diffhh+ | sed 's/+\([0-9]\{1\}\)+/+0\1+/g;s/+//g') pdiffmm=$(echo +$diffmm+ | sed 's/+\([0-9]\{1\}\)+/+0\1+/g;s/+//g') pdiffss=$(echo +$diffss+ | sed 's/+\([0-9]\{1\}\)+/+0\1+/g;s/+//g') echo " +----------------------+" echo " | duration is" $pdiffhh:$pdiffmm:$pdiffss "|" echo " +----------------------+" echo |
Leave a comment