Tommy 碎碎念

Tommy Wu's blog

« 上一篇 | 下一篇 »

讓 Gallery 3 接受更多種類的影片格式
post by tommy @ 16 十二月, 2011 10:54

由於 Gallery 3 使用 flowplayer 這一個 flash player 的原因, 所以只接受 flv 與 mp4 (只能播 h.264, 不過有時會有影無聲) 這兩種檔案. 而在 Gallery 2 的時候, 是透過 QuickTime 的 plugin 來播放, 所以支援的格式似乎多了一些.

基本上, Gallery 對於影片檔案的處理, 是透過 ffmpeg 去抓縮圖, 然後透過一小段 html/javascript 的碼去播放. 而 ffmpeg 幾乎通吃所有的影片格式, 所以縮圖的處理應該不會有問題. 至於播放的指令, 我們也可以針對不同的格式來使用不同的指令去處理, 如果該格式真的沒有任何 player 或 plugin 可以線上播放, 也可以選擇讓使用者可以直接下載那個檔案回去. 所以... 直接在程式裡頭限制只接受 flv 與 mp4 兩種格式, 對我來說是件很奇怪的事.... 尤其是, 目前使用的 Gallery 2 上頭除了 .mp4 之外, 還有 .mov, .avi 的檔案,  總不能到時要轉換到 Gallery 3 時, 要使用者放棄這些原本可以正常使用的檔案吧.

Gallery 3 對於檔案種類接受的選擇, 目前看來並沒有統一的判斷程式碼去處理 (似乎在目前的 git 裡頭, 因為 rawphoto 的關係, 看來會有個 event 可以讓人比較容易的擴充, 到時加一種格式應該就不用每一個用到的模組都要去改了), 所以... 相關的程式碼分散在各個不同處理的模組內, 只能一個個去修改.

PS: 提到 rawphoto, 這應該是我目前打算由 Gallery 2 轉到 Gallery 3 時, 沒辦法處理的地方, 它的新版本只能在 Gallery 目前的開發版本上使用, 而且看來並不怎麼好用, 而且開發版本目前有些功能會有問題 (如相片的搬移與排序會失敗), 所以... 可能會先不管這部份, 等 3.1 出來後再看看能不能放上去.

至於要加入那些格式呢? 除了一般 flash player 支援的 .flv 與 .mp4 外, 我們可以使用 JW WMV Player , 這樣就可以支援 .asf 與 .wmv 的格式, 再來一樣用 QuickTime 的 plugin, 所以 .mov, .avi, .mpg/.mpeg 也可以支援. 所以, 下面的修正就是加上這幾類的格式.

首先是 Galley 對於 item 的處理:

--- gallery3.orig/modules/gallery/models/item.php  2011-05-25 12:04:04.000000000 +0800
+++ gallery3/modules/gallery/models/item.php 2011-12-13 12:55:47.737415378 +0800
@@ -785,7 +785,11 @@
return;
}
 
