Zum Inhalt springen

Brother Johns Encodingwissen

Akapumas Gordian-Knot-Personalisierer

This page is a translation of the original German agkp article from my “Encodingwissen” website. All links to other parts of the Encodingwissen will lead you to German language pages. I created the English version to make access to this fine Gordian Knot add-on easier for the international video community. I’m also willing to give some support on language issues. Contact me or use the Doom9.org thread. But please do not ask me to translate the official documentation included with agkp!

Akapumas Gordian-Knot-Personalisierer, what a name! Much too long to not immediately create an abbreviation, which was done by akapuma himself: agkp. If you want to find out what this thing is all about: read on.

What is It?

Agkp is an external add-on for Gordian Knot developed by akapuma adding three pieces of functionality:

We’ll only look at the support for x264 cli and MKVMerge because without a doubt those are the two most important features. Gordian Knot itself is restricted to the VfW encoder H producing ugly non-compliant streams. And for today’s standards VirtualDubMod’s Matroska features are basic at best.

That means agkp is interesting if we want to use x264 and the Matroska container. In that case I even strongly discourage using Gordian Knot alone because of the VfW mistreated video streams.

Please don’t ask me about filter control. I’m not familiar with that part of agkp myself.

Setup

Agkp’s latest version is available in the first post of the development thread on forum.gleitz.info. We need to install into VirtualDubMod’s folder meaningVirtualDubMod has to be installed before. We simply unpack the complete agkp archive into the VDubMod folder. There we similarly configure all of the three files agkpv.exe, agkpa.exe and agkpAR.exe: With a right click on the file and choosing Properties from the menu we open the properties dialog.

On the Program tab (or is it called Application?) we make sure that Close after exit is ticked. On Windows XP this should be the case anyway.

The file agkp.ini contains all paths to the needed external applications. We can edit the file with every text editor and tell agkp where mkvmerge.exe and x264.exe are located. Those two paths are essential and must always be correct. The player for preview path is only needed for agkp’s own video preview function (we won’t use that) and MP4Box is only needed if we insist on using the x264 VfW encoder with agkp (which generally is a dumb idea). In that case MP4Box converts the twisted VfW stream into native MPEG-4 to correctly mux the video into Matroska.

Now it’s time to load Gordian Knot and switch to the Program Paths tab.

In Where is VirtualDubMod.exe located we use the Locate button to insert the path to agkpV.exe. When Gordian Knot now starts VDubMod, in fact agkp is started. It’s also very easy to perform a classic untampered Gordian Knot encoding in spite of agkp being installed. Just change the VDubMod path again to point to VirtualDubMod.exe.

The last point we have to take care of is installing an x264 VfW encoder. Even though we won’t use it, Gordian Knot needs it to be able to correctly create the VDubMod script, which then agkp analyses. The version of the x264 encoder is not important. It’s only use is to prevent Gordian Knot from aborting with an error. But of course you should use an up to date release of x264.exe (the cli encoder).

Encoding x264 with agkp

Using Gordian Knot is almost business as usual. We choose MKV as container and the x264 video codec. Then we configure the whole encoding as if agkp wasn’t there, except the codec setup. Agkp includes its own x264 presets and ignores the VfW configuration.

When all the configuration work is done and we start the encoding process, the agkp selection window – including mouse support :) – pops up.

The first lines are only needed for filter configuration. It’s best to set them up like in the screenshot and then ignore them. Interesting for us are the three bottom lines.

Translations
ohne = none
Eingabe = Here used in the sense of “Press Enter to input a custom AR”.

This concludes setting up agkp. We are now ready to start the encoding with a click on the OK button at the bottom centre. A window called agkpV.exe pops up showing x264’s commandline and progress info.

The encoding now runs automatically as we know it from Gordian Knot.

Configuration: agkpx264.bat

We configure x264 by editing the file agkpx264.bat inside the VirtualDubMod folder. It is a batch file that some might be familiar with from good old DOS times. Because of its plain text nature editing is possible with every text editor. And that’s what we’re going to do now.

All following line numbers are valid for the unchanged agkpx264.bat included with agkp version 37.

Before delving into the details have a look at some basic facts about batch files.

Comments
Lines starting with REM are comments and will be ignored completely.
Sections
We may divide a batch file into sections by using labels. A label needs a line of its own and starts with a colon followed by the section name. We encounter the first one on line 28 :compcheck containing the compressibility check setup. A section starts with its label and ends at the next label or the end of the file.
Echo
The command echo <Text> displays <Text> on screen. It does not execute anything. Thus echo format c: would display the text “format c:” but not format your hard drive.

The first interesting line is line 9.

set subme=7

Here we globally set x264’s option --subme for the whole file. Possible values are integers between 1 and 7.

Encoder Sections

Further down we are interested in all sections starting with xname. Every one of those sections stands for a separate x264 encoder setup selectable in the agkp GUI program. The following lines contain the default configuration for a 2pass encoding.

