浏览代码

add convertOffice

manto07m 3 年之前
父节点
当前提交
17f9cb6105
共有 1 个文件被更改,包括 53 次插入15 次删除
  1. 53 15
      script/php/videoToHLS.php

+ 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);