Advertisement
timetraveller1992

Bash Script for calculating total time in files from stdin

Aug 29th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.91 KB | None | 0 0
  1. sum=0
  2. count=0
  3. while read line; do
  4. duration=$(/opt/local/bin/ffmpeg -i "${line}" 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//)
  5. hours=$(echo $duration | cut -d":" -f1)
  6. minutes=$(echo $duration | cut -d":" -f2)
  7. seconds=$(echo $duration | cut -d":" -f3 | cut -d"." -f1)
  8. millis=$(echo $duration | cut -d":" -f3 | cut -d"." -f2)
  9. let totalMillis=$millis+$seconds*1000+$minutes*60000+$hours*3600000
  10. let count=$count+1
  11. let sum=$sum+$totalMillis
  12. done < /dev/stdin
  13. echo "$count"
  14. let avSeconds=$sum/1000
  15. let avMillis=$sum-$avSeconds*1000;
  16.  
  17. let avMinutes=$avSeconds/60;
  18. let avSeconds=$avSeconds-$avMinutes*60;
  19.  
  20. let avHours=$avMinutes/60;
  21. let avMinutes=$avMinutes-$avHours*60;
  22. str=""
  23. if [[ $avHours -gt 0 ]]; then
  24. str="$avHours hours, "
  25. fi
  26. if [[ $avMinutes -gt 0 ]]; then
  27. str="${str} ${avMinutes} minutes"
  28. fi
  29. if [[ $avSeconds -gt 0 ]]; then
  30. str="${str} and ${avSeconds}.${avMillis} seconds"
  31. fi
  32. echo $str
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement