Convert MKV (Matroska) to BlackBerry using FFmpeg and Mencoder

MKV is a multimedia container file that can hold several multimedia streams such as video, audio, and subtitle. The other multimedia container that common to use are AVI and MP4.

BlackBerry ® is a device that can play multimedia file, such as Apple ® iPod, LG ® TV USB Player, SONY ® DVD USB Player, etc.

FFmpeg and Mencoder is free and open-source encoding and multiplexing tools. Here is the explanation that shows what their capabilities that usually (common) to use:

  1. Use Mencoder to generate video stream with hard-sub subtitle, because FFmpeg cannot render subtitle.
  2. Use FFmpeg to encode AAC audio codec for MPEG-4 audio stream, because Mencoder usually build without internal libfaac.

There are two other tools that we must have:

  1. MKVtoolnix for extracting MKV stream (usually to get subtitle) using mkvextract with support from mkvinfo.
  2. MP4box for multiplexing some MP4 streams into MP4 file.


Using FFmpeg to get stream info:

> ffmpeg -i "INPUT0.mkv"

If you want to get stream info before mixing stream from several files:

> ffmpeg  -i "INPUT0.mkv" -i "INPUT1.avi" -i "INPUT2.mp3"

Generate DivX+MP3 using FFmpeg:

> ffmpeg -i "INPUT0.mkv"
    -map 0:0
    -vcodec mpeg4
    -vtag DX50
    -b 4000k
    -aspect 16:9
    -map 0:2
    -acodec libmp3lame
    -ab 320k
    "OUTPUT_DivX_MP3.avi"

Generate XviD+AC3 using FFmpeg:

> ffmpeg -i "INPUT0.mkv"
    -map 0:0
    -vcodec libxvid
    -vtag XVID
    -b 4000k
    -aspect 16:9
    -map 0:2
    -acodec ac3
    -ab 320k
    "OUTPUT_XviD_AC3.avi"

Using Mencoder to get stream info:

> mencoder "INPUT.mkv" -o NUL

Generate DivX+MP3 using Mencoder:

> mencoder "INPUT.mkv"
    -vid 0
    -ovc lavc
    -lavcopts vcodec=mpeg4:mbd=2:trell:v4mv:vbitrate=1200:turbo:threads=2
    -ffourcc DX50
    -aid 1
    -oac mp3lame
    -lameopts cbr:mode=1:br=128
    -af resample=44100
    -srate 44100
    -sid 1
    -o "OUTPUT.avi"

Mencoder cannot render SSA subtitle inside MKV container (differ from Mplayer), so you need to extract it first. Use mkvinfo to get the stream-id for subtitle:

> mkvinfo "INPUT0.mkv"

If the subtitle stream for choosen language is in track 4, then:

> mkvextract tracks "INPUT0.mkv" 4:OUTPUT.ssa

Generate MP4-AVC H.264 video stream for BlackBerry 480×360 pixels with external subtitle files using Mencoder:

> mencoder "INPUT0.mkv"
    -nosound
    -vid 0
    -ovc x264
    -x264encopts subq=4:bframes=2:b_pyramid=normal:weight_b:profile=baseline
    -vf scale=480:-2,harddup,ass
    -sws 9
    -sub "OUTPUT.ssa"
    -ass
    -ass-font-scale 1.5
    -of rawvideo
    -o "OUTPUT.264"

Generate AAC MP4 audio stream using FFmpeg:

> ffmpeg -i "INPUT0.mkv"
    -map 0:2
    -acodec aac
    -strict experimental
    -ac 2
    -ar 44100
    -ab 64k
    "OUTPUT.aac"

Note: For the new ffmpeg version use -strict -2 instead -strict experimental.

The last one is to combine (mix) the video and audio stream into MP4 file using MP4box:

> mp4box -add "OUTPUT.264" -fps 23.976 "FINAL.mp4"
> mp4box -add "OUTPUT.aac" "FINAL.mp4"

Additional Info:

Using FFmpeg to encode H264 codecs is easy when using presets. Use -vcodec libx264 to select the H264 codec then use preset -vpre baseline (it will find libx264-baseline.ffpreset file). You should have .ffpreset files in the directory where FFMPEG_DATADIR environment variable points to. Please refer to FFmpeg manual for additional info. For latest Win32 build you can visit Zeranoe FFmpeg builds.

Convert FLV to MP4-AVC H.264 video and AAC audio using ipod640 preset (File: libx264-ipod640.ffpreset, Profile: [email protected] which supported by BlackBerry®) using FFmpeg:

> ffmpeg -i "INPUT.flv"
    -vcodec libx264 -vpre ipod640
    -vf "scale=480:-2, fps=fps=30"
    -acodec aac -strict -2 -ac 2 -ar 44100 -ab 128k
    -f mp4 "OUTPUT.mp4"

Note: If the input audio codec is also using AAC codec, you only need to direct copy the audio stream with -acodec copy parameter.

General Notes:

  1. Use -t secs with FFmpeg (after input file name), or -endPos secs with Mencoder (case-sensitive) to stop encoding after several seconds. This options is very useful to see and debug your encoding options.
  2. FFmpeg and Mencoder’s stream index start from 0, instead MKVtoolnix tools start from 1, don’t be confused just map the index respectively.
  3. MP4box cannot automatically matches video stream FPS so you need to add -fps VALUE option while adding video stream.
  4. For vobsub subtitle stream (usually got from DVD), it seems mencoder doesn’t have any problem to render it using -sid or -slang instead extract it first.
  5. You can set starting point using -ss secs with FFmpeg or Mencoder, otherwise using -chapter begin-end if the streams has chapter meta data.
  6. Sometimes you need to adjust the subtitle delay.