52 :xnametwopass
53 ECHO 2-pass
54 rem default
55 echo %2 -p 1 -B %3 -I 300 -i 120 -r 6 --mixed-refs ...
56 %2 -p 1 -B %3 -I 300 -i 120 -r 6 --mixed-refs ...
57 echo %2 -p 2 -B %3 -I 300 -i 120 -r 6 --mixed-refs ...
58 %2 -p 2 -B %3 -I 300 -i 120 -r 6 --mixed-refs ...
59 goto ende

Line 52
The label name for a codec setup must always start with :xname. The rest of the name after that is not important but it must form a unique identifier, meaning two labels of the same name are not allowed in a single file. But not all characters are valid. We should only use letters from A to Z to be on the safe side, so no numbers, punctuation etc.
Line 53
Here we have the name like it is displayed in the agkp GUI program. Additionally the echo command displays the name in the encoding status window.
Line 54
This line identifies the default selection automatically highlighted in the agkp GUI program. Only one single xname section may contain this line.
Lines 55, 56
Those two lines contain the 1st pass configuration. Lines 55 first displays the commandline on screen. Then line 56 starts the actual encoding process.
Lines 57, 58
Here we have the 2nd pass configuration, that works the same als lines 55 and 56. For a 3pass encoding we would simply add another two lines for the 3rd pass as demonstrated in the xnamethreepass section.
Lines 59
Finally goto ende jumps to the end of the file. Without this line all following sections would be executed as well, encoding our movie several times. This lines must never be omitted from any section.

Adding a New Section

Following the above pattern it’s easy to insert our own encoder section and so add another selection to agkp GUI. Best location for the new setup is after the last xname section.

Above the :ende label – where the arrow points – we insert following lines.

:xnameencwissen
echo Encwissen
echo %2 -B %3 -r 5 --mixed-refs -t 1 -f 0:0 -b 3 --direct auto --b-pyramid -w --bime -m %subme% -A i4x4,p8x8,p4x4,b8x8 %agkpzones% --no-psnr --progress -p 1 -o NUL %5
%2 -B %3 -r 5 --mixed-refs -t 1 -f 0:0 -b 3 --direct auto --b-pyramid -w --bime -m %subme% -A i4x4,p8x8,p4x4,b8x8 %agkpzones% --no-psnr --progress -p 1 -o NUL %5
echo %2 -B %3 -r 5 --mixed-refs -t 1 -f 0:0 -b 3 --direct auto --b-pyramid -w --bime -m %subme% -A i4x4,p8x8,p4x4,b8x8 %agkpzones% --no-psnr --progress -p 3 -o %4 %5
%2 -B %3 -r 5 --mixed-refs -t 1 -f 0:0 -b 3 --direct auto --b-pyramid -w --bime -m %subme% -A i4x4,p8x8,p4x4,b8x8 %agkpzones% --no-psnr --progress -p 3 -o %4 %5
goto ende

Of course we can always change x264’s config as explained in the x264 cli chapter. Right now the bold parts are especially important.

%2
Placeholder for the path to x264.exe like specified in agkp.ini.
%3
Agkp replaces this variable with the bitrate calculated by Gordian Knot.
%4
Represents the output filename, i.e. the encoded video-only file without audio etc.
%5
Represents the input file, i.e. the AviSynth script file.
-m %subme%
With this option we include the subme setting we configured above in the set subme line.
%agkpzones%
Agkp automatically recognises if a credits zone is set in Gordian Knot. To apply this setting in the encoding process %agkpzones% must be present in the commandline.

In agkp’s GUI window we now have an additional option called Encwissen representing our newly added setup.

Encoding Xvid with agkp

Honestly, I’m a little astonished that so far no one has had the crazy idea to fool agkp and use the Xvid commandline encoder instead of x264.exe. It works, and it works well. Only restriction: zones (meaning different settings for credits) can only be set up manually, not with Gordian Knot, because Xvid uses a different syntax than x264.

The Xvid cli encoder is called Xvid_encraw and we discuss it in depth in the Xvid cli chapter. You can find the latest version somewhere at the end of this Doom9.org thread. For the encoding to work you need to additionally install the usual Xvid package! Xvid_encraw.exe can be copied into any folder.

The right place to configure Xvid is the now well known file agkpx264.bat in the VirtualDubMod folder. There we add a new encoder section as described above. But this time for Xvid_encraw.

We need to adjust the bold path to xvid_encraw.exe depending on where we placed the file.

:xnamexvid
echo Xvid ;-)
echo "C:\Encoder\xvid_encraw.exe" -i %5 -type 2 -pass1 -vbvsize 1833216 -vbvmax 8000 -vhqmode 2 -qpel -max_bframes 2 -bquant_ratio 162 -bquant_offset 0 -bvhq -nopacked -qtype 1 -progress 10 -zones 0,w,1,O
"C:\Encoder\xvid_encraw.exe" -i %5 -type 2 -pass1 -vbvsize 1833216 -vbvmax 8000 -vhqmode 2 -qpel -max_bframes 2 -bquant_ratio 162 -bquant_offset 0 -bvhq -nopacked -qtype 1 -progress 10 -zones 0,w,1,O
echo "C:\Encoder\xvid_encraw.exe" -i %5 -type 2 -mkv %4 -pass2 -bitrate %3 -vbvsize 1833216 -vbvmax 8000 -vhqmode 2 -qpel -max_bframes 2 -bquant_ratio 162 -bquant_offset 0 -bvhq -nopacked -qtype 1 -progress 10 -zones 0,w,1,O
"C:\Encoder\xvid_encraw.exe" -i %5 -type 2 -mkv %4 -pass2 -bitrate %3 -vbvsize 1833216 -vbvmax 8000 -vhqmode 2 -qpel -max_bframes 2 -bquant_ratio 162 -bquant_offset 0 -bvhq -nopacked -qtype 1 -progress 10 -zones 0,w,1,O
goto ende

