添加自定义VideoGenerator Laravel Spatie媒体库

时间:2018-07-16 12:28:46

标签: laravel amazon-s3 spatie

在我的一个项目中使用Spatie媒体库,我想添加视频转换。上传视频后,将其他分辨率的视频添加到该特定媒体转换文件夹。

//控制器文件视频上传功能

public function uploadVideo(UploadVideoRequest $request, ListingStatusService $listingStatusService)
{
    $response = \DB::transaction(function() use($request, $listingStatusService) {
        $listing = Listing::find($request->get('listing_id'));

        if (!$listing->remaining_video)
            return response()->json([
                'success' => 0,
                'message' => 'The video may not have more than 3 items.'
            ]);

        $fileName = rand(1111, 9999) . '_' . time() . '.' . $request->file('video')->getClientOriginalExtension();
        if ($request->get('template_type') == VideoTemplate::TEMPLATE_BASIC) {
            $media = $listing->addMedia($request->file('video'))->usingFileName($fileName)
                ->withCustomProperties([
                    'templateType' => $request->get('template_type')
                ])
                ->toMediaCollection('listing_videos');
        } else {
            $media = $listing->addMedia($request->file('video'))->usingFileName($fileName)
                ->withCustomProperties([
                    'templateType' => $request->get('template_type'),
                    'shots' => $request->get('shots'),
                    'seconds' => $request->get('seconds'),
                    'volume' => $request->get('volume')
                ])
                ->toMediaCollection('listing_videos');
        }

        exec('ffmpeg -i '.$request->file('video').' -s 1040x480 -vcodec h264 -acodec copy '.Storage::putFile('listing/'.$listing->id.'/'.$media->id.'/conversions', '1040x480.mp4'));

        $listing->remaining_video = $listing->remaining_video - 1;
        $listing->save();

        $listingStatusService->saveListingStatus(
            new ListingStatus(),
            $listing->id,
            Listing::ITEM_TYPE_VIDEOS,
            $media->id,
            Listing::STATUS_SUBMITTED
        );

        return response()->json([
            'success' => 1,
        ]);
    });

    return $response;
}

//列表模型

class Listing extends Model implements HasMediaConversions{
    use HasMediaTrait;
    use MediaBaseConversions;

public function registerMediaConversions(Media $media = null)
{
    $listingStatus = ListingStatus::where('item_id', $media->id)->where('status', self::STATUS_APPROVED)->whereNotNull('review_by')->first();
    if ($listingStatus)
        $this->registerBaseConversions(true);
}

}

那么,如何进行视频转换?

请帮助我。

谢谢!

0 个答案:

没有答案