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. $arr = str2arr($file_name, '-');
  63. $time = Carbon::parse(str_replace('.', '-', $arr[0]))->toDateTimeString();
  64. $video_name = $arr[1];
  65. $teacher = str2arr($arr[2], '.')[0];
  66. $path = "zhongjingdaketang/{$file['basename']}";
  67. $resource = Resource::query()->updateOrCreate([
  68. 'name' => $file_name,
  69. 'path' => $path,
  70. 'disk' => 'public',
  71. 'original_name' => $file_name,
  72. ], [
  73. 'url' => Storage::disk('public')->url($path)
  74. ]);
  75. Video::query()->updateOrCreate([
  76. 'course_id' => $course->id,
  77. 'title' => $video_name,
  78. 'url' => $resource->id,
  79. 'short_description' => $video_name,
  80. 'description' => $video_name,
  81. 'published_at' => $time,
  82. ], [
  83. 'slug' => Str::random(),
  84. 'status' => ModelStatusEnum::OK
  85. ]);
  86. }
  87. }
  88. public static function getVideoDuration($videoPath)
  89. {
  90. $cmd = "ffmpeg -i " . escapeshellarg($videoPath) . " 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//";
  91. exec($cmd, $output, $returnCode);
  92. if ($returnCode === 0 && !empty($output)) {
  93. $duration = $output[0];
  94. // Convert duration string to seconds
  95. $timeParts = explode(':', $duration);
  96. $hours = (int)$timeParts[0];
  97. $minutes = (int)$timeParts[1];
  98. $seconds = (float)$timeParts[2];
  99. $totalSeconds = ($hours * 3600) + ($minutes * 60) + $seconds;
  100. return $totalSeconds;
  101. } else {
  102. return null;
  103. }
  104. }
  105. }