From 49f4ade5f7216149b51378254b7890ee35f4e556 Mon Sep 17 00:00:00 2001 From: svollbehr Date: Fri, 25 Apr 2008 07:30:05 +0000 Subject: [PATCH] Add support for ID3v2 extension to ISO14496 git-svn-id: http://php-reader.googlecode.com/svn/trunk@86 51a70ab9-7547-0410-9469-37e369ee0574 --- src/ID3v2.php | 45 +++++++------------ src/ISO14496/Box/ID32.php | 93 +++++++++++++++++++++++++++++++++++++++ src/Reader.php | 9 ++-- 3 files changed, 113 insertions(+), 34 deletions(-) create mode 100644 src/ISO14496/Box/ID32.php diff --git a/src/ID3v2.php b/src/ID3v2.php index 47525f8..1483007 100644 --- a/src/ID3v2.php +++ b/src/ID3v2.php @@ -59,26 +59,6 @@ require_once("ID3/Frame.php"); * need not be known to the software that encounters them. Each frame has an * unique and predefined identifier which allows software to skip unknown * frames. - * - * Overall tag structure: - * - *
- *   +-----------------------------+
- *   |      Header (10 bytes)      |
- *   +-----------------------------+
- *   |       Extended Header       |
- *   | (variable length, OPTIONAL) |
- *   +-----------------------------+
- *   |   Frames (variable length)  |
- *   +-----------------------------+
- *   |           Padding           |
- *   | (variable length, OPTIONAL) |
- *   +-----------------------------+
- *   | Footer (10 bytes, OPTIONAL) |
- *   +-----------------------------+
- * 
- * - * In general, padding and footer are mutually exclusive. * * @package php-reader * @subpackage ID3 @@ -105,7 +85,7 @@ final class ID3v2 private $_frames = array(); /** @var string */ - private $_filename; + private $_filename = false; /** @var Array */ private $_options; @@ -118,11 +98,15 @@ final class ID3v2 * o version -- The ID3v2 tag version to use in write operation. This option * is automatically set when a tag is read from a file and defaults to * version 4.0 for tag write. + * o readonly -- Indicates that the tag is read from a temporary file or + * another source it cannot be written back to. The tag can, however, + * still be written to another file. * * @todo Only limited subset of flags are processed. * @todo Utilize the SEEK frame and search for a footer to find the tag * @todo Utilize the LINK frame to fetch frames from other sources - * @param string $filename The path to the file. + * @param string $filename The path to the file, file descriptor of an opened + * file, or {@link Reader} instance. * @param Array $options The options array. */ public function __construct($filename = false, $options = array()) @@ -133,11 +117,17 @@ final class ID3v2 } $this->_options = &$options; - if (($this->_filename = $filename) === false || - file_exists($filename) === false) { + if ($filename === false || + (is_string($filename) && file_exists($filename) === false) || + (is_resource($filename) && get_resource_type($filename) != "file")) { $this->_header = new ID3_Header(null, $options); } else { - $this->_reader = new Reader($filename); + if (is_string($filename) && !isset($options["readonly"])) + $this->_filename = $filename; + if ($filename instanceof Reader) + $this->_reader = $filename; + else + $this->_reader = new Reader($filename); if ($this->_reader->readString8(3) != "ID3") throw new ID3_Exception ("File does not contain ID3v2 tag: " . $filename); @@ -246,10 +236,7 @@ final class ID3v2 * * @return Array */ - public function getFrames() - { - return $this->_frames; - } + public function getFrames() { return $this->_frames; } /** * Returns an array of frames matching the given identifier or an empty array diff --git a/src/ISO14496/Box/ID32.php b/src/ISO14496/Box/ID32.php new file mode 100644 index 0000000..8693752 --- /dev/null +++ b/src/ISO14496/Box/ID32.php @@ -0,0 +1,93 @@ +ID3v2 Box resides under the {@link ISO14496_Box_META Meta Box} and + * stores ID3 version 2 meta-data. There may be more than one ID3v2 Box present + * each with a different language code. + * + * @package php-reader + * @subpackage ISO 14496 + * @author Sven Vollbehr + * @copyright Copyright (c) 2008 The PHP Reader Project Workgroup + * @license http://code.google.com/p/php-reader/wiki/License New BSD License + * @version $Rev$ + */ +final class ISO14496_Box_ID32 extends ISO14496_Box_Full +{ + /** @var string */ + private $_language; + + /** @var ID3v2 */ + private $_tag; + + /** + * Constructs the class with given parameters and reads box related data from + * the ISO Base Media file. + * + * @param Reader $reader The reader object. + */ + public function __construct($reader) + { + parent::__construct($reader); + + $this->_language = + chr(((($tmp = $this->_reader->readUInt16BE()) >> 10) & 0x1f) + 0x60) . + chr((($tmp >> 5) & 0x1f) + 0x60) . chr(($tmp & 0x1f) + 0x60); + $this->_tag = new ID3v2($this->_reader, array("readonly" => true)); + } + + /** + * Returns the three byte language code to describe the language of this + * media, according to {@link http://www.loc.gov/standards/iso639-2/ + * ISO 639-2/T}. + * + * @return string + */ + public function getLanguage() { return $this->_language; } + + /** + * Returns the {@link ID3v2} tag class instance. + * + * @return string + */ + public function getTag() { return $this->_tag; } +} diff --git a/src/Reader.php b/src/Reader.php index 70e277b..fc51c29 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -68,7 +68,9 @@ class Reader */ public function __construct($filename, $mode = "rb") { - if (($this->_fd = fopen($filename, $mode)) === false) + if (is_resource($filename) && get_resource_type($filename) == "file") + $this->_fd = $filename; + else if (($this->_fd = fopen($filename, $mode)) === false) throw new Reader_Exception("Unable to open file:" . $filename); fseek($this->_fd, 0, SEEK_END); @@ -156,10 +158,7 @@ class Reader * * @return integer */ - public function getSize() - { - return $this->_size; - } + public function getSize() { return $this->_size; } /** * Magic function so that $obj->value will work.