1

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.

Sara
  • 189
  • 1
  • 13
  • 1
    Instead of talking about the length of the videos, you should look at the file sizes. Even 15 seconds of video can be huge, depending on the compression and settings so that doesn't really tell us much about the context. I would also check if it was unusually slow from your server (if your server has some limit on upload speed). What you could do is to just upload it to your server, put it in a queue (like a database) and have a cron job that uploads the videos in the background. – M. Eriksson Feb 13 '23 at 08:46
  • Actually i tried commenting uploading in aws s3, still it takes more time during ffmpeg conversion itself. when i did research how can increase the conversion speed, i see there is ``speed`` and `` preset`` parameter. how do i set this parameter programmatically. I see many examples in command link argument only they are setting it.. – Sara Feb 13 '23 at 14:06
  • Is the question about slow upload to S3 or slow FFMpeg conversion? Those are two completely different questions. – M. Eriksson Feb 13 '23 at 14:11
  • Yes I thought it was taking time to upload in aws s3. But to break down the issue I commented the code (saving to s3) still ffmpeg itself taking time to convert in x264 and saving to php temp public folder. @M.Eriksson – Sara Feb 15 '23 at 06:04

0 Answers0