videoToHLS.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. set_time_limit(0);
  3. require '../../vendor/autoload.php';
  4. use Streaming\Representation;
  5. $ffmpeg = Streaming\FFMpeg::create([
  6. 'ffmpeg.binaries' => 'C:\FFmpeg\bin\ffmpeg.exe',
  7. 'ffprobe.binaries' => 'C:\FFmpeg\bin\ffprobe.exe',
  8. 'timeout' => 3600,
  9. ]);
  10. $GLOBALS["ffmpeg"] = $ffmpeg;
  11. $root = '../../assets';
  12. $folder = 'videos';
  13. FindPath($root, $folder, $ffmpeg);
  14. function Convert($file_path, $ffmpeg)
  15. {
  16. $r_1080p = (new Representation)->setKiloBitrate(4096)->setResize(1920, 1080);
  17. if (!file_exists("../." . substr($file_path, 0, -4) . ".m3u8")) {
  18. $video = $ffmpeg->open("../." . $file_path);
  19. $video->hls()
  20. ->x264()
  21. ->setHlsTime(300)
  22. ->addRepresentations([$r_1080p])
  23. ->save("../." . substr($file_path, 0, -4) . ".m3u8");
  24. $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(40))
  25. ->save("../." . substr($file_path, 0, -4) . ".jpg");
  26. $video
  27. ->gif(FFMpeg\Coordinate\TimeCode::fromSeconds(40), new FFMpeg\Coordinate\Dimension(640, 480), 5)
  28. ->save("../." . substr($file_path, 0, -4) . ".gif");
  29. }
  30. }
  31. function FindPath($root, $folder, $ffmpeg)
  32. {
  33. $rootPath = $root . '/' . $folder;
  34. $paths = array_diff(scandir($root . '/' . $folder), array('.', '..', 'Thumbs.db'));
  35. natsort($paths);
  36. echo ("<ul>");
  37. foreach ($paths as $path) {
  38. if (str_contains($path, '.')) {
  39. if (str_contains(strtolower($path), '.mp4') || str_contains(strtolower($path), '.wmv')) {
  40. echo ("<li data-jstree='{ " . '"type" : "file"' . " }' >");
  41. $title = explode(".", $path)[0];
  42. $file_path = substr($rootPath, 4) . "/" . $path;
  43. $path = "title:" . $title . "path:" . $file_path . " dir_id:" . $folder;
  44. echo ($path);
  45. echo ("</li>");
  46. Convert($file_path, $ffmpeg);
  47. }
  48. } else {
  49. echo ("<li>");
  50. echo ("dir_name: " . $path . "parent: " . $folder);
  51. FindPath($rootPath, $path, $ffmpeg);
  52. echo ("</li>");
  53. }
  54. }
  55. echo ("</ul>");
  56. }