<?php
/***************************************************************
* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker:
*
*
* PHP versions 5
*
* --LICENSE NOTICE--
* This source file is subject to version 3.01 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_01.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to hide@address.com so we can mail you a copy immediately.
* --LICENSE NOTICE--
***************************************************************
* $File: flv2mp3.class.php $
* $Author: mschulz $ - $Date: 2009-03-04 19:24:27 +0100 (Mi, 04 Mär 2009) $
* $HeadURL: http://tbox.markoschulz.intern/php-001/class/flv2mp3.class.php $ - $Revision: 5 $
* $Id: flv2mp3.class.php 5 2009-03-04 18:24:27Z mschulz $
* $Description: Class to convert a flv to a mp3 file. $
* $Copyright: (c) 2009 Marko Schulz $
***************************************************************/
/**
* {{{ @exsample
*
* <code>
*
* try {
*
* // Path to the flv file
* (string) $file = "P_gDz-Qlef0.flv";
*
* // Load the Flv2Mp3 class
* include_once( 'flv2mp3.class.php' );
*
* // Create the $mp3 object
* $mp3 = new Flv2Mp3( array( 'delete' => False ) );
*
* // Convert the flv file into mp3
* if ( $mp3->convert( $file ) ) {
* if ( is_file( $mp3->getOutfile( $file ) ) ) {
* $mp3->stream( $file );
* }
* }
*
* // If you have set the parameter delete => Fale you have
* // to delete the converted file manually.
* if ( is_file( $mp3->getOutfile( $file ) ) ) {
* echo $mp3->getOutfile( $file )." exist!";
* }
*
* // You can also stream the mp3 with the convert method
* // if ( !$mp3->convert( $file, True ) ) {
* // echo "Can't convert ".$file;
* // }
*
* } catch ( Exception $error ) {
* echo "Error: <br/>\n";
* echo $error->getMessage()."<br/>\n";
* echo "Line: ".$error->getLine()." in ".$error->getFile()."<br/>\n";
* echo $error;
* }
* </code>
*
* }}}
*/
/**
* The class provides methods to convert a flv into a mp3 file.
*
* @category Audio
* @author Marko Schulz
* @copyright 2009 tuxnet24.de
* @license http://www.php.net/license/3_01.txt PHP License 3.01
* @version File: $Revision: 5 $
*/
class Flv2Mp3 {
// {{{ properties
/**
* Path to the mplayer binary
*
* @var string
* @access protected
*/
protected $mplayer = "/usr/bin/mplayer";
/**
* Path to the temporary directory
*
* @var string
* @access protected
*/
protected $tmpdir = "/tmp";
/**
* Should the converted file deleted
*
* @var bool
* @access protected
*/
protected $delete = True;
/**
* All exeption messages
*
* @var array
* @static
* @access protected
*/
protected static $ERROR = array(
'NOFILE' => "No flv file defined!",
'NOREMOVE' => "Can't remove the output file [%s]!"
);
// }}}
// {{{ __construct()
/**
* This is the class constuctor
*
* @access prublic
* @param array path to mplayer binary and path to temp directory (optional)
* @return void
*/
public function __construct( array $args = array() ) {
if ( isset( $args['mplayer'] ) && $this->isFile( $args['mplayer'] ) ) $this->mplayer = $args['mplayer'];
if ( isset( $args['tmpdir'] ) && $this->isDir( $args['tmpdir'] ) ) $this->tmpdir = $args['tmpdir'];
if ( isset( $args['delete'] ) ) $this->delete = (bool) $args['delete'];
}
// }}}
// {{{ __destruct()
/**
* This is the class destuctor
*
* @access prublic
* @return void
*/
public function __destruct() {
if ( $this->delete && isset($this->outfile) ) {
if ( !$this->remove( $this->outfile ) ) // Remove converted file
throw new Exception( $this->message( 'NOREMOVE', $this->outfile ) );
}
}
// }}}
// {{{ convert()
/**
* This method call the conversion
*
* @access public
* @param string path to input file
* @param bool stream true|false
* @return void
*/
public function convert ( $input, $steam = False ) {
if ( !isset( $input ) || !$this->isFile( $input ) )
throw new Exception( $this->message( 'NOFILE', $input ) );
else {
$this->outfile = $this->getOutfile( $input );
if ( $this->execute( $input, $steam ) ) return True;
else return False;
}
}
// }}}
// {{{ execute()
/**
* This method exec the conversion
*
* @access private
* @param string path to converted file
* @return void
*/
private function execute ( &$infile, $steam ) {
// Execute the mplayer to convert the flv file to mp3 file
exec( $this->mplayer." -dumpaudio -dumpfile ".$this->outfile." ".$infile." 2>/dev/null", $out = array(), $return );
if ( $return !== 0 ) return False;
else {
if ( $stream ) $this->sream( $this->outfile );
return True;
}
}
// }}}
// {{{ stream()
/**
* This method download/stream the converted file
*
* @access private
* @param string path to converted file
* @return void
*/
public function stream ( &$file ) {
header( "Content-Description: File Transfer" );
header( "Content-Length: ".filesize( $file ) );
header( "Content-type: audio/mpeg" );
header( "Content-Disposition: inline; filename=\"".basename( $file )."\";\r\n" );
header("Content-Transfer-Encoding: binary\n");
header('Pragma: no-cache');
readfile( $file );
die();
}
// }}}
// {{{ isDir()
/**
* This method exec a directory check
*
* @access private
* @param string path to input file
* @return bool
*/
protected function isDir( &$file ) {
clearstatcache(); // clear the status chache
return is_dir( $file );
}
// }}}
// {{{ isFile()
/**
* This method exec a file check
*
* @access private
* @param string path to input file
* @return bool
*/
protected function isFile( &$file ) {
clearstatcache(); // clear the status chache
return is_file( $file );
}
// }}}
// {{{ getOutfile()
/**
* This method get the output file
*
* @access private
* @param string path to input file
* @return string
*/
public function getOutfile ( $infile ) {
return $this->tmpdir.'/'.preg_replace( '/[^\.]+$/i', 'mp3', basename( $infile ) );
}
// }}}
// {{{ remove()
/**
* This method remove the converted file
*
* @access private
* @param string path to converted file
* @return bool
*/
public function remove ( &$file ) {
if ( !@unlink( $file ) ) return False;
else return True;
}
// }}}
// {{{ message()
/**
* Return the right exception message
*
* @access private
* @return string
*/
private function message( $err, $which ) {
// Replace the %s placeholder with $which in ERROR array.
(string) $except = str_replace( '%s', $which, Flv2Mp3::$ERROR[$err] );
return $except;
}
// }}}
}
//**************************************************************
// EOF
?>