AnimeSuki Forum (http://forums.animesuki.com/index.php)
-   Tech Support (http://forums.animesuki.com/forumdisplay.php?f=24)
-   -   MKV to MP4 Guide (http://forums.animesuki.com/showthread.php?t=47693)

 martino 2007-05-17 10:15

MKV to MP4 Guide

Recently there has been an upsurge of people asking how to convert MKV to MP4 so that they can play it on their XBOX360 or PS3 (You have to note that the PS3 does not support the High profile yet, in which most fansubs are encoded, so it's very likely that those files will not play unless they are re-encoded to the supported profile). So I have decided to put something like this us since it will be easier than having dozens of threads which try to achieve the same purpose. Also, scroll down to the very bottom to read a bit more...

Before you even start...

Spoiler for boring stuff, read only if you actually want to know what you are getting yourself into:

What files do I need?
• Firstly you need to grab the script itself from here. Save it as mkv2mp4.bat and it's version 1.03 at the moment. The next thing you need to do is unpack the bat script into some folder. Then create a new folder called "bin" inside it. Also make sure to have CCCP installed and fully working.
• Now, the biggest requirement for this to work is to have mkvtoolnix, which is a tool that allows tracks to be removed from MKV files and put back in. You can download it from here. Install it as usual. Then navigate to the directory where you installed it and copy the following files into the bin directory that you created earlier on.
Spoiler for list of files that need to be copied:
• You need to download MP4Box which just simply muxes streams into the MP4 container, which is supported by both the PS3 and XBOX360. You can get it from here and copy the executable (and any *.dll file, if present) into the bin directory.
• You will also need a little package that will do some pretty smart "magic". It's this what makes this whole remuxing process possible without having to add too much user required input. Get it from here and put all of its contents into the bin directory (if it requests a password for the package then use doom9.org).
• You need to download and install AviSynth 2.5 for video frameserving since you might be re-encoding the video. You can get it from here.
• You need to download the nero AAC encoder from here and place neroAacEnc.exe into the bin directory.
• Same with BePipe, placing bepipe.exe into the bin directory.
• Furthermore you will need to download x264, which is the H.264/AVC1 encoder application that might be used. It can be found here. The topmost *.7z file is what you'll be usually looking at getting and put x264.exe into the bin directory.
• Lastly, you need a little tool that does some hex changing, I won't go into details, but it is needed because both Sony and Microsoft fail pretty hard. Get it from here and again place the executable into the bin directory.

All these tools should suffice for all the tasks in this script.

What do I do next then?

The batch script executes different commands based on what media file you have, since not all of them can be treated the same. In this section I will explain how to identify them.

The simplest approach would be to open the media file in Media Player Classic, right-click on the video and move to the Details tab. There you will see a basic description of the file.

Type 1
Video: MPEG4 Video (H264)
Audio: AAC
- just remuxing needed

Type 2
Video: MPEG4 Video (H264)
Audio: MPEG Audio Layer 3 (aka MP3), Vorbis, AC3 or any others
- audio transcoding to AAC needed, rest will be just remuxed

Type 3a
Video: XVID, DIVX or any others
Audio: AAC
- video transcoding needed, audio will be remuxed

Type 3b
Video: MPEG4 Video (H264), XVID, DIVX or any others
Audio: AAC
Subtitle: Advanced SubStation Alpha or any others
- video transcoding needed while hardsubbing subtitles, audio will be remuxed

Type 4a
Video: XVID, DIVX or any others
Audio: MPEG Audio Layer 3 (aka MP3), Vorbis, AC3 or any others
- video and audio transcoding needed

Type 4b
Video: MPEG4 Video (H264), XVID, DIVX or any others
Audio: MPEG Audio Layer 3 (aka MP3), Vorbis, AC3 or any others
Subtitle: Advanced SubStation Alpha or any others
- video and audio transcoding needed while hardsubbing subtitles

About the pixel aspect ratio

Since version 1.02 I had to introduce an option to specify the pixel aspect ratio in order to retain the correct look of the video. Unfortunately this currently cannot be automated and has to be set by the user.

First thing to do is to open once again the basic properties of a file in Media Player Classic as described in the previous section and look closely at the video part. If you see something like "Video: MPEG4 Video (H264) 704x480 (853:480) 23.98fps [Video]" or "Video: MPEG4 Video (H264) 720x480 (853:480) 23.98fps [Video]" or even "Video: MPEG4 Video (H264) 704x400 (711:400) 23.98fps [Video]" it means that there was a custom ratio set by the person who made the video file (for those interested the first resolution is the one at which the file was encoded and the second one, in brackets, is the one that it gets stretched to on playback). You need to make sure to do the same. If there is just one resolution listed it means that nothing was set and you should just choose "1:1" when the option appears.

I'll just simply list the most common possibilities and their respective pixel aspect ratios for ease of use.

704x480 (853:480) or 704x480 (16:9) - 40:33
720x480 (853:480) or 720x480 (16:9) - 32:27
704x400 (711:400) or 704x400 (16:9) - 100:99
704x480 (640:480) or 704x480 (4:3) - 10:11
720x480 (640:480) or 720x480 (4:3) - 8:9

Getting it all down

Before starting the script, make sure to move the file that you would like to convert into the directory where you unpacked the mkv2mp4.bat script and where the bin subdirectory is located. Now run the script by double-clicking on it.

Firstly it will ask for the type of file that you would like to convert. You should have already determined that from the previous section. The possible choices are from 1 to 4.

A word of warning before I proceed to the next step. Please make sure that the files that you want to convert don't have any spaces in them. Best thing is to just replace them with underscores. Same goes for any other files that you may be using later on.

Depending on your choice it will ask for other inputs which I'll describe next;

Type 1
1) This one will ask next for the input filename of the file that you would like to convert without the extension.
2) Then it will ask for the output file, without the extension again, which is just the name of the file that it will create using the MP4 container. After that it should just work through all the lines in the script, delete all the intermediate files at the end and place the output into the directory where the script is found. And you're done.

Type 2
1) Same as step 1 and 2 from Type 1

Type 3
1) Firstly you need to change one setting in CCCP. Go into its Settings application, click Next and check "Autoload VSFilter" and Apply.
2) You need to create an AviSynth script. Place the MKV file into the directory where you unpacked the mkv2mp4.bat script, and create the script there as well. Here is a basic example of an AviSynth script;
Code:

