sormagec/laravel-ffmpeg
Composer 安装命令:
composer require sormagec/laravel-ffmpeg
包简介
FFMpeg for Laravel
关键字:
README 文档
README
This package provides an integration with FFmpeg for Laravel 5.6. The storage of the files is handled by Laravel's Filesystem.
Features
- Super easy wrapper around PHP-FFMpeg, including support for filters and other advanced features.
- Integration with Laravel's Filesystem, configuration system and logging handling.
- Compatible with Laravel 5.6.
- Support for Package Discovery.
- PHP 7.1 and 7.2 only.
Installation
This version of the package is only compatible with Laravel 5.6. If you're still using Laravel 5.1 - 5.5, please use version 1.3 (which is not maintained anymore).
You can install the package via composer:
composer require pbmedia/laravel-ffmpeg
Add the Service Provider and Facade to your app.php config file if you're not using Package Discovery.
// Laravel 5: config/app.php 'providers' => [ ... Sormagec\LaravelFFMpeg\FFMpegServiceProvider::class, ... ]; 'aliases' => [ ... 'FFMpeg' => Sormagec\LaravelFFMpeg\FFMpegFacade::class ... ];
Publish the config file using the artisan CLI tool:
php artisan vendor:publish --provider="Sormagec\LaravelFFMpeg\FFMpegServiceProvider"
Usage
Convert an audio or video file:
FFMpeg::fromDisk('songs') ->open('yesterday.mp3') ->export() ->toDisk('converted_songs') ->inFormat(new \FFMpeg\Format\Audio\Aac) ->save('yesterday.aac');
Instead of the fromDisk() method you can also use the fromFilesystem() method, where $filesystem is an instance of Illuminate\Contracts\Filesystem\Filesystem.
$media = FFMpeg::fromFilesystem($filesystem)->open('yesterday.mp3');
You can add filters through a Closure or by using PHP-FFMpeg's Filter objects:
FFMpeg::fromDisk('videos') ->open('steve_howe.mp4') ->addFilter(function ($filters) { $filters->resize(new \FFMpeg\Coordinate\Dimension(640, 480)); }) ->export() ->toDisk('converted_videos') ->inFormat(new \FFMpeg\Format\Video\X264) ->save('small_steve.mkv'); // or $start = \FFMpeg\Coordinate\TimeCode::fromSeconds(5) $clipFilter = new \FFMpeg\Filters\Video\ClipFilter($start); FFMpeg::fromDisk('videos') ->open('steve_howe.mp4') ->addFilter($clipFilter) ->export() ->toDisk('converted_videos') ->inFormat(new \FFMpeg\Format\Video\X264) ->save('short_steve.mkv');
Sometimes you don't want to use the built-in filters. You can apply your own filter by providing a set of options. This can be an array or multiple strings as arguments:
FFMpeg::fromDisk('videos') ->open('steve_howe.mp4') ->addFilter(['-itsoffset', 1]); // or FFMpeg::fromDisk('videos') ->open('steve_howe.mp4') ->addFilter('-itsoffset', 1);
Chain multiple convertions:
// The 'fromDisk()' method is not required, the file will now // be opened from the default 'disk', as specified in // the config file. FFMpeg::open('my_movie.mov') // export to FTP, converted in WMV ->export() ->toDisk('ftp') ->inFormat(new \FFMpeg\Format\Video\WMV) ->save('my_movie.wmv') // export to Amazon S3, converted in X264 ->export() ->toDisk('s3') ->inFormat(new \FFMpeg\Format\Video\X264) ->save('my_movie.mkv'); // you could even discard the 'toDisk()' method, // now the converted file will be saved to // the same disk as the source! ->export() ->inFormat(new FFMpeg\Format\Video\WebM) ->save('my_movie.webm') // optionally you could set the visibility // of the exported file ->export() ->inFormat(new FFMpeg\Format\Video\WebM) ->withVisibility('public') ->save('my_movie.webm')
Create a frame from a video:
FFMpeg::fromDisk('videos') ->open('steve_howe.mp4') ->getFrameFromSeconds(10) ->export() ->toDisk('thumnails') ->save('FrameAt10sec.png'); // Instead of the 'getFrameFromSeconds()' method, you could // also use the 'getFrameFromString()' or the // 'getFrameFromTimecode()' methods: $media = FFMpeg::open('steve_howe.mp4'); $frame = $media->getFrameFromString('00:00:13.37'); // or $timecode = new FMpeg\Coordinate\TimeCode(...); $frame = $media->getFrameFromTimecode($timecode);
With the Media class you can determinate the duration of a file:
$media = FFMpeg::open('wwdc_2006.mp4'); $durationInSeconds = $media->getDurationInSeconds(); // returns an int $durationInMiliseconds = $media->getDurationInMiliseconds(); // returns a float
When opening or saving files from or to a remote disk, temporary files will be created on your server. After you're done exporting or processing these files, you could clean them up by calling the cleanupTemporaryFiles() method:
FFMpeg::cleanupTemporaryFiles();
HLS
You can create a M3U8 playlist to do HLS. Exporting is currently only supported on local disks.
$lowBitrate = (new X264)->setKiloBitrate(250); $midBitrate = (new X264)->setKiloBitrate(500); $highBitrate = (new X264)->setKiloBitrate(1000); FFMpeg::fromDisk('videos') ->open('steve_howe.mp4') ->exportForHLS() ->setSegmentLength(10) // optional ->addFormat($lowBitrate) ->addFormat($midBitrate) ->addFormat($highBitrate) ->save('adaptive_steve.m3u8');
As of version 1.2.0 the addFormat method of the HLS exporter takes an optional second parameter which can be a callback method. This allows you to add different filters per format:
$lowBitrate = (new X264)->setKiloBitrate(250); $highBitrate = (new X264)->setKiloBitrate(1000); FFMpeg::open('steve_howe.mp4') ->exportForHLS() ->addFormat($lowBitrate, function($media) { $media->addFilter(function ($filters) { $filters->resize(new \FFMpeg\Coordinate\Dimension(640, 480)); }); }) ->addFormat($highBitrate, function($media) { $media->addFilter(function ($filters) { $filters->resize(new \FFMpeg\Coordinate\Dimension(1280, 960)); }); }) ->save('adaptive_steve.m3u8');
As of version 1.3.0 you can monitor the transcoding progress of a HLS export. Use the onProgress method to provide a callback which gives you the completed percentage.
$exporter = FFMpeg::open('steve_howe.mp4') ->exportForHLS() ->onProgress(function ($percentage) { echo "$percentage % transcoded"; });
Advanced
The Media object you get when you 'open' a file, actually holds the Media object that belongs to the underlying driver. It handles dynamic method calls as you can see here. This way all methods of the underlying driver are still available to you.
// This gives you an instance of Sormagec\LaravelFFMpeg\Media $media = FFMpeg::fromDisk('videos')->open('video.mp4'); // The 'getStreams' method will be called on the underlying Media object since // it doesn't exists on this object. $codec = $media->getStreams()->first()->get('codec_name');
If you want direct access to the underlying object, call the object as a function (invoke):
// This gives you an instance of Sormagec\LaravelFFMpeg\Media $media = FFMpeg::fromDisk('videos')->open('video.mp4'); // This gives you an instance of FFMpeg\Media\MediaTypeInterface $baseMedia = $media();
Example app
Here's a blogpost that will help you get started with this package:
https://pascalbaljetmedia.com/en/blog/how-to-use-ffmpeg-in-your-laravel-projects
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email pascal@pascalbaljetmedia.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
sormagec/laravel-ffmpeg 适用场景与选型建议
sormagec/laravel-ffmpeg 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel-ffmpeg」 「sormagec」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sormagec/laravel-ffmpeg 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sormagec/laravel-ffmpeg 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sormagec/laravel-ffmpeg 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
FFMpeg for Laravel
Laravel validator for video and audio files
A very simple activity logger to monitor the users of your website or application (for MongoDB)
FFMpeg for Laravel
FFMpeg for Laravel
FFMpeg for Laravel
统计信息
- 总下载量: 23
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-30