concatenate audio files with an image - ffmpeg

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.

Related

FFMPEG converting MP3 to MP4 is adding blank space, how to fix Terminal command?

I'm creating MP4 files from MP3 files + an image. Searching here at StackOverflow I found a Terminal command that gets me really close:
ffmpeg -loop 1 -r 1 -i pic.jpg -i input.mp3 -c:a copy -shortest -c:v libx264 output.mp4
Works almost perfectly so long as input.mp3 and pic.jpg are in the same folder. The problem is:
It frequently ends up with about 20-40seconds of blank space at the end of the resulting MP4. I can manually chop it off, but I'd love to find out if there's a way to alter this command so that the resulting MP4 file is exactly the length of the input MP3 file.
I don't know the ffmpeg commands well and know just enough Terminal to be dangerous. So I'm hoping it's something obvious haha.
This command should work for you:
ffmpeg -i input.mp3 -loop 1 -i pic.jpg -shortest -c:a copy -c:v mjpeg output.mp4
You do not need to set -r 1 with just one image and -loop 1.
I think -c:v mjpeg is more suitable to encode jpg than -c:v libx264.

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?

ffmpeg add multiple mp3 files to mp4 video at particular position

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

ffmpeg append video with different dimensions

I am cropping and adding subtitles to a video using the following:
ffmpeg -i inputfile.mov -lavfi "crop=720:720:280:360,subtitles=subs.srt:force_style='OutlineColour=&H100000000,BorderStyle=3,Outline=1,Shadow=0,MarginV=20,Fontsize=18'" -crf 1 -c:a copy output.mov
I have another video called credits.mp4 which has the same dimensions as the output.mov (after cropping). Can I do this during the above process, or would I have to use something like concat afterwards?
Using bash in Terminal on a Mac
Use the concat filter:
ffmpeg -i inputfile.mov -i credits.mp4 -lavfi "[0]crop=720:720:280:360,subtitles=subs.srt:force_style='OutlineColour=&H100000000,BorderStyle=3,Outline=1,Shadow=0,MarginV=20,Fontsize=18',setpts=PTS-STARTPTS[v0];[1]setpts=PTS-STARTPTS[v1];[v0][0:a][v1][1:a]concat=n=2:v=1:a=1[v][a]" -map "[v]" -map "[a]" output.mp4
Because no info was provided about your inputs I made some assumptions:
The attributes of both input files are the same as they are fed to concat. If not then perform additional filtering to conform them to a common set of parameters.
credits.mp4 has audio. If not, then add an audio file or use the anullsrc filter as an input to create silent/dummy/filler audio for proper concatenation.

Video file is hanging after concatenating video files and drawtext to output

I'm trying to concat 3 video files and add text to output using ffmpeg.
Each part is 10 sec long.
I've end up with this code:
ffmpeg -i output3.mp4 -i output2.mp4 -i output1.mp4 -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[v][a]; [0:v:0]drawtext=fontfile=tahoma.ttf:text=Sample text:fontcolor=white:fontsize=40:box=1:boxcolor=black#0.7:boxborderw=5:x=100:y=100" -map "[v]" -map "[a]" output.mp4
The result video has 30 seconds but it hangs after 1st part (10s). When I remove drawtext filter part (just concat), then video is fine, but without text...
Anyone can help ?
Use
ffmpeg -i output3.mp4 -i output2.mp4 -i output1.mp4 -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[v][a]; [v]drawtext=fontfile=tahoma.ttf:text=Sample text:fontcolor=white:fontsize=40:box=1:boxcolor=black#0.7:boxborderw=5:x=100:y=100[v]" -map "[v]" -map "[a]" output.mp4
Your existing syntax applied the text on top of the video stream of the first input file, instead of the resultant video from the concat filter.

Resources