summaryrefslogtreecommitdiff
path: root/modules/gallery/helpers/movie.php
diff options
context:
space:
mode:
authorshadlaws <shad@shadlaws.com>2012-12-16 18:11:01 +0100
committershadlaws <shad@shadlaws.com>2012-12-16 18:11:01 +0100
commit94b26e506c339f50b8d094057bffc1877a79afa9 (patch)
tree6926ba85de8269983e09b8ea906aceb0c241087f /modules/gallery/helpers/movie.php
parent7c62c67d56e854f69a411dd56c48ef066f951c48 (diff)
[#1924, #1925, #1927 - enhance compatibility with movies (movie helper) and file types (legal_file helper)]
bug fix: modify movie.php helper to take DAR (display aspect ratio) into account in get_file_metadata bug fix / enhancement: add duration to get_file_metadata output bug fix: modify movie.php helper to use correct resolution and duration from get_file_metadata in extract_frame bug fix: modify movie.php helper to be more robust against ffmpeg failures and limitations, including adding "-threads 1" argument if needed enhancement: modified to include ordered maps of extensions to MIME types (get_photo_types_by_extension and get_movie_types_by_extension functions), modified get_file_metadata in movie and photo helpers to use them gallery_installer, module.info, install.sql: updated to v52 with m4v mime correction code (was video/mp4)
Diffstat (limited to 'modules/gallery/helpers/movie.php')
-rw-r--r--modules/gallery/helpers/movie.php54
1 files changed, 38 insertions, 16 deletions
diff --git a/modules/gallery/helpers/movie.php b/modules/gallery/helpers/movie.php
index b54811df..ddc513ea 100644
--- a/modules/gallery/helpers/movie.php
+++ b/modules/gallery/helpers/movie.php
@@ -63,21 +63,29 @@ class movie_Core {
throw new Exception("@todo MISSING_FFMPEG");
}
+ list($width, $height, $mime_type, $extension, $duration) = movie::get_file_metadata($input_file);
+
+ // extract frame at 0:03, unless movie is shorter than 4 sec.
+ $start_time_arg = ($duration > 4) ? " -ss 00:00:03" : "";
+
$cmd = escapeshellcmd($ffmpeg) . " -i " . escapeshellarg($input_file) .
- " -an -ss 00:00:03 -an -r 1 -vframes 1" .
+ " -an $start_time_arg -an -r 1 -vframes 1" .
+ " -s {$width}x{$height}" .
" -y -f mjpeg " . escapeshellarg($output_file) . " 2>&1";
- exec($cmd);
+ exec($cmd, $exec_output, $exec_return);
clearstatcache(); // use $filename parameter when PHP_version is 5.3+
- if (filesize($output_file) == 0) {
- // Maybe the movie is shorter, fall back to the first frame.
- $cmd = escapeshellcmd($ffmpeg) . " -i " . escapeshellarg($input_file) .
- " -an -an -r 1 -vframes 1" .
+ if (filesize($output_file) == 0 || $exec_return) {
+ // Maybe the movie needs the "-threads 1" argument added
+ // (see http://sourceforge.net/apps/trac/gallery/ticket/1924)
+ $cmd = escapeshellcmd($ffmpeg) . " -threads 1 -i " . escapeshellarg($input_file) .
+ " -an $start_time_arg -an -r 1 -vframes 1" .
+ " -s {$width}x{$height}" .
" -y -f mjpeg " . escapeshellarg($output_file) . " 2>&1";
- exec($cmd);
+ exec($cmd, $exec_output, $exec_return);
clearstatcache();
- if (filesize($output_file) == 0) {
+ if (filesize($output_file) == 0 || $exec_return) {
throw new Exception("@todo FFMPEG_FAILED");
}
}
@@ -96,7 +104,7 @@ class movie_Core {
}
/**
- * Return the width, height, mime_type and extension of the given movie file.
+ * Return the width, height, mime_type, extension and duration of the given movie file.
*/
static function get_file_metadata($file_path) {
$ffmpeg = movie::find_ffmpeg();
@@ -106,18 +114,32 @@ class movie_Core {
$cmd = escapeshellcmd($ffmpeg) . " -i " . escapeshellarg($file_path) . " 2>&1";
$result = `$cmd`;
- if (preg_match("/Stream.*?Video:.*?, (\d+)x(\d+)/", $result, $regs)) {
- list ($width, $height) = array($regs[1], $regs[2]);
+ 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 ($width, $height) = array($matches_res[1], $matches_res[2]);
} else {
list ($width, $height) = array(0, 0);
}
- $pi = pathinfo($file_path);
- $extension = isset($pi["extension"]) ? $pi["extension"] : "flv"; // No extension? Assume FLV.
- $mime_type = in_array(strtolower($extension), array("mp4", "m4v")) ?
- "video/mp4" : "video/x-flv";
+ $extension = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
+ $extension = $extension ? $extension : "flv"; // No extension? Assume FLV.
+ $mime_type = legal_file::get_movie_types_by_extension($extension);
+ $mime_type = $mime_type ? $mime_type : "video/x-flv"; // No MIME found? Default to video/x-flv.
+
+ if (preg_match("/Duration: (\d+):(\d+):(\d+\.\d+)/", $result, $matches)) {
+ $duration = 3600 * $matches[1] + 60 * $matches[2] + $matches[3];
+ } else if (preg_match("/duration.*?:.*?(\d+)/", $result, $matches)) {
+ $duration = $matches[1];
+ } else {
+ $duration = 0;
+ }
- return array($width, $height, $mime_type, $extension);
+ return array($width, $height, $mime_type, $extension, $duration);
}
}