<?php include_once './includes/bootstrap.php'; try { $video = new \PHPVideoToolkit\Video($example_video_path, $config); $result = $video->extractFrame(new \PHPVideoToolkit\Timecode('00:00:50.00'))->save('./output/extract-frame.example2.jpg'); // $result is an \PHPVideoToolkit\Image object on success } catch (\PHPVideoToolkit\Exception $e) { \PHPVideoToolkit\Trace::vars($video->getProcess()->getExecutedCommand()); \PHPVideoToolkit\Trace::vars($video->getProcess()->getBuffer()); \PHPVideoToolkit\Trace::vars($video->getProcess()->getLastSplit()); \PHPVideoToolkit\Trace::vars($e); }
/** * Generates a poster image from the video source file * * @param \WIRO\Html5mediaelements\Domain\Model\Media $media media record * @param array $config conversion configuration for poster * @return void */ protected function generatePosterImage(Media $media, array $config) { // Create a local file for processing $originalFile = $media->getSourceFile()->getOriginalResource()->getOriginalFile(); $localFile = $originalFile->getForLocalProcessing(FALSE); // Extract video information $parser = new \PHPVideoToolkit\MediaParser(); $fileInfo = $parser->getFileInformation($localFile); // Decide which frame from the video should be extracted switch ($media->getAutoPoster()) { // First frame case Media::AUTO_POSTER_START: $timecode = $fileInfo['start']; break; // Middle frame // Middle frame case Media::AUTO_POSTER_MIDDLE: $timecode = new \PHPVideoToolkit\Timecode($fileInfo['duration']->total_seconds / 2); break; // Last frame // Last frame case Media::AUTO_POSTER_END: $timecode = $fileInfo['duration']; break; // Invalid configuration => Skip // Invalid configuration => Skip default: return; } // Generate poster file name $fileName = $this->generateFilename($originalFile, $config['filename']); $filePath = $this->tempPathFiles . $fileName; // Configure output format $className = '\\PHPVideoToolkit\\ImageFormat_' . ucfirst($config['format']); if ($config['format'] && class_exists($className)) { $format = new $className(); } else { $format = \PHPVideoToolkit\Format::getFormatFor($filePath, null, 'ImageFormat'); } // Set format options from configuration if ($config['quality']) { $format->setVideoQuality($config['quality']); } if ($config['width'] && $config['height']) { $format->setVideoDimensions($config['width'], $config['height'], true); } // Extract video frame $video = new \PHPVideoToolkit\Video($localFile); $process = $video->extractFrame($timecode)->save($filePath, $format, \PHPVideoToolkit\Media::OVERWRITE_UNIQUE); // Add poster image to FAL $filePath = $process->getOutput()->getMediaPath(); $file = $originalFile->getParentFolder()->addFile($filePath, $fileName, 'changeName'); // TODO check for permission of user (_cli_scheduler) // Add reference to poster image in media record $errors = $this->addFileReference('tx_html5mediaelements_domain_model_media', 'poster', $media, $file); // Output errors if (!empty($errors)) { throw new \Exception(implode("\n", $errors)); } }
/** * Uploads a new image * @global type $CFG * @param type $recordID * @param type $userID * @param type $file * @param type $label * @param type $tags * @param type $db * @return type */ public function upload_media_video($ffmpeg, $recordID, $file, $label, $tags = '') { global $CFG; //$myFile = "logfile.txt"; $maxsize = 100000000; $filesize = 0; if (is_uploaded_file($file['tmp_name'])) { // check the file is less than the maximum file size if ($file['size'] < $maxsize) { // $filesize = $file['size']; $extArr = explode(".", strtolower($file['name'])); $filetype = end($extArr); // database connection try { $conn = new PDO("mysql:host={$CFG->db};dbname={$CFG->schema}", $CFG->dbuser, $CFG->dbuserpass); } catch (PDOException $e) { die('<data><error>failed connecting to database</error><detail>' . $e->getMessage() . '</detail></data>'); } // move the file to a new place, using a hash. Check that this file hasn't been already uploaded- if so, simply refer to that instead $contenthash = md5(file_get_contents($file['tmp_name'])); $raw_file = $CFG->site_root . "media/" . $contenthash; if (!file_exists($raw_file)) { if (!move_uploaded_file($file['tmp_name'], $raw_file)) { return "<data><error>problem moving file</error><detail/></data>"; } } // if the move is successful, make a screenshot $video = new PHPVideoToolkit\Video($raw_file); $process = $video->extractFrame(new \PHPVideoToolkit\Timecode(10))->save($CFG->site_root . "media/vidthumbs/" . $contenthash . ".jpg", null, \PHPVideoToolkit\Media::OVERWRITE_EXISTING); // make the screenshot smaller $img1 = imagecreatefromjpeg($CFG->site_root . "media/vidthumbs/" . $contenthash . ".jpg"); $width = imagesx($img1); $height = imagesy($img1); $new_height = 75; $new_width = floor($width * ($new_height / $height)); $thumb = imagecreatetruecolor($new_width, $new_height); imagecopyresized($thumb, $img1, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // get an overlay $img2 = imagecreatefrompng($CFG->site_root . "icons/video.png"); // combine them imagecopyresampled($thumb, $img2, 0, 0, 0, 0, 48, 48, 48, 48); // write back to file imagejpeg($thumb, $CFG->site_root . "media/vidthumbs/" . $contenthash . ".jpg"); // our sql query $sql = "INSERT INTO `media` (`recordID`, `type`, `size`, `label`, `name`, `file_path`, `thumb_path`) VALUES (:recordID, :filetype, :filesize, :label, :filename, :raw_file, :thumb_path)"; $stmt = $conn->prepare($sql); $stmt->bindValue(':recordID', $recordID, PDO::PARAM_INT); $stmt->bindValue(':filetype', $filetype, PDO::PARAM_STR); $stmt->bindValue(':filesize', $file['size'], PDO::PARAM_STR); $stmt->bindValue(':label', $label, PDO::PARAM_STR); $stmt->bindValue(':filename', $file['name'], PDO::PARAM_STR); $stmt->bindValue(':raw_file', $raw_file, PDO::PARAM_STR); $stmt->bindValue(':thumb_path', $CFG->site_root . "media/vidthumbs/" . $contenthash . ".jpg", PDO::PARAM_STR); if (!$stmt->execute()) { return '<data><error>file entry into database failed</error><detail>' . var_dump($stmt->errorInfo()) . '</detail></data>'; } } else { return '<data><error>file too big</error><detail></detail></data>'; } } else { return '<data><error>moving file to temp failed</error><detail></detail></data>'; } return '<data><result>ok</result></data>'; }
<?php include_once './includes/bootstrap.php'; try { $video = new \PHPVideoToolkit\Video($example_video_path, $config); $result = $video->extractFrame(new \PHPVideoToolkit\Timecode(50))->save('./output/extract-frame.example1.jpg'); // $result is an \PHPVideoToolkit\Image object on success } catch (\PHPVideoToolkit\Exception $e) { \PHPVideoToolkit\Trace::vars($video->getProcess()->getExecutedCommand()); \PHPVideoToolkit\Trace::vars($video->getProcess()->getBuffer()); \PHPVideoToolkit\Trace::vars($video->getProcess()->getLastSplit()); \PHPVideoToolkit\Trace::vars($e); }