Execute a ffmpeg command for each mp3 file in a folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MrNiceButDim
Posts: 5
Joined: 26 Jan 2024 15:52

Execute a ffmpeg command for each mp3 file in a folder

#1 Post by MrNiceButDim » 28 Jan 2024 07:33

I have this command which works fine

ffmpeg -i "concat:felica.mp3|4sec.mp3|felica.mp3" -acodec copy felica4secfelica.mp3

What is it? It's a pronunciation audio + 4 seconds silence + a repeat of the audio.

I also have this code

dir /b /a-d *.mp3 > mp3file.txt

Which collects the names of all mp3 files in the folder

What I need is a code body to count the number of lines in mp3files.txt and use that number to execute the ffmpeg command for every mp3-file in the folder. (That will of course include that 4sec-file but that's not a problem)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Execute a ffmpeg command for each mp3 file in a folder

#2 Post by aGerman » 28 Jan 2024 09:04

You're about making it an XY problem. Neither do you need to write a text file, nor do you ever need to know the number of files. Just process the output of DIR directly, preferably with a filter that excludes file names containing "4sec".
Give that a go:

Code: Select all

@echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.mp3^|findstr /vc:"4sec"') do ECHO ffmpeg -i "concat:%%~i|4sec.mp3|%%~i" -acodec copy "%%~ni4sec%%~i"
PAUSE
This only prints the ffmpeg commands. If you think it would do what you intend, remove the ECHO command to actually execute the commands listed before.

Steffen

MrNiceButDim
Posts: 5
Joined: 26 Jan 2024 15:52

Re: Execute a ffmpeg command for each mp3 file in a folder

#3 Post by MrNiceButDim » 28 Jan 2024 10:02

Steffen, Du bist ein Mensch.

I've been looking for the answer to this problem for over a week, and you gave it to me in no time.

Don't know if line breaks are allowed in bat files, but with or without them could you break down the code and explain the most important parts?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Execute a ffmpeg command for each mp3 file in a folder

#4 Post by aGerman » 28 Jan 2024 11:55

Code: Select all

@echo off
Don't show the prompt and don't repeat the command.

Code: Select all

setlocal
Not necessary. Just one of my defaults to avoid variables to be predefined if I run scripts from a cmd prompt for debugging reasons.

Code: Select all

for /f "delims=" %%i in 
FOR /F processes streams line by line. It usually devides a line into tokens. "delims=" overrides the default delimiters and, thus, no tokenization is performed which leads to the entire line being saved in the FOR variable %%i in each iteration.
Run FOR /? in a cmd prompt to get more context from the help message.

Code: Select all

('dir /b /a-d *.mp3^|findstr /vc:"4sec"')
The lines outputted by these commands are the stream that the FOR loop processes. The DIR command outputs all *.mp3 files in the current working directory. This output is forwarded to FINDSTR that removes all lines containing "4sec".
Run FINDSTR /? in a cmd prompt and read about options /v and /c.

Code: Select all

do ffmpeg -i "concat:%%~i|4sec.mp3|%%~i" -acodec copy "%%~ni4sec%%~i"
Executed command for each line in %%i where %%~i removes surrounding quotes (if any) and %%~ni expands only to the file name (without extension .mp3). Again refer to the help message of FOR to get more information about all the tilde-prefixed modifiers.

Steffen

MrNiceButDim
Posts: 5
Joined: 26 Jan 2024 15:52

Re: Execute a ffmpeg command for each mp3 file in a folder

#5 Post by MrNiceButDim » 28 Jan 2024 14:01

Steffen, you're a Super-Mensch!

I may finally be able to understand some of the inner workings of this mysterious DOS command world.

The fun thing is that I used to write Java programs, so the code shouldn't be that difficult for me to understand, but the syntax is quite different, so I was left stumped.

Once again, thank you, and in the words of the great philosopher Arnold Schwarzenegger. I'll be back!

MrNiceButDim
Posts: 5
Joined: 26 Jan 2024 15:52

Re: Execute a ffmpeg command for each mp3 file in a folder

#6 Post by MrNiceButDim » 31 Jan 2024 14:07

And I'm back!

Slight hick-up. The audio files are pronunciation of Esperanto words, some of which has diacritics like ĝ or ŭ. This causes the batch file to complain
No such file or directory.

Any way to fix this?

Squashman
Expert
Posts: 4485
Joined: 23 Dec 2011 13:59

Re: Execute a ffmpeg command for each mp3 file in a folder

#7 Post by Squashman » 31 Jan 2024 14:44

I would think your code page would be set properly considering you are in the EU. Would be interesting to see what your code page is currently set to. You can type chcp at a command prompt to get that information. You can change the code page with the chcp command as well. You could try 65001.

MrNiceButDim
Posts: 5
Joined: 26 Jan 2024 15:52

Re: Execute a ffmpeg command for each mp3 file in a folder

#8 Post by MrNiceButDim » 31 Jan 2024 15:00

My code page was set to 850.

I changed to 65001 as per your recommendation, and now the bat file runs smoothly again.

Until the next stumbling block...

Post Reply