Laravel - How to Convert File(image, audio, video) Extension using CloudConvert?

By Hardik Savani April 16, 2024 Category : Laravel

We may sometimes require to convert file extension like if you have video type mov or flv or 3gp etc and you should have to convert it into mp4, OR if you have image type png, jpeg, gif and require to convert it into jpj, Same as for audio mp3, then all the thing you can do using CloudConvert API. you can also convert pdf into jpg using CloudConvert API.

it might be help with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

In this example i use CloudConvert API with Laravel. CloudConvert is a very popular API for convert file extensions. So in this example i used CloudConvert API for any video extension convert into mp4. you can also convert anything as you want like any audio 3gp into mp3 etc.

So, you can convert any video extension into mp4, you have to just follow few step and get example:

1.Package Installation

In first step we have to install cloudconvert-laravel package using composer, so open your terminal and run bellow command:

composer require robbiep/cloudconvert-laravel

After install this package, Now open config/app.php file and add service provider.

config/app.php

'providers' => [

....

RobbieP\CloudConvertLaravel\CloudConvertLaravelServiceProvider::class

],

One more after this, we have to publish configuration file of cloudconvert api file. Run bellow command:

php artisan vendor:publish

Now you have configuration file here config/cloudconvert.php, In this file you have to put your API key. If you haven't API key then you can generate from here : cloudconvert.com And create new account, after email confirmation it will give you API key. you have to just copy that key and put here:

config/cloudconvert.php

return array(

'api_key' => 'Your API Key',

's3' => [

'accesskeyid' => '',

'secretaccesskey' => '',

'bucket' => '',

'acl' => '',

'region' => ''

],

'ftp' => [

'host' => '',

'user' => '',

'password' => '',

'port' => 21,

'dir' => '',

]

);

2. Add Route

First you have add two route in routes.php file. first one for generate view and second one for post method. so let's add bellow route in your routes.php file.

app/Http/routes.php

Route::get('fileUpload', function () {

return view('fileUpload');

});

Route::post('fileUpload', ['as'=>'fileUpload','uses'=>'HomeController@fileUpload']);

3. Add Controller Function

Ok, now we need to add fileUpload() in HomeController.php file. If you don't have HomeController then you can create new and put bellow code on that file. You must have one folder public/videos with full permission because every video will upload in videos folder.

app/Http/Controllers/HomeController.php

namespace App\Http\Controllers;


use Illuminate\Http\Request;

use CloudConvert;


class HomeController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('auth');

}


public function fileUpload(Request $request)

{

$this->validate($request, [

'video' => 'required|mimes:mp4,avi,asf,mov,qt,avchd,flv,swf,mpg,mpeg,mpeg-4,wmv,divx,3gp|max:20480',

]);


$videotmp = time();

$video = $request->file('video');

$input['video'] = $videotmp.'.'.$video->getClientOriginalExtension();

$destinationPath = public_path('/videos');

$video->move($destinationPath, $input['video']);


if($video->getClientOriginalExtension() != "mp4"){

CloudConvert::file($destinationPath.'/'.$input['video'])->to('mp4');

File::delete($destinationPath.'/'. $input['video']);

$input['video'] = $videotmp.'.mp4';

}


return back()->with('success','Video Upload successful');

}


}

4. Create Blade File

In At Last we require to create view file for image or file uploading. so you can create fileUpload.blade.php and put following code in that file.

resources/views/fileUpload.blade.php

@extends('layouts.app')


@section('content')


@if (count($errors) > 0)

<div class="alert alert-danger">

<strong>Whoops!</strong> There were some problems with your input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


{!! Form::open(array('route' => 'fileUpload','enctype' => 'multipart/form-data')) !!}

<div class="row cancel">

<div class="col-md-4">

{!! Form::file('video', array('class' => 'video')) !!}

</div>

<div class="col-md-4">

<button type="submit" class="btn btn-success">Create</button>

</div>

</div>

{!! Form::close() !!}


@endsection

Now you can run, If you want to get more information about package then you can from here : cloudconvert-laravel.

Tags :
Shares