ffmpeg combine separate video and audio of different lengths

5,364

if you just do ffmpeg -i inputfile it will give you informational output including the length of the stream (there is also a ffprobe command that you may or may not have that does the same thing, and may have more output control).

% ffmpeg -i foo.mkv
ffmpeg version 1.0.git-79133fd Copyright (c) 2000-2012 the FFmpeg developers
[...]
Input #0, matroska,webm, from 'foo.mkv':
  Metadata:
    creation_time   : 2009-11-08 16:11:10
  Duration: 00:43:16.71, start: 0.000000, bitrate: 2435 kb/s
    Stream #0:0(eng): Video: h264 (High), yuv420p, 720x480 [SAR 32:27 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
    Stream #0:1(eng): Audio: ac3, 48000 Hz, stereo, s16, 192 kb/s (default)
    Stream #0:2(eng): Subtitle: subrip
At least one output file must be specified

So you'll need to parse this output to get the length of each original file, and then use the -itsoffset option with what you learn.

   -itsoffset offset (input)
       Set the input time offset in seconds.  "[-]hh:mm:ss[.xxx]" syntax
       is also supported.  The offset is added to the timestamps of the
       input files.  Specifying a positive offset means that the
       corresponding streams are delayed by offset seconds.

so you'll end up with something like (untested):

ffmpeg -i foo.mp3 -itsoffset 60 -i blah.mpeg -acodec copy -vcodec copy -copyts out.mkv
Share:
5,364

Related videos on Youtube

Surya
Author by

Surya

Software Developer, Technology fan. https://www.jjerome.com

Updated on September 18, 2022

Comments

  • Surya
    Surya almost 2 years

    I have 2 video files (flv). One has the video and the other has the audio. I wrote a script using ffmpeg that extracts the audio as mp3, then merges it with the video flv. It works, except that my audio and video are out of sync because the audio is longer than the video. They started recording the meeting's audio before the video.

    For completeness, here are my 2 commands:

    msg "Extracting Audio"
    ./ffmpeg -loglevel panic -i cameraVoip*.flv -vn -acodec mp3 output_audio.mp3
    
    msg "Merging Audio with Video"
    ./ffmpeg -loglevel panic -i output_audio.mp3 -i screenshare*.flv -acodec copy -vcodec copy output_video.flv
    

    This got me to thinking, ffmpeg is combining the 2 files together from the beginning of each file. Is there a way I can tell it match from the end, and ignore extra at the beginning?

    I know I could edit each file to get them to the same size, but I'd like to make this as automated as possible.

  • evilsoup
    evilsoup almost 11 years
    It's definitely better to use ffprobe for getting this information, if you're going to script it. The -show_format and -show_streams options will be useful (see the ffprobe man page or the online documentation) -- outputs one key=value pair per line, so it's easier to parse with grep or sed or whatever.
  • Dan Pritts
    Dan Pritts almost 11 years
    Yeah - I went with ffmpeg for the answer because it's a lowest common denominator. Lot of the time people seem to have found a windows ffmpeg binary somewhere and have nothing else.