-
Notifications
You must be signed in to change notification settings - Fork 76
/
query-handbrake-log.sh
executable file
·229 lines (184 loc) · 5.53 KB
/
query-handbrake-log.sh
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/bash
#
# query-handbrake-log.sh
#
# Copyright (c) 2014-2024 Lisa Melton
#
about() {
cat <<EOF
$program 1.0 of November 21, 2014
Copyright (c) 2014-2024 Lisa Melton
EOF
exit 0
}
usage() {
cat <<EOF
Report information from HandBrake-generated \`.log\` files.
Usage: $program INFO [OPTION]... [FILE|DIRECTORY]...
Information types:
time time spent during transcoding
(sorted from short to long)
speed speed of transcoding in frames per second
(sorted from fast to slow)
bitrate video bitrate of transcoded output
(sorted from low to high)
ratefactor average P-frame quantizer for transcoding
(sorted from low to high)
Options:
--reverse reverse direction of sort
--unsorted don't sort
--help display this help and exit
--version output version information and exit
Results are written to standard output.
EOF
exit 0
}
syntax_error() {
echo "$program: $1" >&2
echo "Try \`$program --help\` for more information." >&2
exit 1
}
die() {
echo "$program: $1" >&2
exit ${2:-1}
}
readonly program="$(basename "$0")"
case $1 in
--help)
usage
;;
--version)
about
;;
esac
if (($# < 1)); then
syntax_error 'too few arguments'
fi
readonly info="$1"
case $info in
time|bitrate|ratefactor)
sort_options='--numeric-sort'
;;
speed)
sort_options='--numeric-sort --reverse'
;;
*)
syntax_error "unrecognized information type: $1"
;;
esac
shift
while [ "$1" ]; do
case $1 in
--reverse)
case $info in
time|bitrate|ratefactor)
sort_options='--numeric-sort --reverse'
;;
speed)
sort_options='--numeric-sort'
;;
esac
;;
--unsorted)
sort_options=''
;;
-*)
syntax_error "unrecognized option: $1"
;;
*)
break
;;
esac
shift
done
if (($# < 1)); then
syntax_error 'too few arguments'
fi
logs=()
directory_count='0'
last_directory=''
while [ "$1" ]; do
if [ ! -e "$1" ]; then
die "input not found: $1"
fi
if [ -d "$1" ]; then
directory_logs=("$1"/*.log)
directory_count="$((directory_count + 1))"
last_directory="$(cd "$1" 2>/dev/null && pwd)"
if ((${#directory_logs[*]} == 1)) && [ "$(basename "${directory_logs[0]}")" == '*.log' ]; then
die "\`.log\` files not found: $1"
else
logs=("${logs[@]}" "${directory_logs[@]}")
fi
else
file_directory="$(cd "$(dirname "$1")" 2>/dev/null && pwd)"
if [ "$last_directory" != "$file_directory" ]; then
directory_count="$((directory_count + 1))"
last_directory="$file_directory"
fi
logs=("${logs[@]}" "$1")
fi
shift
done
for item in "${logs[@]}"; do
video_name="$(basename "$item" | sed 's/\.log$//')"
if (($directory_count > 1)); then
video_name="$video_name ($(dirname "$item"))"
fi
case $info in
time|speed)
fps="$(grep 'average encoding speed' "$item" | sed 's/^.* \([0-9.]*\) fps$/\1/')"
if [ ! "$fps" ]; then
if [ "$info" == 'time' ]; then
echo "00:00:00 $video_name"
else
echo "00.000000 fps $video_name"
fi
continue
fi
if [ "$(echo "$fps" | wc -l | sed 's/^[^0-9]*//')" == '2' ]; then
# Calculate single `fps` result from two-pass `.log` file.
#
pass_1_fps="$(echo "$fps" | sed -n 1p)"
pass_2_fps="$(echo "$fps" | sed -n 2p)"
fps="$(ruby -e 'printf "%.6f", (1 / ((1 / '$pass_1_fps') + (1 / '$pass_2_fps')))')"
fi
if [ "$info" == 'time' ]; then
duration="$(grep '+ duration: ' "$item" | sed 's/^.* \([0-9:]*\)$/\1/')"
duration=($(echo " $duration " | sed 's/:/ /g;s/ 0/ /g'))
duration="$(((duration[0] * 60 * 60) + (duration[1] * 60) + duration[2]))"
rate="$(grep '+ frame rate: ' "$item")"
if [ ! "$rate" ]; then
echo "00:00:00 $video_name"
continue
fi
rate="$(echo "$rate" | sed '$!d;s/^.*+ frame rate: //;s/^.* -> constant //;s/ fps -> .*$//;s/ fps$//')"
duration="$(ruby -e 'printf "%.0f", (('$duration' * '$rate') / '$fps')')"
ruby -e 'printf "%02d:%02d:%02d '"$video_name"'\n", '$((duration / (60 * 60)))', '$(((duration / 60) % 60))', '$((duration % 60))
else
echo "$fps fps $video_name"
fi
;;
bitrate)
kbps="$(grep 'mux: track 0' "$item")"
if [ ! "$kbps" ]; then
echo "0000.00 kbps $video_name"
continue
fi
echo "$(echo "$kbps" | sed 's/^.* \([0-9.]* kbps\).*$/\1/') $video_name"
;;
ratefactor)
qp="$(grep 'x26[45] \[info\]: frame P:' "$item")"
if [ ! "$qp" ]; then
echo "00.00 $video_name"
continue
fi
echo "$(echo "$qp" | sed '$!d;s/^.* QP://;s/ *size:.*$//') $video_name"
;;
esac
done |
if [ "$sort_options" ]; then
sort $sort_options
else
cat
fi