Skip to main content

Add audio at specific time in video using ffmpeg


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.



Answer



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


ffmpeg -y -i a.mp4 -itsoffset 00:00:30 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 intial even after giving itsoffset.


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.


Comments

Popular Posts

Use Google instead of Bing with Windows 10 search

I want to use Google Chrome and Google search instead of Bing when I search in Windows 10. Google Chrome is launched when I click on web, but it's Bing search. (My default search engine on Google and Edge is http://www.google.com ) I haven't found how to configure that. Someone can help me ? Answer There is no way to change the default in Cortana itself but you can redirect it in Chrome. You said that it opens the results in the Chrome browser but it used Bing search right? There's a Chrome extension now that will redirect Bing to Google, DuckDuckGo, or Yahoo , whichever you prefer. More information on that in the second link.

linux - Using an index to make grep faster?

I find myself grepping the same codebase over and over. While it works great, each command takes about 10 seconds, so I am thinking about ways to make it faster. So can grep use some sort of index? I understand an index probably won't help for complicated regexps, but I use mostly very simple patters. Does an indexer exist for this case? EDIT: I know about ctags and the like, but I would like to do full-text search. Answer what about cscope , does this match your shoes? Allows searching code for: all references to a symbol global definitions functions called by a function functions calling a function text string regular expression pattern a file files including a file

How do I transmit a single hexadecimal value serial data in PuTTY using an Alt code?

I am trying to sent a specific hexadecimal value across a serial COM port using PuTTY. Specifically, I want to send the hex codes 9C, B6, FC, and 8B. I have looked up the Alt codes for these and they are 156, 182, 252, and 139 respectively. However, whenever I input the Alt codes, a preceding hex value of C2 is sent before 9C, B6, and 8B so the values that are sent are C2 9C, C2 B6, and C2 8B. The value for FC is changed to C3 FC. Why are these values being placed before the hex value and why is FC being changed altogether? To me, it seems like there is a problem internally converting the Alt code to hex. Is there a way to directly input hex values without using Alt codes in PuTTY? Answer What you're seeing is just ordinary text character set conversion. As far as PuTTY is concerned, you are typing (and reading) text , not raw binary data, therefore it has to convert the text to bytes in whatever configured character set before sending it over the wire. In other words, when y

networking - Windows 10, can ping other PC but cannot access shared folders! What gives?

I have a computer running Windows 7 that shares a Git repo on drive D. Let's call this PC " win7 ". This repo is the origin of a project that we push to and pull from. The network is a wireless network. One PC on this network is running on Windows 10. Let's call this PC " win10 ". Win10 can ping every other PC on the network including win7 . Win7 can ping win10 . Win7 can access all shared files on win10 . Neither of the PCs have passwords. Problem : Win10 cannot access any shared files on win7 , not from Explorer, nor from Git Bash or any other Git management system (E-Git on Eclipse or Visual Studio). So, win10 cannot pull/push. Every other PC on the network can access win7 shared files and push/pull to/from the shared Git origin. What's wrong with Windows 10? I have tried these: Control Panel\All Control Panel Items\Network and Sharing Center\Advanced sharing settings\ File sharing is on, Discovery is on, Password protected sharing is off Adapte