-
Notifications
You must be signed in to change notification settings - Fork 2
/
reencode
executable file
·699 lines (639 loc) · 18.4 KB
/
reencode
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#!/bin/bash
# SPDX-FileCopyrightText: 2017 - 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -Eeuo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command bc ffmpeg ffprobe sed && exit 3
#
# Globals
#
A_BITRATE_PER_STREAM=64
NO_DEFAULT_SUBS=false
CROP=false
PROBESIZE=1G
ANALYZEDURATION=1G
#
# Functions
#
function set_audio_bitrate {
local in="${1}"
local n_streams="${2}"
local in_abitrate="${3}"
local abitrate # stores bitrate in kbps
abitrate="$((A_BITRATE_PER_STREAM * n_streams))"
if [[ "${in_abitrate}" = 0 ]]; then
echo "${abitrate}k"
elif [[ "${in_abitrate}" = "N/A" ]]; then
echo "${abitrate}k"
elif [[ "${abitrate}" -gt "${in_abitrate}" ]]; then
echo "${in_abitrate}k"
else
echo "${abitrate}k"
fi
}
function convert_codec {
local in="${BACKUP}"
local out
local cmd=ffmpeg
local idx
cmd="${cmd}${PROBESIZE:+ -probesize ${PROBESIZE}}"
cmd="${cmd}${ANALYZEDURATION:+ -analyzeduration ${ANALYZEDURATION}}"
cmd="${cmd} -v info -i file:${in@Q} -map_chapters 0 -map_metadata 0"
if "${NO_DEFAULT_SUBS}"; then
cmd="${cmd} -default_mode infer_no_subs -disposition:s -default"
fi
if [[ -v START ]] && [[ -n "${START}" ]]; then
cmd="${cmd} -ss ${START}"
fi
if [[ -v DURATION ]] && [[ -n "${DURATION}" ]]; then
cmd="${cmd} -t ${DURATION}"
fi
if ! [[ "${VSTREAM_ID}" = "-1" ]]; then
cmd="${cmd} -map 0:v:${VSTREAM_ID:-?}"
if "${CROP}"; then
local _tmp
if _tmp="$(compute_crop_filter "${in}")"; then
cmd="${cmd} -vf '${_tmp}')'"
else
show_warning "WARNING: No croppable regions detected."
fi
fi
# set video codec
if [ -n "${VSTREAM_ID}" ]; then
if [[ "${IN_VSTREAM_CODEC_NAME[${VSTREAM_ID}]}" =~ jpeg|jpg|png ]]; then
show_error "ERROR: JPEG/PNG stream ${VSTREAM_ID} cannot be reencoded as video. Exiting."
exit 3
fi
case "${OUT_VCODEC}" in
h264 | x264 | libx264 | h.264 | x.264)
cmd="${cmd} -c:v:${VSTREAM_ID} libx264 -x264-params crf=${CRF} -preset slow"
;;
h265 | x265 | libx265 | h.265 | x.265)
cmd="${cmd} -c:v:${VSTREAM_ID} libx265 -x265-params crf=${CRF} -preset slow"
;;
copy)
cmd="${cmd} -c:v:${VSTREAM_ID} copy"
;;
*)
show_error "ERROR: video codec ${OUT_VCODEC@Q} is not supported. Exiting."
exit 3
;;
esac
else
for idx in $(seq 0 $((${#IN_VSTREAM_CODEC_NAME[@]} - 1))); do
if ! [[ "${IN_VSTREAM_CODEC_NAME[${idx}]}" =~ jpeg|jpg|png ]]; then
case "${OUT_VCODEC}" in
h264 | x264 | libx264 | h.264 | x.264)
cmd="${cmd} -c:v:${idx} libx264 -x264-params crf=${CRF} -preset slow"
;;
h265 | x265 | libx265 | h.265 | x.265)
cmd="${cmd} -c:v:${idx} libx265 -x265-params crf=${CRF} -preset slow"
;;
copy)
cmd="${cmd} -c:v:${idx} copy"
;;
*)
show_error "ERROR: video codec ${OUT_VCODEC@Q} is not supported. Exiting."
exit 3
;;
esac
else
show_warning "WARNING: JPEG/PNG stream ${idx} cannot be reencoded as video. Skipping."
cmd="${cmd} -c:v:${idx} copy"
fi
done
fi
fi
if ! [[ "${ASTREAM_ID}" = "-1" ]]; then
cmd="${cmd} -map 0:a:${ASTREAM_ID:-?}"
# set audio codec
case "${OUT_ACODEC}" in
libfdk_aac | aac)
# this only works for ffmpeg manually compiled with libfdk_aac
if [[ -v OUT_ABITRATE ]]; then
cmd="${cmd} -c:a libfdk_aac -b:a ${OUT_ABITRATE}"
else
cmd="${cmd} -c:a libfdk_aac -vbr 5"
fi
;;
libflac | flac)
cmd="${cmd} -c:a flac"
;;
libmp3lame | mp3lame | mp3)
cmd="${cmd} -c:a libmp3lame -b:a ${OUT_ABITRATE:-320k}"
;;
libopus | opus)
if [[ -v OUT_ABITRATE ]]; then
cmd="${cmd} -c:a libopus -b:a ${OUT_ABITRATE}"
else
# set computed bitrates individually for each channel because input
# channels can have different bitrates and channel layouts
cmd="${cmd} -c:a libopus"
if [ -n "${ASTREAM_ID}" ]; then
cmd="${cmd} -b:a:0 $(set_audio_bitrate \
"${in}" \
"${IN_ASTREAM_CHANNEL_COUNT[${ASTREAM_ID}]}" \
"${IN_ASTREAM_BITRATE[${ASTREAM_ID}]}")"
if [[ "${IN_ASTREAM_CHANNEL_LAYOUT[${ASTREAM_ID}]}" = "5.1(side)" ]]; then
show_warning "WARNING: converting layout from 5.1(side) to 5.1."
cmd="${cmd} -channel_layout:a:0 5.1"
fi
else
for idx in $(seq 0 $((${#IN_ASTREAM_CHANNEL_COUNT[@]} - 1))); do
cmd="${cmd} -b:a:${idx} $(set_audio_bitrate \
"${in}" \
"${IN_ASTREAM_CHANNEL_COUNT[${idx}]}" \
"${IN_ASTREAM_BITRATE[${idx}]}")"
if [[ "${IN_ASTREAM_CHANNEL_LAYOUT[${idx}]}" = "5.1(side)" ]]; then
show_warning "WARNING: converting layout from 5.1(side) to 5.1."
cmd="${cmd} -channel_layout:a:${idx} 5.1"
fi
done
fi
fi
;;
copy)
cmd="${cmd} -c:a copy"
;;
*)
show_error "ERROR: audio codec ${OUT_ACODEC@Q} not supported. Exiting."
exit 3
;;
esac
fi
# specify frame size if different from the input
if [[ -v IN_DIMENSIONS ]]; then
if ! [[ "${IN_DIMENSIONS}" = "${OUT_DIMENSIONS}" ]]; then
# only add the size parameter if the output width/height are not larger
# that the corresponding input values
if [[ "$(echo "${IN_DIMENSIONS}" | cut -d"x" -f1)" -lt "$(echo "${OUT_DIMENSIONS}" | cut -d"x" -f1)" ]] ||
[[ "$(echo "${IN_DIMENSIONS}" | cut -d"x" -f2)" -lt "$(echo "${OUT_DIMENSIONS}" | cut -d"x" -f2)" ]]; then
show_warning \
"WARNING: Dimensions ${OUT_DIMENSIONS@Q} exceed input ${IN_DIMENSIONS@Q}."
else
cmd="${cmd} -s ${OUT_DIMENSIONS}"
fi
fi
fi
# set subtitle format
case "${OUT_CONTAINER}" in
mp4)
# convert subtitle format to mov_text for mp4 output
if [[ "${IN_CONTAINER}" = "${OUT_CONTAINER}" ]]; then
cmd="${cmd} -map 0:s:? -c:s copy"
elif [[ "${IN_SCODEC}" = 94213 ]]; then
cmd="${cmd} -map 0:s:? -c:s mov_text"
else
cmd="${cmd} -map 0:s:? -c:s copy"
fi
;;
matroska | mkv)
# convert 94213 to srt
case "${IN_SCODEC}" in
94213)
cmd="${cmd} -map 0:s:? -c:s srt"
;;
*)
cmd="${cmd} -map 0:s:? -c:s copy"
;;
esac
;;
*)
# copy subtitle codec by default
cmd="${cmd} -map 0:s:? -c:s copy"
;;
esac
# copy any attachments
cmd="${cmd} -map 0:t:? -c:t copy"
# manually specify the container format (ffmpeg may not infer from file name)
case "${OUT_CONTAINER}" in
avi)
cmd="${cmd} -f avi"
out="${NAME}.avi"
;;
mpg)
cmd="${cmd} -f mpg"
out="${NAME}.mpg"
;;
mkv | matroska)
cmd="${cmd} -f matroska"
out="${NAME}.mkv"
;;
mka)
cmd="${cmd} -f matroska"
out="${NAME}.mka"
;;
mp4)
cmd="${cmd} -movflags +faststart -f mp4"
out="${NAME}.mp4"
;;
mp3)
cmd="${cmd} -f mp3"
out="${NAME}.mp3"
;;
flac)
cmd="${cmd} -f flac"
out="${NAME}.flac"
;;
wav)
cmd="${cmd} -f wav"
out="${NAME}.wav"
;;
opus)
if [ "${#IN_VSTREAM_CODEC_NAME[@]}" -eq 0 ] || [ "${VSTREAM_ID}" -eq -1 ]; then
cmd="${cmd} -f opus"
out="${NAME}.opus"
else
show_warning "WARNING: Opus does not support video streams. Switching to Matroska."
OUT_CONTAINER=mka
cmd="${cmd} -f matroska"
out="${NAME}.mka"
fi
;;
*)
show_error "ERROR: Container ${OUT_CONTAINER@Q} not supported. Exiting."
exit 3
;;
esac
cmd="${cmd}${EXTRA:+ "${EXTRA}"}"
# add output
cmd="${cmd} file:${out@Q}"
show_header "Re-encoding ${in@Q}:"
show_info "${cmd}"
eval "${cmd}"
}
function scan_input {
local fstring
local astring
local vstring
local sstring
local format_name
local idx
local _ffprobe="ffprobe"
_ffprobe="${_ffprobe}${PROBESIZE:+ -probesize ${PROBESIZE}}"
_ffprobe="${_ffprobe}${ANALYZEDURATION:+ -analyzeduration ${ANALYZEDURATION}}"
fstring="$(eval "${_ffprobe} -v error -show_format file:${IN@Q}")"
sstring="$(eval "${_ffprobe} -v error -select_streams s:0 -show_streams file:${IN@Q}")"
format_name="$(echo "${fstring}" | sed -n "s/^format_name=\(.*\)/\1/p")"
case "${format_name}" in
matroska,webm | matroska | mkv | webm)
if [[ "${EXTENSION,,}" = webm ]]; then
IN_CONTAINER=webm
else
IN_CONTAINER=mkv
fi
;;
mp4 | mov,mp4,m4a,3gp,3g2,mj2 | m4a | mov | mpegts)
IN_CONTAINER=mp4
;;
mp3)
IN_CONTAINER=mp3
;;
avi)
IN_CONTAINER=avi
;;
ogg)
IN_CONTAINER=ogg
;;
flac)
IN_CONTAINER=flac
;;
wav)
IN_CONTAINER=wav
;;
*)
show_error "ERROR: ${format_name@Q} unsupported. Exiting."
exit 3
;;
esac
if ! [[ -v OUT_CONTAINER ]]; then
case "${IN_CONTAINER}" in
matroska,webm | webm)
OUT_CONTAINER=matroska
;;
*)
OUT_CONTAINER="${IN_CONTAINER}"
;;
esac
fi
IN_SCODEC="$(echo "${sstring}" | sed -n "s/^codec_name=\(.*\)$/\1/p")"
IN_VSTREAM_CODEC_NAME=()
IN_VSTREAM_WIDTH=()
IN_VSTREAM_HEIGHT=()
IN_VSTREAM_DISPLAY_ASPECT_RATIO=()
while read -r vstring; do
IN_VSTREAM_CODEC_NAME+=("$(echo "${vstring}" | cut -d"|" -f2)")
IN_VSTREAM_WIDTH+=("$(echo "${vstring}" | cut -d"|" -f4)")
IN_VSTREAM_HEIGHT+=("$(echo "${vstring}" | cut -d"|" -f5)")
IN_VSTREAM_DISPLAY_ASPECT_RATIO+=("$(echo "${vstring}" | cut -d"|" -f6)")
done <<< "$(eval "${_ffprobe} -v error \
-select_streams v \
-show_entries stream=codec_type,codec_name,width,height,display_aspect_ratio \
-of compact=nokey=1 file:${IN@Q}")"
IN_ASTREAM_CHANNEL_COUNT=()
IN_ASTREAM_CHANNEL_LAYOUT=()
IN_ASTREAM_BITRATE=()
while read -r astring; do
IN_ASTREAM_CHANNEL_COUNT+=("$(echo "${astring}" | cut -d"|" -f3)")
IN_ASTREAM_CHANNEL_LAYOUT+=("$(echo "${astring}" | cut -d"|" -f4)")
IN_ASTREAM_BITRATE+=("$(echo "${astring}" | cut -d"|" -f5)")
done <<< "$(eval "${_ffprobe} -v error \
-select_streams a \
-show_entries stream=codec_type,channels,channel_layout,bit_rate \
-of compact=nokey=1 file:${IN@Q}")"
OUT_VCODEC="${OUT_VCODEC:-copy}"
OUT_ACODEC="${OUT_ACODEC:-copy}"
OUT_SCODEC="${OUT_SCODEC:-copy}"
# use first video stream to get display properties
for idx in $(seq 0 $((${#IN_VSTREAM_CODEC_NAME[@]} - 1))); do
if [[ "${IN_VSTREAM_CODEC_NAME[${idx}]}" =~ jpeg|jpg|png ]]; then
continue
else
ASPECTRATIO="${IN_VSTREAM_DISPLAY_ASPECT_RATIO[${idx}]}"
IN_DIMENSIONS="${IN_VSTREAM_WIDTH[${idx}]}x${IN_VSTREAM_HEIGHT[${idx}]}"
break
fi
done
}
function check_compat {
case "${OUT_CONTAINER}" in
avi)
case "${OUT_VCODEC}" in
libx264 | libx265)
show_error "ERROR: ${OUT_CONTAINER} and ${OUT_VCODEC} incompatible."
exit 3
;;
*) ;;
esac
;;
*) ;;
esac
if [[ -v VSTREAM_ID ]]; then
if [[ "${VSTREAM_ID}" -ge "${#IN_VSTREAM_CODEC_NAME[@]}" ]]; then
show_error "ERROR: ${VSTREAM_ID@Q} exceeds number of video streams in input. Exiting."
exit 3
fi
fi
if [[ -v ASTREAM_ID ]]; then
if [[ "${ASTREAM_ID}" -ge "${#IN_ASTREAM_CHANNEL_COUNT[@]}" ]]; then
show_error "ERROR: ${ASTREAM_ID@Q} exceeds number of audio streams in input. Exiting."
exit 3
fi
fi
}
function print_usage {
show_header "Usage: reencode"
show_listitem "\
-i|--input input video
-a|--acodec audio codec (copy, aac, opus, or mp3)
-A|--astream_id select index of one audio stream to encode
-b|--bitrate_audio set global audio output bitrate
-v|--vcodec video codec (h264 or h265)
-V|--vstream_id select index of one video stream to encode
-r|--crf set constant rate factor for video encoding
-p|--crop use ffcropdetect to crop video
-n|--nodefault set no subtitles to be shown by default
-d|--dimensions output dimensions (e.g. 1920x, 1280x720, etc.)
-c|--container container format (matroska or mp4)
-s|--start (optional) starting timestamp for reencoding
-t|--duration (optional) duration for output video
-e|--extra extra ffmpeg cli string
-h|--help print (this) help message"
}
function parse_container {
case "${1,,}" in
avi)
OUT_CONTAINER=avi
;;
mpg)
OUT_CONTAINER=mpg
;;
mkv | matroska | mka)
OUT_CONTAINER=matroska
;;
mp4 | mpeg4 | mpeg-4 | m4v)
OUT_CONTAINER=mp4
;;
mp3)
OUT_CONTAINER=mp3
;;
flac)
OUT_CONTAINER=flac
;;
wav)
OUT_CONTAINER=wav
;;
opus)
OUT_CONTAINER=opus
;;
*)
show_error "ERROR: Output ${1@Q} containers not supported. Exiting."
exit 3
;;
esac
}
function parse_vcodec {
case "${1}" in
h264 | x264 | libx264 | h.264 | x.264 | avc)
OUT_VCODEC=libx264
;;
h265 | x265 | libx265 | h.265 | x.265 | hevc)
OUT_VCODEC=libx265
;;
copy)
OUT_VCODEC=copy
;;
*)
show_error "ERROR: Video codec ${1@Q} is not supported. Exiting."
exit 3
;;
esac
}
function parse_acodec {
case "${1}" in
libfdk_aac | aac)
# this only works for ffmpeg manually compiled with libfdk_aac
OUT_ACODEC=libfdk_aac
;;
libflac | flac)
OUT_ACODEC=flac
;;
libmp3lame | mp3lame | mp3)
OUT_ACODEC=libmp3lame
;;
libopus | opus)
OUT_ACODEC=libopus
;;
copy)
OUT_ACODEC=copy
;;
*)
show_error "ERROR: Audio codec ${1@Q} not supported. Exiting."
exit 3
;;
esac
}
function fill_dimensions {
local out_width
local out_height
if [[ -v IN_DIMENSIONS ]]; then
if ! [[ -v OUT_DIMENSIONS ]]; then
OUT_DIMENSIONS="${IN_DIMENSIONS}"
else
out_width="$(echo "${OUT_DIMENSIONS}" | sed -n "s,^\([0-9]*\)[:x]\([0-9]*\)$,\1,p")"
out_height="$(echo "${OUT_DIMENSIONS}" | sed -n "s,^\([0-9]*\)[:x]\([0-9]*\)$,\2,p")"
if [ -z "${out_width}" ] && [ -n "${out_height}" ]; then
out_width="$(printf %.0f "$(echo "${out_height} * ${ASPECTRATIO}" | sed 's,:,/,g' | bc -l)")"
elif [ -n "${out_width}" ] && [ -z "${out_height}" ]; then
out_height="$(printf %.0f "$(echo "${out_width} / (${ASPECTRATIO})" | sed 's,:,/,g' | bc -l)")"
fi
OUT_DIMENSIONS="${out_width}x${out_height}"
fi
fi
}
function compute_crop_filter {
local cropfilter
cropfilter="$(ffmpeg -i file:"${1}" -vf 'cropdetect=24:2:0' -f null - 2>&1 | tail -n 3 | head -1 | cut -d" " -f15)"
if [[ "$(echo "${cropfilter}" | cut -d":" -f3)" -eq 0 ]] &&
[[ "$(echo "${cropfilter}" | cut -d":" -f4)" -eq 0 ]]; then
return 1
else
echo "${cropfilter}"
fi
}
#
# Parse command line options
#
ASTREAM_ID=
VSTREAM_ID=
OPTIONS=i:e:a:A:b:v:V:r:c:d:s:t:hnp
LONGOPTIONS=input:,extra:,acodec:,asteam_id:,bitrate:,vcodec:,vstream_id:,crf:,dimensions:,container:,start:,duration:,help,nodefault,crop
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-i | --input)
IN="${2}"
NAME="${IN%.*}"
EXTENSION="${IN##*.}"
BACKUP="${IN}_$(date +%Y%m%d-%H%M%S).bak"
scan_input
shift 2
;;
-e | --extra)
EXTRA="${2}"
shift 2
;;
-a | --acodec)
parse_acodec "${2}"
shift 2
;;
-A | --astream_id)
ASTREAM_ID="${2}"
shift 2
;;
-b | --bitrate_audio)
OUT_ABITRATE="${2}"
shift 2
;;
-v | --vcodec)
parse_vcodec "${2}"
shift 2
;;
-V | --vstream_id)
VSTREAM_ID="${2}"
shift 2
;;
-n | --nodefault)
NO_DEFAULT_SUBS=true
shift 1
;;
-r | --crf)
CRF="${2}"
shift 2
;;
-d | --dimensions)
OUT_DIMENSIONS="${2}"
shift 2
;;
-c | --container)
parse_container "${2}"
shift 2
;;
-s | --start)
START="${2}"
shift 2
;;
-t | --duration)
DURATION="${2}"
shift 2
;;
-p | --crop)
CROP=true
shift 1
;;
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
show_error "ERROR: Unknown command line option."
print_usage
exit 3
;;
esac
done
#
# Main
#
if ! [[ -v IN ]]; then
show_error "ERROR: Input file not given."
exit 3
fi
if ! [[ -f "${IN}" ]]; then
show_error "ERROR: Input file not found."
exit 3
fi
case "${OUT_VCODEC}" in
libx264)
CRF="${CRF:-18}" # ffmpeg default is 23
;;
libx265)
CRF="${CRF:-23}" # ffmpeg default is 28
;;
esac
fill_dimensions # must be called after scan_input has run
check_compat # check whether output codecs and compatible
mv "${IN}" "${BACKUP}"
sync
# set traps
if [[ "${IN_CONTAINER}" = "${OUT_CONTAINER}" ]]; then
trap 'mv -f "${BACKUP}" "${IN}"; exit' INT TERM ERR
else
if [[ "${OUT_CONTAINER}" = "matroska" ]]; then
trap 'mv -f "${BACKUP}" "${IN}"; rm -f "${NAME}.mkv"; exit' INT TERM ERR
else
trap 'mv -f "${BACKUP}" "${IN}"; rm -f "${NAME}.${OUT_CONTAINER}"; exit' INT TERM ERR
fi
fi
convert_codec
sync