-
Notifications
You must be signed in to change notification settings - Fork 38
/
dvmkv2mp4
executable file
·389 lines (368 loc) · 15.1 KB
/
dvmkv2mp4
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
#!/bin/bash
VERSION=0.3.0-dev
## Which Languages of Audio and Sub tracks to keep in resulting mp4, if no match then all will be kept
LANGS="ALL"
## Whether to create audio-subs-meta mkv file having all streams except video to preserve TrueHD Atmos and such for future conversion back to original
ASM="no"
## Whether to delete source material after conversion
REMOVESOURCE="no"
## Whether to allow debug by keeping conversion data
DEBUG="no"
## Check OSTYPE and set ionice level to idle to not hammer disks during conversion
ADDSUBS="no"
## Whether to add subs to mp4 file default no as some bad subs can break the encoding"
if [[ $OSTYPE == 'darwin'* ]]
then
ionc="ionice --low"
sed="gsed"
else
ionc="ionice -c 3"
sed="sed"
fi
HEADER="dvmkv2mp4 $VERSION - easily convert Dolby Vision or HDR10+ mkvs to Dolby Vision MP4
Created by github.com/gacopl, Released under GPLv3
"
function print_help {
echo "$HEADER"
echo "-l | --langs - filter audio and subtitle tracks by lang comma separated if not hit by filter keep all tracks "
echo "-a | --asm - create audio-subs-meta mkv file"
echo "-r | --remove-source - remove source video after conversion"
echo "-s | --add-subs - add srt subtitles to mp4 as subtitle tracks"
echo "-d | --debug - keep intermediary conversion files"
echo "-v | --version - print version"
echo ""
echo "dvmkv2mp4 -l und,pol,eng -r -a # will process any DV/HDR10+ mkvs found in current dir and keep only Undefined, Polish and English tracks, will remove source file once done and will create audio-subs-meta file for future needs"
}
TEMP=$(getopt -o al:rsdvh --long asm,langs:,remove-source,add-subs,debug,version,help \
-n 'dvmkv2mp4' -- "$@")
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around '$TEMP': they are essential!
eval set -- "$TEMP"
while true; do
case "$1" in
-v | --version ) echo "$HEADER"; shift; exit ;;
-d | --debug ) DEBUG="yes"; shift ;;
-a | --asm ) ASM="yes"; shift ;;
-r | --remove-source ) REMOVESOURCE="yes"; shift ;;
-s | --add-subs ) ADDSUBS="yes"; shift ;;
-h | --help ) print_help; shift; exit ;;
-l | --langs ) LANGS="$2"; shift 2 ;;
-- ) shift; break ;;
* ) break ;;
esac
done
function cleanup {
while read i;do
rm "`echo "$i" | cut -f1 -d\|`"
done <<< "$(cat audio.exports)"
rm RPU.bin extra.json hdr10plus_metadata.json
rm audio.exports
rm sub.exports
rm tracks.list
rm chapters.list
rm BL*hevc
rm "${input}.dvconverting"
if [ $REMOVESOURCE == "yes" ]; then
rm "${input}"
fi
}
function processsubs {
### PROCESS SUBS
inputbase=${input%.mkv}
# COPY EXISTING SRT
for i in "$inputbase".*.srt; do
eval "cp \"$i\" \"`echo "$i" | $sed 's/\ DV\.\|\ HDR10+\.\|HDR\./\./g' | $sed 's/\.\(.*\)\.srt/\ DV-MP4\.\1\.srt/g'`\"";
done
# CONVERT PGS 2 SRT
if [ -s "sub.exports" ] && [ "$(cat sub.exports | grep hdmv_pgs)" != "" ]; then
while read i;do
stream=`echo "$i" | cut -f1 -d\|`
id=`echo "$i" | cut -f2 -d\|`
codec=`echo "$i" | cut -f3 -d\|`
orig_codec=`echo "$i" | cut -f4 -d\|`
delay=`echo "$i" | cut -f5 -d\|`
lang=`echo "$i" | cut -f6 -d\|`
title=`echo "$i" | cut -f7 -d\|`
/opt/dotnet/dotnet /opt/PgsToSrt/net6/PgsToSrt.dll --input "$stream" --output "${stream%.sup}.srt" --tesseractlanguage=$lang
echo "${stream%.sup}.srt|$id|srt|$orig_codec|$delay|$lang|$title" >> sub.exports
#docker run --rm -v "`pwd`":/data -e INPUT="/data/$stream" -e OUTPUT="/data/${stream%.sup}.srt" -e LANGUAGE=$lang tentacule/pgstosrt
done <<< "$(cat sub.exports | grep hdmv_pgs)"
fi
}
start=`date +%s`
echo "$HEADER"
echo "Starting Conversions `date`"
for f in *.mkv;do
input="$f"
ffstart5_8="$ionc ffmpeg -i \"$input\" -y -loglevel error -stats -map 0:0 -c:v copy -vbsf hevc_mp4toannexb -f hevc BL_RPU.hevc"
ffstart7_1="$ionc ffmpeg -i \"$input\" -y -loglevel error -stats -map 0:0 -c:v copy -vbsf hevc_mp4toannexb -f hevc -"
ffstart7_2="$ionc ffmpeg -i \"$input\" -y -loglevel error -stats -map 0:v:0 -c:v copy -vbsf hevc_mp4toannexb -f hevc BL.hevc -map 0:v:1 -c:v copy -vbsf hevc_mp4toannexb -f hevc -"
ffend7_1="| $ionc dovi_tool -m 2 convert --discard -"
ffend7_2="| $ionc dovi_tool -m 2 extract-rpu - -o RPU.bin"
ffstarthdr10plus="$ionc ffmpeg -i \"$input\" -y -loglevel error -stats -map 0:v:0 -c:v copy -vbsf hevc_mp4toannexb -f hevc BL.hevc -map 0:v:0 -c:v copy -vbsf hevc_mp4toannexb -f hevc -"
ffendhdr10plus="| $ionc hdr10plus_tool extract -o hdr10plus_metadata.json -"
dv=$(mediainfo "$input" | grep 'HDR format.*Dolby Vision')
hdr10plus=$(mediainfo "$input" | grep 'HDR format.*HDR10+')
output=`echo "$input" | $sed 's/\ DV.mkv\|\ HDR10+.mkv\|\ HDR.mkv/.mkv/g' | $sed s/\.mkv/\ DV-MP4\.mkv/g`
output="${output%.mkv}"
## AUTODETECT SOURCE TYPE AND CONSTRUCT FFMPEG
if [ -z "$hdr10plus" ] && [ -z "$dv" ] || [ -f "${input}.dvconverting" ]; then
echo "Not an HDR10+ nor DV source or already converting"
continue
else
touch "${input}.dvconverting"
if [ ! -z "$dv" ]; then
dv_profile=$(mediainfo "$input" | grep "HDR format.*dvhe\." | $sed 's/.*dvhe\.0\(.\).*/\1/')
if { [ "$dv_profile" -ne 4 ] && [ "$dv_profile" -ne 5 ] && [ "$dv_profile" -ne 7 ] && [ "$dv_profile" -ne 8 ]; } ; then
info "Unsupported Dolby Vision profile '$dv_profile'; doing nothing"
rm *.dvconverting
continue
fi
echo "Converting DV$dv_profile: \"$input\""
vscount=`ffprobe -loglevel error -select_streams v -show_entries stream=type:stream=codec_name:stream=index:stream_tags=language:stream_tags=title -of csv=p=0 "$input" | grep hevc | wc -l`
if [ "$dv_profile" -eq 4 ] || [ "$dv_profile" -eq 5 ] || [ "$dv_profile" -eq 8 ];then
ffstring=("$ffstart5_8")
elif [ "$dv_profile" -eq 7 ] && [ "$vscount" -eq 1 ];then
ffstring=("$ffstart7_1")
else
ffstring=("$ffstart7_2")
fi
if [ "$dv_profile" -eq 5 ]; then
dv_target=5
elif [ "$dv_profile" -eq 4 ]; then
dv_target=4
else
dv_target=8
fi
elif [ ! -z "$hdr10plus" ]; then
echo "Converting HDR10+ to DV8: \"$input\""
dv_target=8
ffstring=("$ffstarthdr10plus")
MaxDML=`mediainfo "$input" | grep 'Mastering display luminance' | cut -f 4 -d:`
MaxDML=${MaxDML% cd*}
MaxDML=${MaxDML%.*}
MinDML=`mediainfo "$input" | grep 'Mastering display luminance' | cut -f 3 -d:`
MinDML=${MinDML% cd*}
if [ "$MinDML" == " 0.0050" ]; then
MinDML=50
elif [ "$MinDML" == " 0.0010" ]; then
MinDML=10
else
MinDML=1
fi
MaxFALL=`mediainfo "$input" | grep 'Maximum Frame-Average Light Level' | cut -f 2 -d: | cut -f2 -d" "`
MaxCLL=`mediainfo "$input" | grep 'Maximum Content Light Level' | cut -f 2 -d: | cut -f2 -d" "`
## Fake HDR10 Metadata if missing on source as it's only needed for backwards hdr10 compatibility but will play fine in DV
if [ -z "$MaxDML" ]; then
MaxDML=1000;
fi
if [ -z "$MinDML" ]; then
MinDML=1;
fi
if [ -z "$MaxFALL" ]; then
MaxFALL=500;
fi
if [ -z "$MaxCLL" ]; then
MaxCLL=1000;
fi
FrameCount=`mediainfo --Inform='Video;%FrameCount%' "$input"`
cat > extra.json <<EOF
{
"cm_version": "V29",
"length": $FrameCount,
"level6": {
"max_display_mastering_luminance": $MaxDML,
"min_display_mastering_luminance": $MinDML,
"max_content_light_level": $MaxCLL,
"max_frame_average_light_level": $MaxFALL
}
}
EOF
fi
fi
# SELECT LANG TRACKS
as=$(ffprobe -loglevel error -select_streams a -show_entries stream=type:stream=codec_name:stream=index:stream=start_pts:stream_tags=language:stream_tags=title -of csv=p=0 "$input" | grep $(echo $LANGS | $sed 's/,/,\\|,/g') | $sed 's/,/\|/g')
if [ "$as" == "" ]; then
ffprobe -loglevel error -select_streams a -show_entries stream=type:stream=codec_name:stream=index:stream=start_pts:stream_tags=language:stream_tags=title -of csv=p=0 "$input" | $sed 's/,/\|/g' > tracks.list
else
echo "$as" > tracks.list
fi
ts=$(ffprobe -loglevel error -select_streams s -show_entries stream=type:stream=codec_name:stream=index:stream=start_pts:stream_tags=language:stream_tags=title -of csv=p=0 "$input" | grep $(echo $LANGS | $sed 's/,/,\\|,/g') | $sed 's/,/\|/g')
if [ "$ts" == "" ]; then
ffprobe -loglevel error -select_streams s -show_entries stream=type:stream=codec_name:stream=index:stream=start_pts:stream_tags=language:stream_tags=title -of csv=p=0 "$input" | $sed 's/,/\|/g' >> tracks.list
else
echo "$ts" >> tracks.list
fi
# FFMPEG PROCESS TRACKS
while read i;do
id=`echo "$i" | cut -f1 -d\|`
orig_codec=`echo "$i" | cut -f2 -d\|`
delay=`echo "$i" | cut -f3 -d\|`
lang=`echo "$i" | cut -f4 -d\|`
# cover case of missing lang metadata
valid="[a-za-za-z]"
if [[ ! $lang =~ $valid ]]; then
lang="und";
title=`echo "$i" | cut -f4 -d\|`
else
title=`echo "$i" | cut -f5 -d\|`
fi
title="${title//[\"\',\`]/}"
ffopts=""
if [ "$orig_codec" == "truehd" ]; then
ffopts="-map 0:$id -b:a:0 1024k -c:a:0 eac3 -f eac3 $id.$lang.eac3"
echo "$id.$lang.eac3|$id|eac3|$orig_codec|$delay|$lang|$title" >> audio.exports
fi
if [ "$orig_codec" == "eac3" ]; then
ffopts="-map 0:$id -c:a:0 copy $id.$lang.eac3"
echo "$id.$lang.eac3|$id|eac3|$orig_codec|$delay|$lang|$title" >> audio.exports
fi
if [ "$orig_codec" == "dts" ]; then
ffopts="-map 0:$id -b:a:0 1024k -c:a:0 eac3 -f eac3 $id.$lang.eac3"
echo "$id.$lang.eac3|$id|eac3|$orig_codec|$delay|$lang|$title" >> audio.exports
fi
if [ "$orig_codec" == "ac3" ]; then
ffopts="-map 0:$id -c:a:0 copy $id.$lang.ac3"
echo "$id.$lang.ac3|$id|ac3|$orig_codec|$delay|$lang|$title" >> audio.exports
fi
if [ "$orig_codec" == "aac" ]; then
ffopts="-map 0:$id -c:a:0 copy $id.$lang.aac"
echo "$id.$lang.aac|$id|aac|$orig_codec|$delay|$lang|$title" >> audio.exports
fi
if [ "$orig_codec" == "mp3" ]; then
ffopts="-map 0:$id -c:a:0 copy $id.$lang.mp3"
echo "$id.$lang.mp3|$id|mp3|$orig_codec|$delay|$lang|$title" >> audio.exports
fi
if [ "$orig_codec" == "ass" ]; then
ffopts="-map 0:$id -c:s:0 copy \"$output.${lang}${id}.ass\" -map 0:$id -c:s:0 srt \"$output.${lang}${id}.srt\""
echo "$output.${lang}${id}.ass|$id|ass|$orig_codec|$delay|$lang|$title" >> sub.exports
echo "$output.${lang}${id}.srt|$id|srt|$orig_codec|$delay|$lang|$title" >> sub.exports
fi
if [ "$orig_codec" == "srt" ] || [ "$orig_codec" == "subrip" ]; then
ffopts="-map 0:$id -c:s:0 copy \"$output.${lang}${id}.srt\""
echo "$output.${lang}${id}.srt|$id|srt|$orig_codec|$delay|$lang|$title" >> sub.exports
fi
if [ "$orig_codec" == "hdmv_pgs_subtitle" ]; then
ffopts="-map 0:$id -c:s:0 copy \"$output.${lang}${id}.sup\""
echo "$output.${lang}${id}.sup|$id|hdmv_pgs_subtitles|$orig_codec|$delay|$lang|$title" >> sub.exports
fi
ffstring+=($ffopts)
done <<< "$(cat tracks.list)"
if [ $ASM == "yes" ]; then
# FFMPEG TRACKS FOR ASM
while read i;do
id=`echo "$i" | cut -f1 -d\|`
ffopts="-map 0:$id"
ffstring+=($ffopts)
done <<< "$(cat tracks.list)"
ffstring+=("-c copy -f matroska \"${output}.asm\"")
fi
# FFMPEG HANDLE METADATA INJECTION
if [ ! -z "$dv" ]; then
# HANDLE DV METADATA
if [ "$dv_profile" -eq 7 ] && [ "$vscount" -eq 1 ];then
ffstring+=("$ffend7_1")
echo ${ffstring[*]}
eval ${ffstring[*]}
elif [ "$dv_profile" -eq 7 ] && [ "$vscount" -eq 2 ];then
ffstring+=("$ffend7_2")
echo ${ffstring[*]}
eval ${ffstring[*]}
$ionc dovi_tool inject-rpu -i BL.hevc --rpu-in RPU.bin -o BL_RPU.hevc
rm BL.hevc RPU.bin
else
echo ${ffstring[*]}
eval ${ffstring[*]}
fi
### PROCESS SUBS
processsubs
elif [ ! -z "$hdr10plus" ]; then
# HANDLE HDR10+ METADATA
ffstring+=("$ffendhdr10plus")
echo ${ffstring[*]}
eval ${ffstring[*]}
### PROCESS SUBS
processsubs
### VERIFY HDR10+ METADATA
MetadataCount=`cat hdr10plus_metadata.json | jq -r '.SceneInfo | length'`
MetadataPercent=`bc <<< "scale=2; $MetadataCount/$FrameCount * 100"`
MetadataPercent=${MetadataPercent%.*}
if [ "$MetadataPercent" -gt 95 ]; then
echo "Metadata seems ok proceeding"
$ionc dovi_tool generate -j extra.json --hdr10plus-json hdr10plus_metadata.json -o RPU.bin
$ionc dovi_tool inject-rpu -i BL.hevc --rpu-in RPU.bin -o BL_RPU.hevc
rm BL.hevc
else
echo "Invalid HDR10+ Metadata ABORTING"
echo "HDR10+ Metadata Frames: $MetadataCount VideoFrames: $FrameCount $MetadataPercent%"
echo "Preserving extracted subs"
inputbase=${input%.mkv}
dvmp4sub=`echo ${input%.mkv} | $sed 's/\ DV\|\ HDR10+//g'`
for i in "$dvmp4sub"\ DV-MP4.*.{srt,sup}; do
eval "cp \"$i\" \"`echo "$i" | $sed 's/\ DV-MP4//g' | $sed \"s/.*\.\(.*\)\.\(s..\)/\$inputbase\.\1\.\2/g\"`\"";
done
rm BL.hevc *DV-MP4.asm *DV-MP4.*.srt *DV-MP4.sup ${input}.dvconverting
continue
fi
fi
### GRAB CHAPTERS
mkvextract chapters -s "${input}" > chapters.list
### MUX MP4
mp4string=("$ionc MP4Box -add BL_RPU.hevc:dv-profile=$dv_target")
tcount=2
while read i;do
stream=`echo "$i" | cut -f1 -d\|`
id=`echo "$i" | cut -f2 -d\|`
codec=`echo "$i" | cut -f3 -d\|`
orig_codec=`echo "$i" | cut -f4 -d\|`
delay=`echo "$i" | cut -f5 -d\|`
lang=`echo "$i" | cut -f6 -d\|`
title=`echo "$i" | cut -f7 -d\|`
mp4opts="-add \"$stream\":sopt:gfreg=ffdmx -lang $tcount=$lang -name $tcount=\"$title\" -delay $tcount=$delay"
mp4string+=($mp4opts)
tcount=$((tcount+1))
done <<< "$(cat audio.exports)"
if [ $ADDSUBS == "yes" ]; then
while read i;do
stream=`echo "$i" | cut -f1 -d\|`
id=`echo "$i" | cut -f2 -d\|`
codec=`echo "$i" | cut -f3 -d\|`
orig_codec=`echo "$i" | cut -f4 -d\|`
delay=`echo "$i" | cut -f5 -d\|`
lang=`echo "$i" | cut -f6 -d\|`
title=`echo "$i" | cut -f7 -d\|`
echo $stream
echo $codec
if [ "$codec" == "srt" ] && [ -s "$stream" ]; then
mp4opts="-add \"$stream\":lang=$lang:name=\"$title\" -delay $tcount=$delay"
mp4string+=($mp4opts)
tcount=$((tcount+1))
fi
done <<< "$(cat sub.exports)"
# while read i; do
# mp4opts="-add "$i":lang=pol:name='downloaded'"
# mp4string+=($mp4opts)
# done <<< $(ls *{pl,pl-sync}.srt)
# while read i; do
# if [ "$i" != "" ]; then
# mp4opts="-add "$i":lang=eng:name='downloaded'"
# mp4string+=($mp4opts)
# fi
# done <<< $(ls *{en,en-sync}.srt)
fi
if [ -s "chapters.list" ]; then
mp4string+=("-chap chapters.list")
fi
mp4string+=("-tmp ./tmp -brand mp42isom -ab dby1 \"$output.mp4\"")
echo ${mp4string[*]}
eval ${mp4string[*]}
if [ $DEBUG != "yes" ]; then
cleanup
else
rm "${input}.dvconverting"
fi
done
end=`date +%s`
runtime=$((end-start))
hours=$((runtime / 3600)); minutes=$(( (runtime % 3600) / 60 )); seconds=$(( (runtime % 3600) % 60 ));
echo "Conversions runtime: $hours:$minutes:$seconds (hh:mm:ss)"