[Tutorial]How to encode MPEG4 AVC videos

Last updated on 12 years ago
[color=FF0000]Original credits: Nox1911[/color]

Tools you need:
- x264 encoder (http://x264.nl)
- MeGUI (http://x264.nl) (needs .NET Framework)
- NET Framework (http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=0856eacb-4362-4b0d-8edd-aab15c5e04f5)
- AviSynth (http://sourceforge.net/project/showfiles.php?group_id=57023)
- Nero AAC Encoder (http://www.nero.com/nerodigital/eng/Nero_Digital_Audio.html)
- YAMB (http://yamb.unite-video.com)

- VirtualDub (http://www.virtualdub.org)

Once your recording (avi file) is ready and saved, set up an AviSynth frameserver file by creating an .avs file (standard text file) and put the following into it:

#trim(1000,10500) # Trims the Video - get the framenumbers with VirtualDub - remove the '#' if you want to use it.

#changefps(25) # remove the first '#' if you recorded at 25fps
pointresize(width*2,height*2) # Double-sizes the video (320x200 -> 640x400)
SubsStart=50 # set to 25 if you recorded at 25 fps
SubsDura=250 # set to 125 if you recorded at 25 fps
Subtitle("-= MyGame =-", first_frame=SubsStart, last_frame=SubsStart+SubsDura, y=340, size=24, text_color=$ffffff, align=2)
Subtitle("PLAYED BY YOURNAME", first_frame=SubsStart, last_frame=SubsStart+SubsDura+5, y=365, size=24, text_color=$ffffff, align=2)
Subtitle("WWW.LONGPLAYS.org", first_frame=SubsStart, last_frame=SubsStart+SubsDura+10, y=390, size=24, text_color=$ffff00, align=2)

ConvertToYV12() # Converts to YV12 colorspace for h264

Save and drop the .avs file into your video player to check if its valid (doesn't work with vlc).
Now install x264.exe and MeGUI into the same folder and copy 'neroAacEnc.exe' as well to this folder and fire up MeGUI.exe. Open 'Tools/Settings' and remove 'Open Preview after AviSynth script selection' as we don't need it. Load the Avisynth script at 'Input/Avisynth Script'. Select 'x264' codec and 'MP4' Container and press 'Config'. Now set up settings like this:

[img width=575]http://img80.imageshack.us/img80/9417/x2641mainvs4.png[/img]

[img width=575]http://img218.imageshack.us/img218/4706/x2642rcandmegf1.png[/img]

[img width=575]http://img80.imageshack.us/img80/4671/x2643advancedvb5.png[/img]

Additional Info:
- You can put the .avs files directly into any windows player (all but VLC) to watch the result or check if its valid.
- If you have recorded multiple avi files you want to merge at once try the following:

NOTE: The screens from MeGui above is from a old version! - Better if you download the profiles attached to this thread and put them in your megui/allprofiles folder. That would save you some time.

NOTE2: Make sure you update your MeGui and Tools to latest version before you start.

NOTE: The profiles in this thread is not the best for 3D games. Use them for 2D games only.

Good luck! It's not so complicated as it looks like.
is there a video we can watch or something cause this seems way too confusing
If I can't be the best then I certainly won't be the worst.
Its actully pretty simple.

1: Download and install the programs at the top. You don't need x264 since when you update MeGUi installs it for you and I wouldnt install Yamb but find mkvmerge GUI V2.9.5

2: Create a new .avs file by right clicking > new > AviSynth Script

3: Copy and paste this into it

#trim(1000,10500) # Trims the Video - get the framenumbers with VirtualDub - remove the '#' if you want to use it.

#changefps(20) # remove the first '#' if you recorded at 25fps
SubsStart=25 # set to 25 if you recorded at 25 fps
SubsDura=150 # set to 125 if you recorded at 25 fps
Subtitle("-= Mortal Kombat Mythologies: Sub-Zero =-", first_frame=SubsStart, last_frame=SubsStart+SubsDura, y=410, size=24, text_color=$ffffff, align=2)
Subtitle("Played By RickyC", first_frame=SubsStart, last_frame=SubsStart+SubsDura+5, y=430, size=24, text_color=$ffffff, align=2)
Subtitle("WWW.LONGPLAYS.NET", first_frame=SubsStart, last_frame=SubsStart+SubsDura+10, y=454, size=24, text_color=$ffff00, align=2)

ConvertToYV12() # Converts to YV12 colorspace for h264

It looks less complicated once its been pasted in the text document. You need this for each one of your videos or just edit the one you have each time. You want to change "("C:\gamename.avi")" to where your game is and the name of the file.

At the bottom of all that is the game name and who created the movie and this will be printed on to the video at the start so change that too. Also the numbers after the "Y" will increase/decrease the position of the text at the start and "size" will increase/decrease the text size. The SubsDura=150 and SubsStart=25 numbers are for how long the text will be shown for. It will be a little trial and error to get it perfect specially if you've never done it before but it takes seconds.

If there is a "#" at the start of a line it will stop the function taking place. So if you don't want the "longplays.net" text to be on or don't want the framerate changed just place a # before it. You can see there is one before the framerate text already.

So most of the time you just changed where the file is and what the video is called at the top and on the bottom lines change the name of the player and what the game is called. If you don't see the text at the start adjust the number after the "y" till you get it in a nice place.

4: Save the .Avs file.

5: Check the text that is printed at the bottom is in the right place and is up for a few seconds by opening the .avs file or dragging it in to your video player. Media player classic works or even virtualdub should work.

If the video doesn't show its most likely you didnt type in the correct location of the file in the avs file or you typed the wrong file extension after the name of the video like gamename.mp4 instead of gamename.avi.

6: Once the text shows perfect open up megui and update it. Copy the "'neroAacEnc.exe" files you downloaded at the start int megui's root folder too. If its gives errors when encoding the audio its not the root folder but copy into the folder it tells you to.

7. Under encding setting of megui's main screen select x264 scratchpad and click config in the video encoding section and and change the settings to what is shown above or as close as you can get it since the new megui menus are different.

To lower/increase quality Change the number by quality that is defaulted at 30. Though I normally leave this. If the file size i huge though i would change to 31 or 31.5. If you want it a little better then maybe 28 but im normally ok with default. Higher numbers = lower quality. I find if the number is too high then things on the video seem to shake or jump mainly in 3d games.

Also changing the min/max quantizers under advance from 5 / 30 will help with file size and affect quality much less i find. For faster 2d games or 3d games i change to 20 / 35. For super long games maybe 25 / 40. Higher numers = lower quality. I dont notice much different when changing numbers but flie size gets lower.

If you are unsure what numbers to set create a 1 or 2 min test video of different parts of the game and see how it looks. You don't need to be changing the numbers that often it will generally look good and 20/35 will give nice file size to quality ratio for most games.

You will use your own numbers if you ever get used to it.

8: save the profile. Then under the audio encoding part select Nero AAC scratchpad go to config then audio settings above ans save the profile. You'll only need to config all this once then after this its quicker.

9: Click the top right "..." button and select your .avs file. Change the output name and go to enqueue. Beware Megui will copy over files with the same name so chaning before starting is always good.

10: Do the same for the audio part mke sure the output name isnt the same as the video otherwise it will go over the video. If the avs file doesnt work then save a .wav file of your movie in virtualdub and use that where you would use your avs for the audio encoding.

11: Select Queue and press start.

12: Once finished use MKVmerge to join the audio with the video and BAM.

If there is any unsync in the audio you can set a delay in mkvmerge though youtube won't take notice of it. If delay is more gradual you can either go back to the source files and try to fix with virtaldub or create a wav file of the audio, open it in a program like audacity and add silence or delete parts of the audio in different parts of the track to keep it in sync. Not the most accurate way but i can be faster than going back to the source files and starting over.

13 Videos should be in one complete file and not multipul parts. but you can do a little each time and join them all up at the end using mkvmerge. If you're going to do this make sure the settings you use in megui are exactly the same for time use encode a part of you won't be join them up.

After all the parts are done use mkvmerge and drag the 1st video part to it, highlight that part then click append and pick the 2nd part then highlight that 2nd part append again till all the video parts are there.

Then go to add and pick your first audio file, then highlight it and go to append and add your 2nd audio file etc till all your audio parts and video parts are there.

Then start mkvmerge. All will be joined and hopefuly in sync. If not you can add a delay in the audio with each individual part you append and try again.

I really did want this to be short and uncomplicated but it turned out a little different than how it started. If you have other ways to encode with H264 then you could use them but MEgui is the easiest and fasters way i've found. Same with the audio if you can convert to aac with variable bitrate you could as well.
Took me awhile to get this down and i still make mistakes in encoding from time to time and some videos just don't want to encode correctly.
One tool that's really helpful and makes things easier when working with avs files is "AVSEdit".

Can you remove black borders with .avs-file? And those settings are for 2D games right? What settings I should use for 3D games?
Im not too sure. There might be a way but most people would remove the boarders with virtualdub or whatever editing program you use first though i'd recommend virtualdub and a lossless codec like techsmith each time you edit and encode before the final video to keep it at best quality.

Avs is normally just used to add the title of the movie and perhaps resize the video if needed when doing the final encode.

I used google to find this

line_top = clip_in.Crop( x, y - 1, width, 1)
line_bot = clip_in.Crop( x, y + height, width, 1)
line_left = clip_in.Crop( x-1, y, 1, height )
line_right = clip_in.Crop( x+width, y, 1, height )

Adding that text and chaning the numbers might cut the boarders. You'll need to do a lot of trial and error by changing the numbers and playing the avs file in a media player first. You also need to make sure the end resolution can be devided by at least 4 or recommended 16 or it won't encode. Its a lot easier to just do this with virtualdub when editing like i said since the numbers are all shown before hand.

Or try Parazzing's recommened program. I've never used that so it might make it easier.
nero 404 problem link is dead
Some quick suggestions for increasing compression (lower filesize) while keeping the quality or even increasing it..

Use the latest x264 build. The official, abandoned MeGUI doesn't work with newer releases but try this build of MeGUI: http://forum.doom9.org/showthread.php?t=151159 . Remember to switch to development server for automatic updates!

Or even better, download the x264 executable and start using it by the command line (it's really easy to set up a batch file and adjust it accordingly for all your encodes).

Use --tune animation and --preset veryslow or --preset slower or --preset slow. Those settings will set up most stuff automatically.

Remember to enable b-pyramid, in the recent builds it is disabled because it used to not co-operate with mb-tree. The cli option to use is --b-pyramid normal.

Up-to-date x264 builds can always be found at http://x264.nl.

A simple command line would be

c:\x264\x264.exe --preset veryslow --tune animation --b-pyramid normal --output z:\pathtovideo.mp4 z:\pathtoscript.avs

You can then use the resulting video file to mux with your audio file in MeGUI or MKVMerge.
wait, do I have to do all of this, because so far i dont understand one thing about it, but it only needs to be in mp4 format, because I can do that, I just don't know if I have to do all that other stuff with the game name and junk
If I can't be the best then I certainly won't be the worst.
uh we need a tutorial video to help use not just still shots
I think a Video Tutorial section would be nice.
?"Work is for people who can't play games!" - Billy Mitchell
I was planning on making them, just haven't gotten around to it D:
Metal Gear! uhhggg
Cubex/Moderators: Can you guys not make a Tutorial Video either on this site or on the Youtube page, becuse alot of people still doesent understand all this....
Awaken my child, and embrace the glory that is your birthright. Know that I am the Overmind; the eternal will of the Swarm, and that you have been created to serve me. ~The Overmind, StarCraft
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.