Log in

View Full Version : Batchencoding with x264 and xvid_encraw


Iznogood
25th January 2011, 13:02
A) Why this post?

Since it seems that Megui has several restrictions I wanted to encode directly with x264.exe and xvid_encraw.exe for quite some time now, but for say real I had just no clue how to begin with.
I'm totally aware that there are a bunch of better batch-scripts out there, but: for non-programmers they are really hard to understand and modify the way some noob like me wants to.
So with much help of sneaker_ger (even if he maybe not really be aware of it) I finally managed to get what I wanted (well, there is one problem left and I hope you can help me troubleshooting) and this in a manner any other may understand it and can simple adjust it to his own encoding settings.


B) Needed stuff

- x264
- eac3to
- mp4box
- xvid_encraw
- lame
- ffmpeg
- your original videofiles (in the example below "video.mkv") and your corresponding avisynthscripts ("video.avs")

If you worked so far like me with Megui you'll find pretty all of it in C:\Programs\Megui\tools. The only thing missing is eac3.to.


C) Creating the batch file

First open your Editor (I don't know if it works with Notepad since it seems Notepad is saving some things like " in a way that won't work with batch files) save it as videoencode.bat and copy/paste the following text:

@ECHO OFF

C:\Programs\Videoencodingtools\x264\x264 --pass 1 --bitrate 1500 --ref 5 --direct auto --b-adapt 2 --partitions p8x8,b8x8,i8x8,i4x4
--subme 8 --me umh --sar 1:1 --stats "C:\MyVideos\Temp\mp4\video.log" --output NUL "C:\MyVideos\video.avs"

C:\Programs\Videoencodingtools\x264\x264 --pass 2 --bitrate 1500 --ref 5 --direct auto --b-adapt 2 --partitions p8x8,b8x8,i8x8,i4x4
--subme 8 --me umh --stats "C:\MyVideos\Temp\mp4\video.log" --sar 1:1 --output "C:\MyVideos\Temp\mp4\video.264" "C:\MyVideos\video.avs"

C:\Programs\Videoencodingtools\eac3to\eac3to.exe "C:\MyVideos\video.mkv" "C:\MyVideos\Temp\mp4\audiostream.aac" -quality=0.5

C:\Programs\Videoencodingtools\Megui\tools\mp4box\mp4box -add C:\MyVideos\Temp\mp4\video.264:fps=23.976 -add C:\MyVideos\Temp\mp4\audiostream.aac C:\MyVideos\video-muxed.mp4

ECHO Y | DEL C:\MyVideos\Temp\mp4\*.*


C:\Programs\Videoencodingtools\xvid_encraw\xvid_encraw -pass1 "C:\MyVideos\Temp\video.log" -qtype 0 -nopacked
-bvhq -max_bframes 1 -threads 4 -i "C:\MyVideos\video.avs" -o NUL

C:\Programs\Videoencodingtools\xvid_encraw\xvid_encraw -pass2 "C:\MyVideos\Temp\video.log" -bitrate 1000 -qtype 0 -nopacked -bvhq
-max_bframes 1 -imin 1 -bmin 1 -pmin 1 -threads 4 -i "C:\MyVideos\video.avs" -avi "C:\MyVideos\Temp\videostream.avi"

C:\Programs\Videoencodingtools\ffmpeg\ffmpeg -i C:\MyVideos\video.mkv -vn -f wav - | C:\Programs\Videoencodingtools\lame\lame.exe -m j -b 128 -q 2 - C:\MyVideos\Temp\audiostream.mp3

C:\Programs\Videoencodingtools\ffmpeg\ffmpeg -i C:\MyVideos\Temp\videostream.avi -vcodec copy -i C:\MyVideos\Temp\audiostream.mp3 -acodec copy -f avi C:\MyVideos\video-muxed.avi


ECHO Y | DEL C:\MyVideos\Temp\XVid\*.*

C:\Windows\system32\shutdown.exe -s -t 60 -c "PC will shutdown in 1 minute"

EXIT

