Hein (fub) wrote,
Hein
fub

  • Mood:

Tutorial time!

Here is how to re-encode MKV into AVI. Before this will work, you will have to be able to play MKV files in, say, the Windows Media Player. Go download a codec pack or something, and verify that you can play the file (even though it's choppy on your machine -- we'll be re-encoding it to something more sensible!).
Also, you will need codecs for your target encodings. I'll assume you know how to get and install those.

Software you will need:
- MKVToolnix -- a set of tools to work with the MKV container format.
- Avisynth -- a frameserver that can decode DirectShow video sources. Get at least version 2.5.6.
- VirtualDub -- a great little program for encoding video with tons of options.
- VobSub -- a plugin for AviSynth and/or VirtualDub that interprets subtitle-files.

1. Run mkvinfo on your MKV file
mkvinfo gives you a rundown of what streams (audio, video, subtitles) are contained within the MKV file structure, their encoding and other interesting tidbits that we will need to know. For ease of reference, you could redirect the output of the program into a file:
mkvinfo "D:\Anime\recode\Starship operators - 12.mkv" > info.txt
which gives us the following information:
+ EBML head
|+ Doc type: matroska
|+ Doc type version: 1
|+ Doc type read version: 1
+ Segment, size 358355634
|+ Seek head (subentries will be skipped)
|+ EbmlVoid (size: 3996)
|+ Segment information
| + Timecode scale: 1000000
| + Muxing application: libebml v0.7.5 + libmatroska v0.7.7
| + Writing application: mkvmerge v1.6.0 ('Ist das so') built on Oct 14 2005 15:22:41
| + Duration: 1416.256s (00:23:36.256000000)
| + Date: Sun Feb 12 17:54:31 2006 UTC
| + Segment UID: 0xb0 0x49 0x85 0x35 0xee 0x77 0x40 0x5f 0x94 0x2b 0xcf 0x56 0x78 0x67 0xf5 0x5f
|+ Segment tracks
| + A track
|  + Track number: 1
|  + Track UID: 1
|  + Track type: video
|  + Default flag: 1
|  + Forced flag: 0
|  + Lacing flag: 0
|  + MinCache: 1
|  + Timecode scale: 1.000000
|  + Max BlockAddition ID: 0
|  + Codec ID: V_MPEG4/ISO/AVC
|  + CodecPrivate, length 40
|  + Default duration: 41.708ms (23.976 fps for a video track)
|  + Language: und
|  + Name: Starship Operators 12: War Cry
|  + Video track
|   + Pixel width: 704
|   + Pixel height: 480
|   + Display width: 853
|   + Display height: 480
| + A track
|  + Track number: 2
|  + Track UID: 8817307
|  + Track type: audio
|  + Default flag: 1
|  + Forced flag: 0
|  + Lacing flag: 1
|  + MinCache: 0
|  + Timecode scale: 1.000000
|  + Max BlockAddition ID: 0
|  + Codec ID: A_VORBIS
|  + CodecPrivate, length 4330
|  + Language: jpn
|  + Name: 2.0 Vorbis
|  + Audio track
|   + Sampling frequency: 48000.000000
|   + Channels: 2
| + A track
|  + Track number: 3
|  + Track UID: 965307781
|  + Track type: subtitles
|  + Default flag: 1
|  + Forced flag: 0
|  + Lacing flag: 0
|  + MinCache: 0
|  + Timecode scale: 1.000000
|  + Max BlockAddition ID: 0
|  + Codec ID: S_TEXT/ASS
|  + CodecPrivate, length 3902
|  + Language: eng
|  + Name: ASS
| + A track
|  + Track number: 4
|  + Track UID: 3803979140
|  + Track type: subtitles
|  + Default flag: 0
|  + Forced flag: 0
|  + Lacing flag: 0
|  + MinCache: 0
|  + Timecode scale: 1.000000
|  + Max BlockAddition ID: 0
|  + Codec ID: S_TEXT/UTF8
|  + Language: eng
|  + Name: SRT
|+ EbmlVoid (size: 1024)
|+ Attachments
| + Attached
|  + File name: sydni.TTF
|  + Mime type: application/x-truetype-font
|  + File data, size: 62868
|  + File UID: 2266173367
| + Attached
|  + File name: diskusm.TTF
|  + Mime type: application/x-truetype-font
|  + File data, size: 55256
|  + File UID: 3523965596
| + Attached
|  + File name: HEMIHEAD.TTF
|  + Mime type: application/x-truetype-font
|  + File data, size: 23312
|  + File UID: 63874445
| + Attached
|  + File name: kingrich.ttf
|  + Mime type: application/x-truetype-font
|  + File data, size: 25248
|  + File UID: 1011683744
| + Attached
|  + File name: sydn.TTF
|  + Mime type: application/x-truetype-font
|  + File data, size: 59284
|  + File UID: 2272920464
| + Attached
|  + File name: sydnb.TTF
|  + Mime type: application/x-truetype-font
|  + File data, size: 58116
|  + File UID: 2476481462
| + Attached
|  + File name: sydnbi.TTF
|  + Mime type: application/x-truetype-font
|  + File data, size: 59988
|  + File UID: 1123695506
|+ Chapters
| + EditionEntry
|  + EditionFlagHidden: 0
|  + EditionFlagDefault: 0
|  + EditionUID: 2224685332
|  + ChapterAtom
|   + ChapterUID: 1674640026
|   + ChapterTimeStart: 00:00:00.000000000
|   + ChapterFlagHidden: 0
|   + ChapterFlagEnabled: 1
|   + ChapterDisplay
|    + ChapterString: Intro
|    + ChapterLanguage: eng
|  + ChapterAtom
|   + ChapterUID: 2018697190
|   + ChapterTimeStart: 00:01:31.500000000
|   + ChapterFlagHidden: 0
|   + ChapterFlagEnabled: 1
|   + ChapterDisplay
|    + ChapterString: Starship Operators 12: War Cry
|    + ChapterLanguage: und
|  + ChapterAtom
|   + ChapterUID: 637300074
|   + ChapterTimeStart: 00:22:01.200000000
|   + ChapterFlagHidden: 0
|   + ChapterFlagEnabled: 1
|   + ChapterDisplay
|    + ChapterString: Credits
|    + ChapterLanguage: und
|+ EbmlVoid (size: 101)
|+ Cluster
Here, we can see that track 1 is the video track, that it is encoded in H.624 ('AVC'), with a framerate of 23.976 fps. Track 2 is the audio track, encoded in Ogg-Vorbis. Track 3 is the subtitle track in 'ASS'-format, which is precisely the format that VobSub wants. There's also a subtitle track in 'SRT'-format, which we can safely ignore. Next it's a set of truetype fonts, and some chapter stops.

