Log in

View Full Version : VERY simple batch x264 script for all files in folder...


Lathe
13th March 2019, 05:58
Okay, I promise I've been searching and trying to find a simple script just to take a bunch of tele episodes that I want to reduce in size and re-encode them all. It's been about 2 hrs and I keep coming upon stuff that is WAY more complex than I need.

Here is the simple script I want to use for all the MKV files in a folder that are roughly about 500 megs each and I wish to reduce them to anywhere around 50-150 megs, which this script has been generating for the individual files:

F:\EXECUTABLES\BD-RBV05023\BD_Rebuilder\Tools\x264L.exe "C:\x\00000.mkv" --preset veryfast --crf 24 --output "C:\x\Encode.mkv"

I'm just using the x264.exe that JDobbs has in his BDRB folder.

Now, the closest I got was some guy gave a script similar to this to 'automate' it so that it would encode all the files in a folder and place the resulting files in another folder. I tried running it, but since I do not understand all the commands, of course I got errors. here is what he posted, or something like this:

FOR /R "C:\input-folder" %%i IN ("*.*") DO "C:\Program Files\HandBrake\HandBrakeCLI.exe" -i "C:\input-folder\%%~ni.avi" -t 1 -c 1 -o "D:\output-folder\%%~ni.mkv" -f mkv --strict-anamorphic -e x264 -S 160 -2 -a 1 -E lame -6 stereo -R Auto -B 112 -D 0.0 -x ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0 -v 1

Now, he is using Handbrake, which I don't need, so I substituted my 264.exe file instead. I also did not need all those encoding parameters, so I TRIED to take what I THOUGHT was unneeded out (clearly I didn't do it right :)) I sort of ended up with this:

FOR /R "D:\x" %% IN ("*.*") DO "F:\EXECUTABLES\BD-RBV05023\BD_Rebuilder\Tools\x264L.exe" "D:\x\%%~ni.mkv" --output "D:\output-folder\%%~ni.mkv" --preset veryfast --crf 24 -v 1

But, of course it didn't work. The first error listed in the CMD line was it didn't recognize %%. And, then it couldn't open the input MKVs as listed. And, I have NO bloody idea what that command is and I do NOT know what the 'v 1' at the end it either, but I left it in because it was after the encode parameters he was using.

So, all I really need is a VERY VERY simple way to take the extremely simple re-encode script that I am using above and be able to re-encode all the MKV files in one folder and output the re-encoded MKV files to another folder.

That's it!

There HAS to be a LOT easier way than this :)

Now, if there is an EASY way for the existing audio to be remuxed with the re-encoded file as it processes, well, that would be nice. Otherwise I will simply manually remux the audio of each file when the video re-encodes are done using MKVMerge. But, IF that can easily be automated too, then great! As long as it doesn't get too complicated for me...

I very much appreciate the help! A batch file would be fine that I could simple click on after dumping whatever MKV files into the input folder that I want to re-encode.

Thanks kindly! http://lathe-of-heaven.com/tiphat.gif

sneaker_ger
13th March 2019, 10:07
FOR /R "D:\x" %%
is missing a letter for the variable after the %%. ("%%" instead of "%%i")


Now, if there is an EASY way for the existing audio to be remuxed with the re-encoded file as it processes
It's very simple with ffmpeg:
for %%a in (*.mkv) do ffmpeg -i "%%~a" -map 0 -c:v libx264 -preset veryfast -crf 24 -c:a copy -c:s copy "output_folder\%%~a"


Otherwise I will simply manually remux the audio of each file when the video re-encodes are done using MKVMerge.
You don't need to do this with ffmpeg but just FYI you can use multiple lines in a for loop like:
for %%a in (*.mkv) do (
x264 "%%~a" --preset veryfast --crf 24 -o "temp_folder\%%~a"
mkvmerge -o "output_folder\%%~a" "temp_folder\%%~a" --no-video "%%~a"
)

StainlessS
13th March 2019, 12:45
Also, if you have a general EXE folder for your tools, put it in the PATH environment variable, is lots easier than giving ultra long paths in command lines.
EDIT: Separate items in the PATH with Semi Colon ';', eg "C:\BIN;D:\BIN"

sneaker_ger
13th March 2019, 13:07
Also, if you have a general EXE folder for your tools, put it in the PATH environment variable, is lots easier than giving ultra long paths in command lines.
EDIT: Separate items in the PATH with Semi Colon ';', eg "C:\BIN;D:\BIN"
Note: If you are on Windows 8 or older. On Windows 10 it's a list with one path per line.

https://i.stack.imgur.com/P80es.png

StainlessS
13th March 2019, 13:39
Cheers Sneaky, did not know that, but then I dont run Avs or similar on either of my two Win10 10" mini tablet/laptops (browse internet and nowt else really).
I'll set em up one day but not today, again cheers. :)

Lathe
13th March 2019, 20:40
FOR /R "D:\x" %%
is missing a letter for the variable after the %%. ("%%" instead of "%%i")



It's very simple with ffmpeg:
for %%a in (*.mkv) do ffmpeg -i "%%~a" -map 0 -c:v libx264 -preset veryfast -crf 24 -c:a copy -c:s copy "output_folder\%%~a"



