My encoding script for linux

MKV playback, recompression, remuxing, codec packs, players, howtos, etc.
Post Reply
simbakeeper
Posts: 12
Joined: Fri Oct 03, 2014 3:28 pm

My encoding script for linux

Post by simbakeeper »

Hello!

I like MakeMKV very much and I wanted to share my encoding script for linux. Mainly so that others might give me some suggestions on how to improve it and also so others can use it to get started on their scripts. I know there is a linux encoding script already on the forum, but I wanted to use a slightly different approach.

Before you can run the script, here's a couple things you need:

Download Nero AAC Encoder from here:
http://www.videohelp.com/tools/Nero-AAC-Codec
(The site says it's for Windows but there is a linux binary too.)

Install some needed packages from the command line (Debian based distros):
sudo apt-get install ffmpeg mkvtoolnix normalize-audio
(Newer ffmpeg packages can be obtained by following the instructions on their webpage.)

The script.

Code: Select all

#!/bin/bash

file="$1"

# Check if file name is empty
if [ "$file" == "" ]
then
    echo "Input file was not defined."
    exit
fi

# Check if file exists
if [ ! -f "$file" ]
then
    echo "File does not exists: $file"
    exit
fi


#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Variables
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

chaptersfile="${file%.*}.txt"

dtsfile="${file%.*}.dts"
wavfile="${file%.*}.wav"
m4afile="${file%.*}.m4a"

tsfile="${file%.*}.ts"
h264file="${file%.*}.h264"

muxedfile="${file%.*}-muxed.mkv"


#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Chapters
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

# List chapters from file and output into a file
echo "Exporting chapters from file: $file..."
mkvextract chapters "$file" -s >"$chaptersfile"
echo


#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Audio
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

# Demux DTS track from file
# Use mkvmerge --audio-tracks <track_id> to select specific tracks.
# Use mkvmerge --identify <file> to list all tracks and track IDs.
echo "Demuxing audio track from file: $file..."
mkvmerge -o "$dtsfile" --no-attachments --no-buttons \
    --no-chapters --no-subtitles --no-video "$file"
echo

# Decode and downmix .dts file and output as .wav file
echo "Decoding and downmixing file: $dtsfile..."
ffmpeg -i "$dtsfile" -ac 2 -acodec pcm_s16le -ar 48000 -f wav -vn -sn "$wavfile"
echo

# Normalize .wav file
echo "Normalizing audio file: $wavfile..."
normalize-audio "$wavfile"
echo

# Encode .wav file into .mp4 file
echo "Encoding WAV to AAC: $m4afile..."
./neroAacEnc -lc -q 0.4 -if "$wavfile" -of "$m4afile"

# Remove used files
rm -f "$dtsfile"
rm -f "$wavfile"


#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Video
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

# Demux video into .ts file from .mkv file
echo "Demuxing video track from file: $file..."
mkvmerge -o "$tsfile" --no-attachments --no-audio --no-buttons --no-chapters \
    --no-subtitles "$file"
echo

# Encode video using ffmpeg
# Use -r 25 for PAL 25.000 fps output (keyint=250:min-keyint=25)
# Use -r 24000/1001 for NTSC 23.976 fps output (keyint=240:min-keyint=24)
echo "Encoding video track to h264 file: $h264file..."
ffmpeg -i "$tsfile" -c:v libx264 \
    -preset slow -profile:v high -level 3.1 -crf 19 \
    -aspect 16:9 -r 24000/1001 \
    -x264opts "colormatrix=bt709:keyint=240:min-keyint=24" \
    -sws_flags lanczos -vf "scale=720:404,setsar=sar=1/1" \
    -an -sn "$h264file"
echo

# Remove used files
rm -f "$tsfile"


#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Mux
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

# Mux audio, chapters and video into an mkv file
echo "Muxing chapters, audio and video to file: $muxedfile"
mkvmerge --default-language eng -o "$muxedfile" \
    --language 0:eng --compression 0:none -d 0 --no-chapters -A -S "$h264file" \
    --language 0:eng --compression 0:none -a 0 --no-chapters -D -S "$m4afile" \
    --chapter-language eng --chapters "$chaptersfile"
echo

# Remove used files
rm -f "$chaptersfile"
rm -f "$m4afile"
rm -f "$h264file"
You need to give permissions for the script in order to run it:
chmod a+x encode.sh

Then you can run it like:
./encode.sh file-ripped-using-makemkv.mkv

...Assuming you saved it as "encode.sh" =)

Edit:
I updated the script to use only ffmpeg.
Last edited by simbakeeper on Sun Oct 19, 2014 1:24 pm, edited 1 time in total.
mike admin
Posts: 4065
Joined: Wed Nov 26, 2008 2:26 am
Contact:

Re: My encoding script for linux

Post by mike admin »

Any specific reasons to use Nero AAC encoding instead of libfdk-AAC (that can be used directly from MakeMKV)?
simbakeeper
Posts: 12
Joined: Fri Oct 03, 2014 3:28 pm

Re: My encoding script for linux

Post by simbakeeper »

Well... No. I looked up some listening tests on the internet and Apple QuickTime was always the best encoder. The second was Nero AAC Encoder. Apple QuickTime encoder had some licensing issues, but Nero was free for personal use, so I went with that. Also I heard much praise for Nero when I asked around in IRC.
Post Reply