/** * Returns information about the specified file without having to use ffmpeg-php * as it consults the ffmpeg binary directly. This idea for this function has been borrowed from * a French ffmpeg class located: http://www.phpcs.com/codesource.aspx?ID=45279 * * @access public * @todo Change the search from string explode to a regex based search * @param string $file The absolute path of the file that is required to be manipulated. * @return mixed false on error encountered, true otherwise **/ public function getFileInfo($file = false, $tmp_directory = '/tmp/') { // check to see if this is a static call if ($file !== false && PHPVideoToolkit::$_static === true) { $toolkit = new PHPVideoToolkit($tmp_directory); return $toolkit->getFileInfo($file); } // if the file has not been specified check to see if an input file has been specified if ($file === false) { if (!$this->_input_file) { // input file not valid return $this->_raiseError('getFileInfo_no_input'); //<- exits } $file = $this->_input_file; } $file = escapeShellString($file); // die($file); // create a hash of the filename $hash = md5($file); // check to see if the info has already been generated if (isset(self::$_file_info[$hash])) { return self::$_file_info[$hash]; } // generate a random filename $info_file = $this->_tmp_directory . $this->unique($hash) . '.info'; // execute the ffmpeg lookup // exec(PHPVIDEOTOOLKIT_FFMPEG_BINARY.' -i '.$file.' &> '.$info_file); exec(PHPVIDEOTOOLKIT_FFMPEG_BINARY . ' -i ' . $file . ' 2>&1', $buffer); $buffer = implode("\r\n", $buffer); // $data = false; // try to open the file // $handle = fopen($info_file, 'r'); // if($handle) // { $data = array(); // $buffer = ''; // loop through the lines of data and collect the buffer // while (!feof($handle)) // { // $buffer .= fgets($handle, 4096); // } // die($buffer); // grab the duration and bitrate data preg_match_all('/Duration: (.*)/', $buffer, $matches); if (count($matches) > 0) { $parts = explode(', ', trim($matches[1][0])); $data['duration'] = array(); $timecode = $parts[0]; $data['duration']['seconds'] = $this->timecodeToSeconds($timecode); $data['bitrate'] = intval(ltrim($parts[2], 'bitrate: ')); $data['duration']['start'] = ltrim($parts[1], 'start: '); $data['duration']['timecode'] = array(); $data['duration']['timecode']['rounded'] = substr($timecode, 0, 8); $data['duration']['timecode']['seconds'] = array(); $data['duration']['timecode']['seconds']['exact'] = $timecode; $data['duration']['timecode']['seconds']['excess'] = intval(substr($timecode, 9)); } // match the video stream info preg_match('/Stream(.*): Video: (.*)/', $buffer, $matches); if (count($matches) > 0) { $data['video'] = array(); // get the dimension parts // print_r($matches); preg_match('/([0-9]{1,5})x([0-9]{1,5})/', $matches[2], $dimensions_matches); // print_r($dimensions_matches); $dimensions_value = $dimensions_matches[0]; $data['video']['dimensions'] = array('width' => floatval($dimensions_matches[1]), 'height' => floatval($dimensions_matches[2])); // get the framerate preg_match('/([0-9\\.]+) (fps|tb)\\(r\\)/', $matches[0], $fps_matches); $data['video']['frame_rate'] = floatval($fps_matches[1]); $fps_value = $fps_matches[0]; // get the ratios preg_match('/\\[PAR ([0-9\\:\\.]+) DAR ([0-9\\:\\.]+)\\]/', $matches[0], $ratio_matches); if (count($ratio_matches)) { $data['video']['pixel_aspect_ratio'] = $ratio_matches[1]; $data['video']['display_aspect_ratio'] = $ratio_matches[2]; } // work out the number of frames if (isset($data['duration']) && isset($data['video'])) { // set the total frame count for the video $data['video']['frame_count'] = ceil($data['duration']['seconds'] * $data['video']['frame_rate']); // set the framecode $frames = ceil($data['video']['frame_rate'] * ($data['duration']['timecode']['seconds']['excess'] / 10)); $data['duration']['timecode']['frames'] = array(); $data['duration']['timecode']['frames']['exact'] = $data['duration']['timecode']['rounded'] . '.' . $frames; $data['duration']['timecode']['frames']['excess'] = $frames; $data['duration']['timecode']['frames']['total'] = $data['video']['frame_count']; } // formats should be anything left over, let me know if anything else exists $parts = explode(',', $matches[2]); $other_parts = array($dimensions_value, $fps_value); $formats = array(); foreach ($parts as $key => $part) { $part = trim($part); if (!in_array($part, $other_parts)) { array_push($formats, $part); } } $data['video']['pixel_format'] = $formats[1]; $data['video']['codec'] = $formats[0]; } // match the audio stream info preg_match('/Stream(.*): Audio: (.*)/', $buffer, $matches); if (count($matches) > 0) { // setup audio values $data['audio'] = array('stereo' => -1, 'sample_rate' => -1, 'sample_rate' => -1); $other_parts = array(); // get the stereo value preg_match('/(stereo|mono)/i', $matches[0], $stereo_matches); if (count($stereo_matches)) { $data['audio']['stereo'] = $stereo_matches[0]; array_push($other_parts, $stereo_matches[0]); } // get the sample_rate preg_match('/([0-9]{3,6}) Hz/', $matches[0], $sample_matches); if (count($sample_matches)) { $data['audio']['sample_rate'] = count($sample_matches) ? floatval($sample_matches[1]) : -1; array_push($other_parts, $sample_matches[0]); } // get the bit rate preg_match('/([0-9]{1,3}) kb\\/s/', $matches[0], $bitrate_matches); if (count($bitrate_matches)) { $data['audio']['bitrate'] = count($bitrate_matches) ? floatval($bitrate_matches[1]) : -1; array_push($other_parts, $bitrate_matches[0]); } // formats should be anything left over, let me know if anything else exists $parts = explode(',', $matches[2]); $formats = array(); foreach ($parts as $key => $part) { $part = trim($part); if (!in_array($part, $other_parts)) { array_push($formats, $part); } } $data['audio']['codec'] = $formats[0]; } // check that some data has been obtained if (!count($data)) { $data = false; } else { $data['_raw_info'] = $buffer; } // fclose($handle); // } // if(is_file($info_file)) // { // if the info file exists remove it // unlink($info_file); // } // cache info and return return self::$_file_info[$hash] = $data; }
/** * Determines if the input media has an audio stream. * * @access public * @param string $file The absolute path of the file that is required to be manipulated. * @return bool * */ public function fileHasAudio($file = FALSE) { // check to see if this is a static call if ($file !== FALSE && isset($this) === FALSE) { $toolkit = new PHPVideoToolkit(); $data = $toolkit->getFileInfo($file); } elseif ($file === FALSE) { if (!$this->_input_file) { // input file not valid return $this->_raiseError('inputFileHasAudio_no_input'); // <- exits } $file = $this->_input_file; $data = $this->getFileInfo($file); } return isset($data['audio']); }
$filename = basename($file); $filename_minus_ext = substr($filename, 0, strrpos($filename, '.')); echo '<strong>Extracting ' . $filename . '</strong><br />'; // set the input file $ok = $toolkit->setInputFile($file, $extraction_frame_rate); // check the return value in-case of error if (!$ok) { // if there was an error then get it echo '<b>' . $toolkit->getLastError() . "</b><br />\r\n"; $toolkit->reset(); continue; } // set the output dimensions $toolkit->setVideoOutputDimensions(160, 120); // extract thumbnails from the third second of the video, but we only want to limit the number of frames to 10 $info = $toolkit->getFileInfo(); echo 'We are extracting frames at a rate of ' . $extraction_frame_rate . '/second so for this file we should have ' . ceil($info['duration']['seconds'] * $extraction_frame_rate) . ' frames below.<br />'; $toolkit->extractFrames('00:00:00', false, $extraction_frame_rate, false, '%hh:%mm:%ss'); // set the output details $ok = $toolkit->setOutput($thumbnail_output_dir, $filename_minus_ext . '[%timecode].jpg', PHPVideoToolkit::OVERWRITE_EXISTING); // $ok = $toolkit->setOutput($thumbnail_output_dir, $filename_minus_ext.'[%12index].jpg', PHPVideoToolkit::OVERWRITE_EXISTING); // check the return value in-case of error if (!$ok) { // if there was an error then get it echo '<b>' . $toolkit->getLastError() . "</b><br />\r\n"; $toolkit->reset(); continue; } // execute the ffmpeg command $result = $toolkit->execute(false, true); // get the last command given
/** * Determines if the input media has an audio stream. * * @access public * @param string $file The absolute path of the file that is required to be manipulated. * @return bool **/ function fileHasAudio($file=false) { // check to see if this is a static call if($file !== false && !isset($this)) { $toolkit = new PHPVideoToolkit(); $data = $toolkit->getFileInfo($file); } // if the file has not been specified check to see if an input file has been specified else if($file === false) { if(!$this->_input_file) { // input file not valid return $this->_raiseError('inputFileHasAudio_no_input'); //<- exits } $file = $this->_input_file; $data = $this->getFileInfo($file); } return isset($data['audio']); }
$files_to_process = array(PHPVIDEOTOOLKIT_EXAMPLE_ABSOLUTE_BATH . 'to-be-processed/MOV00007.3gp', PHPVIDEOTOOLKIT_EXAMPLE_ABSOLUTE_BATH . 'to-be-processed/Video000.3gp', PHPVIDEOTOOLKIT_EXAMPLE_ABSOLUTE_BATH . 'to-be-processed/cat.mpeg'); // output files dirname has to exist $video_output_dir = PHPVIDEOTOOLKIT_EXAMPLE_ABSOLUTE_BATH . 'processed/videos/'; // bit rate of audio (valid vaues are 16,32,64) $bitrate = 64; // sampling rate (valid values are 11025, 22050, 44100) $samprate = 44100; // start PHPVideoToolkit class $toolkit = new PHPVideoToolkit($tmp_dir); // set PHPVideoToolkit class to run silently $toolkit->on_error_die = FALSE; // loop through the files to process foreach ($files_to_process as $file) { echo '<strong>Information for : ' . $file . "</strong><br />\r\n"; // set the input file $ok = $toolkit->setInputFile($file); // check the return value in-case of error if (!$ok) { // if there was an error then get it echo $toolkit->getLastError() . "<br />\r\n"; $toolkit->reset(); continue; } $data = $toolkit->getFileInfo(); echo '<pre>'; print_r($data); echo '</pre>'; // reset $toolkit->reset(); } echo '</body></html>';
/** * get mpeg length * * @param string $file * @return void * @author Andy Bennett */ public static function get_mpeg_length($file) { if (!defined('PHPVIDEOTOOLKIT_FFMPEG_BINARY')) { define('PHPVIDEOTOOLKIT_FFMPEG_BINARY', Kohana::config('ffmpeg.binaries_path')); } // require the library require_once 'phpvideotoolkit/phpvideotoolkit.php5.php'; $tmp_dir = Kohana::config('ffmpeg.tmp_path'); // start PHPVideoToolkit class $toolkit = new PHPVideoToolkit($tmp_dir); // set PHPVideoToolkit class to run silently $toolkit->on_error_die = FALSE; $ok = $toolkit->setInputFile($file); // check the return value in-case of error if ($ok) { $data = $toolkit->getFileInfo(); $toolkit->reset(); return $data['duration']['timecode']['rounded']; } else { return '00'; } }