AltairAC
AltairAC

Reputation: 171

Static image and a folder of mp3's?

I want to create a bunch of videos consisting of an single image which is shown throughout the whole video but each video has a different audio file. I can do it manually with various tools but the problem is that I have a lot of audio files and I can't optimize the frame rate (more on that later) and it takes a lot of time to do it that way but ffmpeg offers everything I need but the problem is that I don't know how to batch process everything.

The basic code:
ffmpeg -i song-name.mp3 -loop 1 -i cover.jpg -r frame-rate -t song-length -acodec copy output.mp4

What I want to achieve:
Let's say that I have a folder which consists of several audio files: song-name-1.mp3, song-name-2.mp3, ..., song-name-n.mp3 and cover.jpg.

I need a batch file which takes the name of every mp3 file in a folder (a FOR loop I suppose) and processes it with the same command:
ffmpeg -i song-name.mp3 -loop 1 -i cover.jpg -r frame-rate -t song-length -acodec copy output.mp4

So the image is always the same for every video. The song length can be taken with the tool mp3info and the corresponding command:
mp3info.exe -p %S song-name.mp3

Since I only have one image throughout the whole video, the optimal frame rate would be the inverse of the video length which is 1/length (where length is a variable in seconds which we get from mp3info).

So the final code should look something like this:
ffmpeg -i song-name.mp3 -loop 1 -i cover.jpg -r 1/length -t length -acodec copy song-name.mp4

Where "song-name" is a variable which changes for every iteration of the FOR loop (i.e. for every audio file in the folder) and length is a variable whose value we get with the command:
mp3info.exe -p %S song-name.mp3

I found examples of a FOR loop to fetch all file names of all mp3's in a specific folder but I do not know how to integrate mp3info. I hope that somebody can help me and I have some knowledge of the C programming language if that can be used in any way.

Upvotes: 1

Views: 557

Answers (1)

foxidrive
foxidrive

Reputation: 41257

Here's the edited simplified version without the VBS math.

The reason %%S is used is that % is a special batch character used for %environment% variables and in forINdo loops and to get a single one in a forINdo command it has to be doubled. Similarly echo %% will echo a single percent sign.

@echo off
for %%a in (*.mp3) do (
for /f "delims=" %%b in ('mp3info.exe -p %%S "%%a"') do (
ffmpeg -i "%%a" -loop 1 -i "cover.jpg" -r 1 -t %%b -acodec copy "%%~na.mp4"
)
)

Upvotes: 2

Related Questions