forked from JakeWharton/mkvdts2ac3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkvpcm2eac3.sh
738 lines (662 loc) · 22.1 KB
/
mkvpcm2eac3.sh
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
#!/bin/bash
# mkvPCM2EAC3.sh - add an EAC3 track to an MKV from its PCM track
# Author: Jake Wharton <[email protected]>
# Chris Hoekstra <[email protected]>
# Gert van Yperenburg
# Website: http://jakewharton.com
# http://github.com/JakeWharton/mkvdts2EAC3/
# Version: 2.0.0
# License:
# Copyright 2011 Jake Wharton
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Display version header
echo "mkvPCM2EAC3-2.0.0 - by Jake Wharton <[email protected]>"
echo " Chris Hoekstra <[email protected]>"
echo " Gert van Yperenburg"
echo
# Debugging flags
# DO NOT EDIT THESE! USE --debug OR --test ARGUMENT INSTEAD.
PRINT=0
PAUSE=0
EXECUTE=1
# Default values
PRIORITY=0
FORCE=0
NOCOLOR=0
MD5=0
INITIAL=0
NEW=0
COMP="none"
WD="/mnt/Downloads" # Working Directory (Use the -w/--wd argument to change)
# These are so you can make quick changes to the cmdline args without having to search and replace the entire script
DUCMD="$(which \du) -k"
RSYNCCMD="$(which \rsync) --progress -a"
# Check for a .mkvPCM2EAC3.rc file in user's home directory for custom defaults
if [ -f ~/.mkvPCM2EAC3.rc ]; then
. ~/.mkvPCM2EAC3.rc
fi
# Force English output, grepping for messages may fail otherwise
export LC_MESSAGES=C
#---------- FUNCTIONS --------
displayhelp() {
# Usage: displayhelp
echo "Usage: `basename $0` [options] <filename>"
echo "Options:"
echo " -c TITLE, Custom EAC3 track title."
echo " --custom TITLE"
echo " -d, --default Mark EAC3 track as default."
echo " -e, --external Leave EAC3 track out of file. Does not modify the"
echo " original matroska file. This overrides '-n' and"
echo " '-d' arguments."
echo " -f, --force Force processing when EAC3 track is detected"
echo " -i, --initial New EAC3 track will be first in the file."
echo " -k, --keep-PCM Keep external PCM track (implies '-n')."
echo " -m, --nocolor Do not use colors (monotone)."
echo " --md5 Perform MD5 comparison when copying across drives."
echo " -n, --no-PCM Do not retain the PCM track."
echo " --new Do not copy over original. Create new adjacent file."
echo " -p PRIORITY Modify niceness of executed commands."
echo " -s MODE,"
echo " --compress MODE Apply header compression to streams (See mkvmerge's --compression)."
echo " -t TRACKID,"
echo " --track TRACKID Specify alternate PCM track."
echo " -w FOLDER,"
echo " --wd FOLDER Specify alternate temporary working directory."
echo
echo " --test Print commands only, execute nothing."
echo " --debug Print commands and pause before executing each."
echo
echo " -h, --help Print command usage."
echo " -v, --verbose Turn on verbose output"
echo " -V, --version Print script version information."
}
# Usage: color shade
function color {
# Are we in Cron?
if [ ! -t 0 -o $NOCOLOR = 1 ]; then return 1; fi
case $1 in
off|OFF) echo -n '[0m';;
red|RED) echo -n '[1;31m';;
yellow|YELLOW) echo -n '[1;33m';;
green|GREEN) echo -n '[1;32m';;
blue|BLUE) echo -n '[1;34m';;
bell|BELL) echo -n '';;
*) ;;
esac
}
# Usage: timestamp ["String to preface time display"]
timestamp() {
CURRTIME=$(date +%s)
secs=$(($CURRTIME - $PREVTIME))
PREVTIME=$((CURRTIME))
h=$(( secs / 3600 ))
m=$(( ( secs / 60 ) % 60 ))
s=$(( secs % 60 ))
if [ $EXECUTE = 1 -a $PAUSE = 0 ]; then
echo -n "$1 "
printf "%02d:%02d:%02d " $h $m $s
echo $"($secs seconds)"
echo ""
fi
}
# Usage: error "String to display"
error() {
color BELL;color RED
printf "%s: %s\n" $"ERROR" "$1"
color OFF
}
# Usage: warning "String to display"
warning() {
color YELLOW
printf "%s: %s\n" $"WARNING" "$1"
color OFF
}
info() {
color BLUE
printf "%s: %s\n" $"INFO" "$1"
color OFF
}
# Usage: dopause
dopause() {
if [ $PAUSE = 1 ]; then
read
fi
}
# Usage: checkdep appname
checkdep() {
if [ -z "$(which $1)" -o ! -x "$(which $1)" ]; then
error $"The program '$1' is not in the path. Is $1 installed?"
exit 1
fi
}
# Usage: cleanup file
cleanup() {
if [ -f "$1" ]; then
rm -f "$1"
if [ $? -ne 0 ]; then
$"There was a problem removing the file \"$1\". Please remove manually."
return 1
fi
fi
}
# Usage: checkerror $? "Error message to display" [exit on error:BOOL]
checkerror() {
if [ $1 -ne 0 ]; then
error "$2"
# if optional BOOL then exit, otherwise return errorcode 1
if [ $3 -gt 0 ]; then
# honor KEEPPCM
if [ -z $KEEPPCM ]; then
cleanup "$PCMFILE"
fi
cleanup "$EAC3FILE"
cleanup "$TCFILE"
exit 1
fi
return 1
fi
}
# Usage: doprint "String to print"
doprint() {
if [ $PRINT = 1 ]; then
echo -e "$1"
fi
}
#---------- START OF PROGRAM ----------
# Start the timer and make a working copy for future timings
START=$(date +%s)
PREVTIME=$(($START))
# Parse arguments and/or filename
while [ -z "$MKVFILE" ]; do
# If we're out of arguments no filename was passed
if [ $# -eq 0 ]; then
error $"You must supply a filename."
echo ""
displayhelp
exit 1
fi
case "$1" in
"-c" | "--custom" ) # Use custom name for EAC3 track
shift
PCMNAME=$1
;;
"-d" | "--default" ) # Only allow this if we aren't making the file external
if [ -z $EXTERNAL ]; then
DEFAULT=1
fi
;;
"-e" | "--external" ) # Don't allow -d or -n switches if they're already set
EXTERNAL=1
NOPCM=0
KEEPPCM=0
DEFAULT=0
;;
"-f" | "--force" ) # Test for EAC3 track exits immediately. Use this to continue
FORCE=1
;;
"-i" | "--initial" ) # Make new EAC3 track the first in the file
INITIAL=1
;;
"-k" | "--keep-PCM" ) # Only allow external PCM track if muxing EAC3 track
if [ -z $EXTERNAL ]; then
KEEPPCM=1
fi
;;
"-m" | "--nocolor" | "--monotone" ) # Turns off colors
NOCOLOR=1
;;
"--md5" ) #Perform MD5 comparison when copying across drives
MD5=1
;;
"-n" | "--no-PCM" ) # Only allow this if we aren't making the file external
if [ -z $EXTERNAL ]; then
NOPCM=1
fi
;;
"--new" ) # Do not overwrite original. Create new adjacent file.
NEW=1
;;
"-p" ) # Move required priority value "up"
shift
PRIORITY=$1
;;
"-s" | "--compress" )
shift
COMP=$1
;;
"-t" | "--track" ) # Move required TRACKID argument "up"
shift
PCMTRACK=$1
;;
"-w" | "--wd" ) # Specify working directory manually
shift
WD=$1
;;
"--test" ) # Echo commands and do not execute
if [ $PAUSE = 1 ]; then
warning $"--test overrides previous --debug flag."
fi
PRINT=1
EXECUTE=0
;;
"--debug" ) # Echo commands and pause before executing
if [ $EXECUTE = 0 ]; then
error $"--debug flag not valid with --test."
displayhelp
exit 1
fi
PRINT=1
PAUSE=1
EXECUTE=1
;;
"-h" | "--help" )
displayhelp
exit 0
;;
"-v" | "--verbose" ) # Turn on verbosity
PRINT=1
;;
"-V" | "--version" ) # Version information is always displayed so just exit here
exit 0
;;
-* | --* )
error $"Invalid argument '$1'."
echo ""
displayhelp
exit 1
;;
* )
MKVFILE=$1
shift
# Ensure there are no arguments after the filename
if [ $# -ne 0 ]; then
error $"You cannot supply any arguments after the filename. Please check the command syntax below against what has been parsed."
echo ""
echo $"Control Flags:"
printf " %s: %s" $"Strip PCM:" $NOPCM
printf " %s: %s" $"Keep PCM: " $KEEPPCM
printf " %s: %s" $"Set EAC3 default: " $DEFAULT
printf " %s: %s" $"External EAC3: " $EXTERNAL
printf " %s: %s" $"PCM track: " $PCMTRACK
printf " %s: %s" $"MKV file: " $MKVFILE
echo ""
echo $"Debugging Flags:"
printf " %s: %s" $"Print commands:" $PRINT
printf " %s: %s" $"Pause after print:" $PAUSE
printf " %s: %s" $"Execute commands:" $EXECUTE
echo ""
displayhelp
exit 1
fi
;;
esac
# Move arguments "up" one spot
shift
done
# File and dependency checks
if [ $EXECUTE = 1 ]; then
# Check the file exists and we have permissions
if [ ! -f "$MKVFILE" ]; then
error $"'$MKVFILE' is not a file."
exit 1
elif [ ! -r "$MKVFILE" ]; then
error $"Cannot read '$MKVFILE'."
exit 1
elif [ -z $EXTERNAL ]; then
if [ ! -w "$MKVFILE" ]; then
# Only check write permission if we're not keeping the EAC3 external
error $"Cannot write '$MKVFILE'."
exit 1
fi
fi
# Check dependencies
checkdep mkvmerge
checkdep mkvextract
checkdep mkvinfo
checkdep ffmpeg
checkdep rsync
checkdep perl
fi
# Make some adjustments based on the version of mkvtoolnix
MKVTOOLNIXVERSION=$(mkvmerge -V | cut -d " " -f 2 | sed s/\[\^0-9\]//g)
if [ ${MKVTOOLNIXVERSION} -lt 670 ]; then
AUDIOTRACKPREFIX="audio (A_"
VIDEOTRACKPREFIX="video (V_"
else
AUDIOTRACKPREFIX="audio ("
VIDEOTRACKPREFIX="video ("
fi
# Added check to see if EAC3 track exists. If so, no need to continue
if [ "$(mkvmerge -i "$MKVFILE" | grep -i "${AUDIOTRACKPREFIX}AC-3")" ]; then
echo $"EAC3 track already exists in '$MKVFILE'."
echo ""
if [ $FORCE = 0 ]; then
info $"Use -f or --force argument to bypass this check."
exit 1
fi
info $"Force mode is on. Continuing..."
fi
# Path to file
DEST=$(dirname "$MKVFILE")
# File name without the extension
NAME=$(basename "$MKVFILE" .mkv)
# Setup temporary files
PCMFILE="$WD/$NAME.PCM"
EAC3FILE="$WD/$NAME.EAC3"
TCFILE="$WD/$NAME.tc"
NEWFILE="$WD/$NAME.new.mkv"
doprint $"MKV FILE: $MKVFILE"
doprint $"PCM FILE: $PCMFILE"
doprint $"EAC3 FILE: $EAC3FILE"
doprint $"TIMECODE: $TCFILE"
doprint $"NEW FILE: $NEWFILE"
doprint $"WORKING DIRECTORY: $WD"
# ------ GATHER DATA ------
# If the track id wasn't specified via command line then search for the first PCM audio track
if [ -z $PCMTRACK ]; then
doprint ""
doprint $"Find first PCM track in MKV file."
doprint "> mkvmerge -i \"$MKVFILE\" | grep -m 1 \"${AUDIOTRACKPREFIX}PCM)\" | cut -d ":" -f 1 | cut -d \" \" -f 3"
PCMTRACK="PCMTRACK" #Value for debugging
dopause
if [ $EXECUTE = 1 ]; then
PCMTRACK=$(mkvmerge -i "$MKVFILE" | grep -m 1 "${AUDIOTRACKPREFIX}PCM" | cut -d ":" -f 1 | cut -d " " -f 3)
# Check to make sure there is a PCM track in the MVK
if [ -z $PCMTRACK ]; then
error $"There are no PCM tracks in '$MKVFILE'."
exit 1
fi
fi
doprint "RESULT:PCMTRACK=$PCMTRACK"
else
# Checks to make sure the command line argument track id is valid
doprint ""
doprint $"Checking to see if PCM track specified via arguments is valid."
doprint "> mkvmerge -i \"$MKVFILE\" | grep \"Track ID $PCMTRACK: ${AUDIOTRACKPREFIX}PCM)\""
VALID=$"VALID" #Value for debugging
dopause
if [ $EXECUTE = 1 ]; then
VALID=$(mkvmerge -i "$MKVFILE" | grep "Track ID $PCMTRACK: ${AUDIOTRACKPREFIX}PCM)")
if [ -z "$VALID" ]; then
error $"Track ID '$PCMTRACK' is not a PCM track and/or does not exist."
exit 1
else
info $"Using alternate PCM track with ID '$PCMTRACK'."
fi
fi
doprint "RESULT:VALID=$VALID"
fi
# Get the specified PCM track's information
doprint ""
doprint $"Extract track information for selected PCM track."
doprint "> mkvinfo \"$MKVFILE\""
INFO=$"INFO" #Value for debugging
dopause
if [ $EXECUTE = 1 ]; then
INFO=$(mkvinfo "$MKVFILE")
FIRSTLINE=$(echo "$INFO" | grep -n -m 1 "Track number: $PCMTRACK" | cut -d ":" -f 1)
INFO=$(echo "$INFO" | tail -n +$FIRSTLINE)
LASTLINE=$(echo "$INFO" | grep -n -m 1 "Track number: $(($PCMTRACK+1))" | cut -d ":" -f 1)
if [ -z "$LASTLINE" ]; then
LASTLINE=$(echo "$INFO" | grep -m 1 -n "|+" | cut -d ":" -f 1)
fi
if [ -z "$LASTLINE" ]; then
LASTLINE=$(echo "$INFO" | wc -l)
fi
INFO=$(echo "$INFO" | head -n $LASTLINE)
fi
doprint "RESULT:INFO=\n$INFO"
#Get the language for the PCM track specified
doprint ""
doprint $"Extract language from track info."
doprint '> echo "$INFO" | grep -m 1 \"Language\" | cut -d \" \" -f 5'
PCMLANG=$"PCMLANG" #Value for debugging
dopause
if [ $EXECUTE = 1 ]; then
PCMLANG=$(echo "$INFO" | grep -m 1 "Language" | cut -d " " -f 5)
if [ -z "$PCMLANG" ]; then
PCMLANG=$"eng"
fi
fi
doprint "RESULT:PCMLANG=$PCMLANG"
# Check if a custom name was already specified
if [ -z $PCMNAME ]; then
# Get the name for the PCM track specified
doprint ""
doprint $"Extract name for selected PCM track. Change PCM to EAC3 and update bitrate if present."
doprint '> echo "$INFO" | grep -m 1 "Name" | cut -d " " -f 5- | sed "s/PCM/EAC3/" | awk '"'{gsub(/[0-9]+(\.[0-9]+)?(M|K)bps/,"6144kbps")}1'"''
PCMNAME="PCMNAME" #Value for debugging
dopause
if [ $EXECUTE = 1 ]; then
PCMNAME=$(echo "$INFO" | grep -m 1 "Name" | cut -d " " -f 5- | sed "s/PCM/EAC3/" | awk '{gsub(/[0-9]+(\.[0-9]+)?(M|K)bps/,"6144kbps")}1')
fi
doprint "RESULT:PCMNAME=$PCMNAME"
fi
# ------ EXTRACTION ------
# Extract timecode information for the target track
doprint ""
doprint $"Extract timecode information for the audio track."
doprint "> mkvextract timecodes_v2 \"$MKVFILE\" $PCMTRACK:\"$TCFILE\""
doprint "> sed -n \"2p\" \"$TCFILE\""
doprint "> rm -f \"$TCFILE\""
DELAY=$"DELAY" #Value for debugging
dopause
if [ $EXECUTE = 1 ]; then
color YELLOW; echo $"Extracting Timecodes:"; color OFF
nice -n $PRIORITY mkvextract timecodes_v2 "$MKVFILE" $PCMTRACK:"$TCFILE"
DELAY=$(sed -n "2p" "$TCFILE")
cleanup "$TCFILE"
timestamp $"Timecode extraction took: "
fi
doprint "RESULT:DELAY=$DELAY"
# Extract the PCM track
doprint ""
doprint $"Extract PCM file from MKV."
doprint "> mkvextract tracks \"$MKVFILE\" $PCMTRACK:\"$PCMFILE\""
dopause
if [ $EXECUTE = 1 ]; then
color YELLOW; echo $"Extracting PCM Track: "; color OFF
nice -n $PRIORITY mkvextract tracks "$MKVFILE" $PCMTRACK:"$PCMFILE" 2>&1|perl -ne '$/="\015";next unless /Progress/;$|=1;print "%s\r",$_' #Use Perl to change EOL from \n to \r show Progress %
checkerror $? $"Extracting PCM track failed." 1
timestamp $"PCM track extracting took: "
fi
# ------ CONVERSION ------
# Convert PCM to EAC3
doprint $"Converting PCM to EEAC3."
doprint "> ffmpeg -i \"$PCMFILE\" -acodec eac3 -ac 6 -ab 6144k \"$EAC3FILE\""
dopause
if [ $EXECUTE = 1 ]; then
color YELLOW; echo $"Converting PCM to EAC3:"; color OFF
PCMFILESIZE=$($DUCMD "$PCMFILE" | cut -f1) # Capture PCM filesize for end summary
nice -n $PRIORITY ffmpeg -i "$PCMFILE" -acodec eac3 -ac 6 -ab 6144k "$EAC3FILE" 2>&1|perl -ne '$/="\015";next unless /size=\s*(\d+)/;$|=1;$s='$PCMFILESIZE';printf "Progress: %.0f%\r",450*$1/$s' #run ffmpeg and only show Progress %. Need perl to read \r end of lines
checkerror $? $"Converting the PCM file to EAC3 failed" 1
# If we are keeping the PCM track external copy it back to original folder before deleting
if [ ! -z $KEEPPCM ]; then
color YELLOW; echo $"Moving PCM track to MKV directory."; color OFF
$RSYNCCMD "$PCMFILE" "$DEST"
checkerror $? $"There was an error copying the PCM track to the MKV directory. You can perform this manually from \"$PCMFILE\"." 1
fi
cleanup "$PCMFILE"
echo "Progress: 100%" #The last Progress % gets overwritten so let's put it back and make it pretty
timestamp $"PCM track conversion took: "
fi
# Check there is enough free space for EAC3+MKV
if [ $EXECUTE = 1 ]; then
MKVFILESIZE=$($DUCMD "$MKVFILE" | cut -f1)
EAC3FILESIZE=$($DUCMD "$EAC3FILE" | cut -f1)
WDFREESPACE=$(\df -Pk "$WD" | tail -1 | awk '{print $4}')
if [ $(($MKVFILESIZE + $EAC3FILESIZE)) -gt $WDFREESPACE ]; then
error $"There is not enough free space on \"$WD\" to create the new file."
exit 1
fi
fi
if [ $EXTERNAL ]; then
# We need to trick the rest of the script so that there isn't a lot of
# code duplication. Basically $NEWFILE will be the EAC3 track and we'll
# change $MKVFILE to where we want the EAC3 track to be so we don't
# overwrite the MKV file only an EAC3 track
NEWFILE=$EAC3FILE
MKVFILE="$DEST/$NAME.EAC3"
else
# Start to "build" command
CMD="nice -n $PRIORITY mkvmerge"
# Puts the EAC3 track as the second in the file if indicated as initial
if [ $INITIAL = 1 ]; then
CMD="$CMD --track-order 0:1,1:0"
fi
# Declare output file
CMD="$CMD -o \"$NEWFILE\""
# If user doesn't want the original PCM track drop it
if [ $NOPCM ]; then
# Count the number of audio tracks in the file
AUDIOTRACKS=$(mkvmerge -i "$MKVFILE" | grep "$AUDIOTRACKPREFIX" | wc -l)
if [ $AUDIOTRACKS -eq 1 ]; then
# If there is only the PCM audio track then drop all audio tracks
CMD="$CMD -A"
else
# Get a list of all the other audio tracks
SAVETRACKS=$(mkvmerge -i "$MKVFILE" | grep "$AUDIOTRACKPREFIX" | cut -d ":" -f 1 | grep -vx "Track ID $PCMTRACK" | cut -d " " -f 3 | awk '{ if (T == "") T=$1; else T=T","$1 } END { print T }')
# And copy only those
CMD="$CMD -a \"$SAVETRACKS\""
# Set header compression scheme for all saved tracks
while IFS="," read -ra TID; do
for tid in "${TID[@]}"; do
CMD="$CMD --compression $tid:$COMP"
done
done <<< $SAVETRACKS
fi
fi
# Get track ID of video track
VIDEOTRACK=$(mkvmerge -i "$MKVFILE" | grep -m 1 "$VIDEOTRACKPREFIX" | cut -d ":" -f 1 | cut -d " " -f 3)
# Add original MKV file, set header compression scheme
CMD="$CMD --compression $VIDEOTRACK:$COMP \"$MKVFILE\""
# If user wants new EAC3 as default then add appropriate arguments to command
if [ $DEFAULT ]; then
CMD="$CMD --default-track 0"
fi
# If the language was set for the original PCM track set it for the EAC3
if [ $PCMLANG ]; then
CMD="$CMD --language 0:$PCMLANG"
fi
# If the name was set for the original PCM track set it for the EAC3
if [ "$PCMNAME" ]; then
CMD="$CMD --track-name 0:\"$PCMNAME\""
fi
# If there was a delay on the original PCM set the delay for the new EAC3
if [ $DELAY != 0 ]; then
CMD="$CMD --sync 0:$DELAY"
fi
# Set track compression scheme and append new EAC3
CMD="$CMD --compression 0:$COMP \"$EAC3FILE\""
# ------ MUXING ------
# Run it!
doprint $"Running main remux."
doprint "> $CMD"
dopause
if [ $EXECUTE = 1 ]; then
color YELLOW; echo $"Muxing EAC3 Track in:"; color OFF
eval $CMD 2>&1|perl -ne '$/="\015";next unless /(Progress:\s*\d+%)/;$|=1;print "\r",$1' #Use Perl to change EOL from \n to \r show Progress %
checkerror $? $"Merging the EAC3 track back into the MKV failed." 1
echo #Just need a CR to undo the last \r printed
timestamp $"Muxing EAC3 track in took: "
fi
# Delete EAC3 file if successful
doprint $"Removing temporary EAC3 file."
doprint "> rm -f \"$EAC3FILE\""
dopause
cleanup "$EAC3FILE"
fi
# If we are creating an adjacent file adjust the name of the original
if [ $NEW = 1 ]; then
MKVFILE="$DEST/$NAME-EAC3.mkv"
fi
# Check to see if the two files are on the same device
NEWFILEDEVICE=$(\df "$WD" | tail -1 | cut -d" " -f1)
DSTFILEDEVICE=$(\df "$DEST" | tail -1 | cut -d" " -f1)
if [ "$NEWFILEDEVICE" = "$DSTFILEDEVICE" ]; then
# If we're working on the same device just move the file over the old one
if [ "$NEWFILE" = "$MKVFILE" ]; then
doprint ""
doprint $"New file and destination are the same. No action is required."
else
doprint ""
doprint $"Moving new file over old one."
doprint "> mv \"$NEWFILE\" \"$MKVFILE\""
dopause
if [ $EXECUTE = 1 ]; then
info $"Moving new file over old file. DO NOT KILL THIS PROCESS OR YOU WILL EXPERIENCE DATA LOSS!"
echo $"NEW FILE: $NEWFILE"
echo $"MKV FILE: $MKVFILE"
mv "$NEWFILE" "$MKVFILE"
checkerror $? $"There was an error copying the new MKV over the old one. You can perform this manually by moving '$NEWFILE' over '$MKVFILE'."
fi
fi
else
doprint ""
doprint $"Copying new file over the old one."
doprint "> cp \"$NEWFILE\" \"$MKVFILE\""
dopause
# Check there is enough free space for the new file
if [ $EXECUTE = 1 ]; then
MKVFILEDIFF=$(($($DUCMD "$NEWFILE" | cut -f1) - $MKVFILESIZE))
DESTFREESPACE=$(\df -k "$DEST" | tail -1 | awk '{print $4*1024}')
if [ $MKVFILEDIFF -gt $DESTFREESPACE ]; then
error $"There is not enough free space to copy the new MKV over the old one. Free up some space and then copy '$NEWFILE' over '$MKVFILE'."
exit 1
fi
# Rsync our new MKV with the EAC3 over the old one OR if we're using the -e
# switch then this actually copies the EAC3 file to the original directory
info $"Moving new file over old file. DO NOT KILL THIS PROCESS OR YOU WILL EXPERIENCE DATA LOSS!"
$RSYNCCMD "$NEWFILE" "$MKVFILE"
checkerror $? $"There was an error copying the new MKV over the old one. You can perform this manually by copying '$NEWFILE' over '$MKVFILE'." 1
if [ $MD5 = 1 ]; then
# Check MD5s are equal to ensure the full file was copied (because du sucks across filesystems and platforms)
OLDFILEMD5=$(md5sum "$NEWFILE" | cut -d" " -f1)
NEWFILEMD5=$(md5sum "$MKVFILE" | cut -d" " -f1)
if [ $OLDFILEMD5 -ne $NEWFILEMD5 ]; then
error $"'$NEWFILE' and '$MKVFILE' files do not match. You might want to investigate!"
fi
fi
fi
# Remove new file in $WD
doprint ""
doprint $"Remove working file."
doprint "> rm -f \"$NEWFILE\""
dopause
cleanup "$NEWFILE"
fi
timestamp $"File copy took: "
# Run through the timestamp function manually to display total execution time
END=$(date +%s)
secs=$(($END - $START))
h=$(( secs / 3600 ))
m=$(( ( secs / 60 ) % 60 ))
s=$(( secs % 60 ))
if [ $EXECUTE = 1 -a $PAUSE = 0 ];then
color GREEN
echo -n $"Total processing time: "
printf "%02d:%02d:%02d " $h $m $s
echo $"($secs seconds)"
color OFF
echo
fi
NEWFILESIZE=$($DUCMD "$MKVFILE" | cut -f1) # NEWFILESIZE isn't available in some circumstances so just grab it again
# Print final filesize summary
if [ $EXECUTE = 1 -a $PAUSE = 0 ];then
color YELLOW; printf $"Filesize summary:\n"; color OFF
printf "%23s %15d KB\n" $"Original Filesize:" $MKVFILESIZE|sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
printf "%23s %15d KB\n" $"Extracted PCM Filesize:" $PCMFILESIZE|sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
printf "%23s %15d KB\n" $"Converted EAC3 Filesize:" $EAC3FILESIZE|sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
printf "%23s %15d KB\n" $"Final Filesize:" $NEWFILESIZE|sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
fi