# MeGUI/Guides/HDTV Transcoding Guide
## Setting up the source

First thing you need to do is get your video ready so you can encode it.
    MPEG2 Source For an MPEG2 transport stream the tool of choice is DGIndex Load the ts. Select the Stream menu and click 'Detect PIDS/PMT'. Select MPEG2 Video and press set Video. Select AC3/DTS Audio, press set Audio and done. Make sure audio->output method is set to 'demux all tracks'. Press F4 and choose a directory to save the d2v (video) and ac3 (audio) files.
    h264 Source If you're trying to encode a h264 source you need two programs: ProjectX ( http://www.videohelp.com/tools?tool=ProjectX ( you want "Projectx compiled with Java included here (27MB)")) PTSCalc ( http://www.h264tscutter.de/ ) ProjectX will let you take the ac3 audio out of your transport stream. Open up ProjectX and open your ts. Press quick start (wait until it's done then close ProjectX). Your AC3 audio will be in the same directory as your transport stream. Open up the TS with PTSCalc, press calc. This handy little program will work out the audio delay for you. Rename your ac3 file and put DELAY xx (xx is the delay PTSCalc said, but keep it as a whole number). (e.g. 'The 4400 S01E01 DELAY -300ms.ac3')
    Other Source For other sources, you don't need to do any preprocessing.

## Creating your avs file

This is where you resize your video and apply filters to it if you want.
* Download & install the latest stable version of Avisynth from sourceforge.
* Download the latest copy of the TIVTC package from here. Extract the dlls to your avisynth\plugins directory.
* Move the DGDecode.dll file from your DGIndex directory into avisynth\plugins as well.
* Create a blank file, name it encode.avs and open it up with notepad (or your text editor of choice).

### Loading the video source

    MPEG2 Source MPEG2Source("C:\myvideo.d2v") Ensure the location is correct for the d2v file
    h264 Source To load the video in type: directshowsource("C:\myvideo.ts",fps=###) Ensure the location is correct for the d2v file Replace the '###' with the correct framerate. For BBC caps this is 25. If FFDShow can decode the stream correctly, use it in preference to CoreAVC, which exhibits blocking artifacts in some circumstances. The latest CoreAVC is meant to have fixed this (1.3.0.0), but it's unconfirmed, and older versions are broken.
    Other Source Different sources will need to be loaded different ways. New formats, such as HD DVD and BD can be troublesome to load, and you will need to find current informations in forums to load them in the best possible way. If in doubt, try: directshowsource("path\to\file.ext")

### Deinterlacing / Telecine removal

The next step involves converting the stream to a series of progressive frames with the correct framerate. There are three main types of stream that you might be starting with at the moment:
* Progressive
* Interlaced
* Hybrid: The most common form of which is telecined content. Some frames are progressive, some are interlaced.
You can determine if the source is interlaced or progressive by opening the script you have created so far (which should be just one line) in a media player, and stepping through a section with fast motion frame by frame. If the video has combing in some or all of the frames, the video is interlaced or telecined. If all frames are clean, the video is progressive.
    Progressive Source (i.e., 720p and 1080p captures) These sources will either come at the correct framerate, or a faster rate. For example, most 720p captures come at 60fps, which is faster than the original framerate. If the framerate is 23.976fps, 25fps or 29.970fps, you don't need to do anything! If the framerate is 59.94fps, you have three options: Keep at the original framerate. Sometimes when you have a sports cap or something else with a high framerate, any framerate reduction leads to a jerky picture. In these cases, you can choose to keep the original framerate, which will be more bitrate intensive, but look better. Decimate to 23.976fps. If the video was originally a film, or some other video that started life as a 23.976fps video, you can return it to this framerate with TDecimate: tdecimate(mode=0,cycleR=9,cycle=15) Decimate to 29.97fps. If your video began as 29.970fps, return it to that with the inbuild AviSynth command SelectEven: selecteven
    Interlaced / Telecined Source (i.e., 1080i captures) These sources are sources that contain interlaced frames. An interlaced source is a continuous stream of interlaced frames, while a telecined source has a mix of interlaced and progressive frames. You must process each format differently. You can read the article on detecting interlace for more info on how to do that. If the stream is pure interlaced: You can deinterlace it with TDeint: tdeint If the stream is telecined: You can run an IVTC with TFM & TDecimate: tfm.tdecimate

### Cropping & Resizing

Cropping involves removing all black bars from the source. You must crop in multiples of 2. A simple way to work out the cropping is to open the video in MeGUI's avisynth script creator and use the dynamic cropping tool in there. Then take the crop(...) line from the third tab and add it to your script. Note, you should crop out all the black edges, even if it means you must also slice off a pixel of video. The final result will be better for it. Once you have done this, you need to resize the video. Add in a spline36 resize: spline36resize(1280,720). This will resize to a 16:9 aspect ratio, if the original display ratio of the video was different you much change the 720 to suit. For example, a 2.35:1 AR video becomes 1280x544.

### Filtering

If your source is mpeg2, use colormatrix to correct colors: colormatrix

## x264 encoding

Firstly, download this collection of x264 matrices, and extract them wherever you want. Then start up MeGUI and load the input avs. Scroll through a few frames to make sure everything is ok (cropping at all points in the movie, no overfiltering, etc.), then close the preview window.
* Choose encoding profile: Choose your x264 encoding profile. Use one of the HQ-_____ set (i.e. HQ-Slow, Slower, Slowest, Insane). Slower profiles give better results, but the quality gain reduces every time.
   * Verify 2pass: Open the codec config and check 'Automated 2pass' is selected, and 'turbo' is enabled. If your source is clean and free of grain, you can increase the deblock settings, but don't go higher than -1,-1 for a normal film. Click OK.
   * Set the custom matrix: On the fourth tab, set 'quantization matrix' to custom, and choose either eqm_avc-hr.cfg or M4G HRM V2.cfg. The former is better when slightly bitrate starved, while the latter can get you close to transperancy.
* Calculate the bitrate: Go tools->bitrate calculator. First put in the output size (either use one of the presets or put the number in by hand). Next, set the correct bitrate for your audio track. Finally, make sure 'b-frames' is checked, the framerate is right (although both of these make little difference), and you have selected the right codec (x264) and container (mkv). Note down the average bitrate and click 'apply'. Check it has been correctly applied in the codec config.
* Setup the encoding jobs: If you have a spare 100gb on the drive you have your input script on, you can check the box 'add pre-rendering job'. Click 'enqueue'. The jobs should now be visible on the job tab.
* Start the encoding: On the jobs tab, click 'start'. Wait the 16-72hrs this step takes.

## Muxing the audio and video together

* Download MKVMerge: Download MKVToolnix from its homepage
* Select input files: Open mmg.exe, and add as sources both your encoded video and the original file that contains audio
* Choose output streams: Uncheck all streams that aren't required.
* Set output track options: Set languages for any audio / subtitle streams and if there are more than one, name them. If required, set the correct audio delay for any audio streams.
* Mux: Choose save location, and click start muxing.
All done :)