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.

  • 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 Jul 8 '15 at 21:51

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")
  • Do you by chance have that in PHP? Ruby always seemed weird to me so I never took time to learn it. – Doc Chop Jul 9 '15 at 14:01
  • 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 Jul 9 '15 at 14:43
  • 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 Jul 9 '15 at 14:51
  • You can use Dir.chdir to change the current working directory, before the call to Dir.glob. The documentation for Dir is here: ruby-doc.org/core-1.9.3/Dir.html – Kaathe Jul 9 '15 at 18:03

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
  • How do i specify where the files are with the code? Say the files are in my downloads folder. – Doc Chop Jul 9 '15 at 15:54
  • @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 Jul 9 '15 at 15:57
  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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.