Skip to content

Bash script to convert DTS audio to AC3 within a matroska file.

License

Notifications You must be signed in to change notification settings

tmchow/mkvdts2ac3

 
 

Repository files navigation

mkvdts2ac3 is a bash script which can be used for converting the DTS in Matroska (MKV) files to AC3. It provides you with a healthy set of options for controlling the resulting file.

Original is is by Jake Wharton but is modified here to work for my own use case specifically on Unraid.

Installation

Prerequisites

Make sure the executables for the following libraries are accessible.

  1. mkvtoolnix docker - Matroska tools docker container (lesage/mkvtoolnix)
  2. ffmpeg - Audio conversion tool
  3. rsync - File transfer and synchronization

Installation

If you have git installed, you can just run git clone git://github.com/tmchow/mkvdts2ac3.git.

You can download the script directly with wget or curl: wget https://raw.github.com/tmchow/mkvdts2ac3/master/mkvdts2ac3.sh -or- curl -O https://raw.github.com/tmchow/mkvdts2ac3/master/mkvdts2ac3.sh

Otherwise you can click the "Download" link on the GitHub project page and download an archive and extract its contents.

Edit each of the shell scripts mkvextract, mkvinfo and mkvmerge so the right path mappings are included. The key to getting this to work on Unraid using the docker container for MKVtoolnix is that we map paths exactly the same as main unraid system (outside the docker) so the apps running within the docker reference the files the same as you do outside the container.

Copy shell scripts mkvextract, mkvinfo and mkvmerge into /usr/bin. These are scripts that wrap calls into the MKVToolNix container to avoid needing to change the original mkvdts2ac3.sh script.

Optional: If you want easy access to the script from any directory you can copy or symlink the mkvdts2ac3.sh file to a directory in your PATH variable or else append the script's directory to the PATH variable.

Usage

This script was designed to be very simple and will automatically convert the first DTS track it finds in a Matroska file to AC3 and append it when run without any arguments. Since this was the most common scenario for the developer it is the default action. mkvdts2ac3.sh /path/to/file/Some.Random.Movie.mkv

IMPORTANT: This script only works with docker on unraid (my specific use case) if you pass in full paths

For users who wish to change the behavior there are a variety of options which control various aspects of the script. Here is the output of the --help argument.

mkvdts2ac3-1.5.3 - by Jake Wharton <[email protected]> and
                      Chris Hoekstra <[email protected]>

