Convert HDTV H264 Transport Stream (TS) to H264 in MKV
Lately I changed my HTPC setting in order to get the full experience of HDTV. Because of that I installed Windows 7 and MediaPortal 1.1 RC1 instead of the Microsoft Media Center.
Well, MediaPortal is able to record SDTV and HDTV. The recordings are stored in a Transport Stream file *.ts and because of that those files are merely a dump of the TV signal and rather big. A movie that lasts 2 hours takes about 12 GB (1 Video Stream, 2 MPEG Audio Streams, 1 AC3 Stream, Teletext)
I needed a solution to compress and convert those files somehow. The problem is that there are not many applications out there that can handle Transport Streams with H264 content so far. Most people suggest ProjectX, however ProjectX does NOT SUPPORT Transport Streams with H264 (07.04.2010) and is unable to demux or repair the video of such TS files.
With TS Files you usually can’t use normal video editing tools because a TS file most likely contains errors. In fact, the purpose of Transport Stream files is to transport information that may get corrupted on its way. At the other end, the recipient (TV, HTPC, Player,..) has to work out a way to fix the errors or at least don’t crash while decoding the streams. Most video editing tools and players however work with “Program Streams” and assume that the video and audio data contains no errors.
After some try & error I found a way to convert those TS files using MeGUI.
STEP 1 – Unmux the Streams
Open MeGUI and select TOOLS -> HD STREAMS EXTRACTOR. Check the radio box that you will use a file as input and select your TS file. Mark all Streams in the “Streams(s)” section in order to extract them. The video should be extracted as MKV. Select an output folder and hit Queue.
In the main MeGUI window switch to the queue and start the job.
MeGUI: HD Stream Extractor
After the job is finished you should see several files in the output directory you specified. In my example:
T1_Video – .mkv
T2_Audio – German.mp2
T3_Audio – German.mp2
T4_Audio – German.ac3
T1_Video – - Log.txt (don’t delete this file, we need it later)
This step demuxes and muxes the video stream of the TS file in a MKV file and somehow fixes small errors on the way. I guess the video stream still contains some errors (you cannot fix what’s missing) but the container and seeking functionality get fixed [critication needed]. MeGUI most times displays several errors when you try to create a AVS file with the TS file, however it seems to work with the MKV file created in this step.
STEP 2 – Create a AviSynth Script + Cropping
After the extraction process 2 new windows should appear automatically.
If no windows appear check if you have started the job specified in Step 1 or open it manually with TOOLS -> AVS SCRIPT CREATOR and select the mkv file you created in step 1, not the TS file.
One is the preview window and the other one is a settings window. Most likely your recorded video has some black bars on top, left, right or bottom. If you want to crop the video to remove the black bars you should select “Clever (TM) anamorphic encoding” -> “Overcrop to achieve mod16? and specify the crop parameters in the Crop & Resize section. Hit “Save” after you entered your prefered settings.
If you don’t want to crop anything, just hit “Save” immediately. This Action will create a Avi Synth script *.avs.
MeGUI: AviSynth Script Creator
You can also resize the video in this dialog. If you want to resize it you can choose “resize to mod16? instead of “Overcrop to achieve mod16?.
If you want to cut some parts of the video you can do that afterwards by editing the AVS file with TOOLS -> AVS CUTTER. However I recommend to cut the file after the encoding and muxing everything together simply because if you cut the video file now you have to cut the audio files later manually.
STEP 3 – Encode Video
After you saved the AVS Script a new preview window should appear because MeGui loads the new script right away. If that’s not the case you have to select the script in the input tab of the main MeGUI window. You can close the video preview window.
Choose a place and a filename where the new video file should be created in “Video Output” and the file format should be MKV.
In this step you can also calculate and set a desired filesize using TOOLS -> BITRATE CALCULATOR. However I skip that part and just use the default settings.
Select your prefered encoder settings from the dropdown. hit “Enqueue” and start the job.
MeGUI: Encoder Settings
In my example I use “x264: DXVA-HD-HQ” as encoder setting. That profile ensures that the file that will be created is DXVA compatible and because the input source is HDTV I choosed a HD profile. If the source contains SD Content you should use one of the “x264: DXVA-SD-*” profiles. What kind of profile you use, “Anime_Toons Fast”, “Anime_Toons HQ”, “Balanced”, “Extra Quality”, “Fast”, “HQ” or “Insane” depends on your content. There’s no real guideline for that, just try the different profiles and see what looks best.
STEP 4 – Mux the Streams
After some minutes/hours/days MeGUI has finished the encoding of the video. The last step is the muxing of all the streams (Audio+Video) in a single MKV Container. Select TOOLS -> MUXER -> MKV MUXER from the MeGUI main window.
“Video Input” is the file that MeGUI just created for you.
In the Audio Section of the muxer, add the audio tracks you want to include with your video file. In my case there were 3 audio streams that were broadcasted. Most times *.MP2 files contain Mono or Stereo Audio and *.AC3 files contain Stereo or 5.1 Audio.
We open the small *.txt file that was created in Step 1 with a text editor and see what audio streams we got. In my case I found those sections in my *.txt file:
TS, 1 video track, 3 audio tracks, 2:12:00, 50p
1: h264/AVC, 720p50 (16:9)
2: MP2, German, 2.0 channels, 256kbps, 48khz, -1349ms
3: MP2, German, 2.0 channels, 256kbps, 48khz, -1351ms
4: AC3, German, 2.0 channels, 448kbps, 48khz, dialnorm: -22dB, -1391ms
As you see, all my 3 audio files are Stereo (2.0 channels). A very important information in this file is the delay of each audio track, marked bold.
Back to the muxer. Select the audio file(s) you want to include in your Video. You have to enter the right delay of the audio file you selected. If you don’t enter the right delay, the audio will be ahead or after the video.
MeGUI: MKV Muxer
Choose a path and a filename for the file in “Muxed Output”, hit “Queue” and start the job. This should only take a few seconds/minutes.