ffmpeg add multiple mp3 files to mp4 video at particular position - ffmpeg

It might be a duplicate question but didn't find any helpful answer.
I have 2 audio files and 1 mp4 video file. Want to add the 2 audio files to mp4 videoat specific time.
For example:
Video file:
input.mp4 (2 minutes video)
Audio files:
Audio File 1:
test_0:01.mp3 (15 seconds audio file) I want to insert this file at position 0:01 in the mp4 video
Audio File 2:
test_0:20.mp3(15 seconds audio file) I want to insert this file at position 0:20 in the mp4 video
I tried the following command with offset
It's only inserting test_0:01.mp3 at 0:01 position in the video file
But test_0:20.mp3 is not getting inserted at 0:020 position getting mute for this file no audio.
ffmpeg -i input.mp4 -itsoffset 01 -i test_0:01.mp3 -itsoffset 20 -i test_0:20.mp3 -c:v copy -map 0:v:0 -map 1:a -map 2:a -c:a aac -b:a 192k output.mp4
Any help will be appreciated!

Your command creates two audio tracks in the MP4 file. If you have a look in your video player you can choose between two audio tracks (usually used to choose different audio languages).
Why
This is because every -map parameter creates a new stream. In your example one video with two audio tracks.
Solution
Use the audio filter amix instead. Use also the filter adelay for the delay in the same filter chain to achieve the best result.
ffmpeg -i input.mp4 -i test_0:01.mp3 -i test_0:20.mp3 -filter_complex "[1:a]adelay=1s:all=1[a1];[2:a]adelay=20s:all=1[a2];[a1][a2]amix=inputs=2[amixout]" -map 0:v:0 -map "[amixout]" -c:v copy -c:a aac -b:a 192k output.mp4

Related

FFMPEG: How to convert M2V Video with two single WAV tracks (stereo) to MP4?

How can I create a MP4 file with ffmpeg out of a M2V-Video-File and two WAV-Files (one file for the right audio channel and one for the left audio channel)?
try this:
ffmpeg -i video.m2v -i audio1.wav -i audio2.wav -c:v copy -c:a aac -map 0:v:0 -filter_complex "[1:a][2:a]join=inputs=2:channel_layout=stereo[a]" -map "[a]" output.mp4
I believe mp4 files cannot contain PCM audio, hence converting to aac.

Merge (video and audio) with (background music) with ffmpeg

I have video file input.mp4 that contain video and two audio source - speaking, and music.mp3 that contain background music. Now, I want to merge them.
Here is the command that i use:
ffmpeg -i input.mp4 -i music.mp3 -c:v copy -filter_complex "[0:a]aformat = fltp:44100:stereo,apad[0a];[1] aformat=fltp:44100:stereo,volume=1.5[1a];[0a] [1a] amerge[a]" -map 0:v -map "[a]" -ac 2 -y -shortest output.mp4
input file
https://d9f35555a8b3e9044c8d-95c21efaab8093d23d4124e599a618ee.ssl.cf5.rackcdn.com/mub_audio/1e03dc67-5079-46d5-b692-d69bbb6ee3e3.mp4
audio file
https://d9f35555a8b3e9044c8d-95c21efaab8093d23d4124e599a618ee.ssl.cf5.rackcdn.com/mub_audio/a6cab88a63eb11eb8d8b901b0efa6f1d.mp3
you can check the output file here
https://d9f35555a8b3e9044c8d-95c21efaab8093d23d4124e599a618ee.ssl.cf5.rackcdn.com/mub_audio/6e6e79ef-a732-49d9-8317-4ba97c05a352.mp4
The input file has a pause after every sentence. But in output, that pause duration doesn't have any background music.
What can be the reason? what is the solution for it?

concatenate audio files with an image

