Uploading recording file stream to aws s3 in PHP is very slow

302 Views Asked by At

In my application I can record video and save it to aws s3 bucket using vueJS as front end and Laravel php as backend.

I am using ffmpeg to upload recording stream to s3 bucket.

1 min video taking 4 mins and

3 mins video taking 9 mins (Always not successfully uploaded, some times it fails)

Below is the code in backend.

public function video_upload(Request $request)
    {
                // Response Declaration   
                $response=array();
                $response_code = 200;
                $response['status'] = false;
                $response['data'] = [];
                // Validation
                // TODO: Specify mimes:mp4,webm,ogg etc 
                $validator = Validator::make(
                $request->all(), [
                'file' => 'required',
                ]
                );
                if ($validator->fails()) {
                $response['data']['validator'] = $validator->errors();
                return response()->json($response);
                }
                try{
                    $file = $request->file('file');
                    //covert
                    $ffmpeg = FFMpeg\FFMpeg::create();
                    $video = $ffmpeg->open($file);

                    $format = new X264();
                    $format->on('progress', function ($video, $format, $percentage) {
                        echo "$percentage % transcoded";
                    });

                    $video->save($format, 'output.mp4');
                    //end convert
                    $file_name =  str_replace ('/', '', Hash::make(time())).'.mp4';

                    $file_folder = 'uploads/video/';
                    // Store the file to S3
                    
                    // $store = Storage::disk('s3')->put($file_folder.$file_name, file_get_contents($file));
                    $store = Storage::disk('s3')->put($file_folder.$file_name, file_get_contents('output.mp4'));
                    if($store){
                        // Replace old file if exist
                        //delete the file from public folder
                        $file = public_path('output.mp4');
                        if (file_exists($file)) {
                            unlink($file);
                        }

                        if(isset($request->old_file)){
 
                            if(Storage::disk('s3')->exists($file_folder.basename($request->old_file))) {
                                 Storage::disk('s3')->delete($file_folder.basename($request->old_file));
                            }
                        }
                    }
                    $response['status'] = true;
                    $response['data']= '/s3/'.$file_folder. $file_name;

                }catch (\Exception $e) {
                    $response['data']['message']=$e->getMessage()."line".$e->getLine();
                    $response_code = 400;
                }
                return response()->json($response, $response_code);
    }

I was researching on Transfer Acceleration and multipart upload but question is do i do from aws end or in backend.

0

There are 0 best solutions below