$thumbnail_output_dir = PHPVIDEOTOOLKIT_EXAMPLE_ABSOLUTE_BATH . 'processed/thumbnails/'; // log dir $log_dir = PHPVIDEOTOOLKIT_EXAMPLE_ABSOLUTE_BATH . 'logs/'; // start phpvideotoolkit class $toolkit = new PHPVideoToolkit($tmp_dir); // 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) {
// start PHPVideoToolkit class $toolkit = new PHPVideoToolkit($tmp_dir); // set PHPVideoToolkit class to run silently $toolkit->on_error_die = FALSE; // the number of frames to extract per second $extraction_frame_rate = 5; // 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, '.')); 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);
/** * Add a post */ public function iframe_add() { $this->setView('iframe_add.php'); @set_time_limit(0); $uploaded_files = array(); try { if (!isset(User_Model::$auth_data)) { throw new Exception(__('POST_ADD_ERROR_SESSION_EXPIRED')); } $is_student = isset(User_Model::$auth_data['student_number']); // Message $message = isset($_POST['message']) ? trim($_POST['message']) : ''; if ($message == '' || $message == __('PUBLISH_DEFAULT_MESSAGE')) { throw new Exception(__('POST_ADD_ERROR_NO_MESSAGE')); } $message = preg_replace('#\\n{2,}#', "\n\n", $message); // Category if (!isset($_POST['category']) || !ctype_digit($_POST['category'])) { throw new Exception(__('POST_ADD_ERROR_NO_CATEGORY')); } $category = (int) $_POST['category']; // Official post (in a group) $official = isset($_POST['official']); // Group $group = isset($_POST['group']) && ctype_digit($_POST['group']) ? (int) $_POST['group'] : 0; if ($group == 0) { $group = null; $official = false; } else { $groups_auth = Group_Model::getAuth(); if (isset($groups_auth[$group])) { if ($official && !$groups_auth[$group]['admin']) { throw new Exception(__('POST_ADD_ERROR_OFFICIAL')); } } else { throw new Exception(__('POST_ADD_ERROR_GROUP_NOT_FOUND')); } } // Private message $private = isset($_POST['private']); if ($private && !$is_student) { throw new Exception(__('POST_ADD_ERROR_PRIVATE')); } $attachments = array(); // Photos if (isset($_FILES['attachment_photo']) && is_array($_FILES['attachment_photo']['name'])) { foreach ($_FILES['attachment_photo']['size'] as $size) { if ($size > Config::UPLOAD_MAX_SIZE_PHOTO) { throw new Exception(__('POST_ADD_ERROR_PHOTO_SIZE', array('size' => File::humanReadableSize(Config::UPLOAD_MAX_SIZE_PHOTO)))); } } if ($filepaths = File::upload('attachment_photo')) { foreach ($filepaths as $filepath) { $uploaded_files[] = $filepath; } foreach ($filepaths as $i => $filepath) { $name = isset($_FILES['attachment_photo']['name'][$i]) ? $_FILES['attachment_photo']['name'][$i] : ''; try { $img = new Image(); $img->load($filepath); $type = $img->getType(); if ($type == IMAGETYPE_JPEG) { $ext = 'jpg'; } else { if ($type == IMAGETYPE_GIF) { $ext = 'gif'; } else { if ($type == IMAGETYPE_PNG) { $ext = 'png'; } else { throw new Exception(); } } } if ($img->getWidth() > 800) { $img->setWidth(800, true); } $img->save($filepath); // Thumb $thumbpath = $filepath . '.thumb'; $img->thumb(Config::$THUMBS_SIZES[0], Config::$THUMBS_SIZES[1]); $img->setType(IMAGETYPE_JPEG); $img->save($thumbpath); unset($img); $attachments[] = array($filepath, $name, $thumbpath); $uploaded_files[] = $thumbpath; } catch (Exception $e) { throw new Exception(__('POST_ADD_ERROR_PHOTO_FORMAT')); } } } } // Vidéos /* @uses PHPVideoToolkit : http://code.google.com/p/phpvideotoolkit/ * @requires ffmpeg, php5-ffmpeg */ if (isset($_FILES['attachment_video']) && is_array($_FILES['attachment_video']['name'])) { foreach ($_FILES['attachment_video']['size'] as $size) { if ($size > Config::UPLOAD_MAX_SIZE_VIDEO) { throw new Exception(__('POST_ADD_ERROR_VIDEO_SIZE', array('size' => File::humanReadableSize(Config::UPLOAD_MAX_SIZE_VIDEO)))); } } if ($filepaths = File::upload('attachment_video')) { foreach ($filepaths as $filepath) { $uploaded_files[] = $filepath; } foreach ($filepaths as $i => $filepath) { $name = isset($_FILES['attachment_video']['name'][$i]) ? $_FILES['attachment_video']['name'][$i] : ''; try { $video = new ffmpeg_movie($filepath, false); if (!$video->hasVideo()) { throw new Exception('No video stream found in the file'); } if (!$video->hasAudio()) { throw new Exception('No audio stream found in the file'); } } catch (Exception $e) { throw new Exception(__('POST_ADD_ERROR_VIDEO_FORMAT')); } // Video conversion try { $video_current_width = $video->getFrameWidth(); $video_width = min($video_current_width, Config::VIDEO_MAX_WIDTH); if ($video_width % 2 == 1) { // Even number required $video_width--; } $video_height = $video_width * $video->getFrameHeight() / $video_current_width; if ($video_height % 2 == 1) { // Even number required $video_height--; } // Extract thumb $video_thumb = $video->getFrame(round($video->getFrameCount() * 0.2)); unset($video); $video_thumb = $video_thumb->toGDImage(); $thumbpath = DATA_DIR . Config::DIR_DATA_TMP . File::getName($filepath) . '.thumb'; imagejpeg($video_thumb, $thumbpath, 95); unset($video_thumb); $img = new Image(); $img->load($thumbpath); $img->setWidth($video_width, true); $img->setType(IMAGETYPE_JPEG); $img->save($thumbpath); $uploaded_files[] = $thumbpath; unset($img); // Convert to FLV if (!preg_match('#\\.flv$#i', $filepath)) { $toolkit = new PHPVideoToolkit(); $toolkit->on_error_die = true; // Will throw exception on error $toolkit->setInputFile($filepath); $toolkit->setVideoOutputDimensions($video_width, $video_height); $toolkit->setFormatToFLV(Config::VIDEO_SAMPLING_RATE, Config::VIDEO_AUDIO_BIT_RATE); $toolkit->setOutput(DATA_DIR . Config::DIR_DATA_TMP, File::getName($filepath) . '.flv', PHPVideoToolkit::OVERWRITE_EXISTING); $toolkit->execute(false, false); // Multipass: false, Log: false File::delete($filepath); $filepath = $toolkit->getLastOutput(); $filepath = $filepath[0]; unset($toolkit); } $attachments[] = array($filepath, $name, $thumbpath); $uploaded_files[] = $filepath; } catch (Exception $e) { throw new Exception(__('POST_ADD_ERROR_VIDEO_CONVERT') . $e->getMessage()); } } } } // Audios if (isset($_FILES['attachment_audio']) && is_array($_FILES['attachment_audio']['name'])) { foreach ($_FILES['attachment_audio']['size'] as $size) { if ($size > Config::UPLOAD_MAX_SIZE_AUDIO) { throw new Exception(__('POST_ADD_ERROR_AUDIO_SIZE', array('size' => File::humanReadableSize(Config::UPLOAD_MAX_SIZE_AUDIO)))); } } if ($filepaths = File::upload('attachment_audio')) { foreach ($filepaths as $filepath) { $uploaded_files[] = $filepath; } foreach ($filepaths as $i => $filepath) { if (!preg_match('#\\.mp3$#', $filepath)) { throw new Exception(__('POST_ADD_ERROR_AUDIO_FORMAT')); } $name = isset($_FILES['attachment_audio']['name'][$i]) ? $_FILES['attachment_audio']['name'][$i] : ''; $attachments[] = array($filepath, $name); } } } // Files if (isset($_FILES['attachment_file']) && is_array($_FILES['attachment_file']['name'])) { foreach ($_FILES['attachment_file']['size'] as $size) { if ($size > Config::UPLOAD_MAX_SIZE_FILE) { throw new Exception(__('POST_ADD_ERROR_FILE_SIZE', array('size' => File::humanReadableSize(Config::UPLOAD_MAX_SIZE_FILE)))); } } if ($filepaths = File::upload('attachment_file')) { foreach ($filepaths as $filepath) { $uploaded_files[] = $filepath; } foreach ($filepaths as $i => $filepath) { if (!preg_match('#\\.[a-z0-9]{2,4}$#i', $filepath)) { throw new Exception(__('POST_ADD_ERROR_FILE_FORMAT')); } if (preg_match('#\\.(jpg|png|gif|mp3|flv)$#i', $filepath)) { throw new Exception(__('POST_ADD_ERROR_FILE_FORMAT2')); } $name = isset($_FILES['attachment_file']['name'][$i]) ? $_FILES['attachment_file']['name'][$i] : ''; $attachments[] = array($filepath, $name); } } } // Event if (isset($_POST['event_title']) && isset($_POST['event_start']) && isset($_POST['event_end'])) { // Title $event_title = trim($_POST['event_title']); if ($event_title == '') { throw new Exception(__('POST_ADD_ERROR_EVENT_NO_TITLE')); } // Dates if (!($event_start = strptime($_POST['event_start'], __('PUBLISH_EVENT_DATE_FORMAT')))) { throw new Exception(__('POST_ADD_ERROR_EVENT_DATE')); } if (!($event_end = strptime($_POST['event_end'], __('PUBLISH_EVENT_DATE_FORMAT')))) { throw new Exception(__('POST_ADD_ERROR_EVENT_DATE')); } $event_start = mktime($event_start['tm_hour'], $event_start['tm_min'], 0, $event_start['tm_mon'] + 1, $event_start['tm_mday'], $event_start['tm_year'] + 1900); $event_end = mktime($event_end['tm_hour'], $event_end['tm_min'], 0, $event_end['tm_mon'] + 1, $event_end['tm_mday'], $event_end['tm_year'] + 1900); if ($event_start > $event_end) { throw new Exception(__('POST_ADD_ERROR_EVENT_DATE_ORDER')); } $event = array($event_title, $event_start, $event_end); } else { $event = null; } // Survey if (isset($_POST['survey_question']) && isset($_POST['survey_end']) && isset($_POST['survey_answer']) && is_array($_POST['survey_answer'])) { // Question $survey_question = trim($_POST['survey_question']); if ($survey_question == '') { throw new Exception(__('POST_ADD_ERROR_SURVEY_NO_QUESTION')); } // Date if (!($survey_end = strptime($_POST['survey_end'], __('PUBLISH_EVENT_DATE_FORMAT')))) { throw new Exception(__('POST_ADD_ERROR_SURVEY_DATE')); } $survey_end = mktime($survey_end['tm_hour'], $survey_end['tm_min'], 0, $survey_end['tm_mon'] + 1, $survey_end['tm_mday'], $survey_end['tm_year'] + 1900); // Multiple answers $survey_multiple = isset($_POST['survey_multiple']); // Answers $survey_answers = array(); foreach ($_POST['survey_answer'] as $survey_answer) { $survey_answer = trim($survey_answer); if ($survey_answer != '') { $survey_answers[] = $survey_answer; } } if (count($survey_answers) < 2) { throw new Exception(__('POST_ADD_ERROR_SURVEY_ANSWERS')); } $survey = array($survey_question, $survey_end, $survey_multiple, $survey_answers); } else { $survey = null; } // Creation of the post $id = $this->model->addPost((int) User_Model::$auth_data['id'], $message, $category, $group, $official, $private); // Attach files foreach ($attachments as $attachment) { $this->model->attachFile($id, $attachment[0], $attachment[1], isset($attachment[2]) ? $attachment[2] : null); } // Event if (isset($event)) { $this->model->attachEvent($id, $event[0], $event[1], $event[2]); } // Survey if (isset($survey)) { $this->model->attachSurvey($id, $survey[0], $survey[1], $survey[2], $survey[3]); } $this->addJSCode(' parent.location = "' . Config::URL_ROOT . Routes::getPage('home') . '"; '); } catch (Exception $e) { // Delete all uploading files in tmp foreach ($uploaded_files as $uploaded_file) { File::delete($uploaded_file); } $this->addJSCode(' with(parent){ Post.errorForm(' . json_encode($e->getMessage()) . '); } '); } }
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; }
/** * 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(); } }
echo 'You FFmpeg binary has NOT been compiled with vhook support, you can not watermark video, you can however watermark image outputs.<br /><a href="?gd=1">Click here</a> to run the watermark demo on images only.<br />'; exit; //<- exits } echo 'You FFmpeg binary has been compiled with vhook support.<br /><br />'; } else { echo '<strong>GD watermarking only...</strong><br />'; echo 'Your FFmpeg binary has NOT been compiled with vhook support and we are only testing automated watermarking of images via GD now.<br /><a href="?gd=0">Click here</a> to go back to the vhook watermarking demo.<br /><br />'; } // set ffmpeg class to run silently $toolkit->on_error_die = FALSE; // get the filename parts $filename = basename($video_to_process); $filename_minus_ext = substr($filename, 0, strrpos($filename, '.')); // set the input file $ok = $toolkit->setInputFile($video_to_process); // 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"; exit; } // set the output dimensions $toolkit->setVideoOutputDimensions(PHPVideoToolkit::SIZE_SAS); // are we vhooking the videos? if ($use_vhook) { $toolkit->addWatermark($watermark); $ok = $toolkit->setOutput($video_output_dir, $filename_minus_ext . '-watermarked.3gp', PHPVideoToolkit::OVERWRITE_EXISTING); } else { $toolkit->addGDWatermark($watermark, array('x-offset' => -15, 'y-offset' => -15, 'position' => 'center-middle')); // extract a single frame
// log dir $log_dir = PHPVIDEOTOOLKIT_EXAMPLE_ABSOLUTE_BATH . 'logs/'; // 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; $input_file = array_pop($files_to_process); // get the filename parts $filename = basename($input_file); $filename_minus_ext = substr($filename, 0, strrpos($filename, '.')); // set the input file $ok = $toolkit->setInputFile($input_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(); exit; } // $toolkit->setFormat(PHPVideoToolkit::FORMAT_MPEGVIDEO); $toolkit->setVideoOutputDimensions(PHPVideoToolkit::SIZE_QVGA); // loop through the files to process foreach ($files_to_process as $file) { $toolkit->addVideo($file); } // set the output details and overwrite if nessecary $ok = $toolkit->setOutput($video_output_dir, $filename_minus_ext . '-joined.mpeg', PHPVideoToolkit::OVERWRITE_EXISTING);
echo '<span style="font-size:12px;">• The media is embedded using <a href="http://sourceforge.net/projects/pluginobject/">PluginObject</a> to embed the examples. It is distributed under a BSD License.</span><br /><br />'; } // load the examples configuration $ignore_config_output = true; require_once 'example-config.php'; // set the flv file $flv = PHPVIDEOTOOLKIT_EXAMPLE_ABSOLUTE_BATH . 'to-be-processed/rickroll.flv'; if (isset($_GET['file'])) { // require the library require_once '../phpvideotoolkit.' . $use_version . '.php'; // temp directory $tmp_dir = PHPVIDEOTOOLKIT_EXAMPLE_ABSOLUTE_BATH . 'tmp/'; // start ffmpeg class $toolkit = new PHPVideoToolkit($tmp_dir); // set the flv input $toolkit->setInputFile($flv); // get the incoming stream position $stream_pos = isset($_GET['pos']) ? $_GET['pos'] : 0; // in this example we will enable bandwidth limiting at the extreme and is not really practicle for live purposes // it will only release 100 bytes of the file every second, thus it should take roughly 5 minutes to release a 29Mb file // it will also prevent the browser cache from retaining the file. $toolkit->flvStreamSeek($stream_pos, array('active' => true, 'packet_size' => $packet_size, 'packet_interval' => $packet_interval), false); exit; //<- exits } $size = filesize($flv); echo '<strong>Bandwidth Restrictions and Download Rate.</strong><br />'; echo 'The flv media is ' . $size . ' bytes, using the bandwidth speed limit of ' . $packet_interval . ' kb/s media should be completely loaded in roughly ' . round($size / ($packet_interval * 1024 * $packet_size), 1) . ' seconds.<br />'; echo 'This may not appear to be the case in the player as the player will buffer the contents, You should notice that after a while the player will re-buffer the file. This is because the file was not loaded directly but through this script which buffered the release of the flv.<br />'; echo 'However you can test this better by downloading <a href="example13.php?file=to-be-processed/rickroll.flv&pos=0&packet_size=' . $packet_size . '">this link</a>.<br />'; echo 'You should notice that the file is downloading as a normal file off the internet, however the flv will download at a extremely slowed rate (more like dial-up speed than broadband or DSL) even if you are testing this script on your localhost.<br />';