John Davidson

php - how to trim video in laravel and get into blade

0 comments
Message:


so i am working on a project where a video uploading feature should be there,


this is how i uploading a video


//view for video


video uploader


<form action="{{route('videoUploader')}}" role="form" id="postForm" name="postForm" method="POST" 
enctype="multipart/form-data">
@csrf
<input type="file" name="files[]" multiple ><br/>
<input type="submit" value="upload">
</form>

and on controller side the code is
//controller for video


public function videoUpload(Request $request){
$files=$request->file('files');
$videoArray=[];
if($files){
foreach($files as $video){
$filename= $video->getClientOriginalName();
$extension = $video->getClientOriginalExtension();
$fileNameToStore = $filename. '_'.time().'.'.$extension;
$path = $video->move('storage/videos',$fileNameToStore);
$videoArray[]=$fileNameToStore;
}
}
return view('admin.Post.video',compact('videoArray'));
}

and this is how i am getting and see output on blade


@if (!empty($videoArray))
@foreach ($videoArray as $video)
<iframe src="{{ asset('storage/videos/'.$video) }}"frameborder="0" width="250" height="200"
allowfullscreen></iframe>
@endforeach
@endif

but the issue is, i want these videos to trim first, if video is more than 1 minute longer, it should be trimmed, and get trimmed video to show on the blade, and i don't have any idea how can i achieve this. i could not find any helpful tutorial on web for this, so i came here. please help me and thanks in advance



My Answer:
First, you need to install the "ffmpeg" package in your Laravel project. You can do this by running the following command in your terminal:


composer require pbmedia/laravel-ffmpeg


Next, you need to use the "FFMpeg" facade in your controller to trim the video. Here is an example of how you can trim a video in Laravel:

php
use FFMpeg;

public function trimVideo(Request $request)
{
$video = $request->file('video');
$videoName = time() . '.' . $video->getClientOriginalExtension();
$video->move(public_path('videos'), $videoName);

FFMpeg::fromDisk('local')
->open('videos/' . $videoName)
->export()
->trim(10, 20) // Trim video from 10 seconds to 20 seconds
->toDisk('local')
->inFormat(new X264)
->save('trimmed_videos/' . $videoName);

return view('video.trimmed', ['videoName' => $videoName]);
}


In your blade view, you can then display the trimmed video using the video name:

html



This is just a basic example of how you can trim a video in Laravel using the "ffmpeg" package. You can customize the trimming options according to your requirements.

Rate this post

4 of 5 based on 6519 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us