Mix audio/video of different lengths with ffmpeg

12,580

If video.mp4 has no audio

You can use this command:

ffmpeg -i video.mp4 -ss 00:04:00 -i audio.mp3 -c copy -shortest output.mkv
  • The audio will be from the 4 minute position (-ss 00:04:00) as requested in the question.

  • This example will stream copy (re-mux) the video and audio–no re-encoding will happen.

If video.mp4 has audio

You will have to add the -map option as described here: FFmpeg mux video and audio (from another video) - mapping issue.

If the audio is shorter than the video

Add the apad filter to add silent padding:

ffmpeg -i video.mp4 -ss 00:04:00 -i audio.mp3 -c:v copy -af apad -shortest output.mkv

Note that filtering requires re-encoding, so the audio will be re-encoded in this example.

Share:
12,580
Yura Shinkarev
Author by

Yura Shinkarev

Mobile and web applications development using Java/Kotlin. Born in 1982, living in Smolensk, Russia. Open to any suggestions, including remote work. My interests are Java, Android, Linux.

Updated on August 17, 2022

Comments

  • Yura Shinkarev
    Yura Shinkarev almost 2 years

    I want to mix video from video.mp4 (1 minute duration) and audio from audio.mp3 (10 minute duration) into one output file with a duration of 1 minute. The audio from audio.mp3 should be from the 4 min - 5 min position. How can I do this with ffmpeg?