2012-04-13 10:20:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
2012-04-13 14:34:53 +02:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-04-13 10:20:54 +02:00
|
|
|
namespace FFMpeg\Format;
|
|
|
|
|
|
2012-05-25 16:21:16 +02:00
|
|
|
use FFMpeg\Exception\InvalidArgumentException;
|
|
|
|
|
|
2012-04-13 15:42:34 +02:00
|
|
|
/**
|
|
|
|
|
* The abstract default Video format
|
|
|
|
|
*
|
|
|
|
|
* @author Romain Neutron imprec@gmail.com
|
|
|
|
|
*/
|
2012-04-13 12:45:41 +02:00
|
|
|
abstract class DefaultVideoFormat extends DefaultAudioFormat implements VideoFormat
|
2012-04-13 10:20:54 +02:00
|
|
|
{
|
|
|
|
|
protected $width;
|
|
|
|
|
protected $height;
|
2012-04-27 00:48:57 +02:00
|
|
|
protected $frameRate = 25;
|
2012-04-13 10:20:54 +02:00
|
|
|
protected $videoCodec;
|
2012-04-27 00:48:57 +02:00
|
|
|
protected $GOPsize = 25;
|
2012-04-13 14:15:56 +02:00
|
|
|
protected $kiloBitrate = 1000;
|
2012-04-13 10:20:54 +02:00
|
|
|
|
2012-05-25 16:21:16 +02:00
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
*
|
|
|
|
|
* @param integer $width
|
|
|
|
|
* @param integer $height The height of the video format
|
|
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
public function __construct($width, $height)
|
|
|
|
|
{
|
|
|
|
|
$this->setDimensions($width, $height);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:42:34 +02:00
|
|
|
/**
|
2012-05-25 16:21:16 +02:00
|
|
|
* {@inheritdoc}
|
2012-04-13 15:42:34 +02:00
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
public function getWidth()
|
|
|
|
|
{
|
|
|
|
|
return $this->width;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:42:34 +02:00
|
|
|
/**
|
2012-05-25 16:21:16 +02:00
|
|
|
* {@inheritdoc}
|
2012-04-13 15:42:34 +02:00
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
public function getHeight()
|
|
|
|
|
{
|
|
|
|
|
return $this->height;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:42:34 +02:00
|
|
|
/**
|
|
|
|
|
* Set the dimensions
|
|
|
|
|
*
|
2012-05-25 15:44:43 +02:00
|
|
|
* @param integer $width The heigth
|
|
|
|
|
* @param integer $height The width
|
2012-04-13 15:42:34 +02:00
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
public function setDimensions($width, $height)
|
|
|
|
|
{
|
2012-04-27 00:48:57 +02:00
|
|
|
if ($width < 1) {
|
2012-05-25 16:21:16 +02:00
|
|
|
throw new InvalidArgumentException('Wrong width value');
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
2012-04-27 00:48:57 +02:00
|
|
|
if ($height < 1) {
|
2012-05-25 16:21:16 +02:00
|
|
|
throw new InvalidArgumentException('Wrong height value');
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->width = $this->getMultiple($width, 16);
|
|
|
|
|
$this->height = $this->getMultiple($height, 16);
|
2012-05-25 16:21:16 +02:00
|
|
|
|
|
|
|
|
return $this;
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:42:34 +02:00
|
|
|
/**
|
2012-05-25 16:21:16 +02:00
|
|
|
* {@inheritdoc}
|
2012-04-13 15:42:34 +02:00
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
public function getFrameRate()
|
|
|
|
|
{
|
|
|
|
|
return $this->frameRate;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:42:34 +02:00
|
|
|
/**
|
|
|
|
|
* Set the framerate
|
|
|
|
|
*
|
2012-05-25 15:44:43 +02:00
|
|
|
* @param integer $frameRate
|
2012-05-25 16:21:16 +02:00
|
|
|
*
|
2012-04-13 15:42:34 +02:00
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
public function setFrameRate($frameRate)
|
|
|
|
|
{
|
2012-04-27 00:48:57 +02:00
|
|
|
if ($frameRate < 1) {
|
2012-05-25 16:21:16 +02:00
|
|
|
throw new InvalidArgumentException('Wrong framerate value');
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->frameRate = (int) $frameRate;
|
2012-05-25 16:21:16 +02:00
|
|
|
|
|
|
|
|
return $this;
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:42:34 +02:00
|
|
|
/**
|
2012-05-25 16:21:16 +02:00
|
|
|
* {@inheritdoc}
|
2012-04-13 15:42:34 +02:00
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
public function getVideoCodec()
|
|
|
|
|
{
|
|
|
|
|
return $this->videoCodec;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:42:34 +02:00
|
|
|
/**
|
|
|
|
|
* Set the video codec, Should be in the available ones, otherwise an
|
|
|
|
|
* exception is thrown
|
|
|
|
|
*
|
2012-05-25 15:44:43 +02:00
|
|
|
* @param string $videoCodec
|
2012-04-13 15:42:34 +02:00
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
public function setVideoCodec($videoCodec)
|
|
|
|
|
{
|
2012-04-27 00:48:57 +02:00
|
|
|
if ( ! in_array($videoCodec, $this->getAvailableVideoCodecs())) {
|
2012-05-25 16:21:16 +02:00
|
|
|
throw new InvalidArgumentException(sprintf(
|
2012-04-27 00:48:57 +02:00
|
|
|
'Wrong videocodec value for %s, available formats are %s'
|
|
|
|
|
, $videoCodec, implode(', ', $this->getAvailableVideoCodecs())
|
|
|
|
|
));
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->videoCodec = $videoCodec;
|
2012-05-25 16:21:16 +02:00
|
|
|
|
|
|
|
|
return $this;
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:42:34 +02:00
|
|
|
/**
|
2012-05-25 16:21:16 +02:00
|
|
|
* {@inheritdoc}
|
2012-04-13 15:42:34 +02:00
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
public function getGOPsize()
|
|
|
|
|
{
|
|
|
|
|
return $this->GOPsize;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:42:34 +02:00
|
|
|
/**
|
|
|
|
|
* Set the GOP size
|
|
|
|
|
*
|
2012-05-25 15:44:43 +02:00
|
|
|
* @param integer $GOPsize
|
2012-05-25 16:21:16 +02:00
|
|
|
*
|
2012-04-13 15:42:34 +02:00
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
public function setGOPsize($GOPsize)
|
|
|
|
|
{
|
2012-04-27 00:48:57 +02:00
|
|
|
if ($GOPsize < 1) {
|
2012-05-25 16:21:16 +02:00
|
|
|
throw new InvalidArgumentException('Wrong GOP size value');
|
2012-04-13 12:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-13 10:20:54 +02:00
|
|
|
$this->GOPsize = (int) $GOPsize;
|
2012-05-25 16:21:16 +02:00
|
|
|
|
|
|
|
|
return $this;
|
2012-04-13 10:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-13 15:42:34 +02:00
|
|
|
/**
|
|
|
|
|
* Returns the nearest multiple for a value
|
|
|
|
|
*
|
2012-05-25 15:44:43 +02:00
|
|
|
* @param integer $value
|
|
|
|
|
* @param integer $multiple
|
2012-05-25 16:21:16 +02:00
|
|
|
* @return integer
|
2012-04-13 15:42:34 +02:00
|
|
|
*/
|
2012-04-13 10:20:54 +02:00
|
|
|
protected function getMultiple($value, $multiple)
|
|
|
|
|
{
|
|
|
|
|
$modulo = $value % $multiple;
|
|
|
|
|
|
|
|
|
|
$ret = (int) $multiple;
|
|
|
|
|
|
|
|
|
|
$halfDistance = $multiple / 2;
|
|
|
|
|
if ($modulo <= $halfDistance)
|
2012-04-27 00:48:57 +02:00
|
|
|
$bound = 'bottom';
|
2012-04-13 10:20:54 +02:00
|
|
|
else
|
2012-04-27 00:48:57 +02:00
|
|
|
$bound = 'top';
|
2012-04-13 10:20:54 +02:00
|
|
|
|
2012-04-27 00:48:57 +02:00
|
|
|
switch ($bound) {
|
2012-04-13 10:20:54 +02:00
|
|
|
default:
|
|
|
|
|
case 'top':
|
|
|
|
|
$ret = $value + $multiple - $modulo;
|
|
|
|
|
break;
|
|
|
|
|
case 'bottom':
|
|
|
|
|
$ret = $value - $modulo;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-27 00:48:57 +02:00
|
|
|
if ($ret < $multiple) {
|
2012-04-13 10:20:54 +02:00
|
|
|
$ret = (int) $multiple;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (int) $ret;
|
|
|
|
|
}
|
2012-04-13 15:42:34 +02:00
|
|
|
}
|