How to add offset or delay to audio file with ffmpeg? - audio

I need offset (some silence) in the start of file, so I tried:
./ffmpeg -itsoffset 100 -i 3.mp3 offset_test.mp3
But it doesn't work.
How to add offset to audio file with ffmpeg?

For formats without timestamps, you'll need to add silence, as #Brad mentioned.
ffmpeg -i 3.mp3 -af adelay=100000|100000 delayed.mp3
The adelay takes delay in milliseconds per channel, separated by |.

The easiest way I found so far for ffmpeg ver > 4.2:
ffmpeg -i audio_in.wav -af areverse,apad=pad_dur=1s,areverse audio_out.wav
This will add an offset of 1 second to the audio.

Related

Cut a video in between key frames without re-encoding the full video using ffpmeg?

I would like to cut a video at the beginning at any particular timestamp, and it need to be precise, so the nearest key frame is not good enough.
Also, these videos are rather long - an hour or longer - so I would like to avoid re-encoding this altogether if possible, or otherwise only re-encode a minimal fraction of the total duration. Thus, would like to maximise the use of -vcodec copy.
How can I accomplish this using ffmpeg?
NOTE: See scenario, and my own rough idea for a possible solution below.
Scenario:
Original video
Length of 1:00:00
Has a key frame every 10s
Desired cut:
From 0:01:35 through till the end
Attempt #1:
Using -ss 0:01:35 -i blah.mp4 -vcodec copy, what results is a file where:
audio starts at 0:01:30
video also starts at 0:01:30
this starts both the audio and the video too early
using -i blah.mp4 -ss 0:01:35 -vcodec copy, what results is a file where:
audio starts at 0:01:35,
but the video is blank/ black for the first 5 seconds,
until 0:01:40, when the video starts
this starts the audio on time,
but the video starts too late
Rough idea
(1) cut 0:01:30 to 0:01:40
re-encode this to have new key frames,
including one at the target time of 0:01:35
then cut this to get the 5 seconds from 0:01:35 through 0:01:40
(2) cut 0:01:40 through till the end
without re-encoding, using -vcodec copy
(3) ffmpeg concat the first short clip (the 5 second one)
with the second long clip
I know/ can work out the commands for (2) and (3), but am unsure about what commands are needed for (1).
List timestamps of key frames:
ffprobe -v error -select_streams v:0 -skip_frame nokey -show_entries frame=pkt_pts_time -of csv=p=0 input.mp4
It will output something like:
0.000000
2.502000
3.795000
6.131000
10.344000
12.554000
16.266000
...
Let's say you want to delete timestamps 0 to 5, and then stream copy the remainder. The closest following key frame is 6.131.
Re-encode 5 to 6.131. Ensure the input and output match attributes and formats. For MP4 default settings should do most of the work, assuming H.264/AAC, but you may have to manually match the profile.
ffmpeg -i input.mp4 -ss 5 -to 6.131 trimmed.mp4
Make input.txt for the concat demuxer:
file 'trimmed.mp4'
file 'input.mp4'
inpoint 6.131
Concatenate:
ffmpeg -f concat -i input.mp4 -c copy output.mp4
try
ffmpeg -i src.mp4 -vcodec copy -reset_timestamps 1 -map 0 out.mp4
or
ffmpeg -i src.mp4 -vcodec copy -reset_timestamps 1 -map 0 src_.m3u8
which generates hls playlists

How to add outro background music to another audio file with FFMpeg?

I have two files: story.wav (180 seconds) and background-music.wav (90 seconds). I need a FFMpeg command that merges the two files and fades in background-music.wav (with esin) 30 seconds before the end of story.wav.
I have this in separate commands:
ffmpeg -i background-music.wav -filter_complex afade=t=in:curve=esin:ss=0:d=30 fadein.wav
ffmpeg -i fadein.wav -af "adelay=150000|150000" delayed.wav
ffmpeg -i delayed.wav -i story.wav -filter_complex amix=inputs=2:duration=longest final.wav
This is ugly - and it has the problem, that the volume of the first part is only 50% (the volume should be kept).
There must be an elegant way to achieve this in one command - but how?
Bonus question: how can I convert the result to mp3 (with parameters like bit rate set) in the same command?
Thanks for any help!
Sebastian
Use
ffmpeg -i background-music.wav -i story.wav
-filter_complex
"[0]afade=t=in:curve=esin:ss=0:d=30,adelay=150000|150000[bg];
[1]volume=2[fg];
[bg][fg]amix=inputs=2:duration=longest"
-b:a 128k final.mp3

Use FFMPEG to export audios with gaps filled

I have a MKV file with gaps in the audio. That is, there are gaps in the MKV audio track timestamps. According to "ffmpeg", the AC3 audio length is 802 seconds, but when exporting the audio to WAV, the resulting file length is 801'53 seconds. That is, the "exported" audio is shorter.
Triaging the issue with
ffmpeg -i INPUT.mkv -af ashowinfo -map 0:2 -y -frames:a XXXX -f alaw /dev/null
I can confirm that the length difference is consistent with gaps in the timestamps of the original audio frames. There are a handful of missing audio frames. I guess those are replaced by silence in the player.
The command I use to export the audio is:
ffmpeg -i INPUT.mkv -map 0:1 -ac 2 OUTPUT.wav
My question is: How can I instruct FFMPEG to preserve the gaps in the original audio, zero (silence) filled?. The WAV file duration should be the same than the original AC3 audio.
Given my current workflow, I would rather prefer to not keep the original timestamps in the output file but generate a WAV with (tiny) silences instead. I could consider keeping timestamps if there is no other choice, but this could be quite a pain in my workflow.
Advice? Help?
Thanks a lot in advance!
Use
ffmpeg -i INPUT.mkv -map 0:1 -af aresample=async=1 -ac 2 OUTPUT.wav
The aresample filter will insert silent samples within the gaps.

How can i use ffmpeg to extract audio from an video every 5 seconds

I need to extract audio from live stream. I want to generate an audio file every 5s.
if i use the command
ffmpeg -i ***.flv -c:a aac -fs 128k output.aac
I can only get one file. but i want generate a file every 5s.
if i use the commmand
ffmpeg -i ***.mp4 -vf fps=1/5.0 E:\image%d.jpg
I got an image every 1s.
How can i do the same thing to extract audio.
Use the segment muxer:
ffmpeg -i input -map 0:a -f segment -segment_time 5 output_%03d.aac
Add -c copy and use a compatible output container format if you don't want to re-encode.

Increase volume of one channel in 8channel .wav file ffmpeg

I have 8-channel .wav file with sine wave in there. I would like to increase the volume of only 4th channel in this file. I don't know how to do it though.
ffmpeg -i input.wav -af "volume=1.5" output.wav
This will increase volume for all 8 channels. How can i apply it only to 1 channel (4th one) but still maintain the 8-channel .wav file?
Something like this should work: ffmpeg -i input.wav -af "pan=8c|c0=c0|c1=c1|c2=c2|c3=1.5*c3|c4=c4|c5=c5|c6=c6|c7=c7" output.wav

Resources