ffmpeg-mappable-media/README.md

109 lines
4.1 KiB
Markdown
Raw Normal View History

2022-08-29 11:09:17 -05:00
# Mappable Media for PHP-FFMPEG
2012-04-13 14:16:50 +02:00
2022-08-21 15:37:37 -05:00
[![Latest Version on Packagist](https://img.shields.io/packagist/v/danjones000/ffmpeg-mappable-media.svg?style=flat-square)](https://packagist.org/packages/danjones000/ffmpeg-mappable-media)
[![Total Downloads](https://img.shields.io/packagist/dt/danjones000/ffmpeg-mappable-media.svg?style=flat-square)](https://packagist.org/packages/danjones000/ffmpeg-mappable-media)
2013-10-02 10:24:12 +02:00
2022-08-29 11:09:17 -05:00
Mappable Media is a plugin for [PHP-FFMpeg](https://github.com/PHP-FFMpeg/PHP-FFMpeg/) which provides a mechanism for mapping individual streams from input to output files.
2013-10-02 10:24:12 +02:00
2022-08-29 11:09:17 -05:00
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](https://github.com/PHP-FFMpeg/PHP-FFMpeg/#advancedmedia) provides.
2012-05-31 16:07:30 +02:00
2022-08-21 15:37:37 -05:00
## Installation
2022-02-09 14:36:26 +01:00
2022-08-21 15:37:37 -05:00
You can install the package via composer:
2012-05-31 16:25:42 +02:00
```bash
2022-08-21 15:37:37 -05:00
composer require danjones000/ffmpeg-mappable-media
2013-07-03 18:57:42 +02:00
```
2022-08-21 15:37:37 -05:00
## Usage
```php
2022-08-29 11:09:17 -05:00
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();
```
2022-08-29 11:19:40 -05:00
The `saveStream` method returns the `Map` that created it. Likewise, the `saveMap` returns the `MappableMedia` that created it. All other methods are chainable.
2022-08-29 11:09:17 -05:00
So, this could be written as a very long single call like this:
```php
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:
```shell
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
2013-07-03 20:16:29 +02:00
```
2022-08-29 11:09:17 -05:00
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.
2013-07-03 18:14:44 +02:00
2022-08-29 11:09:17 -05:00
## What's not yet done?
2013-07-03 18:14:44 +02:00
2022-08-29 11:09:17 -05:00
Mostly documentation. There are a lot more ways to use this, including using the `Format` objects from PHP-FFMpeg to set the codecs, and the ability to use callbacks to set additional data on both maps and individual streams.
2013-07-03 18:14:44 +02:00
2022-08-29 11:09:17 -05:00
I also need to figure out how to set the listeners on the codecs to get realtime progress updates (see [Formats](https://github.com/PHP-FFMpeg/PHP-FFMpeg/#formats)). Once that's figured out, I need to document it properly here.
2013-08-05 14:40:53 +02:00
2022-08-29 11:09:17 -05:00
## Contributing
2013-08-05 14:40:53 +02:00
2022-08-29 11:09:17 -05:00
Please see [CONTRIBUTING](https://codeberg.org/spatie/.github/blob/main/CONTRIBUTING.md) for details.
2022-08-21 15:37:37 -05:00
## Credits
2017-10-16 16:53:42 +02:00
2022-08-21 15:37:37 -05:00
- [Dan Jones](https://codeberg.org/danjones000)
- [All Contributors](../../contributors)
2017-10-16 16:53:42 +02:00
2013-07-03 18:14:44 +02:00
## License
2012-05-31 16:07:30 +02:00
2022-08-21 15:37:37 -05:00
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.