HowTo: Encode a Blu-ray rip into a smaller format without losing quality

Those of you who archive or backup their Blu-ray movie media to hard-drive will already be aware that the average movie comes out at a good 25GB. Some of the bigger titles top out at around 40GB or more. This eats up an awful lot of disk space.

Blu-ray titles are already compressed down using the MPEG2 codec, and quality pundits will abhor the idea of re-compressing the title again for fear of losing image and audio quality. Certainly if you go down the Xvid route, you will definitely lose image quality, but as per my previous DVD HowTo, you can do excellent rips with virtually indistinguishable quality to the original using the x264 codec, and have a significantly smaller footprint to go with it.

The process of encoding a Blu-ray rip isn’t quite the same as doing a DVD, however, so here’s a quick guide on how to take your decrypted .m2ts file and finish up with a much smaller, but 99% perfect copy in a Matroska .mkv file.

Pre-requisites:

  • A pre-decoded Blu-ray movie file (.m2ts file).
  • Approximately the same amount of free disk space as the size of the movie file. Eg: If you have a 25GB movie file, then you should have another 25GB free space to work with. You can have less, since the final resulting file will be much smaller than the original movie anyway, but since this process can take a number of hours to complete, you don’t exactly want to discover you ran out of disk space and have to start over, do you?
  • A nice powerful CPU. I use a Intel quad-core Q9450 CPU at 2.66GHz. It takes my machine roughly 9-12 hours to process just one movie using four threads. A dual-core will take longer.
  • Time to let the PC do its work, eg: overnight.

This guide was written using Ubuntu 8.10 Intrepid Ibex 64-bit, but will work quite happily in 32-bit and should also work with most previous versions of Ubuntu.

  1. You will need some extra software installed if you haven’t already got it. Open a terminal and type in the following at the $ prompt:

    $ sudo apt-get install mencoder mplayer gpac x264 mkvtoolnix

    (Don’t worry if you’ve already got some of those apps installed, Ubuntu will skip over them if they already exist on your system)
  2. Create a new text file somewhere using your favourite text editor, eg:

    $ gedit ~/encodevideo.sh

    …will create a new text file called “encodevideo.sh” in the root of your Home directory using the GEdit text editor.
  3. Now copy and paste the following script into it:

    #! /bin/bash
    # =====================================================================
    # Blu-ray encoding script by HyRax February 2009 http://www.serenux.com
    # =====================================================================
    # Make sure the user has specified what to work on.
    if [ -z "$1" ]; then
    echo "\nBlu-ray movie encoding script\n-----------------------------"
    echo "Written by HyRax February 2009\nhttp://www.serenux.com"
    echo "\nUsage: $0 <m2ts file without extension>"
    echo "\nExample: If your movie file is called TheDarkKnight.m2ts then\nyour usage will be: $0 TheDarkKnight\n"
    exit
    fi

    # The crf=21 option controls encoding quality, and indirectly the final
    # filesize. The higher the value, the more compression and thus smaller
    # file size. Reduce the value and file size will go up. A value of 21
    # should produce a file of approximately 4GB in size for a typical movie.

    # Encode the video using x264, ignore the audio for now.
    mencoder $1.m2ts \
    -ovc x264 -x264encopts crf=21:frameref=3:bframes=3:b_pyramid=normal:direct_pred=auto:weight_b:partitions=all:8x8dct:me=umh:mixed_refs:trellis=1:nopsnr:nossim:subq=6:level_idc=41:threads=4 \
    -nosound \
    -of rawvideo \
    -o $1.x264

    # Dump the first original audio track (should be the English track) but
    # don't re-encode it. Ignore the video.
    mplayer $1.m2ts -dumpaudio -dumpfile $1.ac3

    # Copy the raw x264 encoded video into an MP4 container so we can set
    # the correct framerate (generally 23.976 - adjust it if MPlayer or
    # MEncoder report something different)
    MP4Box -add $1.x264 $1.mp4 -fps 23.976

    # Finally, merge everything into a single MKV file
    mkvmerge -o $1.mkv $1.mp4 --track-name 0:Eng $1.ac3

    # Remove the hash in front of the next command to have the script delete # your working files when encoding is complete.
    # rm $1.m2ts $1.x264 $1.ac3

    # Tell the user we're done.
    echo "All done! Your final movie file is called $1.mkv - enjoy!"

  4. Save and exit your text editor.
  5. Change directory to where you have your original .m2ts file, eg:

    $ cd ~/Videos/BDRips/MyMovie
  6. Let’s say your movie file is called TheIsland.m2ts. To begin encoding it, you will use the following command:

    $ sh ~/encodevideo.sh TheIsland

    (Notice that we don’t specify the file extension – the script assumes .m2ts on the end already)
  7. Hit enter and the encoding process will begin. The script does the following in order:
    1. Extract the video component only and encode it in a single pass to a raw x264 video file called TheIsland.x264 (in this example).
    2. When that has finished, go back and extract the first audio track only and save it to a file called TheIsland.ac3 without any re-encoding.
    3. When that has finished, take the raw x264 video data and put it into an MPEG4 container so we can fix the framerate at 23.976 frames per second, typical of most Blu-ray movies. If you don’t do this step, then the video will play faster than the audio in the final product.
    4. Finally we create a new Matroska container called TheIsland.mkv and in it we place the framerate-adjusted MPEG4 video track and the unmodified audio track, producing our final product..
  8. When the script has finished some 9-12 hours later, you will have a file called TheIsland.mkv ready for playback in Totem, MPlayer, VLC, or whatever your favourite player is. You will notice that the filesize is significantly smaller than the original but when you play it back, the image quality will look pretty much 100% identical to the original. About the only compression artefacts you may notice is around the edges of some text titles such as opening credits, but you’d really have to look hard to spot them.
  9. We’ve finished with the working files now, so you can delete the .m2ts, .x264 and .ac3 files now to reclaim the disk space and enjoy your new .mkv file.

