Is there any way to add audio to output when adding an overlay image using FFmpeg?

Just want to know, Is that possible to add sound to the final video along with an overlay image?

1 Like

Using FFmpeg to include an overlay image requires a multi-step process that combines both video and audio streams in order to add audio to the output.

The secret is to manage the complicated filter graph for combining audio and video streams by using the -filter_complex option.

Let us take an example,

ffmpeg -i input_video.mp4 -i overlay_image.png -i input_audio.mp3 -filter_complex "[0:v][1:v]overlay=10:10[outv]" -map "[outv]" -map 2:a -c:a copy output_video.mp4

In this command,

  • input_video.mp4: is the original video file.
  • overlay_image.png: is the image that is to be overlaid onto the video.
  • input_audio.mp3: This is an audio file to be added to the output.
  • "filter_complex": This is a filtergraph. It will combine video and audio streams.
  • "[0:v][1:v]overlay=10:10[outv]": It describes the overlay operation, targetting the position of the overlay image on the video. The resulting video stream is labeled as [outv].
  • "-map "[outv]": You can select the resulting video stream to be included in the output.
  • "-map 2:a": Selects the audio stream from the third input (input_audio.mp3).
  • "-c:a copy": It copies the audio stream without re-encoding, ensuring that there is no loss in audio quality.
  • output_video.mp4: Specifies the name of the output video file.

Using the above command, you will be able to successfully include an overlay image over a video and add audio to the output by utilizing this extensive FFmpeg command. Changes can be made to the overlay’s position and audio synchronization to meet particular needs or preferences.

To add an image overlay to a video, you can use the overlay filter. For example, to add an image called image.png as an overlay on top of the video, you can use the following command:
ffmpeg -i video.mp4 -i image.png -filter_complex “overlay=10:10” output.mp4This will add the image as an overlay on the video, with the top-left corner of the image at the position (10, 10) in the video. You can adjust the position of the overlay by changing the values of the overlay filter.

Got it but, what if I need to combine these two?