How do you replace audio at a given timestamp with ffmpeg? - audio

I am trying to use the ffmpeg library to take two FLAC files and replace the audio in File A with the audio in File B at a given timestamp.
For example if File B was to be played at 00:02 and was a second long, playing the output it would be (00:00-0:01) File A Audio -> (00:02-0:03) File B Audio -> (00:04-...) File A Audio
To do this, I have tried the following
ffmpeg -y -i original.flac -i replacement.flac -acodec copy -ss 2 -to 3 -write_xing 0 result.flac
But this only produces the original audio between the specified timestamps.
Is there any way to achieve this within ffmpeg?

The typical method to do this would be the concat demuxer, but there are issues with FLAC extraction with duration header in the output, so you can use
ffmpeg -y -i original.flac -i replacement.flac \
-filter_complex "[0]atrim=0:2[Apre];[0]atrim=5,asetpts=PTS-STARTPTS[Apost];\
[Apre][1][Apost]concat=n=3:v=0:a=1" out.flac
Where 2 is the insertion point in seconds, and 5 is the insertion point + B's duration.

Related

How to add audio to video file at a specific time using ffmpeg

I have an avi and a wav file. Let's suppose the video file is one hour long, and the audio file 30 minutes long and it's associated with the last 30 minutes of the video file.
Is there a way to create a new video file with the video and audio combined but audio starting after the first 30 minutes of the video?
You can simply append a silent audio to the beginning of the audio file using aevalsrc filter. To create a 30 sec of silent audio,
aevalsrc=0:d=30
To do this you can use filter_complex with map options. Following is the FFmpeg command I'm suggesting.
ffmpeg -i input_video -i input_audio -filter_complex "
aevalsrc=0:d=30[s1];
[s1][1:a]concat=n=2:v=0:a=1[aout]" -c:v copy -map 0:v -map [aout] output_video
Else you can try amerge and adelay where doc itself has a clear explanation.
Hope this helps you!

FFMpeg - Segment audio to chunks

I have tried this example in order to segment a given video file using ffmpeg into an m3u8 file and smaller chunks (.ts files). This actually worked great. Is it possible to do practically the same thing with audio input?
This was my most promising approach so far (capturing live audio on Windows OS):
ffmpeg -f dshow -i audio="<name of input device>" -acodec libmp3lame -ab 64000 | segmenter - 10 stream stream.m3u8 http://<IP_OF_SERVER>/stream/stream/ 5 1
But this returns this error:
At least one output file must be specified.
Could not open input file, make sure it is an mpegts file: -1
I really would not know how to convert the live audio stream to an mpegts file.
Could anyone please give me a hint?
Thanks a lot

Split a video file into separate video and audio files using a single ffmpeg call?

Background: I would like to use MLT melt to render a project, but I'd like that render to result with separate audio and video files. I'd intend to use melt's "consumer" avformat which uses ffmpeg's libraries, so I'm formulating this question as for ffmpeg.
According to Useful FFmpeg Commands For Converting Audio & Video Files (labnol.org), the following is possible:
ffmpeg -i video.mp4 -t 00:00:50 -c copy small-1.mp4 -ss 00:00:50 -codec copy small-2.mp4
... which slices the "merged" audio+video files into two separate "chunk" files, which are also audio+video files, in a single call; that's not what I need.
Then, ffmpeg Documentation (ffmpeg.org), mentions this:
ffmpeg -i INPUT -map_channel 0.0.0 OUTPUT_CH0 -map_channel 0.0.1 OUTPUT_CH1
... which splits the entire duration of the content of two channels of a stereo audio file, into two mono files; that's more like what I need, except I want to split an A+V file into a stereo audio file, and a video file.
So I tried this with elephantsdream_teaser.ogv:
ffmpeg -i /tmp/elephantsdream_teaser.ogv \
-map 0.0 -vcodec copy ele.ogv -map 0.1 -acodec copy ele.ogg
... but this fails with "Number of stream maps must match number of output streams" (even if zero-size ele.ogv and ele.ogg are created).
So my question is - is something like this possible with ffmpeg, and if it is, how can I do it?
Your command works, but you need to specify mapping with columns instead of dots as so:
ffmpeg -i /tmp/elephantsdream_teaser.ogv -map 0:0 -vcodec copy ele.ogv -map 0:1 -acodec copy ele.ogg
You might want to test with a more recent build of ffmpeg. Mine gave correct errors for your command:
[ogg # 00000000043f8480] Invalid stream specifier: .0.
Last message repeated 3 times
Stream map '0.0' matches no streams.

Export each minute of MP3 into separate WAV

This is definitely a strange question but I'm looking for a way to split an mp3 mix of 60 minutes into 60 separate 1 minute long wav files to use with an audio fingerprinting API like Echonest.
Is this possible in a single ffmpeg command or would I have to run multiple iterations of ffmpeg with a the following values:
-ss is the startpoint in seconds.
-t is the duration in seconds.
You can use the segment muxer in ffmpeg:
ffmpeg -i input.mp3 -codec copy -map 0 -f segment -segment_time 60 output%03d.mp3
For a 4 minute input this results in:
$ ls -m1 output*.mp3
output000.mp3
output001.mp3
output002.mp3
output003.mp3
Since -codec copy enables stream copy mode re-encoding will be avoided. See the segment documentation for more information and examples.

Add audio (with an offset) to video with FFMPEG

I have a 10 minute video and a 50 minute audio mp3.
The video starts at 500 seconds into the audio.
Using FFMPEG, how can I add the the audio to the video but specify a 500 seconds audio offset (So that they sync up)?
EDIT:
Down the bottom of this page it suggests how to specify an offset.
$ ffmpeg -i video_source -itsoffet delay -i audio_source -map 0:x -map 1:y .......
However, when I apply this, it still starts the audio from the start.
We are 8 years later, and the -itsoffset does work.
Exactly as in your linked page:
ffmpeg -i input_1 -itsoffset 00:00:03 -i input_2
Note that you place the -itsoffset switch before the input you want to delay, in this case input_2 will be delayed.
So in your case that the video starts later, you would add -itsoffset 00:08:20 before the video input.
I couldn't get audio to offset properly either, and some searching suggests that -itsoffset is currently broken.
You could try and get/compile an old version of ffmpeg before it broke (which doesn't sound like much fun).
Alternately, you could pad your audio with the necessary silence using something like sox and then combine:
sox -null silence.mp3 trim 0 500 # use -r to adjust sample-rate if necessary
sox silence.mp3 input.mp3 padded_input.mp3
ffmpeg -i in.avi -i padded_input.mp3 out.avi

Resources