2. Extract and sanitise the subtitles
You will need to extract the subtitles, since AviSynth will not render subtitles by default. We will need to feed the subtitle-file to VobSub, so that the subtitles can be merged with the video source.
Use mkvextract to extract the third track (the subtitles):
mkvextract tracks "D:\Anime\recode\Starship operators - 12.mkv" 3:sso12_subs.ssa
which will extract the subtitles. If you're lazy and/or CLI-phobic, you might want to try to download MKVExtractGUI, but I couldn't get it to work on my machine.
Open the newly extracted file with a texteditor (I use TextPad, but any good text editor will do), and have a look at it. It may be possible that every line of dialogue starts with 'Dialogue: Marked=' -- for some reason this doesn't happen every time, and it probably has something to do with the source streams that have been muxed into the MKV. For some reason, VobSub doesn't like the 'Marked='-part of the line. Using search/replace, remove every such occurrance of 'Marked=' from the file.

3. Create a script for AviSynth
We will need to instruct AviSynth to open the MKV-file and serve the individual frames to VirtualDub, which will then encode the frames into an AVI.
AviSynth script-files have the extension '.avs', so make a script named (for instance) sso_12.avs, with the following contents:
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\vsfilter.dll")
DirectShowSource("D:\Anime\recode\Starship Operators - 12.mkv", convertfps=true, fps=23.976)
textsub("D:\Anime\recode\sso12_subs.ssa")
which does all the hard work. First we load the VobSub plugin/filter (you may need to adjust your path, of course).
Second, we tell AviSynth to open our MKV-file in 'DirectShow'-mode. DirectShow is the standard API/method that the Windows Media Player (and a host of other media players) use to decode video and audio. So anything that you can play in the WMP can be frame-served through AviSynth, which is a pretty neat trick.
We add two extra parameters: 'convertfps=true', which tells AviSynth to insert frames so that the file will have a constant framerate (keeping the video and audio in sync!), and 'fps=23.976', which tells AviSynth the framerate to use.
Third, we tell the VobSub plugin to interpret the subtitles we had just extracted.

4. Encode the video in VirtualDub
Start VirtualDub, and open your AVS-script. Splitters and plugins will load, and the file will be opened just as if it was a normal AVI file. Don't worry if you don't see any actual image -- for some reason VirtualDub doesn't show the image when working with AviSynth scripts. You can always verify that the encoding process works by clicking the box 'show output video' when encoding (which will slow down the encoding, so turn it off again!).

Now, configure VirtualDub to encode the video:
- Set the Video Compression:
  From the menu, select Video | Compression, and select the codec you want to use. I have a preference for XviD, but yours may be different. Configure the bitrate for your codec -- I get good results with XVid on Simple@LO, single pass, quantifier 2.72.
- Set the Audio Compression:
  If you want to re-encode the audio, select from the menu Audio | Full Processing Mode, and then Audio | Compression to select the compression for the audio. I use MP3 on 192 kBit/sec, but you may have different preferences.
- Save the file
  Select File | Save as Avi. Choose a file name. If you have a lot of files to convert, then you may want to tick the box "Don't run this job now..." so that you can build up a queue of conversions that can run overnight and/or during your working day.

And that is all there is to it! This process will make a 213 MB AVI from a 349 MB MKV file, with hardly any quality loss (at least, not that I can discern on our TV), that can be played on much older/less powerful hardware.
Tags: fansubbing, tools
Subscribe

  • Final RPG-a-Day: Thank

    The last prompt for RPG-a-Day this year is ‘Thank’. If you have read every entry of this year’s RPG-a-Day, then I certainly…

  • Next-to-last RPG-a-Day: Mention

    Today’s prompt is ‘Mention’. I guess this is where I mention people I look up to, or websites I frequent? Ok, here’s…

  • RPG-a-Day 29: System

    We’re in the home stretch for this year’s RPG-a-Day! Today’s prompt is ‘System’. Paulo, who has been doing…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 1 comment