Enjoy! Smilie: :)

NOTES:

  • While not covered by this HowTo (I’ll add to this later), you can optionally extract other audio tracks from the .m2ts file such as other languages or the commentary track, and add them to the .mkv file at any time. You can also download subtitles and add them to the .mkv file at a later date also, all without needing to re-encode the entire video again because all you’re doing is adding tracks to the existing Matroska container. see the man pages for the mkvmerge utility for more information.
  • A high-definition x264-encoded movie takes a fairly reasonable amount of CPU power to decode. Since Ubuntu Intrepid and previous versions do not make any direct use of on-board video decoding hardware such as that found on NVidia and ATi based video cards, you may find certain busy scenes in your movie may stutter or even skip altogether during playback – this does NOT mean you are missing data from your movie file – it’s just that your PC is having trouble decoding AND displaying the movie all at once because the CPU is doing everything, especially those with slower CPU’s. Indeed you will notice that your CPU usage will probably be quite high in the 80-90% region. Ubuntu Jaunty will bring forth with it VDPAU support for NVidia-based video cards (restricted NVidia driver v180 and above). Using a patched version of MPlayer with VDPAU support, all the decoding work is passed completely to the video card, freeing up your CPU considerably to concentrate on other tasks, and dropping utilisation rates down to about 3%. This makes busy movies that much more watchable. If you can’t wait for Jaunty, there are some backported VDPAU modified versions of mencoder, mplayer and even MythTV available here, however you try them at your own risk. If you are an ATi video card user, you’re out in the dark for the moment. Go buy yourself an NVidia card – they are much better supported under Ubuntu than ATi are.
  • If you are using only a dual-core processor, you should modify the threads=4 section of the mencoder line in the script to read either threads=2 or threads=3. If you are using a single-core processor, change this to threads=1 or threads=2 depending on how your CPU performs (and while you’re at it, seriously consider an upgrade!).
  • If you wish to modify the quality of the encoded video to make the resulting file larger or smaller, re-edit the encodevideo.sh script and change the crf=21 value in the mencoder line to a different value. There is no definitive filesize that the resulting encode will have. This value simply adjusts the quality of the encode. With some trial and error, I have found that the average 1080p movie encode ends up roughly 8GB in size when using a value of 21 and provides excellent image quality. If you make the value larger, this will apply greater compression and will reduce the final file size at the expense of losing some image quality. If you reduce the value, then your final file size will increase, however your image quality will also go up. As a guide, animated movies such as those made by Pixar and Dreamworks and visually dark movies such as Underworld, compress very well. In one instance, the final product was only 3.5GB in size. Visually complex movies such as Transformers blew out to 12GB in size using the same encoding script, so you can see that there is no exact science to this. There are mencoder options to specifically set bitrate and target filesize, but I chose to ignore those options for this HowTo as I’m a bit of a quality freak, not a size freak. I store all my movies on a MythTV server at home and backup to an external drive as I’m not a fan of doubling up my movie purchases on the shelf with a second disc containing the compressed version of the same movie!