Merge branch '0.3'

Conflicts:
	CHANGELOG.md
This commit is contained in:
Romain Neutron 2013-09-05 11:34:29 +02:00
commit af6f45f62d
16 changed files with 316 additions and 26 deletions

View file

@ -3,6 +3,11 @@ CHANGELOG
* 0.4.0 (xx-xx-xxxx)
* 0.3.3 (xx-xx-2013)
* Add convenient Stream::getDimensions method to extract video dimension.
* Add DisplayRatioFixer Frame filter.
* 0.3.2 (08-08-2013)
* Fix A/V synchronization over flash and HTML5 players.

View file

@ -0,0 +1,16 @@
<?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\Exception;
class LogicException extends \LogicException implements ExceptionInterface
{
}

View file

@ -11,6 +11,10 @@
namespace FFMpeg\FFProbe\DataMapping;
use FFMpeg\Exception\LogicException;
use FFMpeg\Exception\RuntimeException;
use FFMpeg\Coordinate\Dimension;
class Stream extends AbstractData
{
/**
@ -32,4 +36,63 @@ class Stream extends AbstractData
{
return $this->has('codec_type') ? 'video' === $this->get('codec_type') : false;
}
/**
* Returns the dimension of the video stream.
*
* @return Dimension
*
* @throws LogicException In case the stream is not a video stream.
* @throws RuntimeException In case the dimensions can not be extracted.
*/
public function getDimensions()
{
if (!$this->isVideo()) {
throw new LogicException('Dimensions can only be retrieved from video streams.');
}
$width = $height = $sampleRatio = $displayRatio = null;
if ($this->has('width')) {
$width = $this->get('width');
}
if ($this->has('height')) {
$height = $this->get('height');
}
if (null !== $ratio = $this->extractRatio($this, 'sample_aspect_ratio')) {
$sampleRatio = $ratio;
}
if (null !== $ratio = $this->extractRatio($this, 'display_aspect_ratio')) {
$displayRatio = $ratio;
}
if (null === $height || null === $width) {
throw new RuntimeException('Unable to extract dimensions.');
}
if (null !== $displayRatio && null !== $sampleRatio) {
$width = round($width / $sampleRatio[0] * $sampleRatio[1] * $displayRatio[0] / $displayRatio[1]);
}
return new Dimension($width, $height);
}
/**
* Extracts a ratio from a string in a \d+:\d+ format given a key name.
*
* @param Stream $stream The stream where to look for the ratio.
* @param string $name the name of the key.
* @return null|array An array containing the width and the height, null if not found.
*/
private function extractRatio(Stream $stream, $name)
{
if ($stream->has($name)) {
$ratio = $stream->get($name);
if (preg_match('/\d+:\d+/', $ratio)) {
return array_map(function ($int) { return (int) $int; }, explode(':', $ratio));
}
}
return null;
}
}

View file

@ -47,9 +47,13 @@ class FiltersCollection implements \Countable, \IteratorAggregate
public function getIterator()
{
if (null === $this->sorted) {
if (0 === count($this->filters)) {
$this->sorted = $this->filters;
} else {
krsort($this->filters);
$this->sorted = call_user_func_array('array_merge', $this->filters);
}
}
return new \ArrayIterator($this->sorted);
}

View file

@ -0,0 +1,58 @@
<?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\Frame;
use FFMpeg\Exception\RuntimeException;
use FFMpeg\Media\Frame;
class DisplayRatioFixerFilter implements FrameFilterInterface
{
/** @var integer */
private $priority;
public function __construct($priority = 0)
{
$this->priority = $priority;
}
/**
* {@inheritdoc}
*/
public function getPriority()
{
return $this->priority;
}
/**
* {@inheritdoc}
*/
public function apply(Frame $frame)
{
$dimensions = null;
$commands = array();
foreach ($frame->getVideo()->getStreams() as $stream) {
if ($stream->isVideo()) {
try {
$dimensions = $stream->getDimensions();
$commands[] = '-s';
$commands[] = $dimensions->getWidth() . 'x' . $dimensions->getHeight();
break;
} catch (RuntimeException $e) {
}
}
}
return $commands;
}
}

View file

@ -13,9 +13,8 @@ namespace FFMpeg\Filters\Frame;
use FFMpeg\Filters\FilterInterface;
use FFMpeg\Media\Frame;
use FFMpeg\Format\FrameInterface;
interface FrameFilterInterface extends FilterInterface
{
public function apply(Frame $frame, FrameInterface $format);
public function apply(Frame $frame);
}

View file

