public function hhmmssdd_to_seconds_test() { $times = array("0:00:00.01" => 0.01, "00:00:00.50" => 0.5, "00:00:06.00" => 6, "00:00:59.99" => 59.99, "00:01:00.00" => 60.0, "00:07:00.00" => 7 * 60, "00:45:19.00" => 45 * 60 + 19, "03:45:19.00" => 3 * 3600 + 45 * 60 + 19, "126:45:19.00" => 126 * 3600 + 45 * 60 + 19); foreach ($times as $hhmmssdd => $seconds) { $this->assert_equal($seconds, movie::hhmmssdd_to_seconds($hhmmssdd)); } }
/** * Return the width, height, mime_type, extension and duration of the given movie file. * Metadata is first generated using ffmpeg (or set to defaults if it fails), * then can be modified by other modules using movie_get_file_metadata events. * * This function and its use cases are symmetric to those of photo::get_file_metadata. * * @param string $file_path * @return array array($width, $height, $mime_type, $extension, $duration) * * Use cases in detail: * Input is standard movie type (flv/mp4/m4v) * -> return metadata from ffmpeg * Input is *not* standard movie type that is supported by ffmpeg (e.g. avi, mts...) * -> return metadata from ffmpeg * Input is *not* standard movie type that is *not* supported by ffmpeg but is legal * -> return zero width, height, and duration; mime type and extension according to legal_file * Input is illegal, unidentifiable, unreadable, or does not exist * -> throw exception * Note: movie_get_file_metadata events can change any of the above cases (except the last one). */ static function get_file_metadata($file_path) { if (!is_readable($file_path)) { throw new Exception("@todo UNREADABLE_FILE"); } $metadata = new stdClass(); $ffmpeg = movie::find_ffmpeg(); if (!empty($ffmpeg)) { // ffmpeg found - use it to get width, height, and duration. $cmd = escapeshellcmd($ffmpeg) . " -i " . escapeshellarg($file_path) . " 2>&1"; $result = `{$cmd}`; if (preg_match("/Stream.*?Video:.*?, (\\d+)x(\\d+)/", $result, $matches_res)) { if (preg_match("/Stream.*?Video:.*? \\[.*?DAR (\\d+):(\\d+).*?\\]/", $result, $matches_dar) && $matches_dar[1] >= 1 && $matches_dar[2] >= 1) { // DAR is defined - determine width based on height and DAR // (should always be int, but adding round to be sure) $matches_res[1] = round($matches_res[2] * $matches_dar[1] / $matches_dar[2]); } list($metadata->width, $metadata->height) = array($matches_res[1], $matches_res[2]); } else { list($metadata->width, $metadata->height) = array(0, 0); } if (preg_match("/Duration: (\\d+:\\d+:\\d+\\.\\d+)/", $result, $matches)) { $metadata->duration = movie::hhmmssdd_to_seconds($matches[1]); } else { if (preg_match("/duration.*?:.*?(\\d+)/", $result, $matches)) { $metadata->duration = $matches[1]; } else { $metadata->duration = 0; } } } else { // ffmpeg not found - set width, height, and duration to zero. $metadata->width = 0; $metadata->height = 0; $metadata->duration = 0; } $extension = pathinfo($file_path, PATHINFO_EXTENSION); if (!$extension || !($metadata->mime_type = legal_file::get_movie_types_by_extension($extension))) { // Extension is empty or illegal. $metadata->extension = null; $metadata->mime_type = null; } else { // Extension is legal (and mime is already set above). $metadata->extension = strtolower($extension); } // Run movie_get_file_metadata events which can modify the class. module::event("movie_get_file_metadata", $file_path, $metadata); // If the post-events results are invalid, throw an exception. Note that, unlike photos, having // zero width and height isn't considered invalid (as is the case when FFmpeg isn't installed). if (!$metadata->mime_type || !$metadata->extension || $metadata->mime_type != legal_file::get_movie_types_by_extension($metadata->extension)) { throw new Exception("@todo ILLEGAL_OR_UNINDENTIFIABLE_FILE"); } return array($metadata->width, $metadata->height, $metadata->mime_type, $metadata->extension, $metadata->duration); }