phpffmpegffmpeg-php

how to concatenate different audio files in different format into one mp3 file in ffpmeg or php?


I am allowing users to upload audio files with file extensions mp3, wav, flac. With the following command, I can concatenate audio files if it is in mp3 only.

ffmpeg -i "concat:sample1.mp3|sample2.mp3" -c:a libmp3lame -write_xing 0 merged.mp3

How can i merge if the audio files are in different formats and encoding using ffmpeg or php? In the ffmpeg documentation, it provides an example for concatenating files with different codes but the example includes video files like mp4,webm,mov but i need example for audio only. FFmpeg documentation of concatenate

-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[outv][outa]" \
-map "[outv]" -map "[outa]" output.mkv```


Solution

  • ffmpeg -i sample1.mp3 -i sample2.wav -i sample3.flac -filter_complex "[0:a][1:a][2:a]concat=n=3:v=0:a=1[outa]" -map "[outa]" -c:a libmp3lame merged.mp3
    

    -i sample1.mp3 -i sample2.wav -i sample3.flac : Audio files are specified.

    -filter_complex "[0:a][1:a][2:a]concat=n=3:v=0:a=1[outa] : The filter_complex concatenates the audio streams from the three input files.

    [0:a][1:a][2:a] Three audio streams are represented, and concat=n=3:v=0:a=1[outa] concatenates them into a single audio stream [outa].

    The v=0 Parameter indicates no video streams.

    -map [outa] : [outa] specifies the output should include audio.

    -c:a libmp3lame : Is used to encode the merged audio as an MP3 file.

    merged.mp3 : Specifies the name of the output file.