-
Notifications
You must be signed in to change notification settings - Fork 0
/
fencoder
executable file
·433 lines (372 loc) · 11.6 KB
/
fencoder
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/bin/bash
# Basic vars
NAME=fencoder
OPERATION=""
VIDFILE=""
VERBOSE=0
# Starting encode settings
X264_CRF=22
X264_PRESET='veryslow'
X264_OPTS='-tune film -fast-pskip 0 -partitions p8x8 -deblock -2:-2'
X264_PARAMS='ref=5:bframes=5:output-depth=10:lookahead_threads=8'
X265_CRF=24
X265_OPTS='-profile:v main10'
X265_PRESET='slower'
X265_PIXEL_FORMAT='yuv420p10le'
X265_PARAMS='qcomp=0.7:psy-rdoq=5:psy-rd=3'
# Encoding vars
SETTINGS_WIDTH=''
SETTINGS_HEIGHT=''
SETTINGS_CROP=''
SETTINGS_AUDIO_TRACKS=''
SETTINGS_SUB_TRACKS=''
SETTINGS_VIDEO_CODEC=''
SETTINGS_VIDEO_FILTERS=''
# HDR vars
FOUND_DOVI=0
FOUND_HDR10=0
FOUND_HDR10PLUS=0
HDR_COLOR_SPACE=''
HDR_COLOR_PRIMARIES=''
HDR_COLOR_TRANSFER=''
# HDR vars
# Print help
print_help() {
echo "Usage: ${NAME} COMMAND FILE"
echo
echo "Commands:"
echo " analyze Analyzes the video file & generates encoding options"
echo " encode Encodes the video file"
}
# Grab CLI opts
set +e
options=$(
getopt -n ${NAME} \
-o hv \
--long help,verbose \
-- "$@"
)
[ $? -eq 0 ] || {
echo ; print_help
exit 1
}
eval set -- "$options"
while true; do
case $1 in
-h|--help) print_help ; exit ;;
-v|--verbose) VERBOSE=1 ; shift ; break ;;
--) shift ; break ;;
*) break ;;
esac
shift
done
# Grab & check args
OPERATION="$1"
VIDFILE="$2"
if [ "$OPERATION" == "" ]; then
echo "Please specify a command"
echo
print_help
exit 1
fi
if [ "$OPERATION" != "encode" ] && \
[ "$OPERATION" != "analyze" ]
then
echo "${OPERATION}: unknown command"
echo
print_help
exit 1
fi
# Make sure we were given a video file, and it exists.
check_file() {
if [ "$VIDFILE" == "" ]; then
echo "Please specify a video file"
exit 1
fi
if [ ! -f "$VIDFILE" ]; then
echo "${VIDFILE}: file not found"
exit 1
fi
}
# Make sure all required CLI tools are installed.
check_requirements() {
set +e
which hdr10plus_tool dovi_tool ffmpeg jq >/dev/null
if [ $? -ne 0 ]; then
echo "Install required tools:"
echo " hdr10plus_tool https://github.com/quietvoid/hdr10plus_tool"
echo " dovi_tool https://github.com/quietvoid/dovi_tool"
echo " ffmpeg"
echo " jq"
exit 1
fi
set -e
}
# Ensures metadata values are the correct "ratio".
# HDR displaybrightness colors are returned by ffprobe as a ration of
# <VALUE>/<RATIO>. We need to both strip out the /50000 for passing to the
# ffmpeg command line, and make sure that it is, in fact, /50000, and correct
# if not.
process_hdr_ratios() {
local -n color=$1
local desiredRatio=$2
if [[ "$color" =~ ([0-9]+)\/([0-9]+) ]]; then
local value="${BASH_REMATCH[1]}"
local ratio="${BASH_REMATCH[2]}"
if [ $ratio -eq $desiredRatio ]; then
color=$value
else
local offset=$(($desiredRatio/$ratio))
color=$(($value*$offset))
fi
else
echo "Unexpected ffprobe outout for mastering display metadata"
exit 1
fi
}
# Determine whether video file has HDR.
# HDR video files can have HDR10, HDR10+, and/or Dolby Vision metadata.
detect_hdr() {
echo "Detecting HDR information..."
# FFprobe all metadata for the file
local probe=$(
ffprobe -hide_banner \
-loglevel warning \
-select_streams v \
-print_format json \
-show_frames \
-read_intervals "%+#1" \
-show_entries "frame=color_space,color_primaries,color_transfer,side_data_list,pix_fmt" \
-i "$VIDFILE" \
2>/dev/null
)
# Isolate the types of sidedata we have
local sidedata=$(
echo "$probe" | jq \
'.frames[].side_data_list[].side_data_type' \
2>/dev/null
)
set +e
# Do we have HDR10 metadata?
echo "$sidedata" | grep "Mastering display metadata" >/dev/null
if [ $? -eq 0 ]; then
FOUND_HDR10=1
# Grab basic mastering info
X265_PIXEL_FORMAT=$(echo $probe | jq -r '.frames[].pix_fmt')
HDR_COLOR_SPACE=$(echo $probe | jq -r '.frames[].color_space')
HDR_COLOR_PRIMARIES=$(echo $probe | jq -r '.frames[].color_primaries')
HDR_COLOR_TRANSFER=$(echo $probe | jq -r '.frames[].color_transfer')
# Grab mastering metadata
# This is a heck of a jq command, so break it up to make it easy to read
# Creates variables HDR_* for each RED_X, RED_Y, GREEN_X, etc.
local jq_data=' .frames[].side_data_list[]'
local jq_meta=' | select(.side_data_type | strings | test("Mastering display metadata"))'
local jq_map=' | to_entries|map("HDR_\(.key | ascii_upcase)=\"\(.value|tostring)\"")|.[]'
local hdrvars=$(echo $probe | jq -r "${jq_data}${jq_meta}${jq_map}")
if [ $? -ne 0 ]; then
echo "ERROR: Couldn't read HDR10 metadata"
exit 1
fi
eval $hdrvars
process_hdr_ratios HDR_RED_X 50000
process_hdr_ratios HDR_RED_Y 50000
process_hdr_ratios HDR_GREEN_X 50000
process_hdr_ratios HDR_GREEN_Y 50000
process_hdr_ratios HDR_BLUE_X 50000
process_hdr_ratios HDR_BLUE_Y 50000
process_hdr_ratios HDR_WHITE_POINT_X 50000
process_hdr_ratios HDR_WHITE_POINT_Y 50000
process_hdr_ratios HDR_MAX_LUMINANCE 10000
process_hdr_ratios HDR_MIN_LUMINANCE 10000
# Grab Content light level metadata
# Also a heck of a jq command...
# Creates variables HDR_MAX_CONTENT and HDR_MAX_AVERAGE
jq_data=' .frames[].side_data_list[]'
jq_meta=' | select(.side_data_type | strings | test("Content light level metadata"))'
jq_map=' | to_entries|map("HDR_\(.key | ascii_upcase)=\"\(.value|tostring)\"")|.[]'
hdrvars=$(echo $probe | jq -r "${jq_data}${jq_meta}${jq_map}")
if [ $? -ne 0 ]; then
echo "ERROR: Couldn't read HDR10 metadata (content light level)"
exit 1
fi
eval $hdrvars
fi
# Do we have HDR10+ metadata?
echo "$sidedata" | grep "HDR Dynamic Metadata" >/dev/null
if [ $? -eq 0 ]; then
FOUND_HDR10PLUS=1
fi
# Do we have Dolby Vision metadata?
echo "$sidedata" | grep "Dolby Vision Metadata" >/dev/null
if [ $? -eq 0 ]; then
FOUND_DOVI=1
fi
set -e
}
# Try to autodetect whether we need to crop the video
detect_crop() {
echo "Detecting crop values..."
# Detect crop
local duration=$(
printf "%.0f\n" $(
ffprobe \
-v quiet \
-show_entries format=duration \
-hide_banner \
-of default=noprint_wrappers=1:nokey=1 \
-i "$VIDFILE"
)
)
SETTINGS_CROP=$(
ffmpeg \
-i "$VIDFILE" \
-ss $((duration/10)) -t 10 \
-vf cropdetect \
-f null \
- 2>&1 \
| awk '/crop/ { print $NF }' \
| tail -1
)
# Separate out the crop values
if [[ "$SETTINGS_CROP" =~ crop=([0-9]+):([0-9]+):([0-9]+):([0-9]+) ]]; then
local x="${BASH_REMATCH[1]}"
local y="${BASH_REMATCH[2]}"
local offset_x="${BASH_REMATCH[3]}"
local offset_y="${BASH_REMATCH[4]}"
# If the offsets are low enough, don't crop
if [ "$offset_x" -le 8 ]; then
x=$((x+(offset_x*2)))
offset_x=0
fi
if [ "$offset_y" -le 8 ]; then
y=$((y+(offset_y*2)))
offset_y=0
fi
# Reassemble if we want to crop
if [ "$offset_x" -eq 0 ] && [ "$offset_y" -eq 0 ]; then
SETTINGS_CROP=''
else
SETTINGS_CROP="crop=${x}:${y}:${offset_x}:${offset_y}
"
fi
# Establish what the video size is for later
SETTINGS_WIDTH=$x
SETTINGS_HEIGHT=$y
else
echo "ERROR: Unable to parse crop information from text:"
echo "$SETTINGS_CROP"
exit 1
fi
}
# Parse video info for audio & subtitle tracks.
detect_tracks() {
echo "Detecting tracks..."
# Probe info
local aprobe=$(
ffprobe -hide_banner -loglevel error \
-show_streams \
-show_entries stream=index:stream_tags=title,language \
-select_streams a \
-of json \
"$VIDFILE"
)
local sprobe=$(
ffprobe -hide_banner -loglevel error \
-show_streams \
-show_entries stream=index:stream_tags=title,language \
-select_streams s \
-of json \
"$VIDFILE"
)
# Parse audio info
local i=0
local index=$(echo $aprobe | jq -r ".streams[$i].index")
while [ "$index" != 'null' ]; do
local lang=$(echo $aprobe | jq -r ".streams[$i].tags.language")
local new="-map 0:${index} --metadata:s:${index} language=${lang}"
SETTINGS_AUDIO_TRACKS="${SETTINGS_AUDIO_TRACKS} ${new} \\
"
# Grab next track
i=$((i+1))
index=$(echo $aprobe | jq -r ".streams[$i].index")
done
# Parse subtitle info
i=0
index=$(echo $sprobe | jq -r ".streams[$i].index")
while [ "$index" != 'null' ]; do
local lang=$(echo $sprobe | jq -r ".streams[$i].tags.language")
local new="-map 0:${index} --metadata:s:${index} language=${lang}"
SETTINGS_SUB_TRACKS="${SETTINGS_SUB_TRACKS} ${new} \\
"
# Grab next track
i=$((i+1))
index=$(echo $sprobe | jq -r ".streams[$i].index")
done
}
# Determine which codec we should use for encoding.
# SD content should use H.264, and all higher resolutions should use H.265/HEVC.
detect_codec() {
# Fill in codec, preset, quality & options
if [ $SETTINGS_WIDTH -le 854 ] || [ $SETTINGS_HEIGHT -le 480 ] ; then
SETTINGS_VIDEO_CODEC=" -c:v libx264 \\
-preset ${X264_PRESET} \\
-crf ${X264_CRF} \\
${X264_OPTS} \\
-x264-params ${X264_PARAMS}
"
else
SETTINGS_VIDEO_CODEC=" -c:v libx265 \\
-preset ${X265_PRESET} \\
-crf ${X265_CRF} \\
-pix_fmt ${X265_PIXEL_FORMAT} \\
${X265_OPTS} \\
-x265_params ${X265_PARAMS}"
# Fill in HDR10 metadata, if any
if [ $FOUND_HDR10 ]; then
local hdr_opts="colorprim=${HDR_COLOR_PRIMARIES}:transfer=${HDR_COLOR_TRANSFER}:colormatrix=${HDR_COLOR_SPACE}"
hdr_opts="${hdr_opts}:master-display=\"G(${HDR_GREEN_X},${HDR_GREEN_Y})B(${HDR_BLUE_X},${HDR_BLUE_Y})R(${HDR_RED_X},${HDR_RED_Y})WP(${HDR_WHITE_POINT_X},${HDR_WHITE_POINT_Y})L(${HDR_MAX_LUMINANCE},${HDR_MIN_LUMINANCE})\""
if [ "$HDR_MAX_CONTENT" != "" ]; then
hdr_opts="${hdr_opts}:max-cll=\"${HDR_MAX_CONTENT},${HDR_MAX_AVERAGE}\""
fi
SETTINGS_VIDEO_CODEC="${SETTINGS_VIDEO_CODEC}:${hdr_opts}"
fi
SETTINGS_VIDEO_CODEC="${SETTINGS_VIDEO_CODEC} \\
"
fi
}
# Generate a script file that runs FFmpeg with all our detected options.
create_encode_file() {
echo "Creating runner script..."
local file='fencoder_run'
local body="#!/bin/bash
ffmpeg -n
-hide_banner \\
-ss 0 \\
-i \"${VIDFILE}\" \\
-map_metadata 0 \\
-map_chapters 0 \\
-metadata title=\"\" \\
-map 0:v \\
${SETTINGS_AUDIO_TRACKS}${SETTINGS_SUB_TRACKS} -c copy \\
${SETTINGS_VIDEO_CODEC} \"Output.mkv\"
2>&1 | tee logs/Output.log
"
if [ "$SETTINGS_CROP" != '' ]; then
body="${body} -vf ${SETTINGS_CROP}
"
fi
echo -e "$body" > $file
}
main() {
# Sanity checks
check_requirements
check_file
# Analysis
detect_hdr
detect_tracks
detect_crop
detect_codec
# Create encode file
create_encode_file
}
main