How to combine audio and video into one file with ffmpeg from the different offset?

5,849

If you want to merge two files you should use following command:

ffmpeg-win-x64.exe -ss 25 -i id2.mkv -ss 10 -i punky.mp3 -c copy -map 0:0 -map 1:0 -to 20 res.mkv

Bock1: -ss 25 -i id2.mkv - takes movie clip starting position 25 sec.

Block2: -ss 10 -i punky.mp3 takes mp3 since 10 sec.

Block3: -c copy here you put codec you want. Be warned, there are certain cases when decode/encode is mandatory to get normal sync.

Block4: -map 0:0 -map 1:0 - we map stream 0 (video) from first file, and stream 0 from second file (mp3) to output.

Block5: -to 20 res.mkv - cut 20 seconds. On 'copy' it may vary due to features of source files.

If you had to align one stream along another you could use -itsoffset command, that I mentioned earlier. It doesn't cut stream, just moves it back amount time needed.

Share:
5,849

Related videos on Youtube

JavaRunner
Author by

JavaRunner

Updated on September 18, 2022

Comments

  • JavaRunner
    JavaRunner almost 2 years

    I have very long audio and video files and I want to combine audio and video with one ffmpeg-command. Audio should start from its 7 second position and the video should start from its 15 second. And the length of the combined video should be 20 seconds. How to do it?

    I've tried to do this with:

    ffmpeg -ss 00:00:15.000 -t 20 -i 1.mp4 -ss 00:00:07.000 -t 20 -i journey.mp3 -c:a aac -b:a 192k -r 30 -c:v libx264 -profile:v high -level:v 4.0 -pix_fmt yuv420p 0-60.mp4
    

    It works but without generating a thumbnail in my explorer window. So I think the combined video is broken (there's no audio in windows system but audio sounds fine in linux). For example, this try generates a thumbnail but I need to use codecs as in the previous case above:

    ffmpeg -ss 00:00:00.000 -t 10 -i 1.mp4 -ss 00:00:05.000 -t 10 -i journey.mp3 -c:a copy -c:v copy -r 30 -pix_fmt yuv420p 0-59.mp4
    
    • p0rc0_r0ss0
      p0rc0_r0ss0 about 4 years
      Please consult link Command you seek is -itsoffset offset, as I get it, it is set separately for audio and video stream. As it gets added to -ss, length of your file should be -to 35 (but that needs a bit playing around).
    • JavaRunner
      JavaRunner about 4 years
      @p0rc0_r0ss0 Seems something like that. Can you provide answer with a little example? I'll approve it.
  • Bhavya Gupta
    Bhavya Gupta about 4 years
    p0rc0_r0ss0 i believe you took wrong time-stams in hurry. And can you please tell what cineCutter is? :) I saw it on your repo.
  • p0rc0_r0ss0
    p0rc0_r0ss0 about 4 years
    Nope, it's right time-stamps, just to cut logo of sample clip video and convenient to track sound file place. I guess topic starter is smart enough to adjust timestamps. cineCutter is video manipulation program for rapid video cutting, encoding/decoding based on ffmpeg. For now it's not available for public, but it is planned as free application.