Search code examples
laravelfile-uploadffmpegvideo-processingffmpeg-php

Is there any way that I can speed up the ffmpeg processing time


I am facing a problem with the processing process. I use a real joint server in a digital hosting package of $ 10 and use cloud service from Amazon s3. The problem is when uploading a video, whatever the size of the video, whether its size is 1 megabyte or 2 Giga. After the upload process, the processing process starts to upload, there is no problem But when the processing process takes a very long time so that I cannot complete it, what is the solution to that, is there a problem for me or is this process normal? I use laravel-ffmpeg and through laravel queue I am cutting the video into several qualities I will attach the code to you below.

public function handle()
{
    //180p
    $lowBitrate1 = (new X264('aac'))->setKiloBitrate(613);
    //270p
    $lowBitrate2 = (new X264('aac'))->setKiloBitrate(906);
    //360p
    $midBitrate1 = (new X264('aac'))->setKiloBitrate(1687);
    //540p
    $midBitrate2 = (new X264('aac'))->setKiloBitrate(2227);
    //720p
    $highBitrate1 = (new X264('aac'))->setKiloBitrate(4300);
    //1080
    $highBitrate2 = (new X264('aac'))->setKiloBitrate(7917);

FFMpeg::fromDisk('s3')
    ->open($this->movie->path)
    ->exportForHLS()
    ->onProgress(function ($percent) {
        $this->movie->update([
            'percent' => $percent
        ]);
    })
    ->setSegmentLength(10)// optional
    ->addFormat($lowBitrate1)
    ->addFormat($lowBitrate2)
    ->addFormat($midBitrate1)
    ->addFormat($midBitrate2)
    ->addFormat($highBitrate1)
    ->addFormat($highBitrate2)
    ->toDisk('s3')
    ->save("public/Movies/{$this->movie->id}/{$this->movie->id}.m3u8");
}//end of handle

Solution

  • Not much you can do. x264 is highly optimized. Some minor suggestions (other than upgrading your cloud service):

    • Offer fewer bitrate tiers. Do you really need 6?

    • Ensure that x264 is using CPU capabilities. Refer to the ffmpeg log and look for the using cpu capabilities line. If it says none then you may be missing out on some speed. Recompile x264 and ffmpeg without --disable-asm.

    • Use a faster x264 encoding preset.

    • Make sure your ffmpeg and the linked x264 are not super old.

    • Stream copy the audio instead of re-encoding it if the input audio format is compatible with your output.

    • If encoding the audio, and it is the same for each output, use the tee muxer to encode it only once and propagate it to each output instead of needlessly encoding the same audio per output.

    • Try a hardware accelerated encoder, such as h264_nvenc if it is supported by your environment and your ffmpeg build. It may be faster, but will not provide as good quality per bit as x264. Note that some video cards may have an artificially enforced limit of number of streams that can be simultaneously encoded (although there are some workarounds, if I recall correctly).