Friday, November 18, 2011

Convert MKV (h264, AC3) to MP4 (h264, AAC)


We have already inspected the file, so let's extract the streams:

mkvextract tracks video.mkv 1:video.h264 2:audio.ac3

Then, we need to transcode the audio stream to AAC (let's use high-quality VBR):

ffmpeg -i audio.ac3 -acodec libfaac -aq 255 -ar 44100 -ac 2 -async 1 -vsync 1 audio.aac

Now, we'll use MP4Box to remux the initial video stream and the new audio stream into an MP4 container:

MP4Box -new -add audio.aac -add video.h264 -fps 23.976 video.mp4
AAC import - sample rate 44100 - MPEG-4 audio - 2 channels
AVC-H264 import - frame size 1280 x 720 at 23.976 FPS
Import results: 60146 samples - Slices: 1027 I 36894 P 22225 B - 1 SEI - 968 IDR
Stream uses B-slice references - max frame delay 2
Saving video-ps3.mp4: 0.500 secs Interleaving

That's it :)

Fix FFMpeg to Convert Audio

When trying to use FFMPEG to convert AC3 audio in a video file to AAC, I kept receiving the error message "Unknown encoder 'libfaac'." Finally I found the solution to this on FFMPEG that comes with Ubuntu has no proprietary non-free codecs included. Instead of recompiling FFMPEG, simply install Medibuntu repositories, the install additional codec libraries. From the Medibuntu site:

The following bash command adds Medibuntu's repository to Ubuntu. It also adds Medibuntu's GPG key to your keyring, which is needed to authenticate the Medibuntu packages.

This command should be run in the Terminal (Applications → Accessories → Terminal):

sudo -E wget --output-document=/etc/apt/sources.list.d/medibuntu.list$(lsb_release -cs).list && sudo apt-get --quiet update && sudo apt-get --yes --quiet --allow-unauthenticated install medibuntu-keyring && sudo apt-get --quiet update

Medibuntu's repository is deactivated by upgrading to a newer Ubuntu release, so you should run this command again after the release upgrade.

You may also wish to add the following packages. The first will cause many apps from the Medibuntu repository to appear in Ubuntu Software Center (Ubuntu 9.10+) or Add/Remove Applications (versions prior to 9.10). The second will allow users to generate crash reports against Medibuntu packages and submit them to the Medibuntu bugtracker.

sudo apt-get install app-install-data-medibuntu apport-hooks-medibuntu