Save it after adjusting the paths, input and output filenames and specification of your own settings for x264.exe and xvid_encraw. This is only an example, so just make it better :P.
Every time you want to encode something rightclick on videoencode.bat and [edit] it. Don't click on [open] since this will only start running the batch.


D) Explanations

The example above will create from the original video.mkv with the corresponding video.avs first an h.264/aac-mp4 and then from the same file a XVid/mp3-avi and then shutdown your pc (I wanted this for batch-encoding over night. If you don't want it, delete this line). On a sidenote: "ECHO Y" won't work if your language settings aren't english. So e.g. for german OS you have to replace it with "ECHO J" (J stands for "Ja"). This will delete the workfiles in your temp directory. If you're just erasing it because you don't know your the syntax of your language settings it maybe you'll be asked for every file to overwrite it. In this case just give every workfile another name (e.g. video1.log, video2.log, etc.). Only important if you want to encode more than one file.
Principally you can now just copy/paste the part you need as often as you want, so if you want to encode three different mkvs to avi, just erase the first five lines and copy the XVid-part two more times (don't forget to change the names of the avisynth-scripts and original videofilenames, though). So have fun testing and playing around with it.

D) Problem [solved]
The problem of changing framerate of video by muxing at the mp4-encode could be solved by adding :fps=23.976 to the mp4box-command.

sneaker_ger
25th January 2011, 13:36
mkvmerge assumes 25 fps for elementary streams by default. Make x264 output to Matroska instead.

Things I noticed:
1. Settings both -vn and -r makes no sense
2. File extension for the mkv is set to ".mp4"
3. faac is generally not recommended, as Nero and Apple have better encoders. /edit: nvm, just saw you already mentioned it
4. You can vastly increase the compatibility of the mkv with standalone players if you deactivate compression for all video and audio tracks (see mkvmerge doc, valid for mkvmerge >= 4.1.0 - older versions don't need the extra setting)
5. You might want to use -V (allowed values range from 0 to 10) for vbr instead of -b 128, which will offer a higher quality than cbr

P.S.: DirectShowSource can also use fractions for the fps parameter: fps=24/1.001

Iznogood
25th January 2011, 14:46
Thanks for your fast reply.

ad 1: fixed (the -r 30000/1001 was a rest of testing with the changed framerate, sorry. But I guess -vn =disable video was correct, or?)
ad 2: I don't understand that. I am actually converting the mkv to mp4 so the output file has to be mp4, or not (at least it wouldn't make sense to convert a mkv to mkv I guess)?
ad 3: Yes, I know that faac is not the best solution. I tried it with C:\Programs\Videoencodingtools\ffmpeg\ffmpeg -i C:\MyVideos\video.mkv -vn -f wav - | C:\Programs\Videoencodingtools\neroAacEnc.exe -ignorelength -br 128000 -lc -if --of "C:\MyVideos\Temp\mp4\audiostream.aac" (at least I guess that was it since I overwrote it with the faac-thing after I only got error warnings from Nero). So yeah, I'm working on a better solution but at least the example above is working for the moment.
ad 4: I don't really need the mkv suitable for standalone players (actually the mkv is only my input file, I guess you mean the output mp4-file). mkv/mp4 can and should simply have the best available quality, for standalones I'll reencode to avi (I guess that's the only right to exist for avis nowadays anyway...). Just to point it out: the example above is just the base to work on. Once I get a working result (meaning no change to the original fps) I'll sureley will change the encoding settings that fits better for maybe every single encode.
ad 5: actually I thought changing from CBR to VBR will only increase problems of async A/V for the mp4-output, so I didn't change it. But I will test it for the mp4-encode. For the XVid-encode compatibility is more important as qualitity to me, see explanation ad 4.

Yeah, I know everyone is saying that DirectShowSource is not good, but actually I'm not sure how to replace it. avi has avisource and so far I guess to have understood that the best way to import mkv/mp4 source is DGDecode. But so far I only used it for DGIndexed files so I'm absolutely not sure about the syntax.

sneaker_ger
25th January 2011, 15:07
I was already starting to wonder why you want to convert mkv to mkv. Your attempts have to fail because mkvmerge can not create mp4 files.

Before I suggest you a different script:
1.) Do you really need to reencode the video track? Are you aiming for a lower file size?
2.) What format is the sound track?

