How to extract an audio part from a mp4 using ffmpeg? - audio

I tried to use the following command :
ffmpeg -i video.mp4 -acodec copy -ss 00:30:00 -to 00:60:00 extract.mp3
but it fails and says
[mp3 # 0x3bfade0] Invalid audio stream. Exactly one MP3 audio stream is required.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
I believe it's because I need to extract the mp3 data first and provide it to the command, but is not there a way to do this in one command and avoid creating the full mp3 file that I don't need ?

Your audio track is likely AAC, not MP3.
So either
ffmpeg -i video.mp4 -acodec copy -ss 00:30:00 -to 00:60:00 extract.aac
or
ffmpeg -i video.mp4 -vn -ss 00:30:00 -to 00:60:00 extract.mp3
Save it to a generic MP4 if you need a failsafe command.
ffmpeg -i video.mp4 -map 0:a:0 -acodec copy -ss 00:30:00 -to 00:60:00 extract.mp4

Related

How to extract audio in 8khz using ffmpeg

I am using ffmpeg to extract the audio from a video. Below code downlaods the audio from a video file. I'm not sure how efficient this program is but I do know that it downloaods it in 48KHZ.
How do I use this program to extract audio from a video in 8Khz because the file is getting too big.
ffmpeg -i video_link -vn output.wav
Use -ar option to change frequency rate
ffmpeg -i video_link -vn -ar 8000 output.wav
If you want to try different formats of audio check the available formats in ffmpeg using ffmpeg -formats and available codecs using ffmpeg -codecs
Here's an example to extract to mp3 file
ffmpeg -i video_link -vn -ar 8000 -f mp3 output.mp3
Edit: as #llogan pointed out, -f option is not needed, ffmpeg automatically mux mp3 file.
ffmpeg -i video_link -vn -ar 8000 output.mp3

How to delay audio after a specific position with ffmpeg?

I have a 10 seconds a.mp4 with two streams: Stream #0 is a video stream and Stream #1 is a audio stream.
Now, I want to delay the audio stream by 4 seconds after the time position 00:03. It is to say, in the output file, I want that: 00:00-00:03 is the original audio, 00:03-00:07 has no sound, 00:07-00:14 is the original 00:03-00:10 audio.
I've tried this:
ffmpeg -i a.mp4 -t 00:00:03 -i a.map4 -itsoffset 4 -ss 00:00:03 -i a.mp4 -map 0:v -map 1:a -map 2:a -codec copy output.mp4
But it seems that there are two audio streams in the output.mp4 and only one of them can be played once. Then I tried amix filter:
ffmpeg -i a.mp4 -t 00:00:03 -i a.mp4 -itsoffset 4 -ss 00:00:03 -i a.mp4 -filter_complex "[1:a][2:a] amix=inputs=2" -map 0:v output.mp4
But it also doesn't work. I'm new to ffmpeg so I have no idea what should I do now? Any idea for me? Very much thanks!
Use the asetpts filter to change timestamps, and aresample to (optionally) insert silence in that gap.
ffmpeg -i a.mp4 -af "asetpts='if(lt(T\,3),PTS,PTS+4/TB)',aresample=async=1" -c:v copy output.mp4
Test without aresample to see if your player is tolerant of large gaps in the audio stream.

ffmpeg exported file is broken

I am using using ffmpeg to trim and join several audio files. The ouput audio file can be played as a normal file, but when I open it in some C# codes, exceptions are always throwing, says "MP3 Header is missing". I am new to ffmpeg and I googled for many times but seems no one is encountering this problem.
Here is my ffmpeg command to trim an audio file:
ffmpeg -i input_1.mp3 -ss 00:00:00.000 -to 00:00:01.000 -acodec libmp3lame 1.mp3
(The input audio format can be mp3/wma/wav/m4a/aac)
And the following is for joining all the audio files:
ffmpeg -safe 0 -f concat -i list.txt -acodec libmp3lame join.mp3
The list.txt contents:
file C:\\1.mp3
file C:\\2.mp3
file C:\\3.mp3
Problem soved! Thanks to Gyan's comment under my question.
The main point:
Make sure all converted files have same sampling rate and channel count i.e. add -ar 44100 -ac 2
The above parameters did solve my problem.

Merging MP3 with MP4 using FFMPEG. Unexpected silent at the end

I'm using ffmpeg -i video.mp4 -i audio.mp3 -c:a aac -shortest output.mp4 to join a part of an mp3 file to a video while converting to AAC, but the last 3 centiseconds of the audio are silent. Is there a way to fix this?
Log: https://jpst.it/2aIIh - This one had 10 ms of silence at the end.

ffmpeg 2 audio files in one video file

lets say I have a video file (video.mp4) and 2 audio files (audio1.mp3 and audio2.mp3.) The video has a length of 60 seconds, every audio file has a length of 30 seconds.
What I am trying to achive is:
the first 20 seconds of the video is with original audio stream, followed by 20 seconds of the first audio file (offset of 5 seconds with a length of 20s) and the same with the second audio file.
ffmpeg -i video.mp4 -ss 5 -t 20 -i audio1.mp3 -ss 5 -t 20 -i audio2.mp3 -vcodec copy -acodec copy -copyinkf -map 0:v:0 -map 1:a:0 -map 2:a:0 -shortest final.mp4
The command above takes the video stream of the first input and the audio stream of the third input. The audio stream of the second input seems to be overwritten. How can I put all audio streams together and how can I define the offset when the audio streams should begin?
I assume that you want the combined audio programme in one stream. Even if you applied timestamp offsets to the other audios, most players won't switch audio streams mid-playback.
So,
ffmpeg -i video.mp4 -ss 5 -t 20 -i audio1.mp3 -ss 5 -t 20 -i audio2.mp3
-filter_complex
"[0]atrim=0:20[a];[1]adelay=20000|20000[b];[2]adelay=40000|40000[c];[a][b][c]amix=3"
-vcodec copy -copyinkf -shortest final.mp4

Resources