Usage: mkvdts2ac3.sh [options] <filename>
Options:
     -c TITLE,        Custom AC3 track title.
     --custom TITLE
     -d, --default    Mark AC3 track as default.
     -e, --external   Leave AC3 track out of file. Does not modify the
                      original matroska file. This overrides '-n' and
                      '-d' arguments.
     -f, --force      Force processing when AC3 track is detected
     -i, --initial    New AC3 track will be first in the file.
     -k, --keep-dts   Keep external DTS track (implies '-n').
     -m, --nocolor    Do not use colors (monotone).
     --md5            Perform MD5 comparison when copying across drives.
     -n, --no-dts     Do not retain the DTS track.
     --new            Do not copy over original. Create new adjacent file.
     -p PRIORITY      Modify niceness of executed commands.
     -s MODE,
     --compress MODE  Apply header compression to streams (See mkvmerge's --compression).
     -t TRACKID,
     --track TRACKID  Specify alternate DTS track.
     -w FOLDER,
     --wd FOLDER      Specify alternate temporary working directory.

     --test           Print commands only, execute nothing.
     --debug          Print commands and pause before executing each.

     -h, --help       Print command usage.
     -v, --verbose    Turn on verbose output
     -V, --version    Print script version information.

User Defaults

If you find yourself constantly using the same settings, you can create a configuration file to automatically set them. Copy the following to ~/.mkvdts2ac3.rc and uncomment whichever lines you wish.

#EXTERNAL=1
#NODTS=1
#KEEPDTS=1
#DEFAULT=1
#FORCE=1
#INITIAL=1
#NOCOLOR=1
#MD5=1
#NEW=1
#PRIORITY=0
#DTSTRACK=
#DTSNAME=
#COMP="none"

(Note: If EXTERNAL is set then NODTS and KEEPDTS should not be used. AUDIOMODE is the -o-flag value passed to aften. PRIORITY is the niceness value. DTSNAME is custom title for new AC3 track.)

Examples

Keep only the new AC3 track, discarding the original DTS

mkvdts2ac3.sh -n /dir/Some.Random.Movie.mkv

Specify an alternate directory to use for the temporary files. This can be useful when the partition your /tmp directory on is tiny.

mkvdts2ac3.sh -w /mnt/bigHDD /dir/Some.Random.Movie.mkv

Convert a different DTS track rather than the first one sequentially in the file. This will require you to check the output of a command like mkvmerge -i Some.Random.Movie.mkv which will give you the track ids of each file.

mkvdts2ac3.sh -t 4 /dir/Some.Random.Movie.mkv

If you want to retain the DTS track in an alternate location you can instruct the script not to delete it after the conversion.

mkvdts2ac3.sh -k /dir/Some.Random.Movie.mkv

If you want to keep the original file untouched (such as if you are still seeding it in a torrent) and your player supports external audio tracks you can choose to leave the converted AC3 track out of the file.

mkvdts2ac3.sh -e /dir/Some.Random.Movie.mkv

All of these examples only showcase the use of a single argument but they can be combined to achieve the desired result.

mkvdts2ac3.sh -d -t 3 -w /mnt/media/tmp/ /dir/Some.Random.Movie.mkv

If you're unsure of what any command will do run it with the --test argument to display a list of command execute. You can also use the --debug argument which will print out the commands and wait for the user to press the return key before running each.

$ ./mkvdts2ac3.sh --test -d -t 3 -w /mnt/media/tmp /dir/Some.Random.Movie.mkv

mkvdts2ac3-1.5.3 - by Jake Wharton <[email protected]> and
                      Chris Hoekstra <[email protected]>

MKV FILE: Some.Random.Move.mkv
DTS FILE: /mnt/media/tmp/Some.Random.Movie.dts
AC3 FILE: /mnt/media/tmp/Some.Random.Movie.ac3
TIMECODE: /mnt/media/tmp/Some.Random.Movie.tc
NEW FILE: /mnt/media/tmp/Some.Random.Movie.new.mkv
WORKING DIRECTORY: /mnt/media/tmp
Checking to see if DTS track specified via arguments is valid.
> mkvmerge -i "/dir/Some.Random.Movie.mkv" | grep "Track ID 3: audio (A_DTS)"

Extract track information for selected DTS track.
> mkvinfo "/dir/Some.Random.Movie.mkv" | grep -A 25 "Track number: 3"

Extract language from track info.
> echo "INFO" | grep -m 1 "Language" | cut -d " " -f 5

Extract name for selected DTS track. Change DTS to AC3 and update bitrate if present.
> echo "INFO" | grep -m 1 "Name" | cut -d " " -f 5- | sed "s/DTS/AC3/" | awk '{gsub(/[0-9]+(\.[0-9]+)?(M|K)bps/,"448Kbps")}1'

Extract timecode information for the audio track.
> mkvextract timecodes_v2 "/dir/Some.Random.Movie.mkv" 3:"/mnt/media/tmp/Some.Random.Movie.tc"
> sed -n "2p" "/mnt/media/tmp/Some.Random.Movie.tc"
> rm -f "/mnt/media/tmp/Some.Random.Movie.tc"

Extract DTS file from MKV.
> mkvextract tracks "Some.Random.Movie.mkv" 3:"/mnt/media/tmp/Some.Random.Movie.dts"
Converting DTS to AC3.
> ffmpeg -i "/mnt/media/tmp/Some.Random.Movie.dts" -acodec ac3 -ac 6 -ab 448k "/mnt/media/tmp/Some.Random.Movie.ac3"

Running main remux.
> nice -n 0 mkvmerge -q -o "/mnt/media/tmp/Some.Random.Movie.new.mkv" --compression 1:none "/dir/Some.Random.Movie.mkv" --default-track 0 --language     0:DTSLANG --track-name 0:"DTSNAME" --sync 0:DELAY --compression 0:none "/mnt/media/tmp/Some.Random.Movie.ac3"
Removing temporary AC3 file.
> rm -f "/mnt/media/tmp/Some.Random.Movie.ac3"

Copying new file over the old one.
> cp "/mnt/media/tmp/Some.Random.Movie.new.mkv" "/dir/Some.Random.Movie.mkv"

Remove working file.
> rm -f "/mnt/media/tmp/Some.Random.Movie.new.mkv"

Based on code originally Developed By

Git repository located at github.com/JakeWharton/mkvdts2ac3

License

Copyright 2018 Trevin Chow

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.

About

Bash script to convert DTS audio to AC3 within a matroska file.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Shell 90.6%
  • Python 7.1%
  • Makefile 2.3%