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

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!

Related

ffmpeg cutting the last 2 seconds of audio

When I record both audio and video using ffmpeg the audio recording cuts out for the last two seconds of the video.
ffmpeg \
-f v4l2 -i /dev/video0 \
-f alsa -i hw:2 \
samples/video.mp4
I have tried using different audio and video codecs, as well as different video formats and I, have noticed that mpg format instead of mp4 the audio works better.
I have also tried using different codecs with the mp4 and checked the compatibilities wikipedia but they don't seem to matter much.
So adding the following line seems to solve the problem.
-preset ultrafast -threads 0

Add Audio to video with existing audio using FFmpeg and change volume of of both audios

Add Audio to video with existing audio using FFmpeg and change volume of both audios
I am using this command from ffmpeg to merge audio to video
video.mp4 has video and audio, and audio.m4a only has audio
ffmpeg -i video.mp4 -i audio.m4a -map 0:v -map 1:a -c copy -shortest output.mp4
I want to merge audio to video but maintain audio from both video and the new audio file
I also want to change the volume of audio file that I want to add from current file to 1.5 times
and change the audio from the video file to 0.5 times
how can we change the volume and still maintain new and old audio in the merged file

FFMPEG encode audio and forced subtitles at same time?

I'm using latest static build of ffmpeg windows.
My input file (.mkv) is:
[video] - 1080, V_MPEG4/ISO/AVC, 14.6 Mbps, ID#0
[audio] - DTS 5.1, 1510 Kbps, ID#1
[subtitles] - S_TEXT/ASS Lossless English, ID#14
My problem is this: I convert the audio, so that my target player, a XB1 console (media support faq), is able to play audio/video. However sometimes its rather difficult to hear or parts may be in foreign language, so I want to force the english subtitles into the mix at the same time I convert the audio.
Currently for the audio, I use the following command
ffmpeg -i input.mkv -codec copy -acodec ac3 output.mkv
Can I somehow tie in the forced subtitles (onto the video) in order to save an extra process of taking the output.mkv and trying to force subtitles on?
Edit: I've tried using the following command to extract subtitles to be able to edit them
ffmpeg -i Movie.mkv -map 0:s:14 subs.srt
However i get the error: Stream map '0:s:14' matches no streams
Edit2: attempted to extract subtitles and succeeded with
ffmpeg -i input.mkv -map 0:14 -c copy subtitles.ass
but still looking to force the subtitles, nonetheless!
Also - a little bonus to this question - can I somehow extract the .ass file and edit it to only produce subtitles for foreign parts - so english audio doesn't have subtitles during the movie but foreign audio does have subtitles?
Cheers
Edit3:
When I try to use both of the commands at once (my earlier mentioned audio converter & one from the ffmpeg wiki)
ffmpeg -i input.mkv -codec copy -acodec ac3 -vf "ass=subs.ass" output.mkv
I get the following error from ffmpeg,
Filtergraph 'ass=subs.ass' was defined for video output stream 0:0 but codec copy was selected.
Filtering and streamcopy cannot be used together.
Since your media player does not support subtitles, the text has to be burnt onto the video image. For that, use
ffmpeg -i input.mkv -vf "ass=subs.ass" -c:v libx264 -crf 20 -c:a ac3 output.mkv
This will re-encode the video, since text is being added. The CRF value controls the video quality. Lower values produce better quality but larger files. 18 to 28 is a decent range to try.

Re-encode video stream only with ffmpeg (and with all audio streams)

I'm looking for a way to re-encode the video stream of a movie only and keep all other streams as they are using ffmpeg or more specific streamio/streamio-ffmpeg (Github - StreamIO-FFMPEG).
I already tried various combinations of -map 0 or -map a:0 -map s:0, but in all combinations I tried, either nothing is encoded at all, or not all other streams are copied to the new file. In most cases there is only one audio stream after encoding, when there were two before, and sometimes the subtitle streams are lost, too. Also most times the info what language the streams are in gets lost.
So when I have a movie file (mkv) with the following streams:
0: video [H.264, 1080p]
1: audio [english, mp3]
2: audio [french, mp3]
3: subtitle [english (forced)]
4: subtitle [english]
What should the ffmpeg parameters be, if I want to encode the video file to H.265 and 720p and keep all other streams as they are?
What should the parameters be, if I additionally want to encode the audio streams as AAC?
Thanks in advance!
Use
ffmpeg -i in.mkv -vf scale=hd720 -map 0 -c copy -c:v libx265 out.mkv
To encode audio as well,
ffmpeg -i in.mkv -vf scale=hd720 -map 0 -c copy -c:v libx265 -c:a aac out.mkv
(The order of the arguments above matter)

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