浏览代码

Merge branch 'Dev' of http://10.1.1.202:3030/steve07s/BIM-Monitor into Dev

nate 3 年之前
父节点
当前提交
23153b6a0f
共有 4 个文件被更改,包括 196 次插入112 次删除
  1. 21 0
      pdfConverter.html
  2. 24 0
      pdfConverter.php
  3. 98 97
      script/js/video-list.js
  4. 53 15
      script/php/videoToHLS.php

+ 21 - 0
pdfConverter.html

@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <meta content="text/html; charset=utf-8"
+        http-equiv="content-type">
+  <title>PDF轉檔工具</title>
+</head>
+<body>
+  <h1>PDF轉檔工具</h1>
+  <form action="pdfConverter.php"
+        enctype="multipart/form-data"
+        method="post">
+    選擇Office文件或PPT: <input accept=".docx,.doc,.pptx,.ppt"
+          id="my_file"
+          name="my_file"
+          type="file"> <input name="submit"
+          type="submit"
+          value="上傳文件">
+  </form>
+</body>
+</html>

+ 24 - 0
pdfConverter.php

@@ -0,0 +1,24 @@
+<?php
+require './vendor/autoload.php';
+
+use NcJoes\OfficeConverter\OfficeConverter;
+
+$target_dir = "../uploads/";
+$file = $_FILES['my_file']['name'];
+$path = pathinfo($file);
+$filename = $path['filename'];
+$ext = $path['extension'];
+$temp_name = $_FILES['my_file']['tmp_name'];
+$path_filename_ext = $target_dir . $filename . "." . $ext;
+$filePdf = $target_dir . $filename . ".pdf";
+if (file_exists($path_filename_ext)) {
+    echo "檔案已存在!<br>";
+} else {
+    move_uploaded_file($temp_name, $path_filename_ext);
+    echo "上傳成功! 已轉檔成功!<br>";
+    $converter = new OfficeConverter($path_filename_ext, $target_dir, "soffice.bin ", false);
+    $converter->convertTo($filePdf);
+}
+echo "<a href='" . $target_dir . $filePdf . "' target='_blank'>預覽PDF</a><br>";
+echo "<a download href='" . $target_dir . $filePdf . "' >下載PDF</a>";
+echo "<br><a href='pdfConverter.html' >返回上傳</a>";

+ 98 - 97
script/js/video-list.js

