: ########################################################################## # Title : showvideoduration - print video duration to standard output # Author : Heiner Steven # Date : 2015-04-12 # Category : Video # Requires : ffprobe # SCCS-Id. : @(#) 1.1 18/05/03 ########################################################################## # Description # o Tested with ffprobe 2.6.1 # o ffprobe(1) is part of the ffmpeg(1) package ########################################################################## PN=`basename "$0"` # Program name VER='1.1' set -u usage () { echo >&2 "$PN - print video file duration to standard output usage: $PN [-hFs] videofile [videofile ...] -s: print duration in seconds. Default: print time (e.g. \"01:53:29\") -F: continue running even after errors. Default: stop at first error. -h: print this usage summary" exit 1 } msg () { echo >&2 "$PN:" "$@"; } fatal () { msg "$@"; exit 1; } ########################################################################## # getduration - print video duration to standard output ########################################################################## getduration () { [ $# -eq 1 ] || fatal "usage: getduration videofile" if [ "$OutputFmt" = "seconds" ] then ffprobe -show_entries format=duration -print_format \ 'default=nokey=1:noprint_wrappers=1' "$1" 2>/dev/null | sed 's/[.,].*$//' # Cut off sub-second parts else ffprobe "$1" 2>&1 | awk ' # Example output: # Duration: 00:50:52.08, start: 0.000000, bitrate: 821 kb/s $1 == "Duration:" && $2 ~ /:[0-9][0-9]:[0-9][0-9]/ { duration = $2 sub(/[.,].*$/, "", duration) # No sub-second parts print duration } ' fi } ########################################################################## ForceRun=false OutputFmt=time while getopts :Fhs opt do case "$opt" in F) ForceRun=true;; s) OutputFmt=seconds;; h) usage;; ?) usage;; esac done shift `expr ${OPTIND-1} - 1` [ $# -lt 1 ] && usage error=0 for videofile do errormsg= [ -s "$videofile" ] || errormsg="empty file '$videofile'" [ -r "$videofile" ] || errormsg="cannot read file '$videofile'" if [ -z "$errormsg" ] then duration=`getduration "$videofile"` if [ -n "$duration" ] then echo "$duration $videofile" else errormsg="could not determine video duration for '$videofile'" fi fi if [ -n "$errormsg" ] then error=1 if [ "$ForceRun" != "true" ] then msg "ERROR: $errormsg" break else msg "WARNING: $errormsg - ignored" fi fi done exit $error