Asked  7 Months ago    Answers:  5   Viewed   55 times

Do you know a class to merge two MP3 files using PHP?

I've found nothing on Google.

 Answers

56

If by merging, you mean placing one audio over the other, then please disregard this answer.

If you dont want to re-encode the MP3s, you can probably just append them. I know this worked for MPEG movies, so I guess it could work for MP3s too. Another option would be to add the audo files to a Zip Archive with no compression and then rename the extension to .mp3.

I did a quick test and this

file_put_contents('combined.mp3',
    file_get_contents('file1.mp3') .
    file_get_contents('file2.mp3'));

worked fine. The ID3 tags will be wrong, but the resulting file contains both audio files. For some other possible gotchas, see the link in Pekka's answer.

Also, some quick googling resulted in

  • http://www.sourcerally.net/Scripts/20-PHP-MP3-Class
  • http://codingforums.com/showthread.php?t=169069

and some discussion

  • http://ask.metafilter.com/21381/Merge-mp3s-with-PHP
Wednesday, March 31, 2021
 
Silfverstrom
answered 7 Months ago
96

Answered to help people who might stumble across this

As the the answer at Session lost when switching from HTTP to HTTPS in PHP has concluded, since you are using session.cookie_secure = 1 the cookie that contains the session ID is not transferred when the connection switches from HTTPS to HTTP. At HTTP connection, when you session_start(), PHP creates a new session id, which replaces the previous session id.

The answer also suggests a solution, pass the session id using query string, which is then picked up by the page. This smells of bad of security flaw. Don't forget the reason why we used HTTPS in the first place!

So the solution I suggest to you is that you redirect all http request to https counterparts. Use HTTPS for everything in your site, from css, images, to mundane static html pages. This is actually something that every application that is serious about security does. For example, visiting github page using HTTP will return:

HTTP/1.1 301 Moved Permanently
Server: nginx/0.7.67
Date: Sun, 08 May 2011 15:43:01 GMT
Content-Type: text/html
Content-Length: 185
Connection: close
Location: https://github.com/

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/0.7.67</center>
</body>
</html>

Remember why you used HTTPS in the first place, if you want to be totally secure, use HTTPS for everything.

Detect if the request is HTTPS or not (See this question) at bootstrap.

If the request is HTTP, either redirect all requests to HTTPS home page, or you can try parsing $_SERVER['REQUEST_URI'] and redirecting HTTP request to their HTTPS counterpart using parse_url and http_build_url.


Second Alternative Solution

If you really really don't want to use HTTPS for everything, then don't session_start() on pages that are accessed with HTTP. Secure cookies will be retained when you do this.


Third Alternative Solution

The other solution is to try and detect the user by IP addresses and user agent. This is not guaranteed to be accurate, so what I suggest is just use HTTPS for everything. Paypal, for example, always use HTTPS even for mundane static pages.

Wednesday, March 31, 2021
 
hakre
answered 7 Months ago
22

The server protocol should be available through SERVER_PROTOCOL from the server environment, usually exposed through $_SERVER['SERVER_PROTOCOL'] inside your application.

From phpinfo() under Apache 2.4:

SERVER_PROTOCOL => HTTP/1.1
Saturday, May 29, 2021
 
conmen
answered 5 Months ago
48

By cannibalising a previous answer of mine I've written the following:

<?php

$srcImg = '1.jpg';
$destImg = '2.jpg';
$dstX = 300;
$dstY = 40;
$srcX = 0;
$srcY = 0;
//$srcW = 200; // replaced with $src_width
//$srcH = 226; // replaced with $src_height
$pct = 100;

// create destination image resource.
$dest = imagecreatefromjpeg($destImg);
$dest_width = imagesx($dest);
$dest_height = imagesy($dest);

// create source image resource and define transparent colour.
$src = imagecreatefromjpeg($srcImg);
$src_width = imagesx($src);
$src_height = imagesy($src);
imagecolortransparent($src, imagecolorallocate($src, 255, 0, 255));

// create a circular mask and use it to crop the source image.
$mask = imagecreatetruecolor($src_width, $src_height);
$black = imagecolorallocate($mask, 0, 0, 0);
$magenta = imagecolorallocate($mask, 255, 0, 255);
imagefill($mask, 0, 0, $magenta);
$r = min($src_width, $src_height);
imagefilledellipse($mask, ($src_width / 2), ($src_height / 2), $r, $r, $black);
imagecolortransparent($mask, $black);
imagecopymerge($src, $mask, 0, 0, 0, 0, $src_width, $src_height, 100);
imagedestroy($mask);

// merge the two images to create the result.
imagecopymerge($dest, $src, $dstX, $dstY, $srcX, 0, $src_width, $src_height, $pct);

// output.
header('Content-type: image/jpeg');
imagejpeg($dest);
imagedestroy($src);
imagedestroy($dest);

This correctly crops the source image to a circle and merges it with the destination. You'll need to adjust the positioning to your preference.

enter image description here

Saturday, May 29, 2021
 
dkcwd
answered 5 Months ago
31

Instead of using itsoffset you can append a silent audio to the beginning of the audio. Assume we have three audios to be merged each of 10 sec long. So you can append silent audio as follows.

  • First audio : No silent audio
  • Second audio : Append 10 sec silent audio to the beginning
  • Third audio : Append (10 sec) x 2 silent audio to the beginning

There after you can mix all these audios together. To create a silent audio you can use aevalsrc filter with filter_complex. Following will work for the above example.

ffmpeg -i 0.mp3 -i 1.mp3 -i 2.mp3 -filter_complex 
"aevalsrc=0:d=10[s1];
aevalsrc=0:d=20[s2];
[s1][1:a]concat=n=2:v=0:a=1[ac1];
[s2][2:a]concat=n=2:v=0:a=1[ac2];
[0:a][ac1][ac2]amix=3[aout]" -map [aout] out.mp3

Here [s1] and [s2] are the corresponding silent audio source for second and third input audio streams. Then each silent source will be concatenated with there corresponding audio streams using concat filter. Finally all concatenated audios will be mixed using amix filter.

Else you can try amerge and adelay where doc itself has a clear explanation.

Hope this helps!

Saturday, October 2, 2021
 
Kevin
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :