TestCommand.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Models\Base\Resource;
  5. use App\Repositories\Models\Course\Course;
  6. use App\Repositories\Models\Course\Video;
  7. use Carbon\Carbon;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Facades\Storage;
  10. use Illuminate\Support\Str;
  11. /**
  12. * Class PresenterCommand
  13. * @package Prettus\Repository\Generators\Commands
  14. * @author Anderson Andrade <contato@andersonandra.de>
  15. */
  16. class TestCommand extends Command
  17. {
  18. /**
  19. * The name of command.
  20. *
  21. * @var string
  22. */
  23. protected $name = 'mead:test';
  24. /**
  25. * The description of command.
  26. *
  27. * @var string
  28. */
  29. protected $description = 'Create a new course.';
  30. /**
  31. * The type of class being generated.
  32. *
  33. * @var string
  34. */
  35. protected $type = 'course';
  36. /**
  37. * Execute the command.
  38. *
  39. * @return void
  40. * @see fire()
  41. */
  42. public function handle()
  43. {
  44. $this->loadCourse(1, storage_path('app/public/zhongjingdaketang'));
  45. $this->line('ok');
  46. }
  47. /**
  48. * 加载课程
  49. * @param $id
  50. * @param $path
  51. * @return void
  52. */
  53. public function loadCourse($id, $path)
  54. {
  55. $course = Course::query()->where('id', 1)->first();
  56. $files = [];
  57. foreach (glob("{$path}/*") as $file) {
  58. $files[] = pathinfo($file);
  59. }
  60. foreach ($files as $file) {
  61. $file_name = $file['filename'];
  62. dd($file);
  63. $arr = str2arr($file_name, '-');
  64. $time = Carbon::parse(str_replace('.', '-', $arr[0]))->toDateTimeString();
  65. dd($time);
  66. $video_name = $arr[1];
  67. $teacher = str2arr($arr[2], '.')[0];
  68. $path = "zhongjingdaketang/{$file['basename']}";
  69. // $resource = Resource::query()->updateOrCreate([
  70. // 'name' => $file,
  71. // 'path' => $path,
  72. // 'disk' => 'public',
  73. // 'original_name' => $file,
  74. // ], [
  75. // 'url' => Storage::disk('public')->url($path)
  76. // ]);
  77. // Video::query()->updateOrCreate([
  78. // 'course_id' => $course->id,
  79. // 'title' => $video_name,
  80. // 'url' => $resource->id,
  81. // 'short_description' => $video_name,
  82. // 'description' => $video_name,
  83. // 'published_at' => $time,
  84. // ], [
  85. // 'slug' => Str::random(),
  86. // 'status' => ModelStatusEnum::OK
  87. // ]);
  88. }
  89. }
  90. public static function getVideoDuration($videoPath)
  91. {
  92. $cmd = "ffmpeg -i " . escapeshellarg($videoPath) . " 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//";
  93. exec($cmd, $output, $returnCode);
  94. if ($returnCode === 0 && !empty($output)) {
  95. $duration = $output[0];
  96. // Convert duration string to seconds
  97. $timeParts = explode(':', $duration);
  98. $hours = (int)$timeParts[0];
  99. $minutes = (int)$timeParts[1];
  100. $seconds = (float)$timeParts[2];
  101. $totalSeconds = ($hours * 3600) + ($minutes * 60) + $seconds;
  102. return $totalSeconds;
  103. } else {
  104. return null;
  105. }
  106. }
  107. }