I am trying to concatenate multiple audio files and a single image into one video file using one command.
I have list of mp3 files and a playlist file (.m3u) in a direcotry.
I managed to do this but my solution is bad:
reading the playlist file and creating a new .txt in the ffmpeg required format
concatenating the audio files using the .txt into an .mp3
concatenating the large audio file and the static image into a video
This creates 2 unnecessary files that I have to delete.
I tried a different command
ffmpeg -loop 1 -framerate 1 -i myImage.jpg -i file1.mp3 -i file2.mp3 -i file3.mp3 -filter_complex '[0:0][1:0][2:0]concat=n=3:v=0:a=1' -tune stillimage -shortest output.mp4
however im getting a Error initializing complex filters.
Invalid argument error
Another kick in the nuts is that the system im working on has spaces in the folder names.
i tried using -i "concat:file1.mp3|file2.mp3|..." however i cannot use double quote marks to quote out the path so I get an invalid argument error.
Thank you very much for your help.
Method 1: concat demuxer
Make input.txt containing the following:
file 'file1.mp3'
file 'file2.mp3'
file 'file3.mp3'
Run ffmpeg:
ffmpeg -loop 1 -framerate 1 -i myImage.jpg -f concat -i input.txt -filter_complex "[0]scale='iw-mod(iw,2)':'ih-mod(ih,2)',format=yuv420p[v]" -map "[v]" -r 15 -tune stillimage -map 1:a -shortest -movflags +faststart output.mp4
All MP3 files being input to the concat demuxer must have the same channel layout and sample rate. If they do not then convert them using the -ac and -ar options so they are all the same.
Method 2: concat filter
Update: There seems to be a bug with -shortest not working with the concat filter (I keep forgetting about that). See the method above using the concat demuxer, or replace -shortest with -t. The value for -t should equal the total duration of all three MP3 files.
ffmpeg -loop 1 -framerate 1 -i myImage.jpg -i file1.mp3 -i file2.mp3 -i file3.mp3 -filter_complex "[0]scale='iw-mod(iw,2)':'ih-mod(ih,2)',format=yuv420p[v];[1:a][2:a][3:a]concat=n=3:v=0:a=1[a]" -map "[v]" -r 15 -map "[a]" -tune stillimage -shortest -movflags +faststart output.mp4
Option descriptions
scale filter makes image have even width and height which is required when outputting YUV 4:2:0 with libx264.
format filter sets chroma subsampling to 4:2:0, otherwise libx264 will try to limit subsampling, but most players can only handle 4:2:0.
concat filter is accepting file1.mp3, file2.mp3, and file3.mp3 as inputs. Your original command was trying to concat the video to the audio resulting in Invalid argument.
-map "[v]" chooses the video output from -filter_complex.
-r 15 sets output frame rate to 15 because most players can't handle 1 fps. This is faster than setting -framerate 15.
-map "[a]" chooses the audio output from -filter_complex.
-map 1:a chooses the audio from input #1 (the second input as counting starts from 0).
-movflags +faststart after encoding finishes this option moves some data from the end of the MP4 output file to the beginning. This allows playback to begin faster otherwise the complete file will have to be downloaded first.

Converting a large number of MP3 files to videos for YouTube, each using same the JPEG image

I am looking for a way to convert large number of MP3 files to videos, each using the same image. Efficient processing time is important.
I tried the following:
ffmpeg -i image.jpg -i audio.mp3 -vcodec libx264 video.mp4
VLC media player played the resulting video file with the correct sound, but a blank screen.
Microsoft Media Player played the sound and showed the intended image. I uploaded the video to YouTube and received the message:
"The video has failed to process. Please make sure you are uploading a supported file type."
How can I make this work?
Create video:
ffmpeg -framerate 6 -loop 1 -i input.jpg -c:v libx264 -vf format=yuv420p -t 00:10:00 video.mp4
The duration (-t) should be ≥ the MP3 with the longest duration.
Now stream copy the same video for each MP3:
ffmpeg -i video.mp4 -i audio.mp3 -map 0:v -map 1:a -c copy -movflags +faststart -shortest output.mp4
Some notes regarding compatibility:
MP3 in MP4 does not have universal support, but will be fine in YouTube. If your target players do not like it then add -c:a aac after -c copy to output AAC audio.
If your target player does not like it then increase the -framerate value or add the -r output option with an appropriate value, such as -r 15. Again, YouTube should be able to handle it.

Mix audio/video of different lengths with ffmpeg

I want to mix video from video.mp4 (1 minute duration) and audio from audio.mp3 (10 minute duration) into one output file with a duration of 1 minute. The audio from audio.mp3 should be from the 4 min - 5 min position. How can I do this with ffmpeg?
If video.mp4 has no audio
You can use this command:
ffmpeg -i video.mp4 -ss 00:04:00 -i audio.mp3 -c copy -shortest output.mkv
The audio will be from the 4 minute position (-ss 00:04:00) as requested in the question.
This example will stream copy (re-mux) the video and audio–no re-encoding will happen.
If video.mp4 has audio
You will have to add the -map option as described here: FFmpeg mux video and audio (from another video) - mapping issue.
If the audio is shorter than the video
Add the apad filter to add silent padding:
ffmpeg -i video.mp4 -ss 00:04:00 -i audio.mp3 -c:v copy -af apad -shortest output.mkv
Note that filtering requires re-encoding, so the audio will be re-encoded in this example.

Resources