-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvideo-transcode
More file actions
executable file
·125 lines (106 loc) · 3.44 KB
/
video-transcode
File metadata and controls
executable file
·125 lines (106 loc) · 3.44 KB
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
#!/usr/bin/env bash-with-comments
# Transcode a media file to x264 preserving all video, audio and subtitle tracks
#
# For deinterlacing set DEINTERLACE env var.
# NOTE: for libfdk_aac on Ubuntu 18.04 use snap:
# sudo snap install ffmpeg --devmode
source bash-helpers
# fix aspect "flag" - make 4:3 to display as 16:9 for NTSC:
# mkvpropedit *.mkv --edit track:v1 --set display-width=853
infile="$*"
INFO "Encoding $infile..."
# video quality - crf:
# A change of ±6 should result in about half/double the file size, although your results might vary.
# lower values would result in better quality (at the expense of higher file sizes).
# http://slhck.info/articles/crf
# default crf: 23
# less is better
# DVD needs 23? / the lower the res the higher quality neede?
crf=29
# Even though apparently all dvds are interlaced - usually results seem to be better without deinterlacing.
# deinterlace
if [[ $DEINTERLACE ]] ; then
deinterlacer="-vf yadif"
deinterlaced_name="-deinterlaced"
WARN '#################################### Deinterlacing!'
fi
## AUDIO
# -cutoff: default is 14000Hz for audio
#
# 224 kbit 6-ch AAC seems to be the current standard (2020-06-28).
audio="-c:a libfdk_aac -b:a 224k -cutoff 20000"
if [[ $STEREO ]] ; then
audio="-c:a libfdk_aac -vbr 5 -cutoff 20000 -ac 2"
stereo_name="-stereo"
WARN '#################################### Forcing stereo!'
fi
# Audio "hi-fi transparency demands data rates of at least 128 kbit/s CBR. The
# MPEG-2 audio tests showed that AAC meets the requirements referred to as
# "transparent" for the ITU at 128 kbit/s for stereo, and 320 kbit/s for 5.1 audio."
# (https://en.wikipedia.org/wiki/Advanced_Audio_Coding)
#
# Audio codec options:
# https://www.ffmpeg.org/ffmpeg-codecs.html
# ac3 is the best quality audio encoder directly included in libav - see:
# https://trac.ffmpeg.org/wiki/Encode/HighQualityAudio
# libfdk_aac is the best aac encoder.
# outfile=$(basename "$infile").crf${crf}-aac${deinterlaced_name}${stereo_name}.mkv
outfile="$infile".tmp.mkv
nfoDir=$(dirname "$infile")/nfo
nfo="$nfoDir/recode.nfo"
if [[ -e $outfile ]] ; then
DIE "Outfile: $outfile exists."
fi
# prevent ffmpeg from reading stdin (problem with bash-with-comments)
< /dev/null \
ffmpeg \
# shows -vf option like yadif etc.
-loglevel verbose \
#
-i "$infile" \
#
# include all video streams
-map 0:v \
#
-c:v libx264 \
-crf $crf \
$deinterlacer \
#
# May prevent "Too many packets buffered for output stream":
# Had this problem with a DVD ISO.
-max_muxing_queue_size 9999 \
#
# TODO: Only reencode if not aac etc already.
#
# NOTE: -vbr reports an error but that message is supposably wrong (http://www.ffmpeg-archive.org/FDK-AAC-OUTPUTS-CBR-AUDIO-STREAM-ONLY-td4670348.html)
# The bitrate does seam to fluctuate.
# "the VBR setting is unsupported and only works with some parameter combinations"
#
# include all audio streams
-map 0:a \
#
$audio \
#
# passthrou
# -c:a copy \
#
# downmix to 2 channels (ignores the subwoofer channel)
$stereo \
#
# include all subtitle streams
-map 0:s? \
# copy subtitles as is
-c:s copy \
#
$outfile
INFO "Done: $infile"
mkdir -p "$nfoDir"
echo "$infile reencoded with crf: ${crf}" >> "$nfo"
mv "$outfile" "$infile"
mv "$infile" "$infile.reenc_x264.mkv"
if [[ $DEINTERLACE ]] ; then
WARN '#################################### Deinterlacing!'
fi
if [[ $STEREO ]] ; then
WARN '#################################### Forcing stereo!'
fi