-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtranscode.sh
More file actions
executable file
·274 lines (230 loc) · 5.88 KB
/
transcode.sh
File metadata and controls
executable file
·274 lines (230 loc) · 5.88 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
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
#!/bin/bash
#
# This script will accept video files as input and use Don Melton's
# video transcoding scripts to control Handbrake in order to produce
# portable video files.
#
readonly program="$(basename "$0")"
about() {
cat <<EOF
$program 1.6.1 of November 11, 2018
Copyright (c) 2018 Alex Du Bois
EOF
exit 0
}
usage(){
cat <<EOF
Transcode video files. Works best with Blu-ray or DVD rips.
Transcode.sh automatically determines target video bitrate, number of audio
tracks, etc. WITHOUT ANY command line options.
It is recommended to use Hazel to provide automated queue management and
trigger transcode.sh, but you can also provide multiple files or folders as arguments and
they will be transcoded one at a time.
Usage: $program [FILES...]
--help display basic options and exit
--version display program version and exit
Requires "video_transcoding" from https://github.com/donmelton/video_transcoding
Requires "HandBrakeCLI" from https://handbrake.fr
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}
}
# Test dependencies
#
install_homebrew() {
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
}
test_homebrew() {
if [ `which brew | wc -l` -lt 1 ]; then
echo "Homebrew is not installed. Do you want to install it?"
read -p "[y/n]: " homebrew_install_input
if [ "$homebrew_install_input" = "y" ]; then
install_homebrew
else
die "Please install Homebrew manually and try again."
fi
fi
}
install_handbrake() {
test_homebrew
brew update && brew install handbrake
}
test_handbrake() {
if [ `brew leaves | grep handbrake | wc -l` -lt 1 ]; then
echo "Handbrake is not installed. Do you want to install it?"
read -p "[y/n]:" handbrake_install_input
if [ "$handbrake_install_input" = "y" ]; then
install_handbrake
else
die "Please install Handbrake and try again."
fi
fi
}
install_video_transcoding() {
gem install video_transcoding
}
test_video_transcoding() {
if [ `gem list --quiet video_transcoding | grep video_transcoding | wc -l` -lt 1 ]; then
echo "video_transcoding is not install. Do you want to install it?"
read -p "[y/n]:" video_transcodinginstallinput
if [ "$video_transcodinginstallinput" = "y" ]; then
install_video_transcoding
else
die "Please install video_transcoding and try again."
fi
fi
}
test_dependencies() {
test_handbrake
test_video_transcoding
}
# Set global variables
#
PATH="/usr/local/bin:$PATH"
readonly crop_dir="_crops"
readonly originals_dir="_originals"
readonly finals_dir="_finals"
readonly logs_dir="_logs"
crop_options=''
video_options=''
audio_options=''
subtitle_options=''
logging_options='--quiet'
use_h265=''
dry_run=''
# Option declarations
#
function enableLogging() {
logging_options='--verbose'
}
function enableH265() {
use_h265='--handbrake-option encoder=x265'
}
function dry-run() {
dry_run='--dry-run'
}
# Process Options
#
while [ "$1" ]; do
case "$1" in
--help | -h)
usage
;;
--version | -v)
about
;;
--h265)
enableH265
;;
--dry-run)
dry-run
;;
-*)
syntax_error "unrecognized option: $1"
;;
*)
break
;;
esac
shift
done
if [ ! "$1" ]; then
syntax_error 'too few arguments'
fi
test_dependencies
function transcode() {
if [ ! -e "$1" ]; then
die "file not found: $1"
fi
input="$1"
work_dir=`dirname "$input"`
title_name="$(basename "$input" | sed 's/\.[^.]*$//')"
crop_file="$crop_dir/${title_name}.txt"
media_info=`transcode-video --scan $input`
function setupWorkingDirectory() {
if [ ! -d "$originals_dir" ]; then
mkdir "$originals_dir"
fi
if [ ! -d "$logs_dir" ]; then
mkdir "$logs_dir"
fi
if [ ! -d "$finals_dir" ]; then
mkdir "$finals_dir"
fi
}
function setCroppingOptions() {
if [ ! -d "$crop_dir" ]; then
mkdir "$crop_dir"
fi
detect-crop "$input" &> "$crop_file"
if [[ -f "$crop_file" ]] && [[ ! `grep differ $crop_file` ]]; then
crop_options=`grep transcode-video $crop_file | egrep -o -e "--crop "[0-9]+:[0-9]+:[0-9]+:[0-9]+`
else
crop_options=''
fi
}
function setVideoOptions() {
if [ `echo "$media_info" | egrep "x480" | wc -l` -gt 0 ]; then
video_options="--force-rate 23.976 --filter detelecine"
else
video_options="--max-width 1920 --max-height 1080"
fi
}
function setAudioOptions() {
if [ `echo "$media_info" | egrep "(\d\.\d ch)" | wc -l` -gt 1 ]; then
audio_options="--main-audio eng"
else
audio_options="--add-audio all"
fi
audio_options="$audio_options --audio-width all=double"
}
function setSubtitleOptions() {
srt_file="$title_name.srt"
if [ -e "$srt_file" ]; then
subtitle_options="--add-srt $work_dir/$srt_file"
else
subtitle_options="--burn-subtitle scan"
fi
}
function cleanup() {
setupWorkingDirectory
mv "$input" "$originals_dir"
if [ -e $srt_file ]; then
mv "$srt_file" "$originals_dir"
fi
mv "$title_name.mp4.log" "$logs_dir"
mv "$title_name.mp4" "$finals_dir"
}
if [ -f "$input" ]; then
setCroppingOptions
setVideoOptions
setAudioOptions
setSubtitleOptions
if [ $dry_run ]; then
echo transcode-video --mp4 $crop_options $video_options $audio_options $subtitle_options $logging_options $use_h265 "$input"
else
transcode-video --mp4 $crop_options $video_options $audio_options $subtitle_options $logging_options $use_h265 "$input"
cleanup
fi
else
echo "There was a problem with $input and it could not be transcoded."
fi
}
while [ "$1" ]; do
if [ -d $1 ]; then
for file in $( ls | egrep -e \.[^.]*mkv ); do
transcode $file
done
else
transcode $1
fi
shift
done