@ -21,4 +21,19 @@ class FrameFilters
{
$this->frame = $frame;
}
/**
* 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;
}
}

View file

@ -12,6 +12,7 @@
namespace FFMpeg\Filters\Video;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Exception\RuntimeException;
use FFMpeg\Media\Video;
use FFMpeg\Format\VideoInterface;
@ -80,23 +81,22 @@ class ResizeFilter implements VideoFilterInterface
*/
public function apply(Video $video, VideoInterface $format)
{
$originalWidth = $originalHeight = null;
$dimensions = null;
$commands = array();
foreach ($video->getStreams() as $stream) {
if ($stream->isVideo()) {
if ($stream->has('width')) {
$originalWidth = $stream->get('width');
}
if ($stream->has('height')) {
$originalHeight = $stream->get('height');
try {
$dimensions = $stream->getDimensions();
break;
} catch (RuntimeException $e) {
}
}
}
$commands = array();
if ($originalHeight !== null && $originalWidth !== null) {
$dimensions = $this->getComputedDimensions(new Dimension($originalWidth, $originalHeight), $format->getModulus());
if (null !== $dimensions) {
$dimensions = $this->getComputedDimensions($dimensions, $format->getModulus());
$commands[] = '-s';
$commands[] = $dimensions->getWidth() . 'x' . $dimensions->getHeight();

View file

@ -18,16 +18,30 @@ use FFMpeg\Driver\FFMpegDriver;
use FFMpeg\FFProbe;
use FFMpeg\Exception\RuntimeException;
use FFMpeg\Coordinate\TimeCode;
use FFMpeg\Media\Video;
class Frame extends AbstractMediaType
{
/** @var TimeCode */
private $timecode;
/** @var Video */
private $video;
public function __construct($pathfile, FFMpegDriver $driver, FFProbe $ffprobe, TimeCode $timecode)
public function __construct(Video $video, FFMpegDriver $driver, FFProbe $ffprobe, TimeCode $timecode)
{
parent::__construct($pathfile, $driver, $ffprobe);
parent::__construct($video->getPathfile(), $driver, $ffprobe);
$this->timecode = $timecode;
$this->video = $video;
}
/**
* Returns the video related to the frame.
*
* @return Video
*/
public function getVideo()
{
return $this->video;
}
/**
@ -83,16 +97,22 @@ class Frame extends AbstractMediaType
'-y', '-ss', (string) $this->timecode,
'-i', $this->pathfile,
'-vframes', '1',
'-f', 'image2', $pathfile
'-f', 'image2'
);
} else {
$commands = array(
'-y', '-i', $this->pathfile,
'-vframes', '1', '-ss', (string) $this->timecode,
'-f', 'image2', $pathfile
'-f', 'image2'
);
}
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) {

View file

@ -159,6 +159,6 @@ class Video extends Audio
*/
public function frame(TimeCode $at)
{
return new Frame($this->pathfile, $this->driver, $this->ffprobe, $at);
return new Frame($this, $this->driver, $this->ffprobe, $at);
}
}

View file

@ -2,6 +2,7 @@
namespace FFMpeg\Tests\FFProbe\DataMapping;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Tests\TestCase;
use FFMpeg\FFProbe\DataMapping\Stream;
@ -40,4 +41,45 @@ class StreamTest extends TestCase
array(false, array('codec_type' => 'audio')),
);
}
/**
* @expectedException FFMpeg\Exception\LogicException
* @expectedExceptionMessage Dimensions can only be retrieved from video streams.
*/
public function testGetDimensionsFromAudio()
{
$stream = new Stream(array('codec_type' => 'audio'));
$stream->getDimensions();
}
public function testGetDimensionsFromVideo()
{
$stream = new Stream(array('codec_type' => 'video', 'width' => 960, 'height' => 720));
$this->assertEquals(new Dimension(960, 720), $stream->getDimensions());
}
/**
* @dataProvider provideInvalidPropertiesForDimensionsExtraction
* @expectedException FFMpeg\Exception\RuntimeException
* @expectedExceptionMessage Unable to extract dimensions.
*/
public function testUnableToGetDimensionsFromVideo($properties)
{
$stream = new Stream(array('codec_type' => 'video', 'width' => 960));
$stream->getDimensions();
}
public function provideInvalidPropertiesForDimensionsExtraction()
{
return array(
array('codec_type' => 'video', 'width' => 960),
array('codec_type' => 'video', 'height' => 960),
);
}
public function testGetDimensionsFromVideoWithDisplayRatio()
{
$stream = new Stream(array('codec_type' => 'video', 'width' => 960, 'height' => 720, 'sample_aspect_ratio' => '4:3', 'display_aspect_ratio' => '16:9'));
$this->assertEquals(new Dimension(1280, 720), $stream->getDimensions());
}
}

View file

@ -30,6 +30,12 @@ class FiltersCollectionTest extends TestCase
$this->assertCount(2, $coll->getIterator());
}
public function testEmptyIterator()
{
$coll = new FiltersCollection();
$this->assertInstanceOf('\ArrayIterator', $coll->getIterator());
}
public function testIteratorSort()
{
$coll = new FiltersCollection();

View file

@ -0,0 +1,28 @@
<?php
namespace FFMpeg\Tests\Filters\Frame;
use FFMpeg\Tests\TestCase;
use FFMpeg\Filters\Frame\DisplayRatioFixerFilter;
use FFMpeg\Media\Frame;
use FFMpeg\Coordinate\TimeCode;
use FFMpeg\FFProbe\DataMapping\StreamCollection;
use FFMpeg\FFProbe\DataMapping\Stream;
class DisplayRatioFixerFilterTest extends TestCase
{
public function testApply()
{
$stream = new Stream(array('codec_type' => 'video', 'width' => 960, 'height' => 720));
$streams = new StreamCollection(array($stream));
$video = $this->getVideoMock(__FILE__);
$video->expects($this->once())
->method('getStreams')
->will($this->returnValue($streams));
$frame = new Frame($video, $this->getFFMpegDriverMock(), $this->getFFProbeMock(), new TimeCode(0, 0, 0, 0));
$filter = new DisplayRatioFixerFilter();
$this->assertEquals(array('-s', '960x720'), $filter->apply($frame));
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace FFMpeg\Tests\Filters\Frame;
use FFMpeg\Tests\TestCase;
use FFMpeg\Filters\Frame\FrameFilters;
class FrameFiltersTest extends TestCase
{
public function testResize()
{
$frame = $this->getFrameMock();
$filters = new FrameFilters($frame);
$frame->expects($this->once())
->method('addFilter')
->with($this->isInstanceOf('FFMpeg\Filters\Frame\DisplayRatioFixerFilter'));
$filters->fixDisplayRatio();
}
}

View file

@ -11,7 +11,7 @@ class FrameTest extends AbstractMediaTestCase
*/
public function testWithInvalidFile()
{
new Frame('/No/file', $this->getFFMpegDriverMock(), $this->getFFProbeMock(), $this->getTimeCodeMock());
new Frame($this->getVideoMock('/No/file'), $this->getFFMpegDriverMock(), $this->getFFProbeMock(), $this->getTimeCodeMock());
}
public function testGetTimeCode()
@ -20,7 +20,7 @@ class FrameTest extends AbstractMediaTestCase
$ffprobe = $this->getFFProbeMock();
$timecode = $this->getTimeCodeMock();
$frame = new Frame(__FILE__, $driver, $ffprobe, $timecode);
$frame = new Frame($this->getVideoMock(__FILE__), $driver, $ffprobe, $timecode);
$this->assertSame($timecode, $frame->getTimeCode());
}
@ -30,7 +30,7 @@ class FrameTest extends AbstractMediaTestCase
$ffprobe = $this->getFFProbeMock();
$timecode = $this->getTimeCodeMock();
$frame = new Frame(__FILE__, $driver, $ffprobe, $timecode);
$frame = new Frame($this->getVideoMock(__FILE__), $driver, $ffprobe, $timecode);
$this->assertInstanceOf('FFMpeg\Filters\Frame\FrameFilters', $frame->filters());
}
@ -50,7 +50,7 @@ class FrameTest extends AbstractMediaTestCase
->method('add')
->with($filter);
$frame = new Frame(__FILE__, $driver, $ffprobe, $timecode);
$frame = new Frame($this->getVideoMock(__FILE__), $driver, $ffprobe, $timecode);
$frame->setFiltersCollection($filters);
$frame->addFilter($filter);
}
@ -75,7 +75,7 @@ class FrameTest extends AbstractMediaTestCase
->method('command')
->with($commands);
$frame = new Frame(__FILE__, $driver, $ffprobe, $timecode);
$frame = new Frame($this->getVideoMock(__FILE__), $driver, $ffprobe, $timecode);
$this->assertSame($frame, $frame->save($pathfile, $accurate));
}

View file

@ -40,6 +40,13 @@ class TestCase extends \PHPUnit_Framework_TestCase
->getMock();
}
public function getFrameMock()
{
return $this->getMockBuilder('FFMpeg\Media\Frame')
->disableOriginalConstructor()
->getMock();
}
public function getFFMpegDriverMock()
{
return $this->getMockBuilder('FFMpeg\Driver\FFMpegDriver')
@ -122,10 +129,16 @@ class TestCase extends \PHPUnit_Framework_TestCase
->getMock();
}
protected function getVideoMock()
protected function getVideoMock($filename = null)
{
return $this->getMockBuilder('FFMpeg\Media\Video')
$video = $this->getMockBuilder('FFMpeg\Media\Video')
->disableOriginalConstructor()
->getMock();
$video->expects($this->any())
->method('getPathfile')
->will($this->returnValue($filename));
return $video;
}
}