api_video_tool.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. set_time_limit(0);
  3. require '../../vendor/autoload.php';
  4. $GLOBALS["videoExt"] = array('mp4', 'avi');
  5. $GLOBALS["pdfExt"] = array('doc', 'docx', 'ppt', 'pptx', 'pdf');
  6. $isAJAX = false;
  7. $GLOBALS["output"] = "";
  8. $GLOBALS["ajax"] = [];
  9. use NcJoes\OfficeConverter\OfficeConverter;
  10. use Streaming\Representation;
  11. $ffmpeg = Streaming\FFMpeg::create([
  12. 'ffmpeg.binaries' => 'C:\FFmpeg\bin\ffmpeg.exe',
  13. 'ffprobe.binaries' => 'C:\FFmpeg\bin\ffprobe.exe',
  14. 'timeout' => 3600,
  15. ]);
  16. $GLOBALS["ffmpeg"] = $ffmpeg;
  17. $root = '../../assets/API/Universal';
  18. //$root = '../../上傳測試/Revit';
  19. $folder = 'Revit/托放元件';
  20. if (isset($_POST["APIName"])) {
  21. $folder = $APIName;
  22. $root .= ('/'.$_POST['software']);
  23. $isAJAX = true;
  24. }
  25. FindPath($root, $folder, $ffmpeg);
  26. if (!$isAJAX) {
  27. echo ($GLOBALS["output"]);
  28. }else{
  29. echo (implode(",",$GLOBALS["ajax"]));
  30. }
  31. function ConvertVideo($file_path, $ffmpeg)
  32. {
  33. $r_1080p = (new Representation)->setKiloBitrate(4096)->setResize(1920, 1080);
  34. $dir_name = pathinfo($file_path, PATHINFO_DIRNAME);
  35. $file_name = pathinfo($file_path, PATHINFO_FILENAME);
  36. $ext = pathinfo($file_path, PATHINFO_EXTENSION);
  37. if (!file_exists("../." . $dir_name . '/' . $file_name . ".m3u8")) {
  38. $video = $ffmpeg->open("../." . $file_path);
  39. $video->hls()
  40. ->x264()
  41. ->setHlsTime(300)
  42. ->addRepresentations([$r_1080p])
  43. ->save("../." . $dir_name . '/' . $file_name . ".m3u8");
  44. $GLOBALS["output"] .= "<span style='color:red;'>";
  45. $GLOBALS["output"] .= " " . $ext . " ==> m3u8";
  46. $GLOBALS["output"] .= "</span>";
  47. array_push($GLOBALS["ajax"],$file_name);
  48. }
  49. }
  50. function ConvertPdf($file_path)
  51. {
  52. $dir_name = pathinfo($file_path, PATHINFO_DIRNAME);
  53. $file_name = pathinfo($file_path, PATHINFO_FILENAME);
  54. $ext = pathinfo($file_path, PATHINFO_EXTENSION);
  55. if (!file_exists("../." . $dir_name . '/' . $file_name . ".pdf")) {
  56. $converter = new OfficeConverter("../." . $file_path, "./", "soffice.bin ", false);
  57. $converter->convertTo("../." . $dir_name . '/' . $file_name . ".pdf");
  58. $GLOBALS["output"] .= "<span style='color:red;'>";
  59. $GLOBALS["output"] .= " " . $ext . " ==> pdf";
  60. $GLOBALS["output"] .= "</span>";
  61. }
  62. if (!file_exists("../." . $dir_name . '/' . $file_name . ".jpg")) {
  63. $root = $_SERVER['DOCUMENT_ROOT'] . '/BIM-Monitor' . substr($dir_name, 1) . '/' . $file_name;
  64. $im = new imagick();
  65. $im->readimage($root . '.pdf[0]');
  66. $im->setImageFormat('jpg');
  67. $im->writeImage($root . '.jpg');
  68. $im->clear();
  69. $im->destroy();
  70. $GLOBALS["output"] .= "<span style='color:red;'>";
  71. $GLOBALS["output"] .= " pdf ==> jpg";
  72. $GLOBALS["output"] .= "</span>";
  73. }
  74. }
  75. function FindPath($root, $folder, $ffmpeg)
  76. {
  77. $rootPath = $root . '/' . $folder;
  78. $paths = array_diff(scandir($root . '/' . $folder), array('.', '..', 'Thumbs.db'));
  79. natsort($paths);
  80. $GLOBALS["output"] .= ("<ul>");
  81. foreach ($paths as $path) {
  82. if (str_contains($path, '.')) {
  83. $GLOBALS["output"] .= ("<li data-jstree='{ " . '"type" : "file"' . " }' >");
  84. $title = explode(".", $path)[0];
  85. $file_path = substr($rootPath, 4) . "/" . $path;
  86. $path = "title:" . $title . "path:" . $file_path . " dir_id:" . $folder;
  87. $GLOBALS["output"] .= ($path);
  88. $ext = pathinfo($file_path, PATHINFO_EXTENSION);
  89. if (in_array($ext, $GLOBALS["videoExt"])) {
  90. ConvertVideo($file_path, $ffmpeg);
  91. } else if (in_array($ext, $GLOBALS["pdfExt"])) {
  92. //ConvertPdf($file_path);
  93. }
  94. $GLOBALS["output"] .= ("</li>");
  95. } else {
  96. $GLOBALS["output"] .= ("<li>");
  97. $GLOBALS["output"] .= ("dir_name: " . $path . "parent: " . $folder);
  98. FindPath($rootPath, $path, $ffmpeg);
  99. $GLOBALS["output"] .= ("</li>");
  100. }
  101. }
  102. $GLOBALS["output"] .= ("</ul>");
  103. }