Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Video Encoding > MPEG-4 AVC / H.264

Reply
 
Thread Tools Search this Thread Display Modes
Old 13th March 2019, 05:58   #1  |  Link
Lathe
Registered User
 
Lathe's Avatar
 
Join Date: Aug 2005
Posts: 1,100
VERY simple batch x264 script for all files in folder...

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!
Lathe is offline   Reply With Quote
Old 13th March 2019, 10:07   #2  |  Link
sneaker_ger
Registered User
 
Join Date: Dec 2002
Posts: 5,565
Code:
FOR /R "D:\x" %%
is missing a letter for the variable after the %%. ("%%" instead of "%%i")


Quote:
Originally Posted by Lathe View Post
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:
Code:
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"

Quote:
Originally Posted by Lathe View Post
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:
Code:
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"
)

Last edited by sneaker_ger; 13th March 2019 at 10:15.
sneaker_ger is offline   Reply With Quote
Old 13th March 2019, 12:45   #3  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
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"
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???
StainlessS is offline   Reply With Quote
Old 13th March 2019, 13:07   #4  |  Link
sneaker_ger
Registered User
 
Join Date: Dec 2002
Posts: 5,565
Quote:
Originally Posted by StainlessS View Post
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.

sneaker_ger is offline   Reply With Quote
Old 13th March 2019, 13:39   #5  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
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.
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???
StainlessS is offline   Reply With Quote
Old 13th March 2019, 20:40   #6  |  Link
Lathe
Registered User
 
Lathe's Avatar
 
Join Date: Aug 2005
Posts: 1,100
Quote:
Originally Posted by sneaker_ger View Post
Code:
FOR /R "D:\x" %%
is missing a letter for the variable after the %%. ("%%" instead of "%%i")



It's very simple with ffmpeg:
Code:
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:
Code:
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 is offline   Reply With Quote
Old 13th March 2019, 20:42   #7  |  Link
Lathe
Registered User
 
Lathe's Avatar
 
Join Date: Aug 2005
Posts: 1,100
Quote:
Originally Posted by StainlessS View Post
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 is offline   Reply With Quote
Old 13th March 2019, 20:49   #8  |  Link
Lathe
Registered User
 
Lathe's Avatar
 
Join Date: Aug 2005
Posts: 1,100
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 is offline   Reply With Quote
Old 13th March 2019, 20:55   #9  |  Link
Lathe
Registered User
 
Lathe's Avatar
 
Join Date: Aug 2005
Posts: 1,100
Okay, I ran the code above with the substitutions as shown, and this is what resulted:

Lathe is offline   Reply With Quote
Old 13th March 2019, 21:20   #10  |  Link
sneaker_ger
Registered User
 
Join Date: Dec 2002
Posts: 5,565
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). 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.

Last edited by sneaker_ger; 13th March 2019 at 21:29.
sneaker_ger is offline   Reply With Quote
Old 13th March 2019, 21:48   #11  |  Link
Lathe
Registered User
 
Lathe's Avatar
 
Join Date: Aug 2005
Posts: 1,100
Quote:
Originally Posted by sneaker_ger View Post
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). 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 is offline   Reply With Quote
Old 13th March 2019, 22:35   #12  |  Link
Lathe
Registered User
 
Lathe's Avatar
 
Join Date: Aug 2005
Posts: 1,100
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 is offline   Reply With Quote
Old 13th March 2019, 22:36   #13  |  Link
Lathe
Registered User
 
Lathe's Avatar
 
Join Date: Aug 2005
Posts: 1,100
It LOOKS like after each '%~a' it says 'more?' I'm guessing that means something
Lathe is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 16:17.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.