PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Gallary/modules/gallery/helpers/movie.php

https://bitbucket.org/JakePratt/kupcakz.com
PHP | 282 lines | 164 code | 25 blank | 93 comment | 35 complexity | ac061afc25aa6ac01059cd66039a22c5 MD5 | raw file
  1. <?php defined("SYSPATH") or die("No direct script access.");
  2. /**
  3. * Gallery - a web based photo album viewer and editor
  4. * Copyright (C) 2000-2013 Bharat Mediratta
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /**
  21. * This is the API for handling movies.
  22. *
  23. * Note: by design, this class does not do any permission checking.
  24. */
  25. class movie_Core {
  26. private static $allow_uploads;
  27. static function get_edit_form($movie) {
  28. $form = new Forge("movies/update/$movie->id", "", "post", array("id" => "g-edit-movie-form"));
  29. $form->hidden("from_id")->value($movie->id);
  30. $group = $form->group("edit_item")->label(t("Edit Movie"));
  31. $group->input("title")->label(t("Title"))->value($movie->title)
  32. ->error_messages("required", t("You must provide a title"))
  33. ->error_messages("length", t("Your title is too long"));
  34. $group->textarea("description")->label(t("Description"))->value($movie->description);
  35. $group->input("name")->label(t("Filename"))->value($movie->name)
  36. ->error_messages(
  37. "conflict", t("There is already a movie, photo or album with this name"))
  38. ->error_messages("no_slashes", t("The movie name can't contain a \"/\""))
  39. ->error_messages("no_trailing_period", t("The movie name can't end in \".\""))
  40. ->error_messages("illegal_data_file_extension", t("You cannot change the movie file extension"))
  41. ->error_messages("required", t("You must provide a movie file name"))
  42. ->error_messages("length", t("Your movie file name is too long"));
  43. $group->input("slug")->label(t("Internet Address"))->value($movie->slug)
  44. ->error_messages(
  45. "conflict", t("There is already a movie, photo or album with this internet address"))
  46. ->error_messages(
  47. "not_url_safe",
  48. t("The internet address should contain only letters, numbers, hyphens and underscores"))
  49. ->error_messages("required", t("You must provide an internet address"))
  50. ->error_messages("length", t("Your internet address is too long"));
  51. module::event("item_edit_form", $movie, $form);
  52. $group = $form->group("buttons")->label("");
  53. $group->submit("")->value(t("Modify"));
  54. return $form;
  55. }
  56. /**
  57. * Extract a frame from a movie file. Valid movie_options are start_time (in seconds),
  58. * input_args (extra ffmpeg input args) and output_args (extra ffmpeg output args). Extra args
  59. * are added at the end of the list, so they can override any prior args.
  60. *
  61. * @param string $input_file
  62. * @param string $output_file
  63. * @param array $movie_options (optional)
  64. */
  65. static function extract_frame($input_file, $output_file, $movie_options=null) {
  66. $ffmpeg = movie::find_ffmpeg();
  67. if (empty($ffmpeg)) {
  68. throw new Exception("@todo MISSING_FFMPEG");
  69. }
  70. list($width, $height, $mime_type, $extension, $duration) = movie::get_file_metadata($input_file);
  71. if (isset($movie_options["start_time"]) && is_numeric($movie_options["start_time"])) {
  72. $start_time = max(0, $movie_options["start_time"]); // ensure it's non-negative
  73. } else {
  74. $start_time = module::get_var("gallery", "movie_extract_frame_time", 3); // use default
  75. }
  76. // extract frame at start_time, unless movie is too short
  77. $start_time_arg = ($duration >= $start_time + 0.1) ?
  78. "-ss " . movie::seconds_to_hhmmssdd($start_time) : "";
  79. $input_args = isset($movie_options["input_args"]) ? $movie_options["input_args"] : "";
  80. $output_args = isset($movie_options["output_args"]) ? $movie_options["output_args"] : "";
  81. $cmd = escapeshellcmd($ffmpeg) . " $input_args -i " . escapeshellarg($input_file) .
  82. " -an $start_time_arg -an -r 1 -vframes 1" .
  83. " -s {$width}x{$height}" .
  84. " -y -f mjpeg $output_args " . escapeshellarg($output_file) . " 2>&1";
  85. exec($cmd, $exec_output, $exec_return);
  86. clearstatcache(); // use $filename parameter when PHP_version is 5.3+
  87. if (filesize($output_file) == 0 || $exec_return) {
  88. // Maybe the movie needs the "-threads 1" argument added
  89. // (see http://sourceforge.net/apps/trac/gallery/ticket/1924)
  90. $cmd = escapeshellcmd($ffmpeg) . " -threads 1 $input_args -i " . escapeshellarg($input_file) .
  91. " -an $start_time_arg -an -r 1 -vframes 1" .
  92. " -s {$width}x{$height}" .
  93. " -y -f mjpeg $output_args " . escapeshellarg($output_file) . " 2>&1";
  94. exec($cmd, $exec_output, $exec_return);
  95. clearstatcache();
  96. if (filesize($output_file) == 0 || $exec_return) {
  97. throw new Exception("@todo FFMPEG_FAILED");
  98. }
  99. }
  100. }
  101. /**
  102. * Return true if movie uploads are allowed, false if not. This is based on the
  103. * "movie_allow_uploads" Gallery variable as well as whether or not ffmpeg is found.
  104. */
  105. static function allow_uploads() {
  106. if (empty(self::$allow_uploads)) {
  107. // Refresh ffmpeg settings
  108. $ffmpeg = movie::find_ffmpeg();
  109. switch (module::get_var("gallery", "movie_allow_uploads", "autodetect")) {
  110. case "always":
  111. self::$allow_uploads = true;
  112. break;
  113. case "never":
  114. self::$allow_uploads = false;
  115. break;
  116. default:
  117. self::$allow_uploads = !empty($ffmpeg);
  118. break;
  119. }
  120. }
  121. return self::$allow_uploads;
  122. }
  123. /**
  124. * Return the path to the ffmpeg binary if one exists and is executable, or null.
  125. */
  126. static function find_ffmpeg() {
  127. if (!($ffmpeg_path = module::get_var("gallery", "ffmpeg_path")) ||
  128. !@is_executable($ffmpeg_path)) {
  129. $ffmpeg_path = system::find_binary(
  130. "ffmpeg", module::get_var("gallery", "graphics_toolkit_path"));
  131. module::set_var("gallery", "ffmpeg_path", $ffmpeg_path);
  132. }
  133. return $ffmpeg_path;
  134. }
  135. /**
  136. * Return version number and build date of ffmpeg if found, empty string(s) if not. When using
  137. * static builds that aren't official releases, the version numbers are strange, hence why the
  138. * date can be useful.
  139. */
  140. static function get_ffmpeg_version() {
  141. $ffmpeg = movie::find_ffmpeg();
  142. if (empty($ffmpeg)) {
  143. return array("", "");
  144. }
  145. // Find version using -h argument since -version wasn't available in early versions.
  146. // To keep the preg_match searches quick, we'll trim the (otherwise long) result.
  147. $cmd = escapeshellcmd($ffmpeg) . " -h 2>&1";
  148. $result = substr(`$cmd`, 0, 1000);
  149. if (preg_match("/ffmpeg version (\S+)/i", $result, $matches_version)) {
  150. // Version number found - see if we can get the build date or copyright year as well.
  151. if (preg_match("/built on (\S+\s\S+\s\S+)/i", $result, $matches_build_date)) {
  152. return array(trim($matches_version[1], ","), trim($matches_build_date[1], ","));
  153. } else if (preg_match("/copyright \S*\s?2000-(\d{4})/i", $result, $matches_copyright_date)) {
  154. return array(trim($matches_version[1], ","), $matches_copyright_date[1]);
  155. } else {
  156. return array(trim($matches_version[1], ","), "");
  157. }
  158. }
  159. return array("", "");
  160. }
  161. /**
  162. * Return the width, height, mime_type, extension and duration of the given movie file.
  163. * Metadata is first generated using ffmpeg (or set to defaults if it fails),
  164. * then can be modified by other modules using movie_get_file_metadata events.
  165. *
  166. * This function and its use cases are symmetric to those of photo::get_file_metadata.
  167. *
  168. * @param string $file_path
  169. * @return array array($width, $height, $mime_type, $extension, $duration)
  170. *
  171. * Use cases in detail:
  172. * Input is standard movie type (flv/mp4/m4v)
  173. * -> return metadata from ffmpeg
  174. * Input is *not* standard movie type that is supported by ffmpeg (e.g. avi, mts...)
  175. * -> return metadata from ffmpeg
  176. * Input is *not* standard movie type that is *not* supported by ffmpeg but is legal
  177. * -> return zero width, height, and duration; mime type and extension according to legal_file
  178. * Input is illegal, unidentifiable, unreadable, or does not exist
  179. * -> throw exception
  180. * Note: movie_get_file_metadata events can change any of the above cases (except the last one).
  181. */
  182. static function get_file_metadata($file_path) {
  183. if (!is_readable($file_path)) {
  184. throw new Exception("@todo UNREADABLE_FILE");
  185. }
  186. $metadata = new stdClass();
  187. $ffmpeg = movie::find_ffmpeg();
  188. if (!empty($ffmpeg)) {
  189. // ffmpeg found - use it to get width, height, and duration.
  190. $cmd = escapeshellcmd($ffmpeg) . " -i " . escapeshellarg($file_path) . " 2>&1";
  191. $result = `$cmd`;
  192. if (preg_match("/Stream.*?Video:.*?, (\d+)x(\d+)/", $result, $matches_res)) {
  193. if (preg_match("/Stream.*?Video:.*? \[.*?DAR (\d+):(\d+).*?\]/", $result, $matches_dar) &&
  194. $matches_dar[1] >= 1 && $matches_dar[2] >= 1) {
  195. // DAR is defined - determine width based on height and DAR
  196. // (should always be int, but adding round to be sure)
  197. $matches_res[1] = round($matches_res[2] * $matches_dar[1] / $matches_dar[2]);
  198. }
  199. list ($metadata->width, $metadata->height) = array($matches_res[1], $matches_res[2]);
  200. } else {
  201. list ($metadata->width, $metadata->height) = array(0, 0);
  202. }
  203. if (preg_match("/Duration: (\d+:\d+:\d+\.\d+)/", $result, $matches)) {
  204. $metadata->duration = movie::hhmmssdd_to_seconds($matches[1]);
  205. } else if (preg_match("/duration.*?:.*?(\d+)/", $result, $matches)) {
  206. $metadata->duration = $matches[1];
  207. } else {
  208. $metadata->duration = 0;
  209. }
  210. } else {
  211. // ffmpeg not found - set width, height, and duration to zero.
  212. $metadata->width = 0;
  213. $metadata->height = 0;
  214. $metadata->duration = 0;
  215. }
  216. $extension = pathinfo($file_path, PATHINFO_EXTENSION);
  217. if (!$extension ||
  218. (!$metadata->mime_type = legal_file::get_movie_types_by_extension($extension))) {
  219. // Extension is empty or illegal.
  220. $metadata->extension = null;
  221. $metadata->mime_type = null;
  222. } else {
  223. // Extension is legal (and mime is already set above).
  224. $metadata->extension = strtolower($extension);
  225. }
  226. // Run movie_get_file_metadata events which can modify the class.
  227. module::event("movie_get_file_metadata", $file_path, $metadata);
  228. // If the post-events results are invalid, throw an exception. Note that, unlike photos, having
  229. // zero width and height isn't considered invalid (as is the case when FFmpeg isn't installed).
  230. if (!$metadata->mime_type || !$metadata->extension ||
  231. ($metadata->mime_type != legal_file::get_movie_types_by_extension($metadata->extension))) {
  232. throw new Exception("@todo ILLEGAL_OR_UNINDENTIFIABLE_FILE");
  233. }
  234. return array($metadata->width, $metadata->height, $metadata->mime_type,
  235. $metadata->extension, $metadata->duration);
  236. }
  237. /**
  238. * Return the time/duration formatted in hh:mm:ss.dd from a number of seconds.
  239. * Useful for inputs to ffmpeg.
  240. *
  241. * Note that this is similar to date("H:i:s", mktime(0,0,$seconds,0,0,0,0)), but unlike this
  242. * approach avoids potential issues with time zone and DST mismatch and/or using deprecated
  243. * features (the last argument of mkdate above, which disables DST, is deprecated as of PHP 5.3).
  244. */
  245. static function seconds_to_hhmmssdd($seconds) {
  246. return sprintf("%02d:%02d:%05.2f", floor($seconds / 3600), floor(($seconds % 3600) / 60),
  247. floor(100 * $seconds % 6000) / 100);
  248. }
  249. /**
  250. * Return the number of seconds from a time/duration formatted in hh:mm:ss.dd.
  251. * Useful for outputs from ffmpeg.
  252. */
  253. static function hhmmssdd_to_seconds($hhmmssdd) {
  254. preg_match("/(\d+):(\d+):(\d+\.\d+)/", $hhmmssdd, $matches);
  255. return 3600 * $matches[1] + 60 * $matches[2] + $matches[3];
  256. }
  257. }