Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.66 KB | None | 0 0
  1. #!/bin/bash
  2. # mkvdts2ac3.sh - add an AC3 track to an MKV from its DTS track
  3. # Author: Jake Wharton <jakewharton@gmail.com>
  4. # Chris Hoekstra <chris.hoekstra@gmail.com>
  5. # Website: http://jakewharton.com
  6. # http://github.com/JakeWharton/mkvdts2ac3/
  7. # Version: 1.6.0
  8. # License:
  9. # Copyright 2011 Jake Wharton
  10. #
  11. # Licensed under the Apache License, Version 2.0 (the "License");
  12. # you may not use this file except in compliance with the License.
  13. # You may obtain a copy of the License at
  14. #
  15. # http://www.apache.org/licenses/LICENSE-2.0
  16. #
  17. # Unless required by applicable law or agreed to in writing, software
  18. # distributed under the License is distributed on an "AS IS" BASIS,
  19. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. # See the License for the specific language governing permissions and
  21. # limitations under the License.
  22.  
  23.  
  24. # Display version header
  25. echo "mkvdts2ac3-1.6.0 - by Jake Wharton <jakewharton@gmail.com> and"
  26. echo " Chris Hoekstra <chris.hoekstra@gmail.com>"
  27. echo
  28.  
  29. # Debugging flags
  30. # DO NOT EDIT THESE! USE --debug OR --test ARGUMENT INSTEAD.
  31. PRINT=0
  32. PAUSE=0
  33. EXECUTE=1
  34.  
  35. # Default values
  36. PRIORITY=0
  37. FORCE=0
  38. NOCOLOR=0
  39. MD5=0
  40. INITIAL=0
  41. NEW=0
  42. COMP="none"
  43. WD="/tmp" # Working Directory (Use the -w/--wd argument to change)
  44.  
  45. # These are so you can make quick changes to the cmdline args without having to search and replace the entire script
  46. DUCMD="$(which \du) -k"
  47. RSYNCCMD="$(which \rsync) --progress -a"
  48.  
  49. # Check for a .mkvdts2ac3.rc file in user's home directory for custom defaults
  50. if [ -f ~/.mkvdts2ac3.rc ]; then
  51. . ~/.mkvdts2ac3.rc
  52. fi
  53.  
  54. # Force English output, grepping for messages may fail otherwise
  55. export LC_MESSAGES=C
  56.  
  57. #---------- FUNCTIONS --------
  58. displayhelp() {
  59. # Usage: displayhelp
  60. echo "Usage: `basename $0` [options] <filename>"
  61. echo "Options:"
  62. echo " -c TITLE, Custom AC3 track title."
  63. echo " --custom TITLE"
  64. echo " -d, --default Mark AC3 track as default."
  65. echo " -e, --external Leave AC3 track out of file. Does not modify the"
  66. echo " original matroska file. This overrides '-n' and"
  67. echo " '-d' arguments."
  68. echo " -f, --force Force processing when AC3 track is detected"
  69. echo " -i, --initial New AC3 track will be first in the file."
  70. echo " -k, --keep-dts Keep external DTS track (implies '-n')."
  71. echo " -m, --nocolor Do not use colors (monotone)."
  72. echo " --md5 Perform MD5 comparison when copying across drives."
  73. echo " -n, --no-dts Do not retain the DTS track."
  74. echo " --new Do not copy over original. Create new adjacent file."
  75. echo " -p PRIORITY Modify niceness of executed commands."
  76. echo " -s MODE,"
  77. echo " --compress MODE Apply header compression to streams (See mkvmerge's --compression)."
  78. echo " -t TRACKID,"
  79. echo " --track TRACKID Specify alternate DTS track."
  80. echo " -w FOLDER,"
  81. echo " --wd FOLDER Specify alternate temporary working directory."
  82. echo
  83. echo " --test Print commands only, execute nothing."
  84. echo " --debug Print commands and pause before executing each."
  85. echo
  86. echo " -h, --help Print command usage."
  87. echo " -v, --verbose Turn on verbose output"
  88. echo " -V, --version Print script version information."
  89. }
  90.  
  91.  
  92. # Usage: color shade
  93. function color {
  94. # Are we in Cron?
  95. if [ ! -t 0 -o $NOCOLOR = 1 ]; then return 1; fi
  96. case $1 in
  97. off|OFF) echo -n '';;
  98. red|RED) echo -n '';;
  99. yellow|YELLOW) echo -n '';;
  100. green|GREEN) echo -n '';;
  101. blue|BLUE) echo -n '';;
  102. bell|BELL) echo -n '';;
  103. *) ;;
  104. esac
  105. }
  106.  
  107. # Usage: timestamp ["String to preface time display"]
  108. timestamp() {
  109. CURRTIME=$(date +%s)
  110. secs=$(($CURRTIME - $PREVTIME))
  111. PREVTIME=$((CURRTIME))
  112. h=$(( secs / 3600 ))
  113. m=$(( ( secs / 60 ) % 60 ))
  114. s=$(( secs % 60 ))
  115.  
  116. if [ $EXECUTE = 1 -a $PAUSE = 0 ]; then
  117. echo -n "$1 "
  118. printf "%02d:%02d:%02d " $h $m $s
  119. echo $"($secs seconds)"
  120. echo ""
  121. fi
  122. }
  123.  
  124. # Usage: error "String to display"
  125. error() {
  126. color BELL;color RED
  127. printf "%s: %s\n" $"ERROR" "$1"
  128. color OFF
  129. }
  130.  
  131. # Usage: warning "String to display"
  132. warning() {
  133. color YELLOW
  134. printf "%s: %s\n" $"WARNING" "$1"
  135. color OFF
  136. }
  137.  
  138. info() {
  139. color BLUE
  140. printf "%s: %s\n" $"INFO" "$1"
  141. color OFF
  142. }
  143.  
  144. # Usage: dopause
  145. dopause() {
  146. if [ $PAUSE = 1 ]; then
  147. read
  148. fi
  149. }
  150.  
  151. # Usage: checkdep appname
  152. checkdep() {
  153. if [ -z "$(which $1)" -o ! -x "$(which $1)" ]; then
  154. error $"The program '$1' is not in the path. Is $1 installed?"
  155. exit 1
  156. fi
  157. }
  158.  
  159. # Usage: cleanup file
  160. cleanup() {
  161. if [ -f "$1" ]; then
  162. rm -f "$1"
  163. if [ $? -ne 0 ]; then
  164. $"There was a problem removing the file \"$1\". Please remove manually."
  165. return 1
  166. fi
  167. fi
  168. }
  169.  
  170. # Usage: checkerror $? "Error message to display" [exit on error:BOOL]
  171. checkerror() {
  172. if [ $1 -ne 0 ]; then
  173. error "$2"
  174. # if optional BOOL then exit, otherwise return errorcode 1
  175. if [ $3 -gt 0 ]; then
  176. # honor KEEPDTS
  177. if [ -z $KEEPDTS ]; then
  178. cleanup "$DTSFILE"
  179. fi
  180.  
  181. cleanup "$AC3FILE"
  182. cleanup "$TCFILE"
  183. exit 1
  184. fi
  185. return 1
  186. fi
  187. }
  188.  
  189. # Usage: doprint "String to print"
  190. doprint() {
  191. if [ $PRINT = 1 ]; then
  192. echo -e "$1"
  193. fi
  194. }
  195.  
  196. #---------- START OF PROGRAM ----------
  197. # Start the timer and make a working copy for future timings
  198. START=$(date +%s)
  199. PREVTIME=$(($START))
  200.  
  201. # Parse arguments and/or filename
  202. while [ -z "$MKVFILE" ]; do
  203.  
  204. # If we're out of arguments no filename was passed
  205. if [ $# -eq 0 ]; then
  206. error $"You must supply a filename."
  207. echo ""
  208. displayhelp
  209. exit 1
  210. fi
  211.  
  212. case "$1" in
  213. "-c" | "--custom" ) # Use custom name for AC3 track
  214. shift
  215. DTSNAME=$1
  216. ;;
  217. "-d" | "--default" ) # Only allow this if we aren't making the file external
  218. if [ -z $EXTERNAL ]; then
  219. DEFAULT=1
  220. fi
  221. ;;
  222. "-e" | "--external" ) # Don't allow -d or -n switches if they're already set
  223. EXTERNAL=1
  224. NODTS=0
  225. KEEPDTS=0
  226. DEFAULT=0
  227. ;;
  228. "-f" | "--force" ) # Test for AC3 track exits immediately. Use this to continue
  229. FORCE=1
  230. ;;
  231. "-i" | "--initial" ) # Make new AC3 track the first in the file
  232. INITIAL=1
  233. ;;
  234. "-k" | "--keep-dts" ) # Only allow external DTS track if muxing AC3 track
  235. if [ -z $EXTERNAL ]; then
  236. KEEPDTS=1
  237. fi
  238. ;;
  239. "-m" | "--nocolor" | "--monotone" ) # Turns off colors
  240. NOCOLOR=1
  241. ;;
  242. "--md5" ) #Perform MD5 comparison when copying across drives
  243. MD5=1
  244. ;;
  245. "-n" | "--no-dts" ) # Only allow this if we aren't making the file external
  246. if [ -z $EXTERNAL ]; then
  247. NODTS=1
  248. fi
  249. ;;
  250. "--new" ) # Do not overwrite original. Create new adjacent file.
  251. NEW=1
  252. ;;
  253. "-p" ) # Move required priority value "up"
  254. shift
  255. PRIORITY=$1
  256. ;;
  257. "-s" | "--compress" )
  258. shift
  259. COMP=$1
  260. ;;
  261. "-t" | "--track" ) # Move required TRACKID argument "up"
  262. shift
  263. DTSTRACK=$1
  264. ;;
  265. "-w" | "--wd" ) # Specify working directory manually
  266. shift
  267. WD=$1
  268. ;;
  269. "--test" ) # Echo commands and do not execute
  270. if [ $PAUSE = 1 ]; then
  271. warning $"--test overrides previous --debug flag."
  272. fi
  273. PRINT=1
  274. EXECUTE=0
  275. ;;
  276. "--debug" ) # Echo commands and pause before executing
  277. if [ $EXECUTE = 0 ]; then
  278. error $"--debug flag not valid with --test."
  279. displayhelp
  280. exit 1
  281. fi
  282. PRINT=1
  283. PAUSE=1
  284. EXECUTE=1
  285. ;;
  286. "-h" | "--help" )
  287. displayhelp
  288. exit 0
  289. ;;
  290. "-v" | "--verbose" ) # Turn on verbosity
  291. PRINT=1
  292. ;;
  293. "-V" | "--version" ) # Version information is always displayed so just exit here
  294. exit 0
  295. ;;
  296. -* | --* )
  297. error $"Invalid argument '$1'."
  298. echo ""
  299. displayhelp
  300. exit 1
  301. ;;
  302. * )
  303. MKVFILE=$1
  304. shift
  305.  
  306. # Ensure there are no arguments after the filename
  307. if [ $# -ne 0 ]; then
  308. error $"You cannot supply any arguments after the filename. Please check the command syntax below against what has been parsed."
  309. echo ""
  310. echo $"Control Flags:"
  311. printf " %s: %s" $"Strip DTS:" $NODTS
  312. printf " %s: %s" $"Keep DTS: " $KEEPDTS
  313. printf " %s: %s" $"Set AC3 default: " $DEFAULT
  314. printf " %s: %s" $"External AC3: " $EXTERNAL
  315. printf " %s: %s" $"DTS track: " $DTSTRACK
  316. printf " %s: %s" $"MKV file: " $MKVFILE
  317. echo ""
  318. echo $"Debugging Flags:"
  319. printf " %s: %s" $"Print commands:" $PRINT
  320. printf " %s: %s" $"Pause after print:" $PAUSE
  321. printf " %s: %s" $"Execute commands:" $EXECUTE
  322. echo ""
  323. displayhelp
  324. exit 1
  325. fi
  326. ;;
  327. esac
  328.  
  329. # Move arguments "up" one spot
  330. shift
  331. done
  332.  
  333. # File and dependency checks
  334. if [ $EXECUTE = 1 ]; then
  335. # Check the file exists and we have permissions
  336. if [ ! -f "$MKVFILE" ]; then
  337. error $"'$MKVFILE' is not a file."
  338. exit 1
  339. elif [ ! -r "$MKVFILE" ]; then
  340. error $"Cannot read '$MKVFILE'."
  341. exit 1
  342. elif [ -z $EXTERNAL ]; then
  343. if [ ! -w "$MKVFILE" ]; then
  344. # Only check write permission if we're not keeping the AC3 external
  345. error $"Cannot write '$MKVFILE'."
  346. exit 1
  347. fi
  348. fi
  349.  
  350. # Check dependencies
  351. checkdep mkvmerge
  352. checkdep mkvextract
  353. checkdep mkvinfo
  354. checkdep ffmpeg
  355. checkdep rsync
  356. checkdep perl
  357. fi
  358.  
  359. # Make some adjustments based on the version of mkvtoolnix
  360. MKVTOOLNIXVERSION=$(mkvmerge -V | cut -d " " -f 2 | sed s/\[\^0-9\]//g)
  361. if [ ${MKVTOOLNIXVERSION} -lt 670 ]; then
  362. AUDIOTRACKPREFIX="audio (A_"
  363. VIDEOTRACKPREFIX="video (V_"
  364. else
  365. AUDIOTRACKPREFIX="audio ("
  366. VIDEOTRACKPREFIX="video ("
  367. fi
  368.  
  369. # Added check to see if AC3 track exists. If so, no need to continue
  370. if [ "$(mkvmerge -i "$MKVFILE" | grep -i "${AUDIOTRACKPREFIX}AC3")" ]; then
  371. echo $"AC3 track already exists in '$MKVFILE'."
  372. echo ""
  373. if [ $FORCE = 0 ]; then
  374. info $"Use -f or --force argument to bypass this check."
  375. exit 1
  376. fi
  377. info $"Force mode is on. Continuing..."
  378. fi
  379.  
  380. # Path to file
  381. DEST=$(dirname "$MKVFILE")
  382.  
  383. # File name without the extension
  384. NAME=$(basename "$MKVFILE" .mkv)
  385.  
  386. # Setup temporary files
  387. DTSFILE="$WD/$NAME.dts"
  388. AC3FILE="$WD/$NAME.ac3"
  389. TCFILE="$WD/$NAME.tc"
  390. NEWFILE="$WD/$NAME.new.mkv"
  391.  
  392. doprint $"MKV FILE: $MKVFILE"
  393. doprint $"DTS FILE: $DTSFILE"
  394. doprint $"AC3 FILE: $AC3FILE"
  395. doprint $"TIMECODE: $TCFILE"
  396. doprint $"NEW FILE: $NEWFILE"
  397. doprint $"WORKING DIRECTORY: $WD"
  398.  
  399. # ------ GATHER DATA ------
  400. # If the track id wasn't specified via command line then search for the first DTS audio track
  401. if [ -z $DTSTRACK ]; then
  402. doprint ""
  403. doprint $"Find first DTS track in MKV file."
  404. doprint "> mkvmerge -i \"$MKVFILE\" | grep -m 1 \"${AUDIOTRACKPREFIX}DTS)\" | cut -d ":" -f 1 | cut -d \" \" -f 3"
  405. DTSTRACK="DTSTRACK" #Value for debugging
  406. dopause
  407. if [ $EXECUTE = 1 ]; then
  408. DTSTRACK=$(mkvmerge -i "$MKVFILE" | grep -m 1 "${AUDIOTRACKPREFIX}DTS)" | cut -d ":" -f 1 | cut -d " " -f 3)
  409.  
  410. # Check to make sure there is a DTS track in the MVK
  411. if [ -z $DTSTRACK ]; then
  412. error $"There are no DTS tracks in '$MKVFILE'."
  413. exit 1
  414. fi
  415. fi
  416. doprint "RESULT:DTSTRACK=$DTSTRACK"
  417. else
  418. # Checks to make sure the command line argument track id is valid
  419. doprint ""
  420. doprint $"Checking to see if DTS track specified via arguments is valid."
  421. doprint "> mkvmerge -i \"$MKVFILE\" | grep \"Track ID $DTSTRACK: ${AUDIOTRACKPREFIX}DTS)\""
  422. VALID=$"VALID" #Value for debugging
  423. dopause
  424. if [ $EXECUTE = 1 ]; then
  425. VALID=$(mkvmerge -i "$MKVFILE" | grep "Track ID $DTSTRACK: ${AUDIOTRACKPREFIX}DTS)")
  426.  
  427. if [ -z "$VALID" ]; then
  428. error $"Track ID '$DTSTRACK' is not a DTS track and/or does not exist."
  429. exit 1
  430. else
  431. info $"Using alternate DTS track with ID '$DTSTRACK'."
  432. fi
  433. fi
  434. doprint "RESULT:VALID=$VALID"
  435. fi
  436. # Get the specified DTS track's information
  437. doprint ""
  438. doprint $"Extract track information for selected DTS track."
  439. doprint "> mkvinfo \"$MKVFILE\""
  440.  
  441. INFO=$"INFO" #Value for debugging
  442. dopause
  443. if [ $EXECUTE = 1 ]; then
  444. INFO=$(mkvinfo "$MKVFILE")
  445. FIRSTLINE=$(echo "$INFO" | grep -n -m 1 "Track number: $DTSTRACK" | cut -d ":" -f 1)
  446. INFO=$(echo "$INFO" | tail -n +$FIRSTLINE)
  447. LASTLINE=$(echo "$INFO" | grep -n -m 1 "Track number: $(($DTSTRACK+1))" | cut -d ":" -f 1)
  448. if [ -z "$LASTLINE" ]; then
  449. LASTLINE=$(echo "$INFO" | grep -m 1 -n "|+" | cut -d ":" -f 1)
  450. fi
  451. if [ -z "$LASTLINE" ]; then
  452. LASTLINE=$(echo "$INFO" | wc -l)
  453. fi
  454. INFO=$(echo "$INFO" | head -n $LASTLINE)
  455. fi
  456. doprint "RESULT:INFO=\n$INFO"
  457.  
  458. #Get the language for the DTS track specified
  459. doprint ""
  460. doprint $"Extract language from track info."
  461. doprint '> echo "$INFO" | grep -m 1 \"Language\" | cut -d \" \" -f 5'
  462.  
  463. DTSLANG=$"DTSLANG" #Value for debugging
  464. dopause
  465. if [ $EXECUTE = 1 ]; then
  466. DTSLANG=$(echo "$INFO" | grep -m 1 "Language" | cut -d " " -f 5)
  467. if [ -z "$DTSLANG" ]; then
  468. DTSLANG=$"eng"
  469. fi
  470. fi
  471. doprint "RESULT:DTSLANG=$DTSLANG"
  472.  
  473. # Check if a custom name was already specified
  474. if [ -z $DTSNAME ]; then
  475. # Get the name for the DTS track specified
  476. doprint ""
  477. doprint $"Extract name for selected DTS track. Change DTS to AC3 and update bitrate if present."
  478. doprint '> echo "$INFO" | grep -m 1 "Name" | cut -d " " -f 5- | sed "s/DTS/AC3/" | awk '"'{gsub(/[0-9]+(\.[0-9]+)?(M|K)bps/,"640Kbps")}1'"''
  479. DTSNAME="DTSNAME" #Value for debugging
  480. dopause
  481. if [ $EXECUTE = 1 ]; then
  482. DTSNAME=$(echo "$INFO" | grep -m 1 "Name" | cut -d " " -f 5- | sed "s/DTS/AC3/" | awk '{gsub(/[0-9]+(\.[0-9]+)?(M|K)bps/,"640Kbps")}1')
  483. fi
  484. doprint "RESULT:DTSNAME=$DTSNAME"
  485. fi
  486.  
  487. # ------ EXTRACTION ------
  488. # Extract timecode information for the target track
  489. doprint ""
  490. doprint $"Extract timecode information for the audio track."
  491. doprint "> mkvextract timecodes_v2 \"$MKVFILE\" $DTSTRACK:\"$TCFILE\""
  492. doprint "> sed -n \"2p\" \"$TCFILE\""
  493. doprint "> rm -f \"$TCFILE\""
  494.  
  495. DELAY=$"DELAY" #Value for debugging
  496. dopause
  497. if [ $EXECUTE = 1 ]; then
  498. color YELLOW; echo $"Extracting Timecodes:"; color OFF
  499. nice -n $PRIORITY mkvextract timecodes_v2 "$MKVFILE" $DTSTRACK:"$TCFILE"
  500. DELAY=$(sed -n "2p" "$TCFILE")
  501. cleanup "$TCFILE"
  502. timestamp $"Timecode extraction took: "
  503. fi
  504. doprint "RESULT:DELAY=$DELAY"
  505.  
  506. # Extract the DTS track
  507. doprint ""
  508. doprint $"Extract DTS file from MKV."
  509. doprint "> mkvextract tracks \"$MKVFILE\" $DTSTRACK:\"$DTSFILE\""
  510.  
  511. dopause
  512. if [ $EXECUTE = 1 ]; then
  513. color YELLOW; echo $"Extracting DTS Track: "; color OFF
  514. nice -n $PRIORITY mkvextract tracks "$MKVFILE" $DTSTRACK:"$DTSFILE" 2>&1|perl -ne '$/="\015";next unless /Progress/;$|=1;print "%s\r",$_' #Use Perl to change EOL from \n to \r show Progress %
  515. checkerror $? $"Extracting DTS track failed." 1
  516. timestamp $"DTS track extracting took: "
  517. fi
  518.  
  519. # ------ CONVERSION ------
  520. # Convert DTS to AC3
  521. doprint $"Converting DTS to AC3."
  522. doprint "> ffmpeg -i \"$DTSFILE\" -acodec ac3 -ac 6 -ab 640k \"$AC3FILE\""
  523.  
  524. dopause
  525. if [ $EXECUTE = 1 ]; then
  526. color YELLOW; echo $"Converting DTS to AC3:"; color OFF
  527. DTSFILESIZE=$($DUCMD "$DTSFILE" | cut -f1) # Capture DTS filesize for end summary
  528. nice -n $PRIORITY ffmpeg -i "$DTSFILE" -acodec ac3 -ac 6 -ab 640k "$AC3FILE" 2>&1|perl -ne '$/="\015";next unless /size=\s*(\d+)/;$|=1;$s='$DTSFILESIZE';printf "Progress: %.0f%\r",450*$1/$s' #run ffmpeg and only show Progress %. Need perl to read \r end of lines
  529. checkerror $? $"Converting the DTS file to AC3 failed" 1
  530.  
  531. # If we are keeping the DTS track external copy it back to original folder before deleting
  532. if [ ! -z $KEEPDTS ]; then
  533. color YELLOW; echo $"Moving DTS track to MKV directory."; color OFF
  534. $RSYNCCMD "$DTSFILE" "$DEST"
  535. checkerror $? $"There was an error copying the DTS track to the MKV directory. You can perform this manually from \"$DTSFILE\"." 1
  536. fi
  537. cleanup "$DTSFILE"
  538. echo "Progress: 100%" #The last Progress % gets overwritten so let's put it back and make it pretty
  539. timestamp $"DTS track conversion took: "
  540. fi
  541.  
  542. # Check there is enough free space for AC3+MKV
  543. if [ $EXECUTE = 1 ]; then
  544. MKVFILESIZE=$($DUCMD "$MKVFILE" | cut -f1)
  545. AC3FILESIZE=$($DUCMD "$AC3FILE" | cut -f1)
  546. WDFREESPACE=$(\df -Pk "$WD" | tail -1 | awk '{print $4}')
  547. if [ $(($MKVFILESIZE + $AC3FILESIZE)) -gt $WDFREESPACE ]; then
  548. error $"There is not enough free space on \"$WD\" to create the new file."
  549. exit 1
  550. fi
  551. fi
  552.  
  553. if [ $EXTERNAL ]; then
  554. # We need to trick the rest of the script so that there isn't a lot of
  555. # code duplication. Basically $NEWFILE will be the AC3 track and we'll
  556. # change $MKVFILE to where we want the AC3 track to be so we don't
  557. # overwrite the MKV file only an AC3 track
  558. NEWFILE=$AC3FILE
  559. MKVFILE="$DEST/$NAME.ac3"
  560. else
  561. # Start to "build" command
  562. CMD="nice -n $PRIORITY mkvmerge"
  563.  
  564. # Puts the AC3 track as the second in the file if indicated as initial
  565. if [ $INITIAL = 1 ]; then
  566. CMD="$CMD --track-order 0:1,1:0"
  567. fi
  568.  
  569. # Declare output file
  570. CMD="$CMD -o \"$NEWFILE\""
  571.  
  572. # If user doesn't want the original DTS track drop it
  573. if [ $NODTS ]; then
  574. # Count the number of audio tracks in the file
  575. AUDIOTRACKS=$(mkvmerge -i "$MKVFILE" | grep "$AUDIOTRACKPREFIX" | wc -l)
  576.  
  577. if [ $AUDIOTRACKS -eq 1 ]; then
  578. # If there is only the DTS audio track then drop all audio tracks
  579. CMD="$CMD -A"
  580. else
  581. # Get a list of all the other audio tracks
  582. SAVETRACKS=$(mkvmerge -i "$MKVFILE" | grep "$AUDIOTRACKPREFIX" | cut -d ":" -f 1 | grep -vx "Track ID $DTSTRACK" | cut -d " " -f 3 | awk '{ if (T == "") T=$1; else T=T","$1 } END { print T }')
  583. # And copy only those
  584. CMD="$CMD -a \"$SAVETRACKS\""
  585.  
  586. # Set header compression scheme for all saved tracks
  587. while IFS="," read -ra TID; do
  588. for tid in "${TID[@]}"; do
  589. CMD="$CMD --compression $tid:$COMP"
  590. done
  591. done <<< $SAVETRACKS
  592. fi
  593. fi
  594.  
  595. # Get track ID of video track
  596. VIDEOTRACK=$(mkvmerge -i "$MKVFILE" | grep -m 1 "$VIDEOTRACKPREFIX" | cut -d ":" -f 1 | cut -d " " -f 3)
  597. # Add original MKV file, set header compression scheme
  598. CMD="$CMD --compression $VIDEOTRACK:$COMP \"$MKVFILE\""
  599.  
  600.  
  601. # If user wants new AC3 as default then add appropriate arguments to command
  602. if [ $DEFAULT ]; then
  603. CMD="$CMD --default-track 0"
  604. fi
  605.  
  606. # If the language was set for the original DTS track set it for the AC3
  607. if [ $DTSLANG ]; then
  608. CMD="$CMD --language 0:$DTSLANG"
  609. fi
  610.  
  611. # If the name was set for the original DTS track set it for the AC3
  612. if [ "$DTSNAME" ]; then
  613. CMD="$CMD --track-name 0:\"$DTSNAME\""
  614. fi
  615.  
  616. # If there was a delay on the original DTS set the delay for the new AC3
  617. if [ $DELAY != 0 ]; then
  618. CMD="$CMD --sync 0:$DELAY"
  619. fi
  620.  
  621. # Set track compression scheme and append new AC3
  622. CMD="$CMD --compression 0:$COMP \"$AC3FILE\""
  623.  
  624. # ------ MUXING ------
  625. # Run it!
  626. doprint $"Running main remux."
  627. doprint "> $CMD"
  628. dopause
  629. if [ $EXECUTE = 1 ]; then
  630. color YELLOW; echo $"Muxing AC3 Track in:"; color OFF
  631. 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 %
  632. checkerror $? $"Merging the AC3 track back into the MKV failed." 1
  633. echo #Just need a CR to undo the last \r printed
  634. timestamp $"Muxing AC3 track in took: "
  635. fi
  636.  
  637. # Delete AC3 file if successful
  638. doprint $"Removing temporary AC3 file."
  639. doprint "> rm -f \"$AC3FILE\""
  640. dopause
  641. cleanup "$AC3FILE"
  642. fi
  643.  
  644. # If we are creating an adjacent file adjust the name of the original
  645. if [ $NEW = 1 ]; then
  646. MKVFILE="$DEST/$NAME-AC3.mkv"
  647. fi
  648.  
  649. # Check to see if the two files are on the same device
  650. NEWFILEDEVICE=$(\df "$WD" | tail -1 | cut -d" " -f1)
  651. DSTFILEDEVICE=$(\df "$DEST" | tail -1 | cut -d" " -f1)
  652.  
  653. if [ "$NEWFILEDEVICE" = "$DSTFILEDEVICE" ]; then
  654. # If we're working on the same device just move the file over the old one
  655. if [ "$NEWFILE" = "$MKVFILE" ]; then
  656. doprint ""
  657. doprint $"New file and destination are the same. No action is required."
  658. else
  659. doprint ""
  660. doprint $"Moving new file over old one."
  661. doprint "> mv \"$NEWFILE\" \"$MKVFILE\""
  662. dopause
  663. if [ $EXECUTE = 1 ]; then
  664. info $"Moving new file over old file. DO NOT KILL THIS PROCESS OR YOU WILL EXPERIENCE DATA LOSS!"
  665. echo $"NEW FILE: $NEWFILE"
  666. echo $"MKV FILE: $MKVFILE"
  667. mv "$NEWFILE" "$MKVFILE"
  668. checkerror $? $"There was an error copying the new MKV over the old one. You can perform this manually by moving '$NEWFILE' over '$MKVFILE'."
  669. fi
  670. fi
  671. else
  672. doprint ""
  673. doprint $"Copying new file over the old one."
  674. doprint "> cp \"$NEWFILE\" \"$MKVFILE\""
  675. dopause
  676.  
  677. # Check there is enough free space for the new file
  678. if [ $EXECUTE = 1 ]; then
  679. MKVFILEDIFF=$(($($DUCMD "$NEWFILE" | cut -f1) - $MKVFILESIZE))
  680. DESTFREESPACE=$(\df -k "$DEST" | tail -1 | awk '{print $4*1024}')
  681. if [ $MKVFILEDIFF -gt $DESTFREESPACE ]; then
  682. 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'."
  683. exit 1
  684. fi
  685.  
  686. # Rsync our new MKV with the AC3 over the old one OR if we're using the -e
  687. # switch then this actually copies the AC3 file to the original directory
  688. info $"Moving new file over old file. DO NOT KILL THIS PROCESS OR YOU WILL EXPERIENCE DATA LOSS!"
  689. $RSYNCCMD "$NEWFILE" "$MKVFILE"
  690. checkerror $? $"There was an error copying the new MKV over the old one. You can perform this manually by copying '$NEWFILE' over '$MKVFILE'." 1
  691.  
  692. if [ $MD5 = 1 ]; then
  693. # Check MD5s are equal to ensure the full file was copied (because du sucks across filesystems and platforms)
  694. OLDFILEMD5=$(md5sum "$NEWFILE" | cut -d" " -f1)
  695. NEWFILEMD5=$(md5sum "$MKVFILE" | cut -d" " -f1)
  696. if [ $OLDFILEMD5 -ne $NEWFILEMD5 ]; then
  697. error $"'$NEWFILE' and '$MKVFILE' files do not match. You might want to investigate!"
  698. fi
  699. fi
  700. fi
  701. # Remove new file in $WD
  702. doprint ""
  703. doprint $"Remove working file."
  704. doprint "> rm -f \"$NEWFILE\""
  705. dopause
  706. cleanup "$NEWFILE"
  707. fi
  708.  
  709. timestamp $"File copy took: "
  710.  
  711. # Run through the timestamp function manually to display total execution time
  712. END=$(date +%s)
  713. secs=$(($END - $START))
  714. h=$(( secs / 3600 ))
  715. m=$(( ( secs / 60 ) % 60 ))
  716. s=$(( secs % 60 ))
  717.  
  718. if [ $EXECUTE = 1 -a $PAUSE = 0 ];then
  719. color GREEN
  720. echo -n $"Total processing time: "
  721. printf "%02d:%02d:%02d " $h $m $s
  722. echo $"($secs seconds)"
  723. color OFF
  724. echo
  725. fi
  726.  
  727. NEWFILESIZE=$($DUCMD "$MKVFILE" | cut -f1) # NEWFILESIZE isn't available in some circumstances so just grab it again
  728.  
  729. # Print final filesize summary
  730. if [ $EXECUTE = 1 -a $PAUSE = 0 ];then
  731. color YELLOW; printf $"Filesize summary:\n"; color OFF
  732. printf "%23s %15d KB\n" $"Original Filesize:" $MKVFILESIZE|sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
  733. printf "%23s %15d KB\n" $"Extracted DTS Filesize:" $DTSFILESIZE|sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
  734. printf "%23s %15d KB\n" $"Converted AC3 Filesize:" $AC3FILESIZE|sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
  735. printf "%23s %15d KB\n" $"Final Filesize:" $NEWFILESIZE|sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
  736. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement