Roman Kubiv
Roman Kubiv

Reputation: 131

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:

  1. reading the playlist file and creating a new .txt in the ffmpeg required format
  2. concatenating the audio files using the .txt into an .mp3
  3. 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.

Upvotes: 0

Views: 867

Answers (1)

llogan
llogan

Reputation: 133763

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.

Upvotes: 4

Related Questions