The configuration is similar to the one discussed in the Xvid VfW chapter.

These are all changes needed in agkpx264.bat. We can save the file and open Gordian Knot. There we pretend to use x264 because technically speaking we just created a new x264 preset for agkp. Only with the slight difference that this preset won’t use x264. The means we select MKV as our container and like earlier x264 as the encoder, again without wasting any time on its VfW setup. Xvid comes into play a little later.

The compcheck uses x264 in spite of our neat trick. So its results are useless. Easiest way to still use the compcheck is to select XviD as the encoder, do the check and then switch to x264 and agkp.

Like for Xvid VfW the checkbox Calculate Frame-Overhead on the Bitrate tab should be disabled.

When all the setting up in Gordian Know is done and the encoding started the agkp GUI window opens.

The line x264-Encoder has gained a new entry representing our Xvid setup. Of course at this point no one could prevent us from changing our minds and switch to one of the x264 profiles. For mkvmerge AR the same applies as above in the x264 section.

And now we’re only one klick on OK and a couple of hours away from a native Xvid MPEG-4 video inside Matroska. Well, now, is Gordian Knot capable to manage modern encoding technology or is it not? :)

Anamorphic Encoding with agkp

My Encodingwissen definition of “anamorphic MPEG-4” just means “non-square pixel video”, nothing more specific.

Anamorphic encodings with Gordian Knot need some tricks anyway. And even agkp only provides the possibility to set the AR flag in the Matroska container. But because x264.exe as well as Xvid_encraw support setting the MPEG-4 AR flag we can easily add the missing feature by editing agkpx264.bat.

To do this we open agkpx264.bat and define a new variable following the subme line (line 9 for the unchanged original file).

set pixelar=16:11

All command further down in the file now have access to a new variable called %pixelar% containing the PAR that should be written to the MPEG-4 flag. The value 16:11 from the screenshot stands for a 16:9 PAL DVD. To find the correct value for every movie, have a look at the PAR table. (You don’t need any German to understand that table.) For a classic non-anamorphic encoding we set the variable to 1:1.

Now we need to adapt the encoder sections. For x264 we add the option --sar %pixelar% (double dash!) to every commandline, e.g.:

:xnameencwissen
echo Encwissen
echo %2 -B %3 --sar %pixelar% -r 5 --mixed-refs ...
%2 -B %3 --sar %pixelar% -r 5 --mixed-refs ...
echo %2 -B %3 --sar %pixelar% -r 5 --mixed-refs ...
%2 -B %3 --sar %pixelar% -r 5 --mixed-refs ...
goto ende

For Xvid the option is called -par %pixelar% (single dash!):

:xnamexvid
echo Xvid ;-)
echo "C:\Encoder\xvid_encraw.exe" -i %5 -type 2 -pass1 -par %pixelar% -vbvsize 1833216 ...
"C:\Encoder\xvid_encraw.exe" -i %5 -type 2 -pass1 -par %pixelar% -vbvsize 1833216 ...
echo "C:\Encoder\xvid_encraw.exe" -i %5 -type 2 -mkv %4 -pass2 -par %pixelar% -bitrate %3 ...
"C:\Encoder\xvid_encraw.exe" -i %5 -type 2 -mkv %4 -pass2 -par %pixelar% -bitrate %3 ...
goto ende

At the very end of the file we finally erase the PAR variable by adding this line:

set pixelar=

We’re ready to encode. Gordian Knot should be set up as usual for an anamorphic encoding. Before starting the encoding job – i.e. before agkp GUI is displayed – we need to make sure that the correct value is set in agkpx264.bat’s line set pixelar. Then we will get an encoding with an MPEG-4 AR flag.

In agkp GUI we might additionally set Matroska’s container aspect ratio flag. To do this we click on Eingabe in the mkvmerge AR line, type in the correct value (always using the dot as decimal separator) and confirm by pressing Enter.

mkvmerge AR: [2.370_ ] [4/3] [1.33]

Warning! Matroska uses DAR instead of PAR! The correct value is the same one we would enter in MKVMerge GUI under Aspect Ratio. Further details can be found in the Matroska muxing chapter (sorry, no translation).

At we klick on OK to start the encoding. The result is a cleanly muxed Matroska file including both AR flags and a native MPEG-4 video stream – all this in spite of the VfW centric Gordian Knot. And at this point we really deserve a good hot cup of coffee. :)