-
Notifications
You must be signed in to change notification settings - Fork 0
/
framerate
executable file
·92 lines (83 loc) · 2.64 KB
/
framerate
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/sh
help() { cat <</help
Usage: framerate [--count] FILE
Reads or calculates and then displays FPS for each stream in given movie(s)
Streams are identified by their codec. Some audio codecs have "frames" too.
A part of media-scripts, https://github.com/adamhotep/media-scripts
framerate v0.3+20160103 copyright 2010+ by Adam Katz, GPL
/help
exit
}
while [ -n "$1" ] && [ ! -f "$1" ]; do
case "$1" in
( -c | -count* | --count* | -true* | --true* ) COUNT="-count_frames" ;;
( -h | --help | -ch | -hc ) help ;;
( -- ) shift; break ;;
( -* ) echo "Invalid option $1 ... Try --help" >&2; exit 2 ;;
( * ) break ;;
esac
shift
done
if ! command -v ffprobe >/dev/null 2>&1; then
echo 'ERROR: command `ffprobe` (from FFmpeg) is required for this program' >&2
exit 2
fi
for input in "${@:-/dev/stdin}"; do
if [ "$#" -gt 1 ]; then
FNAME="$input:"
fi
ffprobe -show_streams $COUNT "$input" 2>&1 |awk -v FNAME="$FNAME" -F '[=:]' '
/^ *Duration: [0-9]/ { # backup duration
master_duration = $0
gsub(/,.*/, "", master_duration)
sub(/.*Duration: */, "", master_duration)
split(master_duration, time, ":")
master_duration = time[1] * 3600 + time[2] * 60 + time[3] # H + M + S.ss
}
/^ *Stream.* Video: .*[0-9] fps,/ && ! master_fps { # backup FPS
master_fps = $0
gsub(/ fps,.*/, "", master_fps)
gsub(/.*, /, "", master_fps)
master_codec = $0
gsub(/,.*/, "", master_codec)
sub(/.*Video: /, "", master_codec)
gsub(/ \(.*/, "", master_codec)
}
$1 == "codec_name" {
codec = $2
duration = frames = rate = 0
}
$1 == "duration" && $2 != "N/A" { duration = $2 } # per-stream backup
$1 == "nb_read_frames" && $2 != "N/A" { # this is N/A w/out -count_frames
frames = $2
}
$1 == "avg_frame_rate" {
rate = $2
}
$1 $2 == "TAGDURATION" || codec && $1 == "[/STREAM]" {
if (frames) {
if ($2 == "DURATION") {
# duration comes from: sec + minutes * 60 + hours * 3600
tmp = $NF + $(NF-1) * 60 + $(NF-2) * 3600
if (tmp) { duration = tmp }
}
if (! duration) { duration = master_duration }
printf "%s%s %f\n", FNAME, codec, frames / duration
retval = 1
} else if (rate != 0 && rate != "0/0") {
split(rate, division, "/")
printf "%s%s %f\n",
FNAME, codec, division[1] / division[2]
retval = 1
}
codec = "" # prevent repeating at end of stream
}
END {
if (retval != 1 && master_fps) {
printf "%s%s %f\n", FNAME, master_codec, master_fps
exit 0
}
exit 1 - retval
}
'
done