Recorte automático de un mp3 en PHP
Pregunta
¿Hay alguna forma de recortar automáticamente un MP3 subido a un sitio web a 30 segundos (o alguna otra extensión) en PHP? Si no es así, ¿hay algún servicio de terceros que pueda integrarse (de manera transparente para el usuario) para lograr el mismo efecto?
Gracias.
Solución
Puede probar la Clase de MP3 en las PHPClasses. Cuenta con el siguiente ejemplo:
require_once './class.mp3.php';
$mp3 = new mp3;
$mp3->cut_mp3('input.mp3', 'output.mp3', 0, -1, 'frame', false);
En este caso, el 'marco' puede sustituirse por 'segundo' para basar el corte en un marco de tiempo.
Otros consejos
Preparé un script que genera un clip de 30 segundos de un archivo MP3 sobre la marcha. Si está buscando guardar el archivo, una de las otras opciones que usan una clase / biblioteca probablemente será la mejor. Pero, si solo desea reproducir / descargar la vista previa, sobre la marcha puede ser mejor. Definitivamente te ahorrará espacio en el disco duro.
Compruébelo en http://www.stephenwalcher.com/2013/06/17/how-to-extract-and-play-part-of-an-mp3-in-php/ .
Aquí está el código, pero se puede encontrar una explicación más detallada en mi blog.
$getID3 = new getID3();
$id3_info = $getID3->analyze($filename);
list($t_min, $t_sec) = explode(':', $id3_info['playtime_string']);
$time = ($t_min * 60) + $t_sec;
$preview = $time / 30; // Preview time of 30 seconds
$handle = fopen($filename, 'r');
$content = fread($handle, filesize($filename));
$length = strlen($content);
if (!$session->IsLoggedIn()) {
$length = round(strlen($content) / $preview);
$content = substr($content, $length / 3 /* Start extraction ~10 seconds in */, $length);
}
header("Content-Type: {$id3_info['mime_type']}");
header("Content-Length: {$length}");
print $content;
En Debian / ubuntu intente instalar mpgtx :
apt-get install mpgtx
mptsplit input.mp3 [00:00:00-00:00:30] -o output.mp3
Estoy seguro de que también encontrarás mpgtx disponible en otras distribuciones Linux finas, o simplemente instalar desde la fuente.
Tuve problemas para procesar algunos mp3 con MP3 Class y mpgtx. La mejor opción aquí es ffmpeg, creo. Esta página tiene algunos buenos ejemplos sobre cómo dividir archivos de medios con ffmpeg .
Utilicé PHP MP3 para mi proyecto.
<?php
//Extract 30 seconds starting after 10 seconds.
$path = 'path.mp3';
$mp3 = new PHPMP3($path);
$mp3_1 = $mp3->extract(10,30);
$mp3_1->save('newpath.mp3');
?>
En su caso, puede utilizar extracto (0,30)
o extracto (30,60)
.
//Merge two files
$path = 'path.mp3';
$path1 = 'path1.mp3';
$mp3 = new PHPMP3($path);
$newpath = 'path.mp3';
$mp3->striptags();
$mp3_1 = new PHPMP3($path1);
$mp3->mergeBehind($mp3_1);
$mp3->striptags();
$mp3->setIdv3_2('01','Track Title','Artist','Album','Year','Genre','Comments','Composer','OrigArtist','Copyright','url','encodedBy');
$mp3->save($newpath);
//Extract 30 seconds starting after 10 seconds.
$path = 'path.mp3';
$mp3 = new PHPMP3($path);
$mp3_1 = $mp3->extract(10,30);
$mp3_1->save('newpath.mp3');
//Extract the exact length of time
$path = 'path.mp3';
$mp3 = new PHPMP3($path);
$mp3->setFileInfoExact();
echo $mp3->time;
//note that this is the exact length!
fuente: https://github.com/thegallagher/PHP -MP3 / blob / master / README.md
https://github.com/falahati/PHP-MP3
PHP-MP3 es una biblioteca simple para leer y manipular audio MPEG (MP3).
Instalar:
composer require falahati/php-mp3:dev-master
Cortar audio MPEG:
\falahati\PHPMP3\MpegAudio::fromFile("old.mp3")->trim(10, 30)-saveFile("new.mp3");