Add audio at specific time in video using ffmpeg

18,092

I finally solved the problem by adding just 1 option -async

ffmpeg -y -i a.mp4 -itsoffset 00:00:30 -i sng.m4a -map 0:0 -map 1:0 -c:v copy -preset ultrafast -async 1 out.mp4

By default audio/video timestamps are stretched to match each other; which was my problem of audio starting from initial even after giving the "itsoffset" option.

As per Ffmpeg Doc, using -async 1 corrects the initial timestamp of audio only. I know that this option is deprecated; but anyhow it solved my case.

Share:
18,092

Related videos on Youtube

BlueSword
Author by

BlueSword

Updated on September 18, 2022

Comments

  • BlueSword
    BlueSword over 1 year

    I want to add an M4A audio of 5s duration to an MP4 video file. I have already tried the -itsoffset command of ffmpeg.

    ffmpeg -i a.mp4 -itsoffset 00:00:10 -i b.m4a -map 0:0 -map 1:0 -preset ultrafast o.mp4
    

    Commandline output

    ffmpeg -i psy.mp4 -itsoffset 00:00:10 -i cut.m4a -map 0:0 -map 1:0 -c:v copy -preset ultrafast -y out.mp4
    ffmpeg version N-50911-g9efcfbe Copyright (c) 2000-2013 the FFmpeg developers
    built on Mar 13 2013 21:26:48 with gcc 4.7.2 (GCC)
    configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
    isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
    le-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libg
    sm --enable-libilbc --enable-libmp3lame --enable-libopencore-amrnb --enable-libo
    pencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-li
    bschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-lib
    twolame --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enabl
    e-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
    libavutil      52. 19.100 / 52. 19.100
    libavcodec     55.  0.100 / 55.  0.100
    libavformat    55.  0.100 / 55.  0.100
    libavdevice    54.  4.100 / 54.  4.100
    libavfilter     3. 45.103 /  3. 45.103
    libswscale      2.  2.100 /  2.  2.100
    libswresample   0. 17.102 /  0. 17.102
    libpostproc    52.  2.100 / 52.  2.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'psy.mp4':
    Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2013-12-03 03:22:01
    Duration: 00:04:12.17, start: 0.000000, bitrate: 723 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yu
    v420p, 640x360, 624 kb/s, 23.98 fps, 23.98 tbr, 48k tbn, 47.95 tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 96
    kb/s
    Metadata:
      creation_time   : 2013-12-03 03:22:02
      handler_name    : IsoMedia File Produced by Google, 5-11-2011
    Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'cut.m4a':
    Metadata:
    major_brand     : M4A
    minor_version   : 512
    compatible_brands: isomiso2
    encoder         : Lavf55.0.100
    Duration: 00:00:05.24, start: 0.237460, bitrate: 99 kb/s
    Stream #1:0(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 96
    kb/s
    Metadata:
      handler_name    : SoundHandler
    Codec AVOption preset (Set the encoding preset (cf. x264 --fullhelp)) specified
    for output file #0 (out.mp4) has not been used for any stream. The most likely r
    eason is either wrong type (e.g. a video option with no video streams) or that i
    t is a private option of some encoder which was not actually used for any stream
    .
    Output #0, mp4, to 'out.mp4':
    Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    encoder         : Lavf55.0.100
    Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 640x360, q=
    2-31, 624 kb/s, 23.98 fps, 48k tbn, 48k tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, stereo, s16
    , 128 kb/s
    Metadata:
      handler_name    : SoundHandler
    Stream mapping:
    Stream #0:0 -> #0:0 (copy)
    Stream #1:0 -> #0:1 (aac -> libvo_aacenc)
    Press [q] to stop, [?] for help
    frame=  916 fps=0.0 q=-1.0 size=     865kB time=00:00:38.16 bitrate= 185.7kbits/
    frame= 1758 fps=1749 q=-1.0 size=     865kB time=00:01:13.28 bitrate=  96.7kbits
    frame= 2477 fps=1645 q=-1.0 size=     865kB time=00:01:43.26 bitrate=  68.6kbits
    frame= 3182 fps=1586 q=-1.0 size=     865kB time=00:02:12.67 bitrate=  53.4kbits
    frame= 3961 fps=1580 q=-1.0 size=     865kB time=00:02:45.16 bitrate=  42.9kbits
    frame= 4904 fps=1631 q=-1.0 size=     865kB time=00:03:24.49 bitrate=  34.7kbits
    frame= 5667 fps=1615 q=-1.0 size=     865kB time=00:03:56.31 bitrate=  30.0kbits
    frame= 6046 fps=1549 q=-1.0 Lsize=   19345kB time=00:04:12.12 bitrate= 628.6kbit
    s/s
    video:19234kB audio:83kB subtitle:0 global headers:0kB muxing overhead 0.146119%
    

    The problem is: it does start at 00:00:10 but it keeps repeating after every 10s. (You can notice this only when you play the whole video without touching the seekbar.)

    When I play this video on Android, then the audio starts from the beginning. It plays fine with VLC.

    Expected result: Silence in video till specified time, then my audio, and again silence after audio.

  • Rajib
    Rajib over 10 years
    Out of curiosity, where is this version from: "version N-50911-g9efcfbe"
  • Elisa Cha Cha
    Elisa Cha Cha over 10 years
    @Rajib It was compiled from the source code from the FFmpeg git repository. See git log 9efcfbe in the ffmpeg source directory (if you cloned the FFmpeg git repo), or see git.videolan.org/?p=ffmpeg.git;a=commit;h=9efcfbe