written by cail • posted in How-To • 8,466 views 2 comments

Of course, I can use ffmpeg to do the job. But, it is transcoding, which decodes the original file, then encodes it into the form I want - very slow and "stupid".

29ckgvp.jpeg

The file I have already encoded in x264. What I really need is to extract the audio, video, subtitle (if any) from the original MKV file and then mix them into a new MP4 file. It is sad that both iPhone OS and Android can only play H264 encoded MP4 files, natively.

Here is how I do it in windows, in batch! Since all the tools I used are Open Sourced, it should be easy to adapt the procedure into other operation systems.

  1. Download mine all-in-1 package mkv2mp4.zip. Extract it, you will see a folder named mkv2mp4 and a windows batch file called convert(aac).bat, with the following content
    @ ECHO OFF
    REM convert mkv file into mp4 (option, check file info with mkv2mp4\mkvmerge -i)
    IF EXIST *.mkv FOR %%A IN (*.mkv) DO CALL :mkvConvert "%%A"
    :mkvConvert
    mkv2mp4\mkvextract.exe tracks %* 1:output.h264 2:output.aac 3:output.sub
    mkv2mp4\MP4Box.exe -add output.h264 -add output.aac -add output.sub -new %*.mp4
    DEL output.h264
    DEL output.aac
    DEL output.sub
    ECHO.
    GOTO :EOF
  2. Move the batch file together with the folder into the directory where you keep your MKV files for conversion
  3. Before running the batch file, please check how your MKV files were encoded by running the following command (win+R to trigger the command window)
    mkv2mp4\mkvmerge -i YOUR_FILE_NAME.MKV
  4. Based on the information from the step above, adjust convert(aac).bat file.

    For example:

    mkv2mp4\mkvextract.exe tracks %* 1:output.h264 2:output.aac 3:output.sub

    The command above is for MKV files having track 1 encoded with x264/h264 encoded video, track 2 encoded with AAC format audio, and track 3 encoded with subtitle.

    If based on mkvmerge's info, you find that your files are encoded with h264/x264 video and AC3 audio. You will need a batch file like convert(ac3).bat (there is no subtitle extraction and mixing in this batch file)

    @ ECHO OFF
    REM convert mkv file into mp4 (option, check file info with mkv2mp4\mkvmerge -i)
    IF EXIST *.mkv FOR %%A IN (*.mkv) DO CALL :mkvConvert "%%A"
    :mkvConvert
    mkv2mp4\mkvextract.exe tracks %* 1:output.h264 2:output.ac3
    mkv2mp4\ffmpeg.exe -i output.ac3 -acodec libfaac output.aac
    mkv2mp4\MP4Box.exe -add output.h264 -add output.aac -new %*.mp4
    DEL output.h264
    DEL output.aac
    DEL output.ac3
    ECHO.
    GOTO :EOF

    I did transcoding of AC3 audio into AAC audio with ffmpeg, because only AAC and VBR MP3 audio are allowed in MP4 format. If you google Internet, many places suggested to use Besweet to do the audio conversion. I just could not figure it out :-(

    There is one important value you might need to set, the frame rate (fps). The default frame rate for MP4Box is 25, while most the 720p/1080p high definition MKV files have fps 23.976. In order to mix the audio with video correctly, try the following command

    mkv2mp4\MP4Box.exe -add output.h264:fps=23.976 -add output.aac -add output.sub -new %*.mp4

    If you are not sure about the exact frame rate used in your files, try mkvinfo.exe tool in the mkv2mp4 folder. One example is

    D:\MKV_FILES_CONVERT>mkv2mp4\mkvinfo.exe xtm.mkv
    + EBML head
    |+ Doc type: matroska
    |+ Doc type version: 2
    |+ Doc type read version: 2
    + Segment, size 151286097
    |+ Seek head (subentries will be skipped)
    |+ EbmlVoid (size: 4027)
    |+ Segment information
    | + Timecode scale: 1000000
    | + Muxing application: libebml v0.8.0 + libmatroska v0.9.0
    | + Writing application: mkvmerge v3.3.0 ('Language') built on Mar 24 2010 14:59
    :24
    | + Duration: 2489.600s (00:41:29.600)
    | + Date: Sat May 08 04:15:11 2010 UTC
    | + Segment UID: 0xbf 0x04 0xc2 0x56 0x91 0xec 0x9d 0x18 0x9e 0x30 0xcf 0xd9 0x0
    4 0x43 0x74 0x3f
    |+ Segment tracks
    | + A track
    |  + Track number: 1
    |  + Track UID: 3842651804
    |  + Track type: video
    |  + Enabled: 1
    |  + Default flag: 1
    |  + Forced flag: 0
    |  + Lacing flag: 0
    |  + MinCache: 1
    |  + Timecode scale: 1
    |  + Max BlockAddition ID: 0
    |  + Codec ID: V_MPEG4/ISO/AVC
    |  + Codec decode all: 1
    |  + CodecPrivate, length 38
    |  + Default duration: 33.367ms (29.970 fps for a video track)
    |  + Language: und
    |  + Video track
    |   + Pixel width: 624
    |   + Pixel height: 368
    |   + Interlaced: 0
    |   + Display width: 624
    |   + Display height: 368
    | + A track
    |  + Track number: 2
    |  + Track UID: 2625443974
    |  + Track type: audio
    |  + Enabled: 1
    |  + Default flag: 1
    |  + Forced flag: 0
    |  + Lacing flag: 1
    |  + MinCache: 0
    |  + Timecode scale: 1
    |  + Max BlockAddition ID: 0
    |  + Codec ID: A_AAC
    |  + Codec decode all: 1
    |  + CodecPrivate, length 7
    |  + Default duration: 42.667ms (23.438 fps for a video track)
    |  + Language: und
    |  + Audio track
    |   + Sampling frequency: 24000
    |   + Channels: 1
    |   + Output sampling frequency: 48000
    | + A track
    |  + Track number: 3
    |  + Track UID: 443314862
    |  + Track type: subtitles
    |  + Enabled: 1
    |  + Default flag: 1
    |  + Forced flag: 0
    |  + Lacing flag: 0
    |  + MinCache: 0
    |  + Timecode scale: 1
    |  + Max BlockAddition ID: 0
    |  + Codec ID: S_TEXT/UTF8
    |  + Codec decode all: 1
    |  + Language: und
    |+ EbmlVoid (size: 1024)
    |+ Cluster

    From the information above, we know that xtm.mkv file has a video track at 29.970 fps and an audio track at 23.438 fps. The following command will mix the MP4 file correctly. Because the default audio track frame rate is 23.438, "-add output.aac:fps=23.438" equals to "-add output.aac"

    mkv2mp4\MP4Box.exe -add output.h264:fps=29.970 -add output.aac:fps=23.438 -add output.sub -new %*.mp4
  5. The conversion should be very quick, since there is no video transcoding involved. Enjoy!

Original software I downloaded are (some are not used, such as Besweet, aacenc and NeroAac):
* aacenc_v215.zip
* BeSweetv1.4.zip
* ffmpeg-18639.7z
* mkvtoolnix-unicode-3.3.0.7z
* MP4Box-0.4.5.zip
* NeroAACCodec-1.5.1.zip

If the original files you have are AVI files, have a look at the avidemux project.

Avidemux is a free video editor designed for simple cutting, filtering and encoding tasks. It supports many file types, including AVI, DVD compatible MPEG files, MP4 and ASF, using a variety of codecs. Tasks can be automated using projects, job queue and powerful scripting capabilities.
246AA117-7556-4BA7-9923-AE84F3B3AF7A.jpg
Avidemux is available for Linux, BSD, Mac OS X and Microsoft Windows under the GNU GPL license. The program was written from scratch by Mean, but code from other people and projects has been used as well. Patches, translations and even bug reports are always welcome.

How to put the MP4 files into iPhone or iPad? Try Dropbox desktop application and its iPhone/iPad app. It is slow, because the upload and download go through the Internet, no Lan-Sync yet. But, it works! Remember to mark the files as star/favorite in your iPhone/iPad app, which save them as local copies and you won't need WiFi connection to watch them while you are on the road.

Or, using some file transfer apps, such as Air Sharing (Air Sharing HD for iPad) and ReaddleDocs (ReaddleDocs for iPad).

For Android system, it is easy. Copy the files via the USB sync cable, drag-and-drop into any folder on the phone!

Previous:
Next:

Leave a Reply

QR code of this post

blog by cail