ffmpeg-mappable-media/src/FFMpeg/Format/Video/DefaultVideo.php

226 lines
5.4 KiB
PHP
Raw Normal View History

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-05-30 12:22:22 +02:00
namespace FFMpeg\Format\Video;
2012-04-13 10:20:54 +02:00
2012-05-30 12:22:22 +02:00
use FFMpeg\Format\Audio\DefaultAudio;
2012-05-30 15:06:53 +02:00
use FFMpeg\Format\Dimension;
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-05-30 15:06:53 +02:00
abstract class DefaultVideo extends DefaultAudio implements Interactive, Resamplable, Resizable
2012-04-13 10:20:54 +02:00
{
2012-05-30 12:22:22 +02:00
const RESIZEMODE_FIT = 'fit';
const RESIZEMODE_INSET = 'inset';
2012-04-13 10:20:54 +02:00
protected $width;
protected $height;
2012-04-27 00:48:57 +02:00
protected $frameRate = 25;
protected $resizeMode = self::RESIZEMODE_FIT;
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-04-13 15:42:34 +02:00
/**
2012-05-30 15:06:53 +02:00
* Returns the width setting.
* The return of this method should not depend on a media file size
*
* @return integer
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-30 15:06:53 +02:00
* Returns the height setting
* The return of this method should not depend on a media file size
*
* @return integer
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 = $width;
$this->height = $height;
2012-05-25 16:21:16 +02:00
return $this;
2012-04-13 10:20:54 +02:00
}
2012-05-30 12:22:22 +02:00
/**
* {@inheritdoc)
*/
public function getComputedDimensions($originalWidth, $originalHeight)
{
switch ($this->getResizeMode()) {
case self::RESIZEMODE_INSET:
$originalRatio = $originalWidth / $originalHeight;
$targetRatio = $this->width / $this->height;
if ($targetRatio > $originalRatio) {
$height = $this->height;
$width = round($originalRatio * $this->height);
} else {
$width = $this->width;
$height = round($this->width / $originalRatio);
}
break;
case self::RESIZEMODE_FIT:
default:
if (null !== $this->width && null !== $this->height) {
$width = $this->width;
$height = $this->height;
} else {
$width = $originalWidth;
$height = $originalHeight;
}
2012-05-30 12:22:22 +02:00
break;
}
2012-05-30 15:06:53 +02:00
return new Dimension($width, $height);
2012-05-30 12:22:22 +02:00
}
/**
* Set the resize mode
2012-05-30 12:22:22 +02:00
*
* @param string $mode The mode, one of the self::RESIZEMODE_* constants
*
* @throws InvalidArgumentException
*/
public function setResizeMode($mode)
{
if ( ! in_array($mode, array(self::RESIZEMODE_FIT, self::RESIZEMODE_INSET))) {
throw new InvalidArgumentException(
'Resize mode `%s` is not valid , avalaible values are %s',
$mode,
implode(', ', array(self::RESIZEMODE_FIT, self::RESIZEMODE_INSET))
);
}
$this->resizeMode = $mode;
return $this;
}
/**
2012-05-30 15:06:53 +02:00
* Get the current resize mode name
*
* @return string
*/
public function getResizeMode()
{
return $this->resizeMode;
}
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 16:22:16 +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 16:22:16 +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
}