3

I need a tool to join 2 mp3 files in 1.

The first file ( background ) will be some sound ( music, submitted by user ), second file will be a machine (google) speaking text, which was submitted ( by user ).

In output I need a single mp3 file, with background music and speaking robot text playing together.

I can't really find any PHP standalone solution like some library or something like, only shell commands atc.

So are there some libraries?

or there's some unique shell command which works on all OS to combine files?

Or how do I complete the task?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Maikal
  • 247
  • 2
  • 11
  • Technically speaking, raw mp3 files can just be concatenated. e.g. `cat file1.mp3 file2.mp3 > joined.mp3`. id3 tags can throw a bit of a kink in the works, as well as mp3-in-wav and whatnot. – Marc B Jul 06 '15 at 18:57
  • concatinated means 1file and after it ends 2 file, right ? ( not native english here ).. i need so both mp3 files play same time, background vol 30% and text robot speach vol 100%.. – Maikal Jul 06 '15 at 19:02
  • oh. that's simultaneous playback, not "joining". e.g. `cat + dog` -> `catdog`, not `cdaotg`. and no, there'd be no php libraries for this. php itself has no multimedia capabilities whatsoever. – Marc B Jul 06 '15 at 19:26
  • About "join" i sad that i'm not english nativly, and that's the best word i could think of ) Is there anothere way to do this ? may be throught nodejs ? or using php exec() function ? – Maikal Jul 06 '15 at 19:46

2 Answers2

4

Based off of this question, you should be able to install FFMPEG onto your server (hopefully not a shared host?) and use

//Reduce background volume, assuming it's input2.mp3
shell_exec('ffmpeg -i input2.mp3 -af "volume=0.3" backround.wav');
//Merge the two
shell_exec('ffmpeg -y -i input1.mp3 -i background.wav -filter_complex amerge -c:a libmp3lame -q:a 4 output.mp3');

Then you can simply serve up the output.mp3 file. If this is being performed by PHP running under apache (or other web host, instead of CLI) you'll need to make sure your www-data user has read access to the input files, and write access to the output directory.

All your temporary output, like the background, should be saved as .wav, and only converted back to .mp3 for the final output. Recompressing the audio at each step may result in a very poor final output.

I am making assumptions about how FFMPEG works here, so you may need to consult the documentation for it in order to build a functioning or more efficient set of commands.

Community
  • 1
  • 1
Jerbot
  • 1,168
  • 7
  • 18
-1

You can simply do this

file_put_contents('combined.mp3',
file_get_contents('file1.mp3') .
file_get_contents('file2.mp3'));
  • Audio files need to be decoded before being edited and then encoded back. Think of them as a .ZIP file that you need to open in order to make changes and then put it all together back. ;) – EsaulFarfan Aug 06 '20 at 00:35
  • For mp3s this works. It's not perfect since metadata may get messed up, but it does the job. – BinaryMoon Jul 17 '23 at 08:34