You don't need to do this with ffmpeg but just FYI you can use multiple lines in a for loop like:
for %%a in (*.mkv) do (
x264 "%%~a" --preset veryfast --crf 24 -o "temp_folder\%%~a"
mkvmerge -o "output_folder\%%~a" "temp_folder\%%~a" --no-video "%%~a"
)

Thank you! I appreciate your taking the time to do that for me mate :)

So, if I copy your code above verbatim, substituting the name of the input and output folders only, I can run it as is to re-encode a folder of MKV files and it will keep and mux the existing audio? If I save the code above as a .bat file, can I then execute it that way by clicking on it?

Thanks!

Lathe
13th March 2019, 20:42
Also, if you have a general EXE folder for your tools, put it in the PATH environment variable, is lots easier than giving ultra long paths in command lines.
EDIT: Separate items in the PATH with Semi Colon ';', eg "C:\BIN;D:\BIN"

Yes, I am running W8, so that is also most helpful.

Thank you kindly! :)

Lathe
13th March 2019, 20:49
Oh, with the 2nd code above, wouldn't I also have to substitute the path of my x264.exe?

So, also substituting the path to my x264.exe, my existing input / output folders then, would the resulting code be this:

for %%a in (*.mkv) do (
F:\EXECUTABLES\BD-RBV05023\BD_Rebuilder\Tools\x264L.exe "%%~a" --preset veryfast --crf 24 -o "D:\x\%%~a"
mkvmerge -o "D:\x\output_folder\%%~a" "D:\x\%%~a" --no-video "%%~a"
)

Would that re-encode the MKV files AND mux the existing audio back into the output MKV files too?

Lathe
13th March 2019, 20:55
Okay, I ran the code above with the substitutions as shown, and this is what resulted:

http://lathe-of-heaven.com/CMD line.gif

sneaker_ger
13th March 2019, 21:20
In the cmd window you must use single "%" instead of "%%". "%%" is for batch files.


For the paths:
You have several options. You can set files and folders using their relative paths (relative to the working directory, in your sceenshot it's c:\windows\system32\) or their absolute paths. If you use relative paths for the executables it's easiest to put things into %PATH% like StainlessS said so you can call them from anywhere. It's not strictly necessary, just for convenience.

Personally, I usually open the cmd in the directory where the files I want to work on are. Usually I go into the folder using the Explorer, hold shift and right-click on an empty stop and select "open command prompt here" (with Windows 10 it's difficult (https://www.windowscentral.com/add-open-command-window-here-back-context-menu-windows-10)). Then working directory will be that folder instead of c:\windows\system32\.
I have ffmpeg, x264, mkvmerge and other vital tools in %PATH%.

Then I can just use the simple commands like my ffmpeg example without writing the long, absolute paths.

Lathe
13th March 2019, 21:48
In the cmd window you must use single "%" instead of "%%". "%%" is for batch files.


For the paths:
You have several options. You can set files and folders using their relative paths (relative to the working directory, in your sceenshot it's c:\windows\system32\) or their absolute paths. If you use relative paths for the executables it's easiest to put things into %PATH% like StainlessS said so you can call them from anywhere. It's not strictly necessary, just for convenience.

Personally, I usually open the cmd in the directory where the files I want to work on are. Usually I go into the folder using the Explorer, hold shift and right-click on an empty stop and select "open command prompt here" (with Windows 10 it's difficult (https://www.windowscentral.com/add-open-command-window-here-back-context-menu-windows-10)). Then working directory will be that folder instead of c:\windows\system32\.
I have ffmpeg, x264, mkvmerge and other vital tools in %PATH%.

Then I can just use the simple commands like my ffmpeg example without writing the long, absolute paths.

Ah, I see... Yes, I remember about opening the CMD prompt in the directory. I guess, just to make bloody SURE that this is as direct, linear, and SIMPLE as possible, for now I will try just to stay with the straight forward example that you gave me. So, now I will try to run the script exactly as it is above, but using the CMD window, I will use single % symbols. I will indeed though examine the further details later that you mention. I just wanna see if I can get the damn thing going first :)

Thank you!

Lathe
13th March 2019, 22:35
Hmmm... weird. I didn't seem to get any lengthy errors, but it didn't do anything. Here is the code I used in the CMD window:

for %a in (*.mkv) do (
F:\EXECUTABLES\BD-RBV05023\BD_Rebuilder\Tools\x264L.exe "%~a" --preset veryfast --crf 24 -o "D:\x\%~a"
mkvmerge -o "D:\x\output_folder\%~a" "D:\x\%~a" --no-video "%~a"
)

And, here is the result:

C:\Windows\system32>for %a in (*.mkv) do (
More? F:\EXECUTABLES\BD-RBV05023\BD_Rebuilder\Tools\x264L.exe "%~a" --preset ver
yfast --crf 24 -o "D:\x\%~a"
More? mkvmerge -o "D:\x\output_folder\%~a" "D:\x\%~a" --no-video "%~a"
More? )

C:\Windows\system32>

Lathe
13th March 2019, 22:36
It LOOKS like after each '%~a' it says 'more?' I'm guessing that means something :)