Iznogood
25th January 2011, 15:40
mkvmerge cannont create mp4 files? Well so far I used mkvmerge Gui for over one year to mux my h264-videos and aac-audio to mp4 so I didn't realize that the CLI cannot do that, sorry (even Megui uses mkvmerge to mux the streams to mp4). In that case I guess I'll just have to mux it with ffmpeg like the avi, though.

Well actually I don't want to stick with one input-format. The batch should simply be able to convert input files like ts, mkv and mp4 (with ac3, aac, lame, ogg audio) and put h264/aac-mp4 or XVid/mp3-avi (only for standalones) out. So if I'm right the only problem now is the mkvmerge.

edit: Actually an avisynthscript importing mkv/mp4-videofiles should begin with:
LoadPlugin("F:\Programme\ffmpegsource\ffms2.dll")
FFVideoSource("C:\MyVideos\video.mkv")

sneaker_ger
25th January 2011, 16:30
I assure you that you have not created mp4 files with mkvmerge or mkvmerge GUI, it is simply not possible. MeGUI uses mp4box for mp4 muxing.

Here's my suggestion for a proper script:

c:\somepath\eac3to\eac3to.exe "C:\MyVideos\video.mkv" "C:\MyVideos\Temp\mp4\audiostream.aac" -quality=0.4

C:\Programs\Videoencodingtools\x264\x264 --pass 1 --bitrate 1500 --ref 5 --direct auto --b-adapt 2 --partitions p8x8,b8x8,i8x8,i4x4
--subme 8 --me umh --sar 1:1 --stats "C:\MyVideos\Temp\mp4\video.log" --output NUL "C:\MyVideos\video.mkv" --force-cfr --acodec none

C:\Programs\Videoencodingtools\x264\x264 --pass 2 --bitrate 1500 --ref 5 --direct auto --b-adapt 2 --partitions p8x8,b8x8,i8x8,i4x4
--subme 8 --me umh --stats "C:\MyVideos\Temp\mp4\video.log" --sar 1:1 --output "C:\MyVideos\video-muxed.mp4" "C:\MyVideos\video.mkv" --force-cfr --acodec copy --audiofile "C:\MyVideos\Temp\mp4\audiostream.aac"

Notes:
- you need to copy neroaacenc.exe into the eac3to (http://forum.doom9.org/showthread.php?t=125966) folder
- you need the patched x264 (http://www.cs.helsinki.fi/u/jeekstro/x264/1867/) which includes audio muxing
- I have changed the x264 input to the mkv file. You don't need any avisynth script for simple reencoding jobs. If you want to do AviSynth filtering you may of course change the input back to an .avs file.
- I have specified "--force-cfr" because it's more compatible with devices. If you experience any a/v sync issues you can remove the parameter. (i.e.: files with a variable framerate)

This should work for mkv and ts input files. Eac3to cannot open mp4 files, so you would have to use a different method then. (Like avisynth->wavi (http://www.mediafire.com/?nb76wyvje6582ly)->neroaacenc or ffmpeg->neroaacenc)

If your input is already h.264+aac you can simply remux using this patched ffmpeg build (http://sourceforge.net/projects/direct264/files/Related%20Programs/ffmpeg%20(demuxer_muxer%20only)/).

You can take that as a starting point for automating it further. If you want to use AviSynth all the way I can recommend the ffmpegsource (http://code.google.com/p/ffmpegsource/) plugin, which can open a variety of video and audio formats and does not rely on DirectShow.

Iznogood
25th January 2011, 23:07
Okay, thanks again for all of your help. Since I didn't want to install a patched Version of x264 I tried to get along with the tools Megui gifted me and now it works. And yes I'll stick with avisynth since it's easier for me to do resizing and cutting. This batch shall only be a help for those like me that know what they want to change in settings for xvid-encraw or x264 but just fear typing in shells. For noobs like me it's just easier to make changes in text(=batch)files than use direct command lines. Thanks again, that was really helpful, at least for me.