Wednesday, December 21, 2011

From 1080p MKV to 720p MP4

I started with a 13 GB MKV with 1080p h264 video and DTS 5.1 sound. This was far too overwhelming for my current media centre. So I decided to transform and transcode the file into an mp4 with 720p video and stereo AAC audio.

I did this in Ubuntu.

I couldn't simply use avidemux to transcode the video and audio (as I often have done), perhaps because this MKV was not standard enough to be compatible, perhaps because the MKV was so huge, I don't know exactly. So I took a more circuitous route.

First step, tear apart the MKV into its component parts (elementary streams). The odd file extensions are useful to make it clear that these are containerless streams, that really can't be played by (most) video players.

The H264 video first:
mkvextract tracks original.1080p.video.mkv 1:video.h264

Then the DTS audio:
mkvextract tracks original.1080p.video.mkv 2:audio.dts

Second step, put the h264 video into an MP4 container.

mp4creator -create=video.h264 -rate=23.976 -use64bits h264.video.mp4

I found that I needed the -use64bits parameter to get mp4creator to handle the huge file size. The -rate=23.976 was the frame rate of the video, found using VLC.

Third step, use avidemux to crop, resize and transcode the video. As with many "1080p" videos, the width was 1920 but the height was only 880, and a few rows of pixels at the bottom were duplicated. I cropped the sides and the bottom first to get 1554x874, approximately 16:9 aspect ratio (yes, I lost quite a bit on the sides, but I prefer a full screen experience). I got these dimensions by opening a calculator and entering 16 / 9 x 874. The 874 was my post-crop height. I figured out how much to crop from the sides by using (1920 - 1554) / 2, which gave 183, and you can't crop odd numbers, so I cropped 182 off of the left and 184 off of the right. Then I resized using MPlayer resize (to 1280 x 720) and Lanzcos3 resize method.

I encoded with x264, single pass, using a very high quality setting, (quantizer 20). I saved to "video.720.mp4". This produced a 5.3 GB file. The same encoding, set to quantizer 26, produced a more "smeary", more noticeably compressed video, but it still looked reasonably good and it was only 1.4 GB. So you can decide what you prefer.

Waiting for this bit is the longest piece of the puzzle. Whoo. Hours.

Fourth step, I used VLC to transcode the raw DTS to stereo AAC in an mp4 container. I used "Convert/Save" in the File menu, and set the profile to "Audio - AAC (MP4)". I made sure "Video" was unchecked in the "Video codec" tab and in the "Audio codec" tab, I set the Codec to "MP4 Audio (AAC)", the bitrate to 192 kb/s, channels to 2, and sample rate to 48000. I set the output file name to "audio.aac.mp4". I hit the Save and Start buttons and waited. This was short in comparison to the video transcode.

Fifth and final step, I put the video and audio back together using ffmpeg.

ffmpeg -i audio.aac.mp4 -i video.720.mp4 -acodec copy -vcodec copy final.video.mp4

The final video was 5.5 GB in size, quite large, but so beautiful and sharp! You can still see the film grain. This video not only played on my media PC, it also played very nicely on my HP TouchPad.

Labels: , , , , , , ,