@@ -1,112 +1,113 @@
-$(document).ready(function () {
-    if(folderFromVideoPlay != ""){
-        initList(folderFromVideoPlay);
-    }else{
-        initList("videos");
-    }
-});
-
-function initList(dir) {
-    $.ajax({
-        url: "./script/php/video/getDir.php",
-        type: "POST",
-        data: {
-            dir: dir,
-        },
-        dataType: "json"
-    }).done(function (result) {
-
-        $("#video-list").empty();
-        dir_names = result["dir_names"];
-        file_tables = result["file_tables"];
-        if (result["parent"] != undefined)
-            parent = result["parent"];
-            
-        //directory
-        for (i = 0; i < dir_names.length; i++) {
-            $("#video-list").append('<div class="col-md-3">' +
-                '<section class="panel">' +
-                "<a href='#' onclick='initList(\"" + dir_names[i]["dir_name"] + "\")'>" +
-                '<img class="video-active" src="./assets/images/folder-icon.png">' +
-                '</a>' +
-                '<div class="panel-body" id="">' +
-                '<div class="video-text">' +
-                "<p style='font-size: 20px;'><i class='fa fa-folder'></i><a class='video-font' href='#' onclick='initList(\"" + dir_names[i]["dir_name"] + "\")'>" + dir_names[i]["dir_name"] + "</a></p>" +
-                '</div>' +
-                '</div>' +
-                '</section>' +
-                '</div>');
+(function ($) {
+    $(document).ready(function () {
+        if (folderFromVideoPlay != "") {
+            initList(folderFromVideoPlay);
+        } else {
+            initList("videos");
         }
+    });
+
+    function initList(dir) {
+        $.ajax({
+            url: "./script/php/video/getDir.php",
+            type: "POST",
+            data: {
+                dir: dir,
+            },
+            dataType: "json"
+        }).done(function (result) {
+            $("#video-list").empty();
+            dir_names = result["dir_names"];
+            file_tables = result["file_tables"];
+            if (result["parent"] != undefined)
+                parent = result["parent"];
 
-        //video
-        for (j = 0; j < file_tables.length; j++) {
-            if (file_tables[j]["file_path"].includes(".m3u8")) {
+            //directory
+            for (i = 0; i < dir_names.length; i++) {
                 $("#video-list").append('<div class="col-md-3">' +
                     '<section class="panel">' +
-                    "<a href='#' onclick='getVideo(" + file_tables[j]["id"] + ",\"" + file_tables[j]["file_path"].split("/")[file_tables[j]["file_path"].split("/").length-2] + "\")'>" +
-                    '<img style="aspect-ratio: 4/3;" class="video-static" src="' + file_tables[j]["file_path"].replace("m3u8", "jpg") + '">' +
-                    '<img style="aspect-ratio: 4/3;" class="video-active" src="' + file_tables[j]["file_path"].replace("m3u8", "gif") +
-                    '">' +
+                    "<a href='#' onclick='initList(\"" + dir_names[i]["dir_name"] + "\")'>" +
+                    '<img class="video-active" src="./assets/images/folder-icon.png">' +
                     '</a>' +
                     '<div class="panel-body" id="">' +
                     '<div class="video-text">' +
-                    "<p style='font-size: 20px;'><i class='fa fa-video-camera'></i><a class='video-font' href='#' onclick='getVideo(" + file_tables[j]["id"] + ",\"" + file_tables[j]["file_path"].split("/")[file_tables[j]["file_path"].split("/").length-2] + "\")'>" + file_tables[j]["title"] + "</a></p>" +
+                    "<p style='font-size: 20px;'><i class='fa fa-folder'></i><a class='video-font' href='#' onclick='initList(\"" + dir_names[i]["dir_name"] + "\")'>" + dir_names[i]["dir_name"] + "</a></p>" +
                     '</div>' +
                     '</div>' +
                     '</section>' +
                     '</div>');
-            }else if(file_tables[j]["file_path"].includes(".pdf")){
-                if($("#description").length <= 0){
-                    $("#video-list").prepend('<div class="col-md-12">' +
-                    '<section class="panel" id="video-body">' +
-                    '<div class="panel-body" style="color:black;">' +
-                    '<h4>課程簡介</h4>' +
-                    '<text id="description"></text>' +
-                    '</div>' +
-                    '</section>' +
-                    '</div>');
-                    getVideoInfo(file_tables[j]["id"]);
+            }
+
+            //video
+            for (j = 0; j < file_tables.length; j++) {
+                if (file_tables[j]["file_path"].includes(".m3u8")) {
+                    $("#video-list").append('<div class="col-md-3">' +
+                        '<section class="panel">' +
+                        "<a href='#' onclick='getVideo(" + file_tables[j]["id"] + ",\"" + file_tables[j]["file_path"].split("/")[file_tables[j]["file_path"].split("/").length - 2] + "\")'>" +
+                        '<img style="aspect-ratio: 4/3;" class="video-static" src="' + file_tables[j]["file_path"].replace("m3u8", "jpg") + '">' +
+                        '<img style="aspect-ratio: 4/3;" class="video-active" src="' + file_tables[j]["file_path"].replace("m3u8", "gif") +
+                        '">' +
+                        '</a>' +
+                        '<div class="panel-body" id="">' +
+                        '<div class="video-text">' +
+                        "<p style='font-size: 20px;'><i class='fa fa-video-camera'></i><a class='video-font' href='#' onclick='getVideo(" + file_tables[j]["id"] + ",\"" + file_tables[j]["file_path"].split("/")[file_tables[j]["file_path"].split("/").length - 2] + "\")'>" + file_tables[j]["title"] + "</a></p>" +
+                        '</div>' +
+                        '</div>' +
+                        '</section>' +
+                        '</div>');
+                } else if (file_tables[j]["file_path"].includes(".pdf")) {
+                    if ($("#description").length <= 0) {
+                        $("#video-list").prepend('<div class="col-md-12">' +
+                            '<section class="panel" id="video-body">' +
+                            '<div class="panel-body" style="color:black;">' +
+                            '<h4>課程簡介</h4>' +
+                            '<text id="description"></text>' +
+                            '</div>' +
+                            '</section>' +
+                            '</div>');
+                        getVideoInfo(file_tables[j]["id"]);
+                    }
+                    $("#video-list").append('<div class="col-md-3">' +
+                        '<section class="panel">' +
+                        "<a target='_blank' href='" + file_tables[j]["file_path"] + "'>" +
+                        '<img style="aspect-ratio: 4/3;" class="video-active" src="' + file_tables[j]["file_path"].replace("pdf", "jpg") + '">' +
+                        '</a>' +
+                        '<div class="panel-body" id="">' +
+                        '<div class="video-text">' +
+                        "<p style='font-size: 20px;'><i class='fa fa-file-pdf-o'></i><a class='video-font' target='_blank' href='" + file_tables[j]["file_path"] + "'>" + file_tables[j]["title"] + "</a></p>" +
+                        '</div>' +
+                        '</div>' +
+                        '</section>' +
+                        '</div>');
                 }
-                $("#video-list").append('<div class="col-md-3">' +
-                '<section class="panel">' +
-                "<a target='_blank' href='" + file_tables[j]["file_path"] + "'>" +
-                '<img style="aspect-ratio: 4/3;" class="video-active" src="' + file_tables[j]["file_path"].replace("pdf", "jpg") + '">' +
-                '</a>' +
-                '<div class="panel-body" id="">' +
-                '<div class="video-text">' +
-                "<p style='font-size: 20px;'><i class='fa fa-file-pdf-o'></i><a class='video-font' target='_blank' href='" + file_tables[j]["file_path"] + "'>" + file_tables[j]["title"] + "</a></p>" +
-                '</div>' +
-                '</div>' +
-                '</section>' +
-                '</div>');
             }
-        }
-    }).error(function (error) {
-        console.log(error);
-    });
-}
+        }).error(function (error) {
+            console.log(error);
+        });
+    }
 
-function getVideo(id, folder) {
-    $.redirect('./video-play.php', {
-        'id': id,
-        'folder': folder
-    });
-}
+    function getVideo(id, folder) {
+        $.redirect('./video-play.php', {
+            'id': id,
+            'folder': folder
+        });
+    }
 
-function getVideoInfo(id) {
-    $.ajax({
-        url: "./script/php/video/getInfo.php",
-        type: "POST",
-        data: {
-            id: id,
-        },
-        dataType: "json"
-    }).done(function (result) {
-        if (result[0].description == null) {
-            result[0].description = "暫無描述";
-        }
-        $("#description").text(result[0].description);
-    }).error(function (error) {
-        console.log(error);
-    });
-}
+    function getVideoInfo(id) {
+        $.ajax({
+            url: "./script/php/video/getInfo.php",
+            type: "POST",
+            data: {
+                id: id,
+            },
+            dataType: "json"
+        }).done(function (result) {
+            if (result[0].description == null) {
+                result[0].description = "暫無描述";
+            }
+            $("#description").text(result[0].description);
+        }).error(function (error) {
+            console.log(error);
+        });
+    }
+})(jQuery);

+ 53 - 15
script/php/videoToHLS.php

@@ -1,7 +1,10 @@
 <?php
 set_time_limit(0);
 require '../../vendor/autoload.php';
+$GLOBALS["videoExt"] = array('mp4', 'wmv');
+$GLOBALS["pdfExt"] = array('doc', 'docx', 'ppt', 'pptx', 'pdf');
 
+use NcJoes\OfficeConverter\OfficeConverter;
 use Streaming\Representation;
 
 $ffmpeg = Streaming\FFMpeg::create([
@@ -16,46 +19,81 @@ $root = '../../assets';
 $folder = 'videos';
 FindPath($root, $folder, $ffmpeg);
 
-function Convert($file_path, $ffmpeg)
+function ConvertVideo($file_path, $ffmpeg)
 {
 
     $r_1080p = (new Representation)->setKiloBitrate(4096)->setResize(1920, 1080);
 
-
-    if (!file_exists("../." . substr($file_path, 0, -4) . ".m3u8")) {
+    $dir_name = pathinfo($file_path, PATHINFO_DIRNAME);
+    $file_name = pathinfo($file_path, PATHINFO_FILENAME);
+    $ext = pathinfo($file_path, PATHINFO_EXTENSION);
+    if (!file_exists("../." . $dir_name . '/' . $file_name . ".m3u8")) {
         $video = $ffmpeg->open("../." . $file_path);
         $video->hls()
             ->x264()
             ->setHlsTime(300)
             ->addRepresentations([$r_1080p])
-            ->save("../." . substr($file_path, 0, -4) . ".m3u8");
+            ->save("../." . $dir_name . '/' . $file_name . ".m3u8");
         $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(40))
-            ->save("../." . substr($file_path, 0, -4) . ".jpg");
+            ->save("../." . $dir_name . '/' . $file_name . ".jpg");
         $video
             ->gif(FFMpeg\Coordinate\TimeCode::fromSeconds(40), new FFMpeg\Coordinate\Dimension(640, 480), 5)
-            ->save("../." . substr($file_path, 0, -4) . ".gif");
+            ->save("../." . $dir_name . '/' . $file_name . ".gif");
+        echo "<span style='color:red;'>";
+        echo "   " . $ext . " ==> m3u8";
+        echo "</span>";
+    }
+}
+
+function ConvertPdf($file_path)
+{
+    $dir_name = pathinfo($file_path, PATHINFO_DIRNAME);
+    $file_name = pathinfo($file_path, PATHINFO_FILENAME);
+    $ext = pathinfo($file_path, PATHINFO_EXTENSION);
+    if (!file_exists("../." . $dir_name . '/' . $file_name . ".pdf")) {
+        $converter = new OfficeConverter("../." . $file_path, "./", "soffice.bin ", false);
+        $converter->convertTo("../." . $dir_name . '/' . $file_name . ".pdf");
+        echo "<span style='color:red;'>";
+        echo "   " . $ext . " ==> pdf";
+        echo "</span>";
+    }
+    if (!file_exists("../." . $dir_name . '/' . $file_name . ".jpg")) {
+        $root = $_SERVER['DOCUMENT_ROOT']  . '/BIM-Monitor'  . substr($dir_name, 1)  . '/' . $file_name;
+
+        $im = new imagick();
+        $im->readimage($root . '.pdf[0]');
+        $im->setImageFormat('jpg');
+        $im->writeImage($root . '.jpg');
+        $im->clear();
+        $im->destroy();
+        echo "<span style='color:red;'>";
+        echo "    pdf ==> jpg";
+        echo "</span>";
     }
 }
 
 
 function FindPath($root, $folder, $ffmpeg)
 {
-
     $rootPath = $root . '/' . $folder;
     $paths = array_diff(scandir($root . '/' . $folder), array('.', '..', 'Thumbs.db'));
     natsort($paths);
     echo ("<ul>");
     foreach ($paths as $path) {
         if (str_contains($path, '.')) {
-            if (str_contains(strtolower($path), '.mp4') || str_contains(strtolower($path), '.wmv')) {
-                echo ("<li data-jstree='{ " . '"type" : "file"' . " }' >");
-                $title = explode(".", $path)[0];
-                $file_path = substr($rootPath, 4) . "/" . $path;
-                $path = "title:" . $title . "path:" . $file_path . "  dir_id:" . $folder;
-                echo ($path);
-                echo ("</li>");
-                Convert($file_path, $ffmpeg);
+            echo ("<li data-jstree='{ " . '"type" : "file"' . " }' >");
+            $title = explode(".", $path)[0];
+            $file_path = substr($rootPath, 4) . "/" . $path;
+            $path = "title:" . $title . "path:" . $file_path . "  dir_id:" . $folder;
+            echo ($path);
+
+            $ext = pathinfo($file_path, PATHINFO_EXTENSION);
+            if (in_array($ext, $GLOBALS["videoExt"])) {
+                ConvertVideo($file_path, $ffmpeg);
+            } else if (in_array($ext, $GLOBALS["pdfExt"])) {
+                ConvertPdf($file_path);
             }
+            echo ("</li>");
         } else {
             echo ("<li>");
             echo ("dir_name: " . $path . "parent:  " . $folder);