- if ($this->is_movie() && !preg_match("/^(flv|mp4|m4v)$/i", $ext)) {
+ // twu2 begin
+ // add more movie type
+ if ($this->is_movie() && !preg_match("/^(flv|mp4|m4v|mov|mpg|mpeg|avi|asf|wmv)$/i", $ext)) {
+ //if ($this->is_movie() && !preg_match("/^(flv|mp4|m4v)$/i", $ext)) {
+ // twu2 end
$v->add_error("name", "illegal_data_file_extension");
} else if ($this->is_photo() && !preg_match("/^(gif|jpg|jpeg|png)$/i", $ext)) {
$v->add_error("name", "illegal_data_file_extension");
@@ -877,7 +881,13 @@
switch($field) {
case "mime_type":
if ($this->is_movie()) {
- $legal_values = array("video/flv", "video/x-flv", "video/mp4");
+ // twu2 begin
+ // add more type
+ $legal_values = array("video/flv", "video/x-flv", "video/mp4",
+ "video/quicktime", "video/mpeg", "video/avi", "video/msvideo",
+ "video/x-msvideo", "video/x-ms-asf", "video/x-ms-wmv");
+ //$legal_values = array("video/flv", "video/x-flv", "video/mp4");
+ // twu2 end
} if ($this->is_photo()) {
$legal_values = array("image/jpeg", "image/gif", "image/png");
}

對於 movie 格式的處理:

--- gallery3.orig/modules/gallery/helpers/movie.php  2011-05-25 12:04:04.000000000 +0800
+++ gallery3/modules/gallery/helpers/movie.php 2011-12-12 14:44:18.310190863 +0800
@@ -114,8 +114,45 @@
 
$pi = pathinfo($file_path);
$extension = isset($pi["extension"]) ? $pi["extension"] : "flv"; // No extension? Assume FLV.
+ // twu2 begin
+ // add more mime type here
+ /*
$mime_type = in_array(strtolower($extension), array("mp4", "m4v")) ?
"video/mp4" : "video/x-flv";
+ */
+ switch (strtolower($extension)) {
+ case "mp4":
+ case "m4v":
+ $mime_type = "video/mp4";
+ break;
+
+ case "mov":
+ $mime_type = "video/quicktime";
+ break;
+
+ case "mpg":
+ case "mpeg":
+ $mime_type = "video/mpeg";
+ break;
+
+ case "avi":
+ $mime_type = "video/avi";
+ break;
+
+ case "asf":
+ $mime_type = "video/x-ms-asf";
+ break;
+
+ case "wmv":
+ $mime_type = "video/x-ms-wmv";
+ break;
+
+ case "flv":
+ default:
+ $mime_type = "video/x-flv";
+ break;
+ }
+ // twu2 end
 
return array($width, $height, $mime_type, $extension);
}

接著是在 /file_proxy/ 對於這些檔案格式的存取:

--- gallery3.orig/modules/gallery/controllers/file_proxy.php  2011-05-25 12:04:04.000000000 +0800
+++ gallery3/modules/gallery/controllers/file_proxy.php 2011-12-12 14:31:18.971974159 +0800
@@ -64,7 +64,12 @@
// requesting the thumbnail for a movie. In that case, the .flv, .mp4 or .m4v file would
// have been converted to a .jpg. So try some alternate types:
if (preg_match('/.jpg$/', $path)) {
- foreach (array("flv", "mp4", "m4v") as $ext) {
+ // twu2 begin
+ // add more movie extension
+ $movie_ext = array("flv", "mp4", "m4v", "mov", "mpg", "mpeg", "avi", "asf", "wmv");
+ foreach ($movie_ext as $ext) {
+ //foreach (array("flv", "mp4", "m4v") as $ext) {
+ // twu2 end
$movie_path = preg_replace('/.jpg$/', ".$ext", $path);
$item = item::find_by_path($movie_path);
if ($item->loaded()) {

上傳的 form 接受的檔案格式 filter:

--- gallery3.orig/modules/gallery/views/form_uploadify.html.php  2011-05-25 12:04:04.000000000 +0800
+++ gallery3/modules/gallery/views/form_uploadify.html.php 2011-12-13 13:11:40.976149205 +0800
@@ -28,7 +28,7 @@
uploader: "<?= url::file("lib/uploadify/uploadify.swf") ?>",
script: "<?= url::site("uploader/add_photo/{$album->id}") ?>",
scriptData: <?= json_encode($script_data) ?>,
- fileExt: "*.gif;*.jpg;*.jpeg;*.png;*.GIF;*.JPG;*.JPEG;*.PNG<? if ($movies_allowed): ?>;*.flv;*.mp4;*.m4v;*.FLV;*.MP4;*.M4V<? endif ?>",
+ fileExt: "*.gif;*.jpg;*.jpeg;*.png;*.GIF;*.JPG;*.JPEG;*.PNG<? if ($movies_allowed): ?>;*.flv;*.mp4;*.m4v;*.mov;*.mpg;*.mpeg;*.avi;*.asf;*.wmv;*.FLV;*.MP4;*.M4V;*.MOV;*.MPG;*.MPEG;*.AVI;*.ASF;*.WMV<? endif ?>",
fileDesc: <?= t("Photos and movies")->for_js() ?>,
cancelImg: "<?= url::file("lib/uploadify/cancel.png") ?>",
simUploadLimit: <?= $simultaneous_upload_limit ?>,

上傳檔案後對檔案的處理:

--- gallery3.orig/modules/gallery/controllers/uploader.php  2011-05-25 12:04:04.000000000 +0800
+++ gallery3/modules/gallery/controllers/uploader.php 2011-12-12 14:35:57.473710457 +0800
@@ -51,7 +51,11 @@
$file_validation = new Validation($_FILES);
$file_validation->add_rules(
"Filedata", "upload::valid", "upload::required",
- "upload::type[gif,jpg,jpeg,png,flv,mp4,m4v]");
+ // twu2 begin
+ // add more movie extension
+ "upload::type[gif,jpg,jpeg,png,flv,mp4,m4v,mov,mpg,mpeg,avi,asf,wmv]");
+ //"upload::type[gif,jpg,jpeg,png,flv,mp4,m4v]");
+ // twu2 end
 
if ($form->validate() && $file_validation->validate()) {
$temp_filename = upload::save("Filedata");
@@ -63,8 +67,14 @@
$item->set_data_file($temp_filename);
 
$path_info = @pathinfo($temp_filename);
+ // twu2 begin
+ // add more movie extension
+ $movie_ext = array("flv", "mp4", "m4v", "mov", "mpg", "mpeg", "avi", "asf", "wmv");
if (array_key_exists("extension", $path_info) &&
- in_array(strtolower($path_info["extension"]), array("flv", "mp4", "m4v"))) {
+ in_array(strtolower($path_info["extension"]), $movie_ext)) {
+ //if (array_key_exists("extension", $path_info) &&
+ // in_array(strtolower($path_info["extension"]), array("flv", "mp4", "m4v"))) {
+ // twu2 end
$item->type = "movie";
$item->save();
log::success("content", t("Added a movie"),

經由 serveradd 模組匯入的處理:

--- gallery3.orig/modules/server_add/controllers/server_add.php  2011-05-25 12:04:04.000000000 +0800
+++ gallery3/modules/server_add/controllers/server_add.php 2011-12-12 14:42:18.386101239 +0800
@@ -61,7 +61,11 @@
}
if (!is_dir($file)) {
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
- if (!in_array($ext, array("gif", "jpeg", "jpg", "png", "flv", "mp4", "m4v"))) {
+ // twu2 begin
+ // add more extension
+ if (!in_array($ext, array("gif", "jpeg", "jpg", "png", "flv", "mp4", "m4v", "mov", "mpg", "mpeg", "avi", "asf", "wmv"))) {
+ //if (!in_array($ext, array("gif", "jpeg", "jpg", "png", "flv", "mp4", "m4v"))) {
+ // twu2 end
continue;
}
}
@@ -169,7 +173,11 @@
foreach ($child_paths as $child_path) {
if (!is_dir($child_path)) {
$ext = strtolower(pathinfo($child_path, PATHINFO_EXTENSION));
- if (!in_array($ext, array("gif", "jpeg", "jpg", "png", "flv", "mp4", "m4v")) ||
+ // twu2 begin
+ // add more extension
+ if (!in_array($ext, array("gif", "jpeg", "jpg", "png", "flv", "mp4", "m4v", "mov", "mpg", "mpeg", "avi", "asf", "wmv")) ||
+ //if (!in_array($ext, array("gif", "jpeg", "jpg", "png", "flv", "mp4", "m4v")) ||
+ // twu2 end
!filesize($child_path)) {
// Not importable, skip it.
continue;
@@ -266,7 +274,11 @@
$photo->owner_id = $owner_id;
$photo->save();
$entry->item_id = $photo->id;
- } else if (in_array($extension, array("flv", "mp4", "m4v"))) {
+ // twu2 begin
+ // add more extension
+ } else if (in_array($extension, array("flv", "mp4", "m4v", "mov", "mpg", "mpeg", "avi", "asf", "wmv"))) {
+ //} else if (in_array($extension, array("flv", "mp4", "m4v"))) {
+ // twu2 end
$movie = ORM::factory("item");
$movie->type = "movie";
$movie->parent_id = $parent->id;

經由 g2_import 模組由 Gallery 2 匯入的處理:

diff -Nur gallery3.orig/modules/g2_import//helpers/g2_import.php gallery3/modules/g2_import//helpers/g2_import.php
--- gallery3.orig/modules/g2_import//helpers/g2_import.php 2011-05-25 12:04:04.000000000 +0800
+++ gallery3/modules/g2_import//helpers/g2_import.php 2011-12-12 14:20:56.994794072 +0800
@@ -678,7 +678,14 @@
 
case "GalleryMovieItem":
// @todo we should transcode other types into FLV
- if (in_array($g2_item->getMimeType(), array("video/mp4", "video/x-flv"))) {
+ // twu2 begin
+ // add more movie type
+ $movie_mimetype = array("video/flv", "video/x-flv", "video/mp4",
+ "video/quicktime", "video/mpeg", "video/avi", "video/msvideo",
+ "video/x-msvideo", "video/x-ms-asf", "video/x-ms-wmv");
+ if (in_array($g2_item->getMimeType(), $movie_mimetype)) {
+ //if (in_array($g2_item->getMimeType(), array("video/mp4", "video/x-flv"))) {
+ // twu2 end
try {
$item = ORM::factory("item");
$item->type = "movie";
@@ -700,8 +707,8 @@
$item = null;
}
} else {
- Kohana_Log::add("alert", "$g2_path is an unsupported movie type");
- $messages[] = t("'%path' is an unsupported movie type", array("path" => $g2_path));
+ Kohana_Log::add("alert", "$g2_path is an unsupported movie type: ".$g2_item->getMimeType());
+ $messages[] = t("'%path' is an unsupported movie type: '%type'", array("path" => $g2_path, "type" => $g2_item->getMimeType()));
$corrupt = 1;
}

最後是對於 movie 格式在產生播放頁面的處理:

diff -Nur gallery3.orig/modules/gallery/views//movieplayer.flowplayer.html.php gallery3/modules/gallery/views//movieplayer.flowplayer.html.php
--- gallery3.orig/modules/gallery/views//movieplayer.flowplayer.html.php 1970-01-01 08:00:00.000000000 +0800
+++ gallery3/modules/gallery/views//movieplayer.flowplayer.html.php 2011-05-25 12:04:04.000000000 +0800
@@ -0,0 +1,26 @@
+<?php defined("SYSPATH") or die("No direct script access.") ?>
+<?= html::anchor($item->file_url(true), "", $attrs) ?>
+<script type="text/javascript">
+ flowplayer(
+ "<?= $attrs["id"] ?>",
+ {
+ src: "<?= url::abs_file("lib/flowplayer.swf") ?>",
+ wmode: "transparent",
+ provider: "pseudostreaming"
+ },
+ {
+ clip: {
+ scaling: 'fit'
+ },
+ plugins: {
+ pseudostreaming: {
+ url: "<?= url::abs_file("lib/flowplayer.pseudostreaming.swf") ?>"
+ },
+ controls: {
+ autoHide: 'always',
+ hideDelay: 2000
+ }
+ }
+ }
+ ).ipad();
+</script>
diff -Nur gallery3.orig/modules/gallery/views//movieplayer.html.php gallery3/modules/gallery/views//movieplayer.html.php
--- gallery3.orig/modules/gallery/views//movieplayer.html.php 2011-05-25 12:04:04.000000000 +0800
+++ gallery3/modules/gallery/views//movieplayer.html.php 2011-12-14 14:10:06.420400577 +0800
@@ -1,26 +1,82 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
-<?= html::anchor($item->file_url(true), "", $attrs) ?>
-<script type="text/javascript">
- flowplayer(
- "<?= $attrs["id"] ?>",
- {
- src: "<?= url::abs_file("lib/flowplayer.swf") ?>",
- wmode: "transparent",
- provider: "pseudostreaming"
- },
- {
- clip: {
- scaling: 'fit'
- },
- plugins: {
- pseudostreaming: {
- url: "<?= url::abs_file("lib/flowplayer.pseudostreaming.swf") ?>"
- },
- controls: {
- autoHide: 'always',
- hideDelay: 2000
- }
- }
+<?
+$done = false;
+$mime_type = $item->mime_type;
+if ($mime_type == 'video/flv' || $mime_type == 'video/x-flv' ||
+ $mime_type == 'video/mp4') {
+ // flv/mp4 format, we can use the flash player
+ //$flash_player_view = dirname(__FILE__).'/movieplayer.flowplayer.html.php';
+ //or you want jwplayer
+ $flash_player_view = dirname(__FILE__).'/movieplayer.jwplayer.html.php';
+ if (file_exists($flash_player_view)) {
+ include($flash_player_view);
+ $done = true;
}
- ).ipad();
-</script>
+}
+if (!$done &&
+ ($mime_type == 'video/x-ms-asf' || $mime_type == 'video/x-ms-asx' ||
+ $mime_type == 'video/x-ms-wmv')) {
+ // asf/wmv format, we can use the flash player
+ $flash_player_view = dirname(__FILE__).'/movieplayer.wmvplayer.html.php';
+ if (file_exists($flash_player_view)) {
+ include($flash_player_view);
+ $done = true;
+ }
+}
+if (!$done) {
+ $width = $item->width;
+ $src = $item->file_url(true);
+ switch ($mime_type) {
+ case 'video/quicktime':
+ case 'video/x-quicktime':
+ $height = $item->height + 16;
+ print <<<QUICKTIME
+<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
+ codebase="http://www.apple.com/qtactivex/qtplugin.cab"
+ width="$width"" height="$height" id="move">
+ <param name="src" value="$src"/>
+ <param name="controller" value="true"/>
+ <param name="autoplay" value="true"/>
+ <param name="loop" value="false"/>
+ <embed src="$src" width="$width" height="$height" type="$mime_type"
+ pluginspage="http://www.apple.com/quicktime/download/"
+ controller="true" autoplay="true" loop="false"/>
+ <noembed><a href="$src">Download Movie</a></noembed>
+</object>
+QUICKTIME;
+ break;
+
+ case 'video/x-ms-asf':
+ case 'video/x-ms-asx':
+ if (!isset($classId))
+ $classId = 'CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95';
+ case 'video/mpeg':
+ //case 'video/mp4':
+ case 'video/msvideo':
+ case 'video/x-msvideo':
+ case 'video/x-ms-wmv':
+ case 'video/avi':
+ default:
+ if (!isset($classId))
+ $classId = 'CLSID:05589FA1-C356-11CE-BF01-00AA0055595A';
+ $height = $item->height + 50;
+ // in my site, I need to use video/mpeg for avi file
+ if ($mime_type == 'video/avi')
+ $mime_type = 'video/mpeg';
+ print <<<OTHERS
+<object classid="$classId" width="$width" height="$height" id="movie">
+ <param name="ShowDisplay" value="0"/>
+ <param name="ShowControls" value="1"/>
+ <param name="AutoStart" value="1"/>
+ <param name="AutoRewind" value="-1"/>
+ <param name="Volume" value="0"/>
+ <param name="FileName" value="$src"/>
+ <embed src="$src" width="$width" height="$height" type="$mime_type"
+ controller="true" autoplay="true" loop="false"/>
+ <noembed><a href="$src">Download Movie</a></noembed>
+</object>
+OTHERS;
+ break;
+ }
+}
+?>
diff -Nur gallery3.orig/modules/gallery/views//movieplayer.jwplayer.html.php gallery3/modules/gallery/views//movieplayer.jwplayer.html.php
--- gallery3.orig/modules/gallery/views//movieplayer.jwplayer.html.php 1970-01-01 08:00:00.000000000 +0800
+++ gallery3/modules/gallery/views//movieplayer.jwplayer.html.php 2011-12-14 10:00:54.048671918 +0800
@@ -0,0 +1,23 @@
+<?php defined("SYSPATH") or die("No direct script access.") ?>
+<script type="text/javascript" src="<?= url::abs_file("lib/jwplayer.js") ?>"></script>
+<div id="<?= $attrs["id"] ?>">Loading JW Player...</div>
+<script type="text/javascript">
+ jwplayer("<?= $attrs["id"] ?>").setup(
+ {
+ 'height': '<?= $item->height+20 ?>',
+ 'width': '<?= $item->width ?>',
+ 'file': '<?= $item->file_url(true) ?>',
+ 'title': '<?= htmlspecialchars($item->title) ?>',
+ 'autostart': 'true',
+ 'bufferlength': '10',
+ 'controlbar': 'bottom',
+ 'display.showmute': 'true',
+ 'stretching': 'uniform',
+ 'wmode': 'transparent',
+ 'modes': [
+ { 'type': 'flash', 'src': '<?= url::abs_file("lib/player.swf") ?>' },
+ { 'type': 'html5' },
+ { 'type': 'download' }
+ ]
+ });
+</script>
diff -Nur gallery3.orig/modules/gallery/views//movieplayer.wmvplayer.html.php gallery3/modules/gallery/views//movieplayer.wmvplayer.html.php
--- gallery3.orig/modules/gallery/views//movieplayer.wmvplayer.html.php 1970-01-01 08:00:00.000000000 +0800
+++ gallery3/modules/gallery/views//movieplayer.wmvplayer.html.php 2011-12-14 14:14:31.625509462 +0800
@@ -0,0 +1,16 @@
+<?php defined("SYSPATH") or die("No direct script access.") ?>
+<script type="text/javascript" src="<?= url::abs_file("lib/silverlight.js") ?>"></script>
+<script type="text/javascript" src="<?= url::abs_file("lib/wmvplayer.js") ?>"></script>
+<div id="<?= $attrs["id"] ?>">Loading JW Player...</div>
+<script type="text/javascript">
+ var elm = document.getElementById("<?= $attrs["id"] ?>");
+ var src = '<?= url::abs_file("lib/wmvplayer.xaml") ?>';
+ var cfg = {
+ file:'<?= $item->file_url(true) ?>',
+ width:'<?= $item->width ?>',
+ height:'<?= $item->height+20 ?>'',
+ windowless: 'true',
+ autostart: 'true'
+ };
+ var ply = new jeroenwijering.Player(elm,src,cfg);
+</script>

這個 player 頁面的修改, 會使用到 JW Player (優先使用 flash, 如果不支援就改用 html5, 如果都沒有就是下載的方式) 與 JW WMV Player, 所以, 請自行下載這兩個 player, 把 JW Player 使用的 jwplayer.js 與 player.swf, 以及 JW WMV Player 使用的 silverlight.js, wmvplayer.js 與 wmvplayer.xaml 這幾個檔案放到 Gallery 的 lib 目錄底下.

經過這樣子的處理之後, 至少常用的幾種格式都可以正常的加入 Gallery 裡頭了.

Del.icio.us Furl HEMiDEMi Technorati MyShare
迴響
暱稱:
標題:
個人網頁:
電子郵件:
迴響

  

Bad Behavior 已經阻擋了 111 個過去 7 天試圖闖關的垃圾迴響與引用。
Power by LifeType. Template design by JamesHuang. Valid XHTML and CSS