如果只需要查询元数据:
ffprobe -hide_banner -v quiet -show_streams -print_format flat video.mp4
[...]
streams.stream.0.duration="5221.146009"
[...]
因此,您可以解析它:
while read -r; do
if [[ "$REPLY" =~ ^streams\.stream\.([0-9])+\.duration=\"([^"]+)\"$ ]]; then
echo -E Duration of stream "${BASH_REMATCH[1]}": "${BASH_REMATCH[2]}"
fi
done < <(ffprobe -hide_banner -v quiet -show_streams -print_format flat video.mp4)
但是,如果要获得有效容器的持续时间,则需要对其进行解码:
AV_LOG_FORCE_NOCOLOR=y ffmpeg -nostdin -hide_banner -nostats -loglevel info -i video.mp4 -f null -c copy - 2>&1 | tail -n 2
解码它需要一些CPU时间,直到:
[...]
frame=130527 fps=53271 q=-1.0 Lsize=N/A time=01:27:01.12 bitrate=N/A speed=2.13e+03x
video:152014kB audio:40790kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
然后,您可以解析它:
if [[ "$(AV_LOG_FORCE_NOCOLOR=y ffmpeg -nostdin -hide_banner -nostats -loglevel info -i video.mp4 -f null -c copy - 2>&1 | tail -n 2 | head -n 1)" =~ \ time=([0-9]+):([0-9]{2}):([0-9]{2})\.([0-9]+) ]]; then
declare duration=0 us="${BASH_REMATCH[4]}" t
for t in "${BASH_REMATCH[@]:1:3}"; do
((duration *= 60))
((duration += ${t#0} ))
done
while [ ${#us} -lt 6 ]; do us+=0; done
((us >= 500000)) && ((duration++))
((duration)) || ((duration++))
fi
echo -E Duration: "$duration"