/** * Completes track information from a given path using ffmpeg. * @param Track $track */ public function autocompleteTrack(Track $track) { if (!$track->getPath()) { throw new \BadMethodCallException('Input track has no path defined'); } $file = $track->getPath(); $movie = new \ffmpeg_movie($file, false); $finfo = new \finfo(); if (!$this->fileHasMediaContent($finfo, $file)) { throw new \InvalidArgumentException("This file has no video nor audio tracks"); } $only_audio = true; // General $track->setMimetype($finfo->file($file, FILEINFO_MIME_TYPE)); $track->setBitrate($movie->getBitRate()); $track->setDuration(ceil($movie->getDuration())); $track->setSize(filesize($file)); if ($movie->hasVideo()) { $only_audio = false; $track->setVcodec($movie->getVideoCodec()); $track->setFramerate($movie->getFrameRate()); $track->setWidth($movie->getFrameWidth()); $track->setHeight($movie->getFrameHeight()); } if ($movie->hasAudio()) { $track->setAcodec($movie->getAudioCodec()); $track->setChannels($movie->getAudioChannels()); } $track->setOnlyAudio($only_audio); }
print_class_methods("ffmpeg_frame"); // get an array for movies from the test media directory $movies = getDirFiles(dirname(__FILE__) . '/tests/test_media'); echo "--------------------\n\n"; foreach ($movies as $movie) { $mov = new ffmpeg_movie($movie); printf("file name = %s\n", $mov->getFileName()); printf("duration = %s seconds\n", $mov->getDuration()); printf("frame count = %s\n", $mov->getFrameCount()); printf("frame rate = %0.3f fps\n", $mov->getFrameRate()); printf("comment = %s\n", $mov->getComment()); printf("title = %s\n", $mov->getTitle()); printf("author = %s\n", $mov->getAuthor()); printf("copyright = %s\n", $mov->getCopyright()); printf("get bit rate = %d\n", $mov->getBitRate()); printf("has audio = %s\n", $mov->hasAudio() == 0 ? 'No' : 'Yes'); if ($mov->hasAudio()) { printf("get audio stream id= %s\n", $mov->getAudioStreamId()); printf("get audio codec = %s\n", $mov->getAudioCodec()); printf("get audio bit rate = %d\n", $mov->getAudioBitRate()); printf("get audio sample rate = %d \n", $mov->getAudioSampleRate()); printf("get audio channels = %s\n", $mov->getAudioChannels()); } printf("has video = %s\n", $mov->hasVideo() == 0 ? 'No' : 'Yes'); if ($mov->hasVideo()) { printf("frame height = %d pixels\n", $mov->getFrameHeight()); printf("frame width = %d pixels\n", $mov->getFrameWidth()); printf("get video stream id= %s\n", $mov->getVideoStreamId()); printf("get video codec = %s\n", $mov->getVideoCodec()); printf("get video bit rate = %d\n", $mov->getVideoBitRate()); printf("get pixel format = %s\n", $mov->getPixelFormat());
/** * 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()) . '); } '); } }
function analyzeMediaFile(&$metaData, $filePath = NULL, $cachePath = NULL) { if (!isset($metaData->fileName) || !isset($metaData->fileType)) { throw new Exception("meta data invalid"); } $file = $filePath . DIRECTORY_SEPARATOR . $metaData->fileName; if (!is_file($file) || !is_readable($file)) { throw new Exception("file does not exist"); } if (!is_dir($cachePath)) { throw new Exception("cache dir does not exist"); } switch ($metaData->fileType) { case "video/quicktime": case "application/ogg": case "audio/mpeg": case "video/mp4": case "video/x-msvideo": case "video/ogg": case "video/webm": case "video/x-ms-asf": case "video/x-flv": case "audio/x-wav": case "application/octet-stream": if (isMediaFile($metaData, $filePath)) { $media = new ffmpeg_movie($file, FALSE); /**************************************************** * VIDEO ****************************************************/ if ($media->hasVideo()) { $metaData->video->codec = $media->getVideoCodec(); $metaData->video->width = $media->getFrameWidth(); $metaData->video->height = $media->getFrameHeight(); $metaData->video->bitrate = $media->getVideoBitRate(); $metaData->video->framerate = $media->getFrameRate(); $metaData->video->duration = $media->getDuration(); /* Video Still */ $stillFile = $cachePath . DIRECTORY_SEPARATOR . uniqid("ft_") . ".png"; $fc = (int) ($media->getFrameCount() / 32); /* frame count is not necessarily reliable! */ foreach (array($fc, 100, 10, 1) as $fno) { if ($fno == 0) { continue; } $f = $media->getFrame($fno); if ($f !== FALSE) { imagepng($f->toGDImage(), $stillFile); $metaData->video->still = basename($stillFile); break; } } /* Video Thumbnails */ $thumbSizes = array(90, 180, 360); foreach ($thumbSizes as $tS) { $thumbFile = $cachePath . DIRECTORY_SEPARATOR . uniqid("ft_") . ".png"; list($thumb_width, $thumb_height) = generateThumbnail($stillFile, $thumbFile, $tS); $metaData->video->thumbnail->{$tS}->file = basename($thumbFile); $metaData->video->thumbnail->{$tS}->width = $thumb_width; $metaData->video->thumbnail->{$tS}->height = $thumb_height; } /* Schedule for transcoding */ $transcodeSizes = array(360); $metaData->transcodeStatus = 'WAITING'; $metaData->transcodeProgress = 0; foreach ($transcodeSizes as $tS) { $transcodeFile = $cachePath . DIRECTORY_SEPARATOR . uniqid("ft_") . ".webm"; list($width, $height) = scaleVideo($media->getFrameWidth(), $media->getFrameHeight(), $tS); $metaData->video->transcode->{$tS}->file = basename($transcodeFile); $metaData->video->transcode->{$tS}->width = $width; $metaData->video->transcode->{$tS}->height = $height; } } /**************************************************** * AUDIO ****************************************************/ if ($media->hasAudio()) { $metaData->audio->codec = $media->getAudioCodec(); $metaData->audio->bitrate = $media->getAudioBitRate(); $metaData->audio->samplerate = $media->getAudioSampleRate(); $metaData->audio->duration = $media->getDuration(); $metaData->transcodeStatus = 'WAITING'; $metaData->transcodeProgress = 0; $transcodeFile = $cachePath . DIRECTORY_SEPARATOR . uniqid("ft_") . ".ogg"; $metaData->audio->transcode->file = basename($transcodeFile); } } else { /* No media? */ } break; case "image/jpeg": case "image/png": case "image/gif": case "image/bmp": list($width, $height) = getimagesize($file); $metaData->image->width = $width; $metaData->image->height = $height; $thumbFile = $cachePath . DIRECTORY_SEPARATOR . uniqid("ft_") . ".png"; list($thumb_width, $thumb_height) = generateThumbnail($file, $thumbFile, 360); $metaData->image->thumbnail->{360}->file = basename($thumbFile); $metaData->image->thumbnail->{360}->width = $thumb_width; $metaData->image->thumbnail->{360}->height = $thumb_height; break; default: /* no idea about this file, let it go... */ } }
$i = 1; foreach ($movies as $movie) { $mov = new ffmpeg_movie($movie); echo '<table width="90%" class="hor-minimalist-a">'; printf('<caption>Processing Test File: %s...</caption>', basename($mov->getFilename())); printf('<thead><tr><th width="150">Method</th><th>Result<br/></th></tr></thead>', $mov->getFileName()); printf("<tr><td>file name:</td><td>%s<br/></td></tr>", $mov->getFileName()); printf("<tr><td>duration</td><td>%s seconds<br/></td></tr>", $mov->getDuration()); printf("<tr><td>frame count</td><td>%s<br/></td></tr>", $mov->getFrameCount()); printf("<tr><td>frame rate</td><td>%0.3f fps<br/></td></tr>", $mov->getFrameRate()); printf("<tr><td>comment</td><td>%s<br/></td></tr>", $mov->getComment()); printf("<tr><td>title</td><td>%s<br/></td></tr>", $mov->getTitle()); printf("<tr><td>author</td><td>%s<br/></td></tr>", $mov->getAuthor()); printf("<tr><td>copyright</td><td>%s<br/></td></tr>", $mov->getCopyright()); printf("<tr><td>get bit rate</td><td>%d<br/></td></tr>", $mov->getBitRate()); printf("<tr><td>has audio</td><td>%s<br/></td></tr>", $mov->hasAudio() == 0 ? 'No' : 'Yes'); if ($mov->hasAudio()) { printf("<tr><td>get audio stream id</td><td>%s<br/></td></tr>", $mov->getAudioStreamId()); printf("<tr><td>get audio codec</td><td>%s<br/></td></tr>", $mov->getAudioCodec()); printf("<tr><td>get audio bit rate</td><td>%d<br/></td></tr>", $mov->getAudioBitRate()); printf("<tr><td>get audio sample rate</td><td>%d<br/></td></tr>", $mov->getAudioSampleRate()); printf("<tr><td>get audio channels</td><td>%s<br/></td></tr>", $mov->getAudioChannels()); } printf("<tr><td>has video</td><td>%s<br/></td></tr>", $mov->hasVideo() == 0 ? 'No' : 'Yes'); if ($mov->hasVideo()) { printf("<tr><td>frame height</td><td>%d pixels<br/></td></tr>", $mov->getFrameHeight()); printf("<tr><td>frame width</td><td>%d pixels<br/></td></tr>", $mov->getFrameWidth()); printf("<tr><td>get video stream id</td><td>%s<br/></td></tr>", $mov->getVideoStreamId()); printf("<tr><td>get video codec</td><td>%s<br/></td></tr>", $mov->getVideoCodec()); printf("<tr><td>get video bit rate</td><td>%d<br/></td></tr>", $mov->getVideoBitRate()); printf("<tr><td>get pixel format</td><td>%s<br/></td></tr>", $mov->getPixelFormat());