1

I have a folder full of audio file with .mp3 .amr and .wav extension. I have a image.jpg. What I want is combine the image.jpg with every audio file in that folder and convert them to separate vidoe.mp4 file. How do I do it with FFmpge in Ubuntu? I am using Ubuntu 14.04.

2 Answers 2

1

Adding sound to a video is straightforward

ffmpeg -framerate 60 -i input.png -i input.mp3 -vcodec libx264 -crf 20 -preset medium -acodec aac -vf scale=1280:-2,format=yuv420p output.mp4
  • -i input.mp3 The audio filename
  • -acodec copy Copies the audio from the input stream to the output stream
  • -s is the output resolution of the video

Basically, just change the input.png to the filename of your picture, and the input.mp3 to the audio file. Finally, alter output.mp4 to the destination file name you want.

This is a very simple method for doing what you want, but it is possible to script this in the following way:

#! /bin/bash
###################################################################
picture="input.jpg"  # Change this to the file name of your picture
###################################################################
for i in *.mp3; do
  ffmpeg -framerate 60 -f image2 -i $picture -i input.mp3 -vcodec libx264 -crf 20 -preset medium -acodec aac -vf scale=1280:-2,format=yuv420p "${i%.*}.".mp4
done

Put the filename of your picture in the top, paste it into a file, save it as something like convert.sh. Change the permissions chmod 777 convert.sh, then run it ./convert.sh

It should read all the mp3 files, add a picture and output an mp4 file with the same name as the mp3.

If you want to do the same with wav files or amr, just change for i in *.mp3; do to for i in *.wav; do or for i in *.amr; do.

Hope that helps!

Good luck :)

4
  • 1
    For the image file demuxer -f image2 is superfluous (but you may need it in a script). Use -framerate instead of -r for image input. Instead of declaring an arbitrary bitrate use -crf. If you want to scale it use -s as an output option, not input. There is no preset named "normal". If you want to declare a preset use -preset because -vpre is used for custom, user created presets. Not all players can handle MP3 audio in MP4 container. For more info see FFmpeg Wiki: H.264. Typo: $720.
    – llogan
    Nov 4, 2016 at 17:19
  • 1
    Lastly, you'll need to use -vf format=yuv420p or -pix_fmt yuv420p output options or the output won't play on dumb players like QuickTime or WMP.
    – llogan
    Nov 4, 2016 at 17:21
  • Updated to reflect your comments, and fixed minor typos. Nov 4, 2016 at 17:44
  • Thanks for fixing up the code. It was on the fly to quickly answer the question :) Nov 4, 2016 at 19:14
0

to combine same image with different mp3 files resulting in seperate avi files with the same name as the mp3 file, the code is as follows: for i in *.mp3; do name=echo "$i" | cut -d'.' -f1; echo "$name"; ffmpeg -y -i image.jpg -i "$i" "${name}.avi"; done. Just change the name of your image file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .