laravel-ffmpeg icon indicating copy to clipboard operation
laravel-ffmpeg copied to clipboard

CopyFormat and HLS conversion

Open fcno opened this issue 4 years ago • 2 comments

Hey Pascal.

In the project I'm working on, I need to generate the HLS with the same characteristics as the original file. I tried to use use ProtoneMedia\LaravelFFMpeg\FFMpeg\CopyFormat in some ways, but it doesn't seem to work with HLS conversion.

Is this 'incompatibility' intentional? Is there a reason for it or should it work and is it a flaw in my code?

The road so far:

use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;
use ProtoneMedia\LaravelFFMpeg\FFMpeg\CopyFormat;
        FFMpeg::fromDisk('upload')
            ->open('video.mp4')
            ->exportForHLS()
            ->toDisk('stream')
            ->addFormat(new CopyFormat)
            ->save('video.m3u8');

The code above throws the follow error:

TypeError: ProtoneMedia\LaravelFFMpeg\Exporters\HLSExporter::getSegmentPatternAndFormatPlaylistPath(): Argument #2 ($format) must be of type FFMpeg\Format\VideoInterface, ProtoneMedia\LaravelFFMpeg\FFMpeg\CopyFormat given

I also tried the code bellow

use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;
use ProtoneMedia\LaravelFFMpeg\FFMpeg\CopyFormat;
        FFMpeg::fromDisk('upload')
            ->open('video.mp4')
            ->exportForHLS()
            ->toDisk('stream')
            ->inFormat(new CopyFormat) // changed here
            ->save('video.m3u8');

The code above throws the follow error:

ProtoneMedia\LaravelFFMpeg\Exporters\NoFormatException

Sure, if I use the recommend way new X264('aac') everything works just fine.

fcno avatar Mar 15 '21 15:03 fcno

I just tagged v7.5.11 that includes a CopyVideoFormat class. Could you try that format and report the result?

pascalbaljet avatar Apr 25 '21 21:04 pascalbaljet

Hello @pascalbaljet . It worked very well and the time to generate the HLS was greatly reduced. However, there was a side effect. The size of the default segments length has changed.

With the following code, the segment length is around 10 seconds. (as it the default value definied in the library)

            FFMpeg::fromDisk('video')
                ->open($file)
                ->exportForHLS()
                ->addFormat($bitrate)
                ->toDisk('video')
                ->onProgress(function ($percentage) {
                    $this->processamento = $percentage;
                    $this->save();
                })
                ->save($stream);

But if i use the following code, the length is between 15 and 35 seconds and ->setSegmentLength(10) make no diference

use ProtoneMedia\LaravelFFMpeg\FFMpeg\CopyVideoFormat;
            //cria o stream
            FFMpeg::fromDisk('video')
                ->open($file)
                ->exportForHLS()
                ->addFormat(new CopyVideoFormat)//changed
                ->toDisk('video')
                ->onProgress(function ($percentage) {
                    $this->processamento = $percentage;
                    $this->save();
                })
                ->save($stream);

fcno avatar May 12 '21 21:05 fcno