First implemtation of the waveform feature
This commit is contained in:
parent
010b3b8a6d
commit
b7a8be46aa
5 changed files with 300 additions and 0 deletions
20
src/FFMpeg/Filters/Waveform/WaveformFilterInterface.php
Normal file
20
src/FFMpeg/Filters/Waveform/WaveformFilterInterface.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of PHP-FFmpeg.
|
||||
*
|
||||
* (c) Alchemy <dev.team@alchemy.fr>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FFMpeg\Filters\Waveform;
|
||||
|
||||
use FFMpeg\Filters\FilterInterface;
|
||||
use FFMpeg\Media\Waveform;
|
||||
|
||||
interface WaveformFilterInterface extends FilterInterface
|
||||
{
|
||||
public function apply(Waveform $waveform);
|
||||
}
|
||||
39
src/FFMpeg/Filters/Waveform/WaveformFilters.php
Normal file
39
src/FFMpeg/Filters/Waveform/WaveformFilters.php
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of PHP-FFmpeg.
|
||||
*
|
||||
* (c) Alchemy <dev.team@alchemy.fr>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FFMpeg\Filters\Waveform;
|
||||
|
||||
use FFMpeg\Media\Waveform;
|
||||
|
||||
class WaveformFilters
|
||||
{
|
||||
private $waveform;
|
||||
|
||||
public function __construct(Waveform $waveform)
|
||||
{
|
||||
$this->waveform = $waveform;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fixes the display ratio of the output frame.
|
||||
*
|
||||
* In case the sample ratio and display ratio are different, image may be
|
||||
* anamorphozed. This filter fixes this by specifying the output size.
|
||||
*
|
||||
* @return FrameFilters
|
||||
*/
|
||||
public function fixDisplayRatio()
|
||||
{
|
||||
$this->frame->addFilter(new DisplayRatioFixerFilter());
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
69
src/FFMpeg/Filters/Waveform/WaveformRatioFixerFilter.php
Normal file
69
src/FFMpeg/Filters/Waveform/WaveformRatioFixerFilter.php
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of PHP-FFmpeg.
|
||||
*
|
||||
* (c) Alchemy <dev.team@alchemy.fr>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FFMpeg\Filters\Waveform;
|
||||
|
||||
use FFMpeg\Exception\RuntimeException;
|
||||
use FFMpeg\Media\Waveform;
|
||||
|
||||
class WaveformRatioFixerFilter implements WaveformFilterInterface
|
||||
{
|
||||
/** @var boolean */
|
||||
private $downmix;
|
||||
|
||||
// By default, the downmix value is set to FALSE.
|
||||
public function __construct($downmix = FALSE)
|
||||
{
|
||||
$this->downmix = $downmix;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDownmix()
|
||||
{
|
||||
return $this->downmix;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function apply(Waveform $waveform)
|
||||
{
|
||||
$dimensions = null;
|
||||
$commands = array();
|
||||
|
||||
foreach ($waveform->getVideo()->getStreams() as $stream) {
|
||||
if ($stream->isVideo()) {
|
||||
try {
|
||||
|
||||
// Get the dimensions of the video
|
||||
$dimensions = $stream->getDimensions();
|
||||
|
||||
// If the downmix parameter is set to TRUE, we add an option to the FFMPEG command
|
||||
if(!$this->downmix) {
|
||||
$commands[] = '"showwavespic=s=' . $dimensions->getWidth() . 'x' . $dimensions->getHeight().'"';
|
||||
}
|
||||
else {
|
||||
$commands[] = '"aformat=channel_layouts=mono,showwavespic=s=' . $dimensions->getWidth() . 'x' . $dimensions->getHeight().'"';
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
} catch (RuntimeException $e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $commands;
|
||||
}
|
||||
}
|
||||
101
src/FFMpeg/Media/Waveform.php
Normal file
101
src/FFMpeg/Media/Waveform.php
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of PHP-FFmpeg.
|
||||
*
|
||||
* (c) Alchemy <info@alchemy.fr>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace FFMpeg\Media;
|
||||
|
||||
use Alchemy\BinaryDriver\Exception\ExecutionFailureException;
|
||||
use FFMpeg\Filters\Waveform\WaveformFilterInterface;
|
||||
use FFMpeg\Filters\Waveform\WaveformFilters;
|
||||
use FFMpeg\Driver\FFMpegDriver;
|
||||
use FFMpeg\FFProbe;
|
||||
use FFMpeg\Exception\RuntimeException;
|
||||
use FFMpeg\Coordinate\TimeCode;
|
||||
|
||||
class Waveform extends AbstractMediaType
|
||||
{
|
||||
/** @var Video */
|
||||
private $video;
|
||||
|
||||
public function __construct(Video $video, FFMpegDriver $driver, FFProbe $ffprobe)
|
||||
{
|
||||
parent::__construct($video->getPathfile(), $driver, $ffprobe);
|
||||
$this->video = $video;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the video related to the waveform.
|
||||
*
|
||||
* @return Video
|
||||
*/
|
||||
public function getVideo()
|
||||
{
|
||||
return $this->video;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return WaveformFilters
|
||||
*/
|
||||
public function filters()
|
||||
{
|
||||
return new WaveformFilters($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return Waveform
|
||||
*/
|
||||
public function addFilter(WaveformFilterInterface $filter)
|
||||
{
|
||||
$this->filters->add($filter);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the waveform in the given filename.
|
||||
*
|
||||
* @param string $pathfile
|
||||
*
|
||||
* @return Waveform
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function save($pathfile)
|
||||
{
|
||||
/**
|
||||
* might be optimized with http://ffmpeg.org/trac/ffmpeg/wiki/Seeking%20with%20FFmpeg
|
||||
* @see http://ffmpeg.org/ffmpeg.html#Main-options
|
||||
*/
|
||||
$commands = array(
|
||||
'-i', 'input', '-filter_complex',
|
||||
'-frames:v', '1',
|
||||
$this->pathfile
|
||||
);
|
||||
|
||||
foreach ($this->filters as $filter) {
|
||||
$commands = array_merge($commands, $filter->apply($this));
|
||||
}
|
||||
|
||||
$commands = array_merge($commands, array($pathfile));
|
||||
|
||||
try {
|
||||
$this->driver->command($commands);
|
||||
} catch (ExecutionFailureException $e) {
|
||||
$this->cleanupTemporaryFile($pathfile);
|
||||
throw new RuntimeException('Unable to save waveform', $e->getCode(), $e);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue