Page 1 of 1

Combining Single Video With Multiple Audio Files Using FFmpeg

Posted: 09 Mar 2021 18:06
by biont
Folks, I hope I came to the right forum. I have very basic knowledge of batch files, and somebody here might actually enjoy helping me with a concept I have. Much greatful in advance! :D

Here's my idea. I have a video file that needs to be combined with all mp3 files in a specific folder, creating multiple instances of that same video, but with different audio tracks. The combining happens thanks to FFmpeg module that is able to take any video and glue it with any sound. The previous script that I used only combined video and audio files based on the same name. That means a video can only be combined with one audio that has exactly the same name as video, which isn't what I want this time. The previous code was this:

Code: Select all

set	CONTENT=C:\MyProject
set	PROGRAM=%ProgramFiles%\ffmpeg\bin\ffmpeg.exe

for	%%a IN ("%CONTENT%\SOURCE\*.mp4") ^
do	"%PROGRAM%" -i "%%a" -i "%PROGRAM%\SOURCE\%%~na.mp3" -map 0:v -map 1:a -c ^
copy	"%CONTENT%\YOUTUBE\%%~na.mp4"
As soon as filename.mp4 a filename.mp3 were found in SOURCE folder, they would be combined and copied to filename.mp4 under YOUTUBE folder. Perfect! But not for my current goal. I now need to create an automatic task that combines filename.mp4 with filename1.mp3, filename2.mp3, filename3.mp3, etc.

Actually, the perfect representation of this concept would be: if I have two video files called rabbit.mp4 and elephant.mp4, and a bunch of rabbits and elephants in mp3 (each followed by a consecutive number), then running this batch would create as many rabbits and elephants, as there are mp3 files (with a correct video track, of course). And the resulting filenames would be the same as the mp3 files used.

It would be super cool if this batch could consider just the letters (and ignore the numeric differences) to combine rabbit vids with rabbit audios, and elephant vids with elephant audios. If it's impossible to do, I could settle for splitting rabbits and elephants by folders. Basically, coding a batch that takes an mp4 file and with a help of FFmpeg combines it with each mp3 file in some folder, then outputs the resulting mp4 file under the name of mp3 file.

rabbit.mp4
rabbit1.mp3
rabbit2.mp3
rabbit3.mp3

RUN BATCH

rabbit1.mp4
rabbit2.mp4
rabbit3.mp4

Is this something that .BAT file can handle? Again, thanks a ton for claryfying this to me!

Re: Combining Single Video With Multiple Audio Files Using FFmpeg

Posted: 10 Mar 2021 08:44
by Squashman
What is stopping you from nesting another FOR command to iterate the MP3 files?

Re: Combining Single Video With Multiple Audio Files Using FFmpeg

Posted: 10 Mar 2021 11:26
by biont
Squashman wrote:
10 Mar 2021 08:44
What is stopping you from nesting another FOR command to iterate the MP3 files?
Well, this leaves me then with a more fixed approach where I need those mp3 files to be of a certain name, right? As I said, I have very basic idea of how to construct batches, so if I understand you correctly, you suggest I turn my batch into something like:

take filename.mp4 combine with filename01.mp3 output to filename01.mp4
take filename.mp4 combine with filename02.mp3 output to filename02.mp4
take filename.mp4 combine with filename03.mp3 output to filename03.mp4... etc

Is this your idea? If it is, then it's not as flexible as I would dream for this batch to be - basically, combining filename.mp4 with filename??.mp3 until there are no more mp3's left in the specified folder.

Oh, wait, or did you mean putting FOR infront of the current FOR? But, like, how do you tell this batch to name output mp4 files the same as mp3 files it encounters? Please clarify.

Re: Combining Single Video With Multiple Audio Files Using FFmpeg

Posted: 10 Mar 2021 21:44
by Squashman
It would help if you would fix your existing code in your original post. I am doubting that it runs successfully.

This looks incorrect
"%PROGRAM%\SOURCE\%%~na.mp3"
The variable program is the full path to the executable, so that cannot be correct.

Re: Combining Single Video With Multiple Audio Files Using FFmpeg

Posted: 11 Mar 2021 21:42
by Squashman
I am not sure why you think a nested FOR command would not work. For the sake of argument I used a FOR /F to parse the output of the DIR and FINDSTR commands so that it would only find MP3 files with the base file name of the MP4 and has numbers in the MP3 file name.

Based on your description and example I used the following file names.

Code: Select all

Elephant.mp4
Elephant12.mp3
rabbit.mp4
rabbit01.mp3
rabbit2.mp3
I created the following code with nested FOR commands.

Code: Select all

@echo off
set CONTENT=C:\MyProject
set PROGRAM=%ProgramFiles%\ffmpeg\bin\ffmpeg.exe

for %%G IN ("%CONTENT%\SOURCE\*.mp4") do (
	FOR /F "delims=" %%H IN ('dir /b "%CONTENT%\SOURCE\%%~nG*.mp3" ^|findstr /IRC:"%%~nG[0-9]*.mp3"') DO (
		ECHO "%PROGRAM%" -i "%%G" -i "%CONTENT%\SOURCE\%%H" -map 0:v -map 1:a -c copy "%CONTENT%\YOUTUBE\%%~nH.mp4"
	)
)
This produced the following output.

Code: Select all

"C:\Program Files\ffmpeg\bin\ffmpeg.exe" -i "C:\MyProject\SOURCE\Elephant.mp4" -i "C:\MyProject\SOURCE\Elephant12.mp3" -map 0:v -map 1:a -c copy "C:\MyProject\YOUTUBE\Elephant12.mp4"
"C:\Program Files\ffmpeg\bin\ffmpeg.exe" -i "C:\MyProject\SOURCE\rabbit.mp4" -i "C:\MyProject\SOURCE\rabbit01.mp3" -map 0:v -map 1:a -c copy "C:\MyProject\YOUTUBE\rabbit01.mp4"
"C:\Program Files\ffmpeg\bin\ffmpeg.exe" -i "C:\MyProject\SOURCE\rabbit.mp4" -i "C:\MyProject\SOURCE\rabbit2.mp3" -map 0:v -map 1:a -c copy "C:\MyProject\YOUTUBE\rabbit2.mp4"

Re: Combining Single Video With Multiple Audio Files Using FFmpeg

Posted: 11 Mar 2021 23:00
by biont
Squashmaaaan, thank you, thank you, THANK YOU!! You have totally made my day! It's not like I "thought" that nested FOR would not work. It's more like I said in my first message - I have very basic knowledge of batch files - basically, I'm dumb at this stuff and thanks to you my idea has just become a reality. This code of yours - it's a real gem, man! Thanks a lot, this forum is awesome! :D

P.S. Yeah, when I edited the script for this forum, I made this little mistake that you've spotted.

P.P.S. Oh, one last thing. The idea just came to mind: what would I have to add to this script in order for each mp3, that has been combined with the mp4, to be automatically moved to a specified folder, say Library for example?

This is the current version:

Code: Select all

@echo off

set	CONTENT=A:\BAUS
set	PROGRAM=A:\BAUS\ffmpeg\bin\ffmpeg.exe

for %%G in ("%CONTENT%\SOURCE\*.mp4") do (
	for /F "delims=" %%H in ('dir /b "%CONTENT%\SOURCE\%%~nG*.mp3" ^|findstr /IRC:"%%~nG-[0-9]*.mp3"') do (
		"%PROGRAM%" -i "%%G" -i "%CONTENT%\SOURCE\%%H" -map 0:v -map 1:a -c copy "%CONTENT%\YOUTUBE\%%~nH.mp4"
	)
)

pause
Does it have any redundant info, by the way? I was wondering if this little guy (^) needs to be there? I put it in my previous script to keep the lines of code a bit shorter (Googled it up, of course), but here it's in the middle of the line. And with my level of knowledge I'm like :shock: :D

Re: Combining Single Video With Multiple Audio Files Using FFmpeg

Posted: 12 Mar 2021 00:04
by Squashman
biont wrote:
11 Mar 2021 23:00
P.P.S. Oh, one last thing. The idea just came to mind: what would I have to add to this script in order for each mp3, that has been combined with the mp4, to be automatically moved to a specified folder, say Library for example?
Ironically the command to do that is called move. Open up a command prompt and type: move /? to read the help file
biont wrote:
11 Mar 2021 23:00
was wondering if this little guy (^) needs to be there?
Remove it and see what happens.

Re: Combining Single Video With Multiple Audio Files Using FFmpeg

Posted: 12 Mar 2021 01:08
by biont
Okay, I've added

Code: Select all

move "%CONTENT%\SOURCE\%%H" "%CONTENT%\AUDIO"
before the closing brackets. Works fine, if the folder is there. If not, it's a mess - mp3's are just copied into a file AUDIO over and over again, they get destroyed. Anyway to make this fool proof in case someone renames the folder?

Re: Combining Single Video With Multiple Audio Files Using FFmpeg

Posted: 12 Mar 2021 09:21
by Squashman
biont wrote:
12 Mar 2021 01:08
Okay, I've added

Code: Select all

move "%CONTENT%\SOURCE\%%H" "%CONTENT%\AUDIO"
before the closing brackets. Works fine, if the folder is there. If not, it's a mess - mp3's are just copied into a file AUDIO over and over again, they get destroyed. Anyway to make this fool proof in case someone renames the folder?
Use the make directory command to create the directory. If the directory already exists you get a small error message but that can be suppressed by redirecting error messages to nul.

Code: Select all

md "%CONTENT%\AUDIO" 2>nul
Next time you have a command prompt open, type: HELP and begin to familiarize yourself with all the commands you have access to.