Compare commits
10 commits
aeb73dba94
...
42c112c942
| Author | SHA1 | Date | |
|---|---|---|---|
| 42c112c942 | |||
| 249a559c12 | |||
| cb892134ca | |||
| 77cf08260a | |||
| 82d2fdaeae | |||
| 77d43e9f69 | |||
| 4a833b4ca9 | |||
| e07743625c | |||
| 7baacb86a5 | |||
| 18c3cbb284 |
6 changed files with 219 additions and 121 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -12,3 +12,5 @@ vendor
|
|||
.php-cs-fixer.cache
|
||||
|
||||
*.mkv
|
||||
tags
|
||||
*.mp3
|
||||
|
|
|
|||
|
|
@ -2,6 +2,15 @@
|
|||
|
||||
All notable changes to `ffmpeg-mappable-media` will be documented in this file.
|
||||
|
||||
## 0.2.0
|
||||
|
||||
- ✨ Support attachments
|
||||
|
||||
## 0.1.3
|
||||
|
||||
- ✨ Support progress listeners
|
||||
- 📝 Improve documentation
|
||||
|
||||
## 0.1.0
|
||||
|
||||
🎉 Initial release. Basic functionality works.
|
||||
|
|
|
|||
98
README.md
98
README.md
|
|
@ -88,11 +88,103 @@ ffmpeg -y -i video.mp4 -i audio.opus -i sub.srt -map 0:0 -c:0 libvp9 -map 1:0 -c
|
|||
|
||||
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.
|
||||
|
||||
## What's not yet done?
|
||||
### Using FormatInterface
|
||||
|
||||
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.
|
||||
It is also possible to set the codec from a stream using the [Formats](https://github.com/PHP-FFMpeg/PHP-FFMpeg/#formats) from PHP-FFMPEG.
|
||||
|
||||
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.
|
||||
```php
|
||||
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.
|
||||
|
||||
```php
|
||||
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.
|
||||
|
||||
```php
|
||||
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.
|
||||
|
||||
```php
|
||||
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.
|
||||
|
||||
```php
|
||||
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
|
||||
|
||||
|
|
|
|||
46
src/Attachment.php
Normal file
46
src/Attachment.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace Danjones\FFMpeg;
|
||||
|
||||
use FFMpeg\Format\FormatInterface;
|
||||
|
||||
class Attachment extends Stream
|
||||
{
|
||||
protected string $input = '';
|
||||
|
||||
public function __construct(Map $map, string $file = '')
|
||||
{
|
||||
$this->map = $map;
|
||||
$this->input = $file;
|
||||
// Shouldn't be necessary, but just in case
|
||||
$this->codec = new Format\Copy();
|
||||
}
|
||||
|
||||
public function mime(string $mime): static
|
||||
{
|
||||
return $this->addMetadata('mimetype', $mime);
|
||||
}
|
||||
|
||||
public function setCodec(FormatInterface $codec): static
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function parseCodec(): static
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function saveAttachment(): Map
|
||||
{
|
||||
return $this->saveStream();;
|
||||
}
|
||||
|
||||
public function buildCommand(int $idx = 0): array
|
||||
{
|
||||
$commands = parent::buildCommand($idx);
|
||||
$commands[0] = '-attach';
|
||||
|
||||
return $commands;
|
||||
}
|
||||
}
|
||||
46
src/Map.php
46
src/Map.php
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
namespace Danjones\FFMpeg;
|
||||
|
||||
use FFMpeg\Format\ProgressableInterface;
|
||||
use FFMpeg\Format\ProgressListener\AbstractProgressListener;
|
||||
|
||||
class Map
|
||||
{
|
||||
use Traits\HasMetadata;
|
||||
|
|
@ -10,6 +13,10 @@ class Map
|
|||
protected string $path;
|
||||
/** @var Stream[] */
|
||||
protected array $streams = [];
|
||||
/** @var Attachment[] */
|
||||
protected array $attachments = [];
|
||||
/** @var AbstractProgressListener[] */
|
||||
protected array $listeners = [];
|
||||
|
||||
public function __construct(MappableMedia $media)
|
||||
{
|
||||
|
|
@ -30,9 +37,8 @@ class Map
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function stream(callable $callback = null): Stream|static
|
||||
protected function doStream(Stream $stream, callable $callback = null): Stream|static
|
||||
{
|
||||
$stream = new Stream($this);
|
||||
if (!$callback) {
|
||||
return $stream;
|
||||
}
|
||||
|
|
@ -43,17 +49,49 @@ class Map
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function stream(callable $callback = null): Stream|static
|
||||
{
|
||||
return $this->doStream(new Stream($this), $callback);
|
||||
}
|
||||
|
||||
public function attach(string $file = '', callable $callback = null): Attachment|static
|
||||
{
|
||||
return $this->doStream(new Attachment($this, $file), $callback);
|
||||
}
|
||||
|
||||
public function saveStream(Stream $stream): static
|
||||
{
|
||||
$this->streams[] = $stream;
|
||||
if ($stream instanceof Attachment){
|
||||
$this->attachments[] = $stream;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->streams[] = $stream;
|
||||
$format = $stream->getCodec();
|
||||
if ($format instanceof ProgressableInterface) {
|
||||
$listener = $format->createProgressListener(
|
||||
$this->media,
|
||||
$this->media->getFFProbe(),
|
||||
1, 1, 0
|
||||
);
|
||||
$this->listeners = array_merge($this->listeners, $listener);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getListeners(): array
|
||||
{
|
||||
return $this->listeners;
|
||||
}
|
||||
|
||||
public function buildCommand(): array
|
||||
{
|
||||
$commands = [];
|
||||
foreach ($this->streams as $idx => $stream) {
|
||||
$streams = $this->streams;
|
||||
array_push($streams, ...$this->attachments);
|
||||
foreach ($streams as $idx => $stream) {
|
||||
array_push($commands, ...$stream->buildCommand($idx));
|
||||
}
|
||||
foreach ($this->metadata as $k => $v) {
|
||||
|
|
|
|||
|
|
@ -3,31 +3,27 @@
|
|||
namespace Danjones\FFMpeg;
|
||||
|
||||
use Alchemy\BinaryDriver\Exception\ExecutionFailureException;
|
||||
use Evenement\EventEmitterInterface;
|
||||
use Evenement\EventEmitterTrait;
|
||||
use FFMpeg\Driver\FFMpegDriver;
|
||||
use FFMpeg\Exception\RuntimeException;
|
||||
use FFMpeg\FFMpeg;
|
||||
use FFMpeg\FFProbe;
|
||||
use FFMpeg\Filters\AdvancedMedia\ComplexCompatibleFilter;
|
||||
use FFMpeg\Filters\AdvancedMedia\ComplexFilterContainer;
|
||||
use FFMpeg\Filters\AdvancedMedia\ComplexFilterInterface;
|
||||
use FFMpeg\Filters\AdvancedMedia\ComplexFilters;
|
||||
use FFMpeg\Filters\FiltersCollection;
|
||||
use FFMpeg\Format\AudioInterface;
|
||||
use FFMpeg\Format\FormatInterface;
|
||||
use FFMpeg\Format\ProgressableInterface;
|
||||
use FFMpeg\Format\ProgressListener\AbstractProgressListener;
|
||||
use FFMpeg\Format\VideoInterface;
|
||||
use FFMpeg\Format\ProgressListener\VideoProgressListener;
|
||||
use FFMpeg\Media\AbstractMediaType;
|
||||
|
||||
/**
|
||||
* AdvancedMedia may have multiple inputs and multiple outputs.
|
||||
* This class accepts only filters for -filter_complex option.
|
||||
* MappableMedia may have multiple inputs and multiple outputs.
|
||||
* This class does not accept filters.
|
||||
* But you can set initial and additional parameters of the ffmpeg command.
|
||||
*
|
||||
* @see http://trac.ffmpeg.org/wiki/Creating%20multiple%20outputs
|
||||
*/
|
||||
class MappableMedia extends AbstractMediaType
|
||||
class MappableMedia extends AbstractMediaType implements EventEmitterInterface
|
||||
{
|
||||
use EventEmitterTrait;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
|
|
@ -46,16 +42,12 @@ class MappableMedia extends AbstractMediaType
|
|||
*/
|
||||
protected array $additionalParameters = [];
|
||||
|
||||
/**
|
||||
* @var AbstractProgressListener[]
|
||||
*/
|
||||
protected array $listeners = [];
|
||||
/** @var AbstractProgressListener[] */
|
||||
protected array $progressListeners = [];
|
||||
|
||||
public function __construct(FFMpegDriver $driver, FFProbe $ffprobe)
|
||||
{
|
||||
// In case of error user will see this text in the error log.
|
||||
// But absence of inputs is a correct situation for some cases.
|
||||
// For example, if the user will use filters such as "testsrc".
|
||||
$pathfile = 'you_can_pass_empty_inputs_array_only_if_you_use_computed_inputs';
|
||||
|
||||
parent::__construct($pathfile, $driver, $ffprobe);
|
||||
|
|
@ -68,7 +60,7 @@ class MappableMedia extends AbstractMediaType
|
|||
|
||||
public function filters()
|
||||
{
|
||||
return $this->filters;
|
||||
return null;
|
||||
}
|
||||
|
||||
public function addInput(string $path): static
|
||||
|
|
@ -133,7 +125,9 @@ class MappableMedia extends AbstractMediaType
|
|||
|
||||
public function getFinalCommand(): string
|
||||
{
|
||||
return implode(' ', $this->buildCommand());
|
||||
$proc = $this->driver->getProcessBuilderFactory()->create($this->buildCommand());
|
||||
|
||||
return $proc->getCommandLine();
|
||||
}
|
||||
|
||||
public function map(callable $callback = null): Map|static
|
||||
|
|
@ -152,118 +146,35 @@ class MappableMedia extends AbstractMediaType
|
|||
public function saveMap(Map $map): static
|
||||
{
|
||||
$this->maps[] = $map;
|
||||
$this->progressListeners = array_merge($this->progressListeners, $map->getListeners());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select the streams for output.
|
||||
*
|
||||
* @param string[] $outs output labels of the -filter_complex part
|
||||
* @param FormatInterface $format format of the output file
|
||||
* @param string $outputFilename output filename
|
||||
* @param bool $forceDisableAudio
|
||||
* @param bool $forceDisableVideo
|
||||
*
|
||||
* @return $this
|
||||
* @todo Redo all of this.
|
||||
* @see https://ffmpeg.org/ffmpeg.html#Manual-stream-selection
|
||||
*/
|
||||
private function map2(
|
||||
array $outs,
|
||||
FormatInterface $format,
|
||||
$outputFilename,
|
||||
$forceDisableAudio = false,
|
||||
$forceDisableVideo = false
|
||||
) {
|
||||
$commands = [];
|
||||
foreach ($outs as $label) {
|
||||
$commands[] = '-map';
|
||||
$commands[] = $label;
|
||||
}
|
||||
|
||||
// Apply format params.
|
||||
$commands = array_merge(
|
||||
$commands,
|
||||
$this->applyFormatParams($format, $forceDisableAudio, $forceDisableVideo)
|
||||
);
|
||||
|
||||
// Set output file.
|
||||
$commands[] = $outputFilename;
|
||||
|
||||
// Create a listener.
|
||||
if ($format instanceof ProgressableInterface) {
|
||||
$listener = $format->createProgressListener($this, $this->ffprobe, 1, 1, 0);
|
||||
$this->listeners = array_merge($this->listeners, $listener);
|
||||
}
|
||||
|
||||
$this->mapCommands = array_merge($this->mapCommands, $commands);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply added filters and execute ffmpeg command.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function save(): void
|
||||
{
|
||||
$command = $this->buildCommand();
|
||||
$this->addListener();
|
||||
|
||||
try {
|
||||
$this->driver->command($command, false, $this->listeners);
|
||||
$this->driver->command($command, false, $this->progressListeners);
|
||||
} catch (ExecutionFailureException $e) {
|
||||
throw new RuntimeException('Encoding failed', $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $forceDisableAudio
|
||||
* @param bool $forceDisableVideo
|
||||
*
|
||||
* @return array
|
||||
* @todo Redo it all
|
||||
*/
|
||||
protected function applyFormatParams(
|
||||
FormatInterface $format,
|
||||
$forceDisableAudio = false,
|
||||
$forceDisableVideo = false
|
||||
) {
|
||||
// Set format params.
|
||||
$commands = [];
|
||||
if (!$forceDisableVideo && $format instanceof VideoInterface) {
|
||||
if (null !== $format->getVideoCodec()) {
|
||||
$commands[] = '-vcodec';
|
||||
$commands[] = $format->getVideoCodec();
|
||||
}
|
||||
// If the user passed some additional format parameters.
|
||||
if (null !== $format->getAdditionalParameters()) {
|
||||
$commands = array_merge($commands, $format->getAdditionalParameters());
|
||||
}
|
||||
}
|
||||
if (!$forceDisableAudio && $format instanceof AudioInterface) {
|
||||
if (null !== $format->getAudioCodec()) {
|
||||
$commands[] = '-acodec';
|
||||
$commands[] = $format->getAudioCodec();
|
||||
}
|
||||
if (null !== $format->getAudioKiloBitrate()) {
|
||||
$commands[] = '-b:a';
|
||||
$commands[] = $format->getAudioKiloBitrate().'k';
|
||||
}
|
||||
if (null !== $format->getAudioChannels()) {
|
||||
$commands[] = '-ac';
|
||||
$commands[] = $format->getAudioChannels();
|
||||
}
|
||||
protected function addListener(): void
|
||||
{
|
||||
$self = $this;
|
||||
$listener = new VideoProgressListener($this->ffprobe, $this->getPathfile(), 1, 1, 0);
|
||||
|
||||
$listener->on('progress', function (...$args) use ($self) {
|
||||
$self->emit('progress', array_merge([$self], $args));
|
||||
});
|
||||
|
||||
$this->progressListeners[] = $listener;
|
||||
}
|
||||
|
||||
// If the user passed some extra parameters.
|
||||
if ($format->getExtraParams()) {
|
||||
$commands = array_merge($commands, $format->getExtraParams());
|
||||
}
|
||||
|
||||
return $commands;
|
||||
}
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue