当前位置:首页 > 编程技术 > PHP编程 > 正文内容

PHP将两个MP3文件进行合并,可以多个MP3文件合并

曾经2年前 (2022-06-06)PHP编程759

以下代码是基于PHP 7.x 的,版本小于7.x 的请自行将 代码中的__construct 改为 mp3


<?php
class mp3 {
	var $str;
	var $time;
	var $frames;
	// Create a new mp3 
	function __construct($path="") {
		if($path!="") {
			$this->str = file_get_contents($path);
		}
	}
	// Put an mp3 behind the first mp3 
	function mergeBehind($mp3) {
		$this->str .= $mp3->str;
	}
	// Calculate where's the end of the sound file 
	function getIdvEnd() {
		$strlen = strlen($this->str);
		$str = substr($this->str,($strlen-128));
		$str1 = substr($str,0,3);
		if(strtolower($str1) == strtolower('TAG')) {
			return $str;
		} else {
			return false;
		}
	}
	// Calculate where's the beginning of the sound file 
	function getStart() {
		$strlen = strlen($this->str);
		for ($i=0;$i<$strlen;$i++) {
			$v = substr($this->str,$i,1);
			$value = ord($v);
			if($value == 255) {
				return $i;
			}
		}
	}
	// Remove the ID3 tags 
	function striptags() {
		//Remove start stuff... 
		$newStr = '';
		$s = $start = $this->getStart();
		if($s===false) {
			return false;
		} else {
			$this->str = substr($this->str,$start);
		}
		//Remove end tag stuff 
		$end = $this->getIdvEnd();
		if($end!==false) {
			$this->str = substr($this->str,0,(strlen($this->str)-129));
		}
	}
	// Display an error 
	function error($msg) {
		//Fatal error 
		die('<strong>audio file error: </strong>'.$msg);
	}
	// Send the new mp3 to the browser 
	function output($path) {
		//Output mp3 
		//Send to standard output 
		if(ob_get_contents()) 
		      $this->error('Some data has already been output, can\'t send mp3 file');
		if(php_sapi_name()!='cli') {
			//We send to a browser 
			header('Content-Type: audio/mpeg3');
			if(headers_sent()) 
			       $this->error('Some data has already been output to browser, can\'t send mp3 file');
			header('Content-Length: '.strlen($this->str));
			header('Content-Disposition: attachment; filename="'.$path.'"');
		}
		echo $this->str;
		return '';
	}
	//Save MP3
	function  saveMp3($filename) {
		$myfile = fopen($filename, "w") or die("Unable to open file!");
		fwrite($myfile,  $this->str);
		fclose($myfile);
	}
}
// First File: (Google speech) 
$mp3 = new mp3('1.mp3');
$mp3->striptags();
//Second file 
$second = new mp3("2.mp3");
$mp3->mergeBehind($second);
$mp3->striptags();
 
//$mp3->output('word.mp3'); 下载文件
$mp3-saveMp3('word.mp3'); //保存文件
?>


扫描二维码推送至手机访问。

版权声明:本文由珍惜发布,如需转载请注明出处。

本文链接:https://www.zp68.com/article/35.html

分享给朋友:

“PHP将两个MP3文件进行合并,可以多个MP3文件合并” 的相关文章

PHP多数组组合-颜色尺码大小

抽象一下需求:在构建某个对象时,它拥有多个属性,每个属性拥有多个可选的值,需要穷举出每个属性不同的选择组合构建出的不同对象,比如:输入参数:$arr = [     'Name' => ['...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。