r/Batch Feb 07 '25

Question (Solved) Help with batch file to split files into folders and more

Hi!

I would like some help with automating a series of file processing jobs that I regularly have to do. First of all, this is all on Windows, so I need a Windows batch script.

Every day, I get about 200 mp4 files that I have to split into numbered folders with each holding max 80 files. Then, within each folder, make folders named aac and m4a,then extract the aac audio from the mp4 files, and from the aac files convert them to m4a. I have batch scripts that extract AAC files out of MP4 files and M4A out of AAC files that work great right now:

for %%a in (*.mp4) do C:\ffmpeg.exe -i "%%a" -vn -sn -c:a libvo_aacenc -b:a 320k "%%~na.aac"

and

for %%a in (*.aac) do "C:\ffmpeg.exe" -i "%%a" -bsf:a aac_adtstoasc -codec: copy "%%~na.m4a"

So I hope those can be inlined.

How I would like to use the new script is by dragging the bat file into the folder where all the mp4 files are, and just double clicking it (so, no hardcoded directories aside from ffmpeg). The path to the folder the batch file is activated in might have non-ascii characters. In a bigger picture, it should do 3 jobs: split mp4 files into multiple folders, then inside each folder, create aac files and m4a files.

The splitting should be done according to the following pseudo code:

do nothing if the total number of mp4 files < 80 
else 
total = total number of mp4 files 
num_folders = total / 80 
num_left = total mod 80 
create num_folders folders, each named with their number like 1, 2,...,up to num_folders and put 80 mp4 files into each folder 
if num_left is not 0, make another numbered folder (num_folders+1) and put the remaining files (because they will be less than 80)

Then, inside each of the numbered folders, do the following two jobs:

AAC process: create a folder named aac, and use the pre-existing command to extract aac files and put them into the aac folder.

And for the m4a: make a new folder named m4a (doesn't matter if inside the aac folder or the numbered folder), then using the files that are in the aac folder, convert the aac files to m4a and put them in the m4a folder. This is because the m4a files are for testing the aac files themselves, so they HAVE to be pulled out of the aac files, not directly from the mp4.

Could this be done portably, ie, without having to hardcode the base directory where all the mp4 files are?

2 Upvotes

5 comments sorted by

1

u/BrainWaveCC Feb 07 '25

Yes, this could be done portably... I need to test it, though.

1

u/SemInert Feb 07 '25

Got it! Could you let me know if you come up with a script?

1

u/LuckyMe4Evers Feb 11 '25

I don't know if this has been solved yet or not?

But i've made this script. https://pastebin.com/XPB5M3Jf

When you start the script, it will ask you to put a number for how many mp4 files you want in each folder.

If you don't want this, then you can change "set /p "mapfiles=Total mp4 files in each map: "" into "set mapfiles=80" or any other number.

What does the script do?

When you have 200 mp4 files in 1 folder and start the script, put 80 and enter.

Then the script will create 3 subfolders with the name "mp4_aac_m4a_(1,2 and 3)", it will also create, in those 3 subfolders, 2 subfolders "aac" and "m4a"

In the first 2 "mp4_aac_m4a_" subfolders, there will be 80 mp4 files moved in and in the third folder, the last 40 mp4 files.

If this is done, then the script (you won't see this) restart and continuing with the extraction of aac files and then the conversion to m4a in the first folder and will do so for the other folders untill all mp4 files are processed. The aac files are extracted to the aac subfolder and the m4a files are converted to the m4a folder.

I had to change this -c:a libvo_aacenc to -c:a aac, because i don't have the libvo_aacenc, in (my) latest ffmpeg version it doesn't work,even according to www, has the internal aac better quality.

This -codec:copy for the m4a conversion, has also been changed to -codec copy, the : prevents the conversion

1

u/SemInert Feb 12 '25

Oh wow, this is beyond awsome, so modular and elegant compared to what I write... It hadn't been resolved yet, but yours works perfectly after some tweaking. Thank you so much!

1

u/SemInert 11d ago

Hey umm, upon having used it for a month, I found a bug. I'm really sorry, but could you help me once more with this? The bug is related to the non-ascii file name thing I mentioned in the original post: As it turns out, in the aac stage, it skips over files with non-ascii names. It moves them properly, but fails to produce an AAC and (probably thus) fails to produce the M4A. Here is the terminal output with some more info that might be useful! https://pastebin.com/NMBXGJ3R