DirectShowSource("C:\path\to\file.mkv")
3) Same as step 1 in Type 1
4) It will ask for the AviSynth script filename, without the extension.
5) Same as step 2 in Type 1
6) Now it will ask whether the video is standard or high definition. This just tells the script to use different encoding settings, since for once you don't really want to kill the encoding speed or playback too much, right? ;)
7) Finally it will ask for the bitrate. Usually go with some value given in the recommendations range. It should give decent results.
8) Now just wait for it to do its job and spit out an MP4 video file.

Type 4
1) Same as steps 1-8 in Type 3

---

Spoiler for the old "manual" way (no longer updated):

Constructive feedback is of course welcome. (Hopefully I haven't missed anything important or made some silly mistake, since you could consider this as an intermediate version.)

 GHDpro 2007-05-17 13:07

Alternative Guide
This version is a bit easier. However, as the video will be re-encoded (even if the subtitles are already encoded into the video), the result will most likely be lower quality.

1. Required Software
- MKVExtractGUI
- mkvtoolnix
- VSFilter (part of CCCP, so you should have it already)
- PS3 Video 9 (similar tools might also work for other systems such as PSP or Wii)
- AviSynth (optional - also included in PS3 Video 9!)

The first three files are only necessary if the file you are trying to convert MKV files with soft subtitles.

2. Extracting the subtitles
If the file has soft subtitles, you need to extract the subtitles first using MKVExtractGUI. Make sure you put MKVExtractGUI in the same folder as mkvtoolnix (C:\Program Files\mkvtoolnix by default) btw.

The subtitle file (shown as S_TEXT/ASS for styled subtitles or S_TEXT/UTF8 for plain subtitles) will be put in the same folder as the video file.

Note: some MKV files also include fonts. You may need to extract and install the fonts (using the Control Panel) in order for the styled subtitles to look just the way they're supposed to look.

3. Write the AVS (AviSynth) file
This is the hard part: writing the AVS file. Actually the hard part is getting the folders and filenames just right (this could be tricky if the file has a complicated filename and is in a folder a few levels deep). Btw, an AVS file is simply a text file with a .AVS file extension.

Write a AVS file that looks like this:
Quote:
 LoadPlugin("C:\Program Files\Combined Community Codec Pack\Filters\vsfilter.dll") DirectShowSource("C:\Anime\Hayate no Gotoku\[SS-Eclipse] Hayate no Gotoku! - 07 (1280x720 h264) [367A4C60].mkv") TextSub("C:\Anime\Hayate no Gotoku\[SS-Eclipse] Hayate no Gotoku! - 07 (1280x720 h264) [367A4C60]_Track3.ass")
Of course make sure all the paths and filenames are changed to whatever file you want to convert. TIP: right click and select "Rename" on the file you want the filename of, and then press CTRL-C to copy the filename to the clipboard. Then just press CTRL-V to paste the filename in Notepad.

If the MKV file you want to convert does not contain subtitles (in case they're already encoded in the video) leave out line 1 ("LoadPlugin") and line 3 ("TextSub") and keep just line 2 ("DirectShowSource").

Before converting, verify the AVS file works by opening it in MPC (Media Player Classic). If you get a AviSynth error message, you'll need to fix the AVS file.

4. Convert with PS3 Video 9
I'm not going to explain the usage of PS3 Video 9 here, as it's pretty simple. Also you'll need to test for yourself which conversion profile works best for you. But if you have followed the steps above, you should be able to convert the MKV file just like you would any AVI file.

If you need a hint: select "Convert", then "Current conversion" tab. Then just click the "Convert video" button and select the AVS file you just created.

Optional hint
If you go to Start -> Programs -> Combined Community Codec Pack -> Media Splitter Settings and then under the Options tab enable "Autoload VSFilter" (in Compatibility section), you might be able to skip the whole MKVExtractGUI phase! You probably still need to write the AVS file, but you can get away with just the "DirectShowSource" line (and leave out the others).

Disclaimer
I have neither a PS3 nor a Xbox 360, so I can't test this. But I did succesfully convert various files to PSP format (using PSP Video 9) this way.

 Nicholi 2007-05-17 19:39

Loading VSFilter within DirectShowSource (if you use Autoload VSFilter) might get flipped images and such depending, just be sure to preview before you hit encode.

Also specific note in terms of re-encoding while using the CCCP's ffdshow, you should turn on YV12 ouput in FFDShow Video Decoder Config under Outputs. Less colorspace conversions = more good for you. Next CCCP version will already have this on by default, but currently all past versions have used YUY2.

 Aegelward 2007-05-18 17:45

i've figured out a way to do this on the mac, it eats up a bit of disc space however. and it only works for files with one audio track.

doing converts straight from viddyup sometimes resulted in stuttering files so...

make sure you have perian 1.0 (beta) installed (http://perian.org/beta.html) and download viddy up (www.splasm.com) the unregistered version works fine, as we are only going to transfer it into a .mov (renaming won't work)

from there you can use isquint (www.isquint.org) or visualhub (£££) (http://www.techspansion.com/visualhub/) ... well, personally i just use the apple tv settings & h.264 - high or standard. some files i notice a noticable quality difference, some i don't. just depends on the original encode i guess.

- oh, directly converting from mkv using visualhub sometimes works, but this is a surefire way to make sure you get subtitles.

 Hinosaki 2007-08-10 14:25

Hmm, I have a slight problem. I successfully converted one file to .MP4 and it played fine. However when I was working on my second one I get this problem.

Spoiler for :

Not sure what to do here. I rechecked the steps and redid everything but the same problem occurs.

EDIT : Scratch that. I got it to work somehow. I guess I mistyped something. >_>.

 kujoe 2007-08-11 21:58

Ok, I'm obviously a newbie at this... so, I have question if you guys don't mind me asking.

Do any of the illustrated methods work for videos (.mkv) with multiple sound channels? I would like to convert something which has "AAC 48000Hz 6ch [Audio]."

EDIT: Never mind. Figured it out for myself. Thanks for the guides.

 Fleabitten8907 2007-12-01 14:41

Question/concern~ So like, I'm using the alternate method since it looked more nub-friendly, and for some reason I'm having problems. When I get to the conversion process, it for some reason won't add to my iTunes library, and when I try to open it manually, it will only play audio in Winamp. However, using the exact same method, but using the 30second test option, it works perfectly.... What could be causing this? x_x

 hobbes_fan 2007-12-01 21:10

I've put a link to this in my sig. I also request that this be stickied. It has become a once a week question.

 martino 2007-12-01 21:40

Quote:
 Originally Posted by Fleabitten8907 (Post 1279239) Question/concern~ So like, I'm using the alternate method since it looked more nub-friendly, and for some reason I'm having problems. When I get to the conversion process, it for some reason won't add to my iTunes library, and when I try to open it manually, it will only play audio in Winamp. However, using the exact same method, but using the 30second test option, it works perfectly.... What could be causing this? x_x
You shouldn't be really using Winamp to play the output file. It's meant to be for audio playback and its handling of video files just simply doesn't cut it. Not sure what iTunes has to do with this, maybe it's somehow interfaced with that xx Video 9 program. I've never run that program so I wouldn't know, sorry.

Also as an update I will attempt to write a script for ease of use, after my exams though. However I currently do have a batch script for converting any file that plays in DirectShow to XviD/MP3 in AVI. It's currently being tested... Just a heads up.

 zuka 2007-12-21 07:08

mp4mobile is probably the best choice if you are willing to pay for it
http://www.videomobileconverter.com/mp4Mobile/

 DuplicateGoose 2008-01-07 05:55

Metroska (.mkv etc) to common Windows video file types

I'm not sure I want to go into great detail explaining this but, I always managed to convert Metroska file containers to more common video file types like .wmv or MPEG2 using the Combined Community Codec Pack and the full version of TMPGEnc 4.0 Xpress. I'm new to the forum and joined because I found a great bunch of technical support threads through Google. I hope this has helped anybody who was hoping in not having to overheat their heads over the challenge and I'm sure you can pick up TMPGEnc Xpress through torrent searching. ;)

 hobbes_fan 2008-01-14 20:15

you realise that this product is a retail product. TMPENGC v4.0 is not a free product and any torrented version of it is pirated. It is sold for \$110USD

http://forums.animesuki.com/faq.php?...#faq_rules_1_1

 Quarkboy 2008-01-14 21:11

I just noticed this thread... and came to gloat a bit.

Ah, I remember the days back in 2005 when I was ridiculed for switching to h.264 in mp4 in my group (one of, if not the first group to do so)... And now to see people actually requesting their mkvs be converted into mp4s, for exactly the reason I predicted (better hardware playback compatability) makes my day.

I love being right.

 garra113 2008-01-21 23:38

I have a question about this. Whenever I encode mkv to mp4 files, it will leave me too extract the fansubs and merge them back to the mp4 file. Question #1: Is it possible for a way to merge the Typesetting along with the subtitles into the newly encoded mp4 file? Question #2: If it don't work, then is there like another way for it to see it with typesetting using the fansub group typesetting?

 Salt 2008-01-22 10:30

If you are just converting for your PSP then there's also XviD4PSP, which contrarily to it's name, handles just about any file format. It seems to work OK.

It appears to uses FFdshow, so it your mkv has subtitles, you might want to go to FFdshow video decoder options to turn on subtitles and embedded subtitles. The subtitles will end up hardcoded to the resulting MP4 file.

 garra113 2008-01-22 12:09

Yeah but the whole point of encoding it to mp4 is for the softsubs though. Anyway to do it the way I said up my 1st post?

 martino 2008-01-22 17:51

MP4 does not support softsubs, ok maybe it does, but no device supports the TTXT format. Plus, you're not gaining anything by converting a softsubbed MKV to a softsubbed MP4, in terms of niceness and compatibility. It's just... pointless.

Quote:
 Originally Posted by Salt (Post 1355687) It appears to uses FFdshow, so it your mkv has subtitles, you might want to go to FFdshow video decoder options to turn on subtitles and embedded subtitles. The subtitles will end up hardcoded to the resulting MP4 file.
Hopefully not. It should be using VSFilter's rendering, and not FFDShow's which should be used just as the audio/video decoder. That is if it is using DirectShow output for frameserving.

Quote:
 Originally Posted by garra113 (Post 1354997) I have a question about this. Whenever I encode mkv to mp4 files, it will leave me too extract the fansubs and merge them back to the mp4 file. Question #1: Is it possible for a way to merge the Typesetting along with the subtitles into the newly encoded mp4 file? Question #2: If it don't work, then is there like another way for it to see it with typesetting using the fansub group typesetting?
Merge? What do you mean by that... Is that like hadrsubbing it, or converting to TTXT?

 garra113 2008-01-22 18:42

I wish to see the same typesetting as the fansub groups already provided in the mp4 BUT the mkvmerge doesn't allow you to merge the typesettings and only the subtitles. In other words, I wish to merge the typesettings but I don't know how loll.

 Salt 2008-01-25 09:46

Quote:
 Originally Posted by martino (Post 1356342) Hopefully not. It should be using VSFilter's rendering, and not FFDShow's which should be used just as the audio/video decoder. That is if it is using DirectShow output for frameserving.
LOL. I have no idea what you are talking about. :p

I'm clueless as to what goes on behind the scenes (hence my using of a program with a relative simple GUI).

I had to fiddle around quite a bit to get subtitles to appear (hardcoded of course) in the resulting PSP compatible MP4 file. I tried adjusting DirectVobSub's setting, to no valid, then I read something about turning on the subtitles in FFDshow, and that did the trick.

 martino 2008-01-25 12:19

Quote:
 Originally Posted by garra113 (Post 1356418) I wish to see the same typesetting as the fansub groups already provided in the mp4 BUT the mkvmerge doesn't allow you to merge the typesettings and only the subtitles. In other words, I wish to merge the typesettings but I don't know how loll.
Typesetting is usually hadsubbed on the video, or softsubbed in the same track as the dialogue because you can't have two softsub tracks playing at the same time. Also, what are you trying to achieve? Softsub an MP4? For what point? It's totally pointless as I posted before...

All times are GMT -5. The time now is 03:19.