Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ahmed96's avatar
Level 13

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?

0 likes
4 replies
Tray2's avatar
Tray2
Best Answer
Level 73

The only way to do this is to do it in the background with a worker. As for speed the only way to do that is to have more cpu power and more memory.

What I would do is use this kind of setup.

  1. The file is uploaded to a server.
  2. The file is moved to a processing server
  3. The file is split into 100Mb chunks
  4. The chunks are sent out to multiple recoding servers
  5. The recoded chunks are sent back to the processing server and joined
  6. The finished file is them moved to the final storage place and presented to the users.

There are of course lots of switches that can increase the speed of the conversion but it will most likely affect the quality of the movie.

Ahmed96's avatar
Level 13

Regarding the speed, it affects hosting. Is it normal, even if the video is less than 1 megabyte, also affected by the speed of hosting because I have slow speed in all sizes, whether I upload a video of 1 megabyte or 1 Giga.

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

This special code in the process of raising and processing in Java script will be attached below.

$('#movie__file-input').on('change',function() {
    $('#movie__upload-wrapper').css('display' , 'none');
    $('#movie__properties').css('display' , 'block');

    var url = $(this).data('url');
    var movie = this.files[0];
    var movieId = $(this).data('movie-id');
    var movieName = movie.name.split('.').slice(0, -1).join('.');
    $('#movie__name').val(movieName);

    var formData = new FormData();
    formData.append('movie_id', movieId);
    // formData.append('name', movieName);
    formData.append('movie', movie);

    $.ajax({
        url: url,
        data: formData,
        method: 'POST',
        processData: false,
        contentType: false,
        cache: false,
        success: function (movieBeforeProcessing) {
            var interval = setInterval (function () {
                $.ajax({
                    url: `/ATeChatH/posts/${movieBeforeProcessing.id}`,
                    method: 'GET',
                    success: function (movieWhileProcessing) {
                        $('#movie__upload-status').html('Processing');
                        $('#movie__upload-progress').css('width', movieWhileProcessing.percent + '%');
                        $('#movie__upload-progress').html(movieWhileProcessing.percent + '%');

                        if (movieWhileProcessing.percent == 100) {
                            clearInterval(interval); //break interval
                            $('#movie__upload-status').html('Done Processing');
                            $('#movie__upload-progress').parent().css('display', 'none');
                            $('#movie__submit-btn').css('display', 'block');
                        }
                    },

                });//end of ajax call
            }, 3000);
        },
        xhr: function () {
            var xhr = new window.XMLHttpRequest();
            xhr.upload.addEventListener("progress", function (evt) {
                if (evt.lengthComputable) {
                    var percentComplete = Math.round(evt.loaded / evt.total * 100) + "%";
                    $('#movie__upload-progress').css('width', percentComplete).html(percentComplete);
                }
            }, false);
            return xhr;
        },
    }); // end of ajax call


}); // end of file input
Tray2's avatar

It's a no brainer that it takes time.

You are running 6 conversions so yes it will take time.

I just ran one conversion on my local server a one minute clip 720p and converted it to 480p and it took about 8 seconds to finish. It's a Intel I5-7400 @3.00Ghz with 16Gb of ram running Ubuntu.

You do this six times for each clip.

Are you running them in paralell or in sequence?

Ahmed96's avatar
Level 13

I think in sequence, accuracy after true accuracy, I am using the special method laravel -ffmpeg, but I expect in sequence, in addition to your high specifications, it is true that the reason may be this thing is poor hosting and many of the qualities are the reason I will try to raise my site on hosting Contabo gives better resources and more appropriate prices.

Please or to participate in this conversation.