testtest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 Streaming\Representation;
  10. $ffmpeg = Streaming\FFMpeg::create([
  11. 'ffmpeg.binaries' => 'C:\FFmpeg\bin\ffmpeg.exe',
  12. 'ffprobe.binaries' => 'C:\FFmpeg\bin\ffprobe.exe',
  13. 'timeout' => 3600,
  14. ]);
  15. $GLOBALS["ffmpeg"] = $ffmpeg;
  16. $root = '../../assets/API/Universal/Dynamo';
  17. //$root = '../../上傳測試/Revit';
  18. $folder = '潛盾洞道走道_by 自適應元件';
  19. FindPath($root, $folder, $ffmpeg);
  20. if (!$isAJAX) {
  21. echo ($GLOBALS["output"]);
  22. }else{
  23. echo (implode(",",$GLOBALS["ajax"]));
  24. }
  25. function ConvertVideo($file_path, $ffmpeg)
  26. {
  27. $r_1080p = (new Representation)->setKiloBitrate(4096)->setResize(1920, 1080);
  28. $dir_name = pathinfo($file_path, PATHINFO_DIRNAME);
  29. $file_name = pathinfo($file_path, PATHINFO_FILENAME);
  30. $ext = pathinfo($file_path, PATHINFO_EXTENSION);
  31. if (!file_exists("../." . $dir_name . '/' . $file_name . ".m3u8")) {
  32. $video = $ffmpeg->open("../." . $file_path);
  33. $video->hls()
  34. ->x264()
  35. ->setHlsTime(300)
  36. ->addRepresentations([$r_1080p])
  37. ->save("../." . $dir_name . '/' . $file_name . ".m3u8");
  38. $GLOBALS["output"] .= "<span style='color:red;'>";
  39. $GLOBALS["output"] .= " " . $ext . " ==> m3u8";
  40. $GLOBALS["output"] .= "</span>";
  41. array_push($GLOBALS["ajax"],$file_name);
  42. }
  43. }
  44. function FindPath($root, $folder, $ffmpeg)
  45. {
  46. $rootPath = $root . '/' . $folder;
  47. $paths = array_diff(scandir($root . '/' . $folder), array('.', '..', 'Thumbs.db'));
  48. natsort($paths);
  49. $GLOBALS["output"] .= ("<ul>");
  50. foreach ($paths as $path) {
  51. if (str_contains($path, '.')) {
  52. $GLOBALS["output"] .= ("<li data-jstree='{ " . '"type" : "file"' . " }' >");
  53. $title = explode(".", $path)[0];
  54. $file_path = substr($rootPath, 4) . "/" . $path;
  55. $path = "title:" . $title . "path:" . $file_path . " dir_id:" . $folder;
  56. $GLOBALS["output"] .= ($path);
  57. $ext = pathinfo($file_path, PATHINFO_EXTENSION);
  58. if (in_array($ext, $GLOBALS["videoExt"])) {
  59. ConvertVideo($file_path, $ffmpeg);
  60. } else if (in_array($ext, $GLOBALS["pdfExt"])) {
  61. //ConvertPdf($file_path);
  62. }
  63. $GLOBALS["output"] .= ("</li>");
  64. } else {
  65. $GLOBALS["output"] .= ("<li>");
  66. $GLOBALS["output"] .= ("dir_name: " . $path . "parent: " . $folder);
  67. FindPath($rootPath, $path, $ffmpeg);
  68. $GLOBALS["output"] .= ("</li>");
  69. }
  70. }
  71. $GLOBALS["output"] .= ("</ul>");
  72. }