I am using ffmpeg in my Android application. I want to create video from images with background video and audio. Images are overlaid on video with borders. Video and audio will be repeated until all the slides are not visible.

This video shows an example of what I want to achieve.

What command would I need for that? I don't know how to write ffmpeg commands.

In my application the user can select the images from a gallery. Using the selected images I have to create a video, each image will show for 5 seconds. I have a video and audio of small duration (as shown in linked video, e.g. the fire video) which will be repeated until all the slides are shown.


This is possible, but will require additional scripting because of the variable number of selected images and also due to the lack of a real looping option for video and audio inputs.

This answer provides some examples, but you have the additional requirement of looping a video and audio stream.

Here are examples you can adapt and build upon. The scripting will be up to you.

Example 1

This example is assuming that:

  • All image inputs are the same frame size. This is a requirement otherwise timing issues may occur.

  • All image inputs are named img001.jpg, img002.jpg, img003.jpg, etc. Alternatively you can use a glob pattern as described in the image file demuxer documentation.


ffmpeg -i video.mkv -i audio.mp3 -framerate 1/5 -i img%03.jpg -filter_complex \
"[0:v]setpts=PTS-STARTPTS[vid]; \
 [vid][vid][vid]concat=n=3:v=1:a=0[bg]; \
 [1:a][1:a][1:a]concat=n=3:v=0:a=1[a]; \
 [2:v]drawbox=t=5:c=white[fg]; \
 [bg][fg]overlay=(W-w)/2:(H-h)/2,format=yuv420p[v]" \
-map "[v]" -map "[a]" -shortest output.mp4

Example 2

Another method is to explicitly list each image as a separate input. This can allow you to perform any scaling if needed, but makes the command even more complex:

ffmpeg -i video.mkv -i audio.mp3 -loop 1 -i hound.jpg -loop 1 -i crow.jpg \
-loop 1 -i donkey.png -filter_complex \
"[0:v]setpts=PTS-STARTPTS[vid]; \
 [vid][vid][vid]concat=n=3:v=1:a=0[bg]; \
 [1:a][1:a][1:a]concat=n=3:v=0:a=1[a]; \
 [2:v]trim=duration=5,scale=640:360,drawbox=t=5:c=white[fg0]; \
 [3:v]trim=duration=5,scale=640:360,drawbox=t=5:c=white[fg1]; \
 [4:v]trim=duration=5,scale=640:360,drawbox=t=5:c=white[fg2]; \
 [bg][fg0]overlay=(W-w)/2:(H-h)/2[ol0]; \
 [ol0][fg1]overlay=(W-w)/2:(H-h)/2[ol1]; \
 [ol1][fg2]overlay=(W-w)/2:(H-h)/2,format=yuv420p[v]" \
-map "[v]" -map "[a]" -shortest output.mp4

There are several answers on Super User that provide more complex scaling examples. This example is simple, but may result in squished or stretched output images if the input images vary in aspect ratio.

What you need to determine

For both methods, based on how many images the user selects, and the duration of the video and audio, you will have to determine how many segments you need for the concat filter to provide an appropriate looping.

You may experience some issues. The image demuxer and concat are sometimes finicky.

  • Hello Firstly, Very Thank full for very nice answer. I am using Example 1 Command in my code, But its not working properly, Images are not change after every 5 sec, only first image is visible in the video. Second, background video is working for one time but not repeated. Third, also having problem with audio its repeated in video but stop some times. Please Check the following link: drive.google.com/file/d/0B3zXdKH-t-GqOWdkbzNZWmQycUE/…. I have created above linked video with six images. – Mandy Crisley Apr 2 '15 at 13:34
  • @MandyCrisley Not much I can do without seeing your command and complete console output. – llogan Apr 3 '15 at 19:23
  • ,I am using following ffmpeg command: "ffmpeg", "-i", "fire_1.mp4", "-i", input_file_path,"-framerate", "1/5", "-i" ,PHOTO_%3d.jpg", "-filter_complex" ,"[0:v]setpts=PTS-STARTPTS[vid];[vid][vid][vid]concat=n=3:v=1:a=0[bg];[1:a][1:a][1:a]concat=n=3:v=0:a=1[a];[2:v]drawbox=t=5:c=white[fg];[bg][fg]overlay=(W-w)/2:(H-h)/2,format=yuv420p[v]", "-map","[v]","-map","[a]","-s","640x480","-r","25","-b:v","2000k","-c:v","mpeg4","-c:a","aac", "-strict","experimental","-b:a", "32k","-shortest", output_file_path – Mandy Crisley Apr 4 '15 at 8:21
  • For console log file please check the following link: link – Mandy Crisley Apr 4 '15 at 8:21

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.