danjones000/ffmpeg-mappable-media
Composer 安装命令:
composer require danjones000/ffmpeg-mappable-media
包简介
A plugin for php-ffmpeg/php-ffmpeg providing greater control of mapping input files to output files
关键字:
README 文档
README
Mappable Media is a plugin for PHP-FFMpeg which provides a mechanism for mapping individual streams from input to output files.
It allows for greater control regarding specific codecs and metadata from a particular input stream to a particular output stream than the built-in AdvancedMedia provides.
Installation
You can install the package via composer:
composer require danjones000/ffmpeg-mappable-media
Usage
use Danjones\FFMpeg\MappableMedia;
use FFMpeg\FFMpeg;
$ffmpeg = FFMpeg::create();
$media = MappableMedia::make($ffmpeg);
$media->addInput('video.mp4');
$media->addInput('audio.opus');
$media->addInput('sub.srt');
$output1 = $media->map();
$output1->saveAs('no-subs.webm');
$output1->metadata('title', 'The Greatest Stury Ever Hulaed');
$stream1 = $output1->stream();
$stream1->setInput('0:0');
$stream1->video('libvp9');
$stream1->saveStream();
$stream2 = $output1->stream();
$stream2->setInput('1:0');
$stream2->copy();
$stream2->saveStream();
$output1->saveMap();
$output2 = $media->map();
$output2->saveAs('all-copy.mkv');
// Most methods return the object that calls them.
$output2->stream()->setInput('0:0')->copy()->saveStream();
$output2->stream()->setInput('1:0')->copy()->saveStream();
$output2->stream()->setInput('2:0')->copy()->metadata('language', 'eng')->saveStream();
$output2->saveMap();
$media->save();
The saveStream method returns the Map that created it. Likewise, the saveMap returns the MappableMedia that created it. All other methods are chainable.
So, this could be written as a very long single call like this:
MappableMedia::make($ffmpeg)
->addInput('video.mp4')
->addInput('audio.opus')
->addInput('sub.srt')
->map()
->saveAs('no-subs.webm')
->metadata('title', 'The Greatest Story Ever Hulaed')
->stream()->setInput('0:0')->video('libvp9')->saveStream()
->stream()->setInput('1:0')->copy()->saveStream()
->saveMap()
->map()
->saveAs('all-copy.mkv')
->stream()->setInput('0:0')->copy()->saveStream()
->stream()->setInput('1:0')->copy()->saveStream()
->stream()->setInput('2:0')->copy()->metadata('language', 'eng')->saveStream()
->saveMap()
->save()
Both of these calls would result in the following command being run:
ffmpeg -y -i video.mp4 -i audio.opus -i sub.srt -map 0:0 -c:0 libvp9 -map 1:0 -c:1 copy -metadata title="The Greatest Story Ever Hulaed" no-subs.webm -map 0:0 -c:0 copy -map 1:0 -c:1 copy -map 2:0 -c:2 copy -metadata:s:2 language=eng all-copy.mkv
This will result in two files. The first will be a webm file (suitable for play in a browser) with a title field on the file set to "The Greatest Story Ever Hulaed". The second file will copy the first stream from each file directly, and set the language of the subtitle stream to English.
Using FormatInterface
It is also possible to set the codec from a stream using the Formats from PHP-FFMPEG.
use FFMpeg\Format\Video\X264;
MappableMedia::make($ffmpeg)
->addInput('input.mkv')
->map()
->saveAs('output.mkv')
->stream()->setCodec(new X264())->saveStream()
->saveMap()
->save()
Using callbacks
The map and stream methods can take an optional callback, allowing you to set the properties on those individual objects. When a callback is passed the object itself is returned, allowing you to continue the chain.
MappableMedia::make($ffmpeg)
->addInput('input.mkv')
->map(function (Map $map) {
$map->saveAs('output.mkv')
->stream(function (Stream $stream) {
$stream->copy()->setInput('0:0');
});
})->save()
Note that when using callbacks, you also don't need to call saveMap() or saveStream().
Getting progress updates
It is possible to set a listener on the individual streams, using the Format class. However, this don't reliably report the progress of a particular stream. So, this package adds a listener on the MappableMedia object itself, which represents the progress of the entire job.
MappableMedia::make($ffmpeg)
->addInput('input.mkv')
->map()
->saveAs('output.mkv')
->stream()->copy()->saveStream()
->saveMap()
->on('progress', function (MappableMedia $media, int $percent, int $remaining, int $rate) {
echo "Finished {$percent}% of ", $media->getPathfile(), "\n";
})
->save()
Attachments
Some formats (mkv, for example) support arbitrary data attachments. These can be used as cover art, fonts for subtitles, or any arbitrary data.
FFMpeg does support attachments as an additional input. This works well for images, but can be finicky for other file types. Because of this, FFMpeg also supports an -attach flag which can be used to explicitly attach a new stream.
Due to the way FFMpeg handles -attach differently than -i, these need to be added as streams to a specific map, rather than the whole media. Here are a few examples.
MappableMedia::make($ffmpeg)
->addInput('input.mkv')
->map()
->saveAs('output.mkv')
->stream()->copy()->saveStream()
->attach('image.jpg')
->mime('image/jpeg')
->addMetadata('filename', 'cover.jpg')
->saveAttachment()
->saveMap()
->save();
In this example, we added cover art to the file. Notice the use of the mime method to specify the mime-type. This must always be done. Note that we also specified a different filename so that the media player would recognize it as cover art. If you don't specify a filename, the original will be used.
MappableMedia::make($ffmpeg)
->addInput('input.mkv')
->addInput('subs.ass')
->map()
->saveAs('output.mkv')
->stream()->copy()->saveStream()
->stream()->setInput('1:0')->copy()->saveStream()
->attach(verdana.ttf')
->mime('application/x-truetype-font')
->saveAttachment()
->saveMap()
->save();
In this example, we've added a font, which is likely referenced in the subtitle file, subs.ass.
Future Plans
- [ ] Add listeners that return all the stdin/stderr
- [ ] Support itsoffset for inputs
- [ ] Support -t and -ss
- I need to figure out how this will affect the progress listener.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
danjones000/ffmpeg-mappable-media 适用场景与选型建议
danjones000/ffmpeg-mappable-media 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 08 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「audio」 「video processing」 「video」 「audio processing」 「avconv」 「ffmpeg」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 danjones000/ffmpeg-mappable-media 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 danjones000/ffmpeg-mappable-media 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 danjones000/ffmpeg-mappable-media 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Library for manipulating audio files (validating, transcoding, and tagging)
paywant api php client
The yii2-videojs-widget is a Yii 2 wrapper for the [video.js](http://www.videojs.com/). A JavaScript and CSS library that makes it easier to work with and build on HTML5 video. This is also known as an HTML5 Video Player.
Display a video using native HTML5 video
Convert and operate with FIPS codes for states, counties, etc.
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-29