Add write support for iTunes/iPod and ID3v2 extensions for ISO 14496
git-svn-id: http://php-reader.googlecode.com/svn/trunk@92 51a70ab9-7547-0410-9469-37e369ee0574
This commit is contained in:
@@ -58,64 +58,164 @@ class ISO14496_Box
|
||||
*/
|
||||
protected $_reader;
|
||||
|
||||
/**
|
||||
* The file offset to box start.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $_offset;
|
||||
/** @var Array */
|
||||
private $_options;
|
||||
|
||||
/**
|
||||
* The object size in bytes, including the size and type header, fields, and
|
||||
* all contained boxes.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $_size;
|
||||
/** @var integer */
|
||||
private $_offset = -1;
|
||||
|
||||
/** @var integer */
|
||||
private $_size = -1;
|
||||
|
||||
/** @var string */
|
||||
protected $_type;
|
||||
private $_type;
|
||||
|
||||
|
||||
/** @var ISO14496_Box */
|
||||
private $_parent = null;
|
||||
|
||||
|
||||
/** @var boolean */
|
||||
protected $_container = false;
|
||||
|
||||
/**
|
||||
* An array of boxes the box contains.
|
||||
*
|
||||
* @var Array
|
||||
*/
|
||||
protected $_boxes = array();
|
||||
private $_container = false;
|
||||
|
||||
/** @var Array */
|
||||
private $_boxes = array();
|
||||
|
||||
/** @var Array */
|
||||
private static $_path = array();
|
||||
|
||||
/**
|
||||
* Constructs the class with given parameters.
|
||||
*
|
||||
* @param Reader $reader The reader object.
|
||||
*/
|
||||
public function __construct($reader)
|
||||
public function __construct($reader, &$options = array())
|
||||
{
|
||||
$this->_reader = $reader;
|
||||
$this->_offset = $this->_reader->getOffset();
|
||||
$this->_size = $this->_reader->readUInt32BE();
|
||||
$this->_type = $this->_reader->read(4);
|
||||
if (($this->_reader = $reader) === null) {
|
||||
$this->_type = strtolower(substr(get_class($this), -4));
|
||||
} else {
|
||||
$this->_offset = $this->_reader->getOffset();
|
||||
$this->_size = $this->_reader->readUInt32BE();
|
||||
$this->_type = $this->_reader->read(4);
|
||||
|
||||
if ($this->_size == 1)
|
||||
$this->_size = $this->_reader->readInt64BE();
|
||||
if ($this->_size == 0)
|
||||
$this->_size = $this->_reader->getSize() - $offset;
|
||||
if ($this->_size == 1)
|
||||
$this->_size = $this->_reader->readInt64BE();
|
||||
if ($this->_size == 0)
|
||||
$this->_size = $this->_reader->getSize() - $this->_offset;
|
||||
|
||||
if ($this->_type == "uuid")
|
||||
$this->_type = $this->_reader->readGUID();
|
||||
if ($this->_type == "uuid")
|
||||
$this->_type = $this->_reader->readGUID();
|
||||
}
|
||||
$this->_options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of the ISO base media file object.
|
||||
* Returns the options array.
|
||||
*
|
||||
* @return Array
|
||||
*/
|
||||
public function getOptions() { return $this->_options; }
|
||||
|
||||
/**
|
||||
* Returns the given option value, or the default value if the option is not
|
||||
* defined.
|
||||
*
|
||||
* @param string $option The name of the option.
|
||||
* @param mixed $defaultValue The default value to be returned.
|
||||
*/
|
||||
public function getOption($option, $defaultValue = false)
|
||||
{
|
||||
if (isset($this->_options[$option]))
|
||||
return $this->_options[$option];
|
||||
return $defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the options array. See {@link ISO14496} class for available options.
|
||||
*
|
||||
* @param Array $options The options array.
|
||||
*/
|
||||
public function setOptions(&$options) { $this->_options = $options; }
|
||||
|
||||
/**
|
||||
* Sets the given option the given value.
|
||||
*
|
||||
* @param string $option The name of the option.
|
||||
* @param mixed $value The value to set for the option.
|
||||
*/
|
||||
public function setOption($option, $value)
|
||||
{
|
||||
$this->_options[$option] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file offset to box start, or -1 if the box was created on heap.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getOffset() { return $this->_offset; }
|
||||
|
||||
/**
|
||||
* Sets the file offset where the box starts.
|
||||
*
|
||||
* @param integer $offset The file offset to box start.
|
||||
*/
|
||||
public function setOffset($offset) { $this->_offset = $offset; }
|
||||
|
||||
/**
|
||||
* Returns the box size in bytes, including the size and type header,
|
||||
* fields, and all contained boxes, or -1 if the box was created on heap.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getSize() { return $this->_size; }
|
||||
|
||||
/**
|
||||
* Sets the box size. The size must include the size and type header,
|
||||
* fields, and all contained boxes.
|
||||
*
|
||||
* The method will propagate size change to box parents.
|
||||
*
|
||||
* @todo Size does not propagate to parent
|
||||
* @param integer $size The box size.
|
||||
*/
|
||||
public function setSize($size)
|
||||
{
|
||||
if ($this->_parent !== null)
|
||||
$this->_parent->setSize
|
||||
(($this->_parent->getSize() > 0 ? $this->_parent->getSize() : 0) +
|
||||
$size - ($this->_size > 0 ? $this->_size : 0));
|
||||
$this->_size = $size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the box type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType() { return $this->_type; }
|
||||
|
||||
/**
|
||||
* Sets the box type.
|
||||
*
|
||||
* @param string $type The box type.
|
||||
*/
|
||||
public function setType($type) { $this->_type = $type; }
|
||||
|
||||
/**
|
||||
* Returns the parent box containing this box.
|
||||
*
|
||||
* @return ISO14496_Box
|
||||
*/
|
||||
public function getParent() { return $this->_parent; }
|
||||
|
||||
/**
|
||||
* Sets the parent containing box.
|
||||
*
|
||||
* @param ISO14496_Box $parent The parent box.
|
||||
*/
|
||||
public function setParent(&$parent) { $this->_parent = $parent; }
|
||||
|
||||
/**
|
||||
* Returns a boolean value corresponding to whether the box is a container.
|
||||
*
|
||||
@@ -142,35 +242,57 @@ class ISO14496_Box
|
||||
|
||||
/**
|
||||
* Reads and constructs the boxes found within this box.
|
||||
*
|
||||
* @todo Does not parse iTunes internal ---- boxes.
|
||||
*/
|
||||
protected function constructBoxes($defaultclassname = "ISO14496_Box")
|
||||
{
|
||||
$base = $this->getOption("base", "");
|
||||
if ($this->getType() != "file")
|
||||
self::$_path[] = $this->getType();
|
||||
$path = implode(self::$_path, ".");
|
||||
|
||||
while (true) {
|
||||
$offset = $this->_reader->getOffset();
|
||||
if ($offset >= $this->_offset + $this->_size)
|
||||
break;
|
||||
$size = $this->_reader->readUInt32BE();
|
||||
$type = $this->_reader->read(4);
|
||||
$type = rtrim($this->_reader->read(4), " ");
|
||||
if ($size == 1)
|
||||
$size = $this->_reader->readInt64BE();
|
||||
if ($size == 0)
|
||||
$size = $this->_reader->getSize() - $offset;
|
||||
$this->_reader->setOffset($offset);
|
||||
|
||||
if (@fopen($filename = "ISO14496/Box/" . strtoupper($type) . ".php",
|
||||
"r", true) !== false)
|
||||
require_once($filename);
|
||||
if (class_exists($classname = "ISO14496_Box_" . strtoupper($type)))
|
||||
$box = new $classname($this->_reader);
|
||||
else
|
||||
$box = new $defaultclassname($this->_reader);
|
||||
|
||||
if (!isset($this->_boxes[$type]))
|
||||
$this->_boxes[$type] = array();
|
||||
$this->_boxes[$type][] = $box;
|
||||
|
||||
if (preg_match("/^\xa9?[a-z]{3,4}$/i", $type) &&
|
||||
substr($base, 0, min(strlen($base), strlen
|
||||
($tmp = $path . ($path ? "." : "") . $type))) ==
|
||||
substr($tmp, 0, min(strlen($base), strlen($tmp))))
|
||||
{
|
||||
$this->_reader->setOffset($offset);
|
||||
if (@fopen($filename = "ISO14496/Box/" . strtoupper($type) . ".php",
|
||||
"r", true) !== false)
|
||||
require_once($filename);
|
||||
if (class_exists($classname = "ISO14496_Box_" . strtoupper($type)))
|
||||
$box = new $classname($this->_reader, $this->_options);
|
||||
else
|
||||
$box = new $defaultclassname($this->_reader, $this->_options);
|
||||
$box->setParent($this);
|
||||
if (!isset($this->_boxes[$box->getType()]))
|
||||
$this->_boxes[$box->getType()] = array();
|
||||
$this->_boxes[$box->getType()][] = $box;
|
||||
}
|
||||
$this->_reader->setOffset($offset + $size);
|
||||
}
|
||||
|
||||
array_pop(self::$_path);
|
||||
}
|
||||
|
||||
protected function getPath()
|
||||
{
|
||||
$path = "";
|
||||
echo "box: " .$this->getType() . ", parent: " . $this->getParent()."\n";
|
||||
for ($child = $this; ($parent = $child->getParent()) !== null; $child = $parent){echo "parent found";$path = $parent->getType() . "." . $path;};
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -227,8 +349,23 @@ class ISO14496_Box
|
||||
foreach ($this->_boxes as $identifier => $boxes)
|
||||
if (preg_match($searchPattern, $identifier))
|
||||
foreach ($boxes as $box)
|
||||
$boxes[] = $box;
|
||||
return $boxes;
|
||||
$matches[] = $box;
|
||||
return $matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new box into the current box and returns it.
|
||||
*
|
||||
* @param ISO14496_Box The box to add
|
||||
* @return ISO14496_Box
|
||||
*/
|
||||
public function addBox($box)
|
||||
{
|
||||
$box->setParent($this);
|
||||
$box->setOptions($this->_options);
|
||||
if (!$this->hasBox($box->getType()))
|
||||
$this->_boxes[$box->getType()] = array();
|
||||
return $this->_boxes[$box->getType()][] = $box;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -237,7 +374,9 @@ class ISO14496_Box
|
||||
* matches the identifier, and if not found, invoke a getter method.
|
||||
*
|
||||
* If there are no boxes or getter methods with given name, the method
|
||||
* will yield an exception.
|
||||
* attempts to create a frame with given identifier.
|
||||
*
|
||||
* If none of these work, an exception is thrown.
|
||||
*
|
||||
* @param string $name The box or field name.
|
||||
* @return mixed
|
||||
@@ -248,9 +387,29 @@ class ISO14496_Box
|
||||
return $this->_boxes[$name][0];
|
||||
if (method_exists($this, "get" . ucfirst($name)))
|
||||
return call_user_func(array($this, "get" . ucfirst($name)));
|
||||
if (@fopen($filename = "ISO14496/Box/" .
|
||||
strtoupper($name) . ".php", "r", true) !== false)
|
||||
require_once($filename);
|
||||
if (class_exists($classname = "ISO14496_Box_" . strtoupper($name)))
|
||||
return $this->addBox(new $classname());
|
||||
throw new ISO14496_Exception("Unknown box/field: " . $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic function so that assignments with $obj->value will work.
|
||||
*
|
||||
* @param string $name The field name.
|
||||
* @param string $value The field value.
|
||||
* @return mixed
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
if (method_exists($this, "set" . ucfirst($name)))
|
||||
call_user_func
|
||||
(array($this, "set" . ucfirst($name)), $value);
|
||||
else throw new ISO14496_Exception("Unknown field: " . $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic function so that isset($obj->value) will work. This method checks
|
||||
* whether the box is a container and contains a box that matches the
|
||||
@@ -263,4 +422,39 @@ class ISO14496_Box
|
||||
{
|
||||
return ($this->isContainer() && isset($this->_boxes[$name]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic function so that unset($obj->value) will work. This method removes
|
||||
* all the boxes from this container that match the identifier.
|
||||
*
|
||||
* @param string $name The box name.
|
||||
*/
|
||||
public function __unset($name)
|
||||
{
|
||||
if ($this->isContainer())
|
||||
unset($this->_boxes[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the box raw data.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString($data = "")
|
||||
{
|
||||
if ($this->isContainer())
|
||||
foreach ($this->getBoxes() as $name => $boxes)
|
||||
foreach ($boxes as $box)
|
||||
$data .= $box;
|
||||
$size = strlen($data) + 8;
|
||||
if ($size > 0xffffffff)
|
||||
$size += 8;
|
||||
if (strlen($this->_type) > 4)
|
||||
$size += 16;
|
||||
return ($size > 0xffffffff ?
|
||||
Transform::toUInt32BE(1) : Transform::toUInt32BE($size)) .
|
||||
(strlen($this->_type) > 4 ? "uuid" : $this->_type) .
|
||||
($size > 0xffffffff ? Transform::toInt64BE($size) : "") .
|
||||
(strlen($this->_type) > 4 ? Transform::toGUID($this->_type) : "") . $data;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user