How to convert multiple mp3 files to mp4 with ffmpeg?

1

The following command takes the mp3, attaches an image to it, and converts it to an mp4:

ffmpeg -i input.mp3 -loop 1 -i image.png -c:a copy -c:v libx264 -shortest output.mp4

I'm trying to apply the same image to each mp3 in a folder. The operating system is windows 8 and the desired output for the file is mp4.

I need to do this for all the mp3s in a folder. How would I code this?

P.S. I'm also in need of the code that can attach an intro mp4 to multiple mp4 files in a folder.

Doc Chop

Posted 2015-07-08T21:38:51.683

Reputation: 31

Your PS may be better asked as a separate question. It may be a simple case of using the concat filter (for example); but that is based on certain assumptions and so it would be better asked separately :) – bertieb – 2015-07-08T21:51:20.040

Answers

0

  1. Create a file and save it with .bat extension
  2. Write the code below in it. (specify the right image file)
  3. Copy the batch file in the directory containing your .mp3 files.
  4. Assuming the path to ffmpeg.exe is set in your environment variables, execute the batch file
  5. Locate converted files in the created folder "converted".

@echo off
mkdir converted
for %%a in ("*.mp3") do ffmpeg -i "%%a" -loop 1 -i image.png -c:a copy -c:v libx264 -shortest "converted\%%~na.mp4"
pause

Kencer

Posted 2015-07-08T21:38:51.683

Reputation: 1

0

Assuming bog-standard Windows cmd.exe (ie no lovely cygwin, or Powershell, or <insert scripting language here>):

for %f in (*.mp3) do ffmpeg -i %f -loop 1 -i image.png -c:a copy -c:v libx264 -shortest %~nf.mp4

(also assumes not in a batch file; if that is the case the %s need doubled to %%)

Uses the fact that you can read in the current file (or directory, etc depending on what arguments you pass to for) to a single-character variable; which you can then get the basename of via %~n.

So, assuming your mp3s and image.png are in your Downloads folder as per your comment:

  1. Start a command prompt: <windows key>+r --> cmd --> <enter>
  2. Change to Downloads directory: cd Downloads
  3. Run the looped ffmpeg command: for %f in (*.mp3) do ffmpeg -i %f -loop 1 -i image.png -c:a copy -c:v libx264 -shortest %~nf.mp4
  4. (optional) Enjoy mp4 versions of your mp3s with the image

bertieb

Posted 2015-07-08T21:38:51.683

Reputation: 6 181

How do i specify where the files are with the code? Say the files are in my downloads folder. – Doc Chop – 2015-07-09T15:54:13.203

@DocChop Are you doing this interactively or from a batch file? If interactive: win+r -> cmd (ie start command prompt); cd Downloads; for %f in (*.mp3) do ffmpeg -i %f -loop 1 -i image.png -c:a copy -c:v libx264 -shortest %~nf.mp4 (assumes both the mp3s and the image are there). I've edited my answer to illustrate this process. – bertieb – 2015-07-09T15:57:28.633

0

You could do this sort of thing with using a scripting language. Here's a little Ruby script that should do the job, if you run it in the folder containing the .mp3 files and the image.png:

Dir.glob("*.mp3").each do |file|
    system("ffmpeg -i #{file} -loop 1 -i image.png -c:a copy -c:v libx264 -shortest output.mp4")
end

Kaathe

Posted 2015-07-08T21:38:51.683

Reputation: 101

Do you by chance have that in PHP? Ruby always seemed weird to me so I never took time to learn it. – Doc Chop – 2015-07-09T14:01:10.367

Sorry - I've never used PHP, so I'm not sure I can help with that. You should be able to write something similar to this in PHP though - list the files in the directory, make an ffmpeg command string for each, and then call system(). – Kaathe – 2015-07-09T14:43:48.343

I downloaded ruby interactive, and I assumed you just paste the code in and press enter but then I saw the DIR.glob and realized I'd have to specify the folder. How would I do this in ruby interactive or any program? – Doc Chop – 2015-07-09T14:51:14.120

You can use Dir.chdir to change the current working directory, before the call to Dir.glob. The documentation for Dir is here: http://ruby-doc.org/core-1.9.3/Dir.html

– Kaathe – 2015-07-09T18:03:11.010