123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace App\Console\Commands;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\Base\Resource;
- use App\Repositories\Models\Course\Course;
- use App\Repositories\Models\Course\Video;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- /**
- * Class PresenterCommand
- * @package Prettus\Repository\Generators\Commands
- * @author Anderson Andrade <contato@andersonandra.de>
- */
- class TestCommand extends Command
- {
- /**
- * The name of command.
- *
- * @var string
- */
- protected $name = 'mead:test';
- /**
- * The description of command.
- *
- * @var string
- */
- protected $description = 'Create a new course.';
- /**
- * The type of class being generated.
- *
- * @var string
- */
- protected $type = 'course';
- /**
- * Execute the command.
- *
- * @return void
- * @see fire()
- */
- public function handle()
- {
- $this->loadCourse(1, storage_path('app/public/zhongjingdaketang'));
- $this->line('ok');
- }
- /**
- * 加载课程
- * @param $id
- * @param $path
- * @return void
- */
- public function loadCourse($id, $path)
- {
- $course = Course::query()->where('id', 1)->first();
- $files = [];
- foreach (glob("{$path}/*") as $file) {
- $files[] = pathinfo($file);
- }
- foreach ($files as $file) {
- $file_name = $file['filename'];
- $arr = str2arr($file_name, '-');
- $time = Carbon::parse(str_replace('.', '-', $arr[0]))->toDateTimeString();
- $video_name = $arr[1];
- $teacher = str2arr($arr[2], '.')[0];
- $path = "zhongjingdaketang/{$file['basename']}";
- $resource = Resource::query()->updateOrCreate([
- 'name' => $file_name,
- 'path' => $path,
- 'disk' => 'public',
- 'original_name' => $file_name,
- ], [
- 'url' => Storage::disk('public')->url($path)
- ]);
- Video::query()->updateOrCreate([
- 'course_id' => $course->id,
- 'title' => $video_name,
- 'url' => $resource->id,
- 'short_description' => $video_name,
- 'description' => $video_name,
- 'published_at' => $time,
- ], [
- 'slug' => Str::random(),
- 'status' => ModelStatusEnum::OK
- ]);
- }
- }
- public static function getVideoDuration($videoPath)
- {
- $cmd = "ffmpeg -i " . escapeshellarg($videoPath) . " 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//";
- exec($cmd, $output, $returnCode);
- if ($returnCode === 0 && !empty($output)) {
- $duration = $output[0];
- // Convert duration string to seconds
- $timeParts = explode(':', $duration);
- $hours = (int)$timeParts[0];
- $minutes = (int)$timeParts[1];
- $seconds = (float)$timeParts[2];
- $totalSeconds = ($hours * 3600) + ($minutes * 60) + $seconds;
- return $totalSeconds;
- } else {
- return null;
- }
- }
- }
|