{"id":101,"date":"2009-02-19T11:57:00","date_gmt":"2009-02-19T01:57:00","guid":{"rendered":"https:\/\/www.serenux.com\/?p=101"},"modified":"2021-01-14T12:04:05","modified_gmt":"2021-01-14T02:04:05","slug":"howto-encode-a-blu-ray-rip-into-a-smaller-format-without-losing-quality","status":"publish","type":"post","link":"https:\/\/www.serenux.com\/index.php\/2009\/02\/19\/howto-encode-a-blu-ray-rip-into-a-smaller-format-without-losing-quality\/","title":{"rendered":"HowTo: Encode a Blu-ray rip into a smaller format without losing quality"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The process of encoding a Blu-ray rip isn\u2019t quite the same as doing a DVD, however, so here\u2019s 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Pre-requisites:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A pre-decoded Blu-ray movie file (.m2ts file).<\/li><li>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\u2019t exactly want to discover you ran out of disk space and have to start over, do you?<\/li><li>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&nbsp;<em>one<\/em>&nbsp;movie using four threads. A dual-core will take longer.<\/li><li>Time to let the PC do its work, eg: overnight.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>You will need some extra software installed if you haven\u2019t already got it. Open a terminal and type in the following at the $ prompt:<br><br><code>$ sudo apt-get install mencoder mplayer gpac x264 mkvtoolnix<\/code><br><br>(Don\u2019t worry if you\u2019ve already got some of those apps installed, Ubuntu will skip over them if they already exist on your system)<br><\/li><li>Create a new text file somewhere using your favourite text editor, eg:<br><br><code>$ gedit ~\/encodevideo.sh<\/code><br><br>\u2026will create a new text file called \u201cencodevideo.sh\u201d in the root of your Home directory using the GEdit text editor.<br><\/li><li>Now copy and paste the following script into it:<br><br><code>#! \/bin\/bash<br># =====================================================================<br># Blu-ray encoding script by HyRax February 2009 http:\/\/www.serenux.com<br># =====================================================================<br># Make sure the user has specified what to work on.<br>if [ -z \"$1\" ]; then<br>  echo \"\\nBlu-ray movie encoding script\\n-----------------------------\"<br>  echo \"Written by HyRax February 2009\\nhttp:\/\/www.serenux.com\"<br>  echo \"\\nUsage: $0 &lt;m2ts file without extension>\"<br>  echo \"\\nExample: If your movie file is called TheDarkKnight.m2ts then\\nyour usage will be: $0 TheDarkKnight\\n\"<br>  exit<br>fi<br><br># The crf=21 option controls encoding quality, and indirectly the final<br># filesize. The higher the value, the more compression and thus smaller<br># file size. Reduce the value and file size will go up. A value of 21<br># should produce a file of approximately 4GB in size for a typical movie.<br><br># Encode the video using x264, ignore the audio for now.<br>mencoder $1.m2ts \\<br>-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 \\<br>-nosound \\<br>-of rawvideo \\<br>-o $1.x264<br><br># Dump the first original audio track (should be the English track) but<br># don't re-encode it. Ignore the video.<br>mplayer $1.m2ts -dumpaudio -dumpfile $1.ac3<br><br># Copy the raw x264 encoded video into an MP4 container so we can set<br># the correct framerate (generally 23.976 - adjust it if MPlayer or<br># MEncoder report something different)<br>MP4Box -add $1.x264 $1.mp4 -fps 23.976<br><br># Finally, merge everything into a single MKV file<br>mkvmerge -o $1.mkv $1.mp4 --track-name 0:Eng $1.ac3<br><br># Remove the hash in front of the next command to have the script delete # your working files when encoding is complete.<br># rm $1.m2ts $1.x264 $1.ac3<br><br># Tell the user we're done.<br>echo \"All done! Your final movie file is called $1.mkv - enjoy!\"<\/code><br><\/li><li>Save and exit your text editor.<br><\/li><li>Change directory to where you have your original .m2ts file, eg:<br><br><code>$ cd ~\/Videos\/BDRips\/MyMovie<\/code><br><\/li><li>Let\u2019s say your movie file is called\u00a0<em>TheIsland.m2ts<\/em>. To begin encoding it, you will use the following command:<br><br><code>$ sh ~\/encodevideo.sh TheIsland<\/code><br><br>(Notice that we don\u2019t specify the file extension \u2013 the script assumes .m2ts on the end already)<br><\/li><li>Hit enter and the encoding process will begin. The script does the following in order:<ol><li>Extract the video component only and encode it in a single pass to a raw x264 video file called\u00a0<em>TheIsland.x264<\/em>\u00a0(in this example).<br><\/li><li>When that has finished, go back and extract the first audio track only and save it to a file called\u00a0<em>TheIsland.ac3<\/em>\u00a0without any re-encoding.<br><\/li><li>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\u2019t do this step, then the video will play faster than the audio in the final product.<br><\/li><li>Finally we create a new Matroska container called\u00a0<em>TheIsland.mkv<\/em>\u00a0and in it we place the framerate-adjusted MPEG4 video track and the unmodified audio track, producing our final product..<br><\/li><\/ol><\/li><li>When the script has finished some 9-12 hours later, you will have a file called\u00a0<em>TheIsland.mkv<\/em>\u00a0ready 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\u2019d really have to look hard to spot them.<br><\/li><li>We\u2019ve 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.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Enjoy!&nbsp;<img decoding=\"async\" src=\"https:\/\/web.archive.org\/web\/20160315122427im_\/http:\/\/www.serenux.com\/wp-content\/themes\/grey-opaque\/images\/smilies\/icon_smile.gif\" alt=\"Smilie: :)\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>NOTES:<\/strong><\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>While not covered by this HowTo (I\u2019ll 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\u2019re doing is adding tracks to the existing Matroska container. see the man pages for the mkvmerge utility for more information.<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>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 \u2013 this does NOT mean you are missing data from your movie file \u2013 it\u2019s 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\u2019s. 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\u2019t wait for Jaunty, there are some backported VDPAU modified versions of mencoder, mplayer and even MythTV available\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/web.archive.org\/web\/20160315122427\/http:\/\/www.avenard.org\/media\/Home.html\" target=\"_blank\">here<\/a>, however you try them at your own risk. If you are an ATi video card user, you\u2019re out in the dark for the moment. Go buy yourself an NVidia card \u2013 they are much better supported under Ubuntu than ATi are.<br><\/li><li>If you are using only a dual-core processor, you should modify the\u00a0<em>threads=4<\/em>\u00a0section of the mencoder line in the script to read either\u00a0<em>threads=2<\/em>\u00a0or\u00a0<em>threads=3<\/em>. If you are using a single-core processor, change this to\u00a0<em>threads=1<\/em>\u00a0or\u00a0<em>threads=2<\/em>\u00a0depending on how your CPU performs (and while you\u2019re at it, seriously consider an upgrade!).<br><\/li><li>If you wish to modify the quality of the encoded video to make the resulting file larger or smaller, re-edit the\u00a0<em>encodevideo.sh<\/em>\u00a0script and change the\u00a0<em>crf=21<\/em>\u00a0value in the mencoder line to a different value. There is no definitive filesize that the resulting encode will have. This value simply adjusts the\u00a0<em>quality<\/em>\u00a0of 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\u00a0<em>very<\/em>\u00a0well. 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\u2019m 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\u2019m not a fan of doubling up my movie purchases on the shelf with a second disc containing the compressed version of the same movie!<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":1,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/posts\/101\/revisions\/102"}],"wp:attachment":[{"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}