public static function gif($file, $options=array(), $target_extension='gif') { // merge the options with the defaults $options = array_merge(array( 'temp_dir' => '/tmp', 'width' => 320, 'height' => 240, 'ratio' => false, //PHPVideoToolkit::RATIO_STANDARD, 'frame_rate' => 1, 'loop_output' => 0, // 0 will loop endlessly 'output_dir' => null, // this doesn't have to be set it can be automatically retreived from 'output_file' 'output_file' => '#filename.#ext', // you can use #filename to automagically hold the filename and #ext to automagically hold the target format extension 'output_title' => '#filename', // you can use #filename to automagically hold the filename and #ext to automagically hold the target format extension 'use_multipass' => false, 'generate_log' => true, 'log_directory' => null, 'die_on_error' => false, 'overwrite_mode' => PHPVideoToolkit::OVERWRITE_FAIL ), $options); // start PHPVideoToolkit class require_once dirname(dirname(__FILE__)).DS.'phpvideotoolkit.php5.php'; $toolkit = new PHPVideoToolkit($options['temp_dir']); $toolkit->on_error_die = $options['die_on_error']; // get the output directory if($options['output_dir']) { $output_dir = $options['output_dir']; } else { $output_dir = dirname($options['output_file']); $output_dir = $output_dir == '.' ? dirname($file) : $output_dir; } // get the filename parts $filename = basename($file); $filename_minus_ext = substr($filename, 0, strrpos($filename, '.')); // get the output filename $output_filename = str_replace(array('#filename', '#ext'), array($filename_minus_ext, $target_extension), basename($options['output_file'])); // set the input file $ok = $toolkit->setInputFile($file); // check the return value in-case of error if(!$ok) { $toolkit->reset(); array_push(self::$_error_messages, $toolkit->getLastError()); return false; } $toolkit->setFormat(PHPVideoToolkit::FORMAT_GIF); $toolkit->disableAudio(); if($options['ratio'] !== false) { $toolkit->setVideoAspectRatio($options['ratio']); } $toolkit->setVideoOutputDimensions($options['width'], $options['height']); $toolkit->setVideoFrameRate($options['frame_rate']); $toolkit->addCommand('-loop_output', $options['loop_output']); // set the output details and overwrite if nessecary $ok = $toolkit->setOutput($output_dir, $output_filename, $options['overwrite_mode']); // check the return value in-case of error if(!$ok) { $toolkit->reset(); array_push(self::$_error_messages, $toolkit->getLastError()); return false; } // execute the ffmpeg command using multiple passes and log the calls and PHPVideoToolkit results $result = $toolkit->execute($options['use_multipass'], $options['generate_log']); array_push(self::$_commands, $toolkit->getLastCommand()); // check the return value in-case of error if($result !== PHPVideoToolkit::RESULT_OK) { // move the log file to the log directory as something has gone wrong if($options['generate_log']) { $log_dir = $options['log_directory'] ? $options['log_directory'] : $output_dir; $toolkit->moveLog($log_dir.$filename_minus_ext.'.log'); array_push(self::$_log_files, $log_dir.$filename_minus_ext.'.log'); } $toolkit->reset(); array_push(self::$_error_messages, $toolkit->getLastError()); return $result; } array_push(self::$_outputs, $toolkit->getLastOutput()); // reset $toolkit->reset(); return $result; }
// set phpvideotoolkit class to run silently $toolkit->on_error_die = FALSE; // start the timer collection $total_process_time = 0; // loop through the files to process foreach ($files_to_process as $key => $file) { // get the filename parts $filename = basename($file); $filename_minus_ext = substr($filename, 0, strrpos($filename, '.')); // 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 '<b>' . $toolkit->getLastError() . "</b><br />\r\n"; $toolkit->reset(); continue; } // set the output dimensions $toolkit->setVideoOutputDimensions(PHPVideoToolkit::SIZE_SQCIF); // extract a thumbnail from the fifth frame two seconds into the video $toolkit->extractFrame('00:00:02.5'); // set the output details $ok = $toolkit->setOutput($thumbnail_output_dir, $filename_minus_ext . '.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; }
/** * extract frame * * @param string $file * @param string $time_frame * @return void * @author Andy Bennett */ public static function extract_frame($file = null, $time_frame = '00:00:00.1') { if (is_null($file)) { return null; } if (!defined('PHPVIDEOTOOLKIT_FFMPEG_BINARY')) { define('PHPVIDEOTOOLKIT_FFMPEG_BINARY', Kohana::config('ffmpeg.binaries_path')); } try { // require the library require_once 'phpvideotoolkit/phpvideotoolkit.php5.php'; // start the timer collection $total_process_time = 0; $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; // check the return value in-case of error if (!$toolkit->setInputFile($file)) { throw new Exception($toolkit->getLastError()); } // set the output dimensions $toolkit->setVideoOutputDimensions(PHPVideoToolkit::SIZE_SAS); // same as source // extract a thumbnail $toolkit->extractFrame($time_frame); // get the filename parts $path = dirname($file) . '/'; $filename = basename($file); $filename_minus_ext = substr($filename, 0, strrpos($filename, '.')); // set the output details $ok = $toolkit->setOutput($path, $filename_minus_ext . '.jpg', PHPVideoToolkit::OVERWRITE_EXISTING); // check the return value in-case of error if (!$ok) { throw new Exception($toolkit->getLastError()); } // execute the ffmpeg command $result = $toolkit->execute(false, true); // check the return value in-case of error if ($result !== PHPVideoToolkit::RESULT_OK) { throw new Exception($toolkit->getLastError()); } // reset $toolkit->reset(); return true; } catch (Exception $e) { // echo $toolkit->getLastCommand().'<br />'; echo $e->getMessage(); $toolkit->reset(); } }