File_Ogg
[ class tree: File_Ogg ] [ index: File_Ogg ] [ all elements ]

Source for file Media.php

Documentation is available at Media.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------------+
  4. // | File_Ogg PEAR Package for Accessing Ogg Bitstreams                         |
  5. // | Copyright (c) 2005-2007                                                    |
  6. // | David Grant <david@grant.org.uk>                                           |
  7. // | Tim Starling <tstarling@wikimedia.org>                                     |
  8. // +----------------------------------------------------------------------------+
  9. // | This library is free software; you can redistribute it and/or              |
  10. // | modify it under the terms of the GNU Lesser General Public                 |
  11. // | License as published by the Free Software Foundation; either               |
  12. // | version 2.1 of the License, or (at your option) any later version.         |
  13. // |                                                                            |
  14. // | This library is distributed in the hope that it will be useful,            |
  15. // | but WITHOUT ANY WARRANTY; without even the implied warranty of             |
  16. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU          |
  17. // | Lesser General Public License for more details.                            |
  18. // |                                                                            |
  19. // | You should have received a copy of the GNU Lesser General Public           |
  20. // | License along with this library; if not, write to the Free Software        |
  21. // | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA |
  22. // +----------------------------------------------------------------------------+
  23.  
  24. require_once('File/Ogg/Bitstream.php');
  25.  
  26. /**
  27.  * Parent class for media bitstreams
  28.  * Contains some functions common to various media formats
  29.  */
  30. abstract class File_Ogg_Media extends File_Ogg_Bitstream
  31. {
  32.     /**
  33.      * Array to hold each of the comments.
  34.      *
  35.      * @access  private
  36.      * @var     array 
  37.      */
  38.     var $_comments = array();
  39.     /**
  40.      * Vendor string for the stream.
  41.      *
  42.      * @access  private
  43.      * @var     string 
  44.      */
  45.     var $_vendor;
  46.  
  47.     /**
  48.      * Length of the stream in seconds
  49.      */
  50.     var $_streamLength;
  51.     
  52.     /* Start offset of the stream in seconds */
  53.     var $_startOffset = 0;
  54.  
  55.     /**
  56.      * Get a short string describing the type of the stream
  57.      * @return string 
  58.      */
  59.     abstract function getType();
  60.  
  61.     /**
  62.      * Get the 6-byte identification string expected in the common header
  63.      * @return string 
  64.      */
  65.     function getIdentificationString()
  66.     {
  67.         return '';
  68.     }
  69.  
  70.     /**
  71.      * @access  private
  72.      * @param   int     $packetType 
  73.      * @param   int     $pageOffset 
  74.      */
  75.     function _decodeCommonHeader($packetType$pageOffset)
  76.     {
  77.         fseek($this->_filePointer$this->_streamData['pages'][$pageOffset]['body_offset']SEEK_SET);
  78.         if ($packetType !== false{
  79.             // Check if this is the correct header.
  80.             $packet unpack("Cdata"fread($this->_filePointer1));
  81.             if ($packet['data'!= $packetType)
  82.                 throw new PEAR_Exception("Stream Undecodable"OGG_ERROR_UNDECODABLE);
  83.         
  84.             // The following six characters should be equal to getIdentificationString()
  85.             $id $this->getIdentificationString();
  86.             if ($id !== '' && fread($this->_filePointerstrlen($id)) !== $id)
  87.                 throw new PEAR_Exception("Stream is undecodable due to a malformed header."OGG_ERROR_UNDECODABLE);
  88.         // else seek only, no common header
  89.     }
  90.  
  91.     /**
  92.      * Parse a Vorbis-style comments header.
  93.      *
  94.      * This function parses the comments header.  The comments header contains a series of
  95.      * UTF-8 comments related to the audio encoded in the stream.  This header also contains
  96.      * a string to identify the encoding software.  More details on the comments header can
  97.      * be found at the following location: http://xiph.org/vorbis/doc/v-comment.html
  98.      *
  99.      * @access  private
  100.      */
  101.     function _decodeBareCommentsHeader()
  102.     {
  103.         // Decode the vendor string length as a 32-bit unsigned integer.
  104.         $vendor_len unpack("Vdata"fread($this->_filePointer4));
  105.         if $vendor_len['data'> 0 {
  106.             // Retrieve the vendor string from the stream.
  107.             $this->_vendor  fread($this->_filePointer$vendor_len['data']);
  108.         else {
  109.             $this->_vendor '';
  110.         }
  111.         // Decode the size of the comments list as a 32-bit unsigned integer.
  112.         $comment_list_length unpack("Vdata"fread($this->_filePointer4));
  113.         // Iterate through the comments list.
  114.         for ($i = 0; $i $comment_list_length['data']; ++$i{
  115.             // Unpack the length of this comment.
  116.             $comment_length unpack("Vdata"fread($this->_filePointer4));
  117.             // Comments are in the format 'ARTIST=Super Furry Animals', so split it on the equals character.
  118.             // NOTE: Equals characters are strictly prohibited in either the COMMENT or DATA parts.
  119.             $comment        explode("="fread($this->_filePointer$comment_length['data']));
  120.             $comment_title  = (string) $comment[0];
  121.             $comment_value  = (string) utf8_decode($comment[1]);
  122.     
  123.             // Check if the comment type (e.g. ARTIST) already exists.  If it does,
  124.             // take the new value, and the existing value (or array) and insert it
  125.             // into a new array.  This is important, since each comment type may have
  126.             // multiple instances (e.g. ARTIST for a collaboration) and we should not
  127.             // overwrite the previous value.
  128.             if (isset($this->_comments[$comment_title])) {
  129.                 if (is_array($this->_comments[$comment_title]))
  130.                     $this->_comments[$comment_title][$comment_value;
  131.                 else
  132.                     $this->_comments[$comment_title= array($this->_comments[$comment_title]$comment_value);
  133.             else
  134.                 $this->_comments[$comment_title$comment_value;
  135.         }
  136.     }
  137.     
  138.     /**
  139.      * Provides a list of the comments extracted from the Vorbis stream.
  140.      *
  141.      * It is recommended that the user fully inspect the array returned by this function
  142.      * rather than blindly requesting a comment in false belief that it will always
  143.      * be present.  Whilst the Vorbis specification dictates a number of popular
  144.      * comments (e.g. TITLE, ARTIST, etc.) for use in Vorbis streams, they are not
  145.      * guaranteed to appear.
  146.      *
  147.      * @access  public
  148.      * @return  array 
  149.      */
  150.     function getCommentList()
  151.     {
  152.         return (array_keys($this->_comments));
  153.     }
  154.  
  155.     /**
  156.      * Provides an interface to the numerous comments located with a Vorbis stream.
  157.      *
  158.      * A Vorbis stream may contain one or more instances of each comment, so the user
  159.      * should check the variable type before printing out the result of this method.
  160.      * The situation in which multiple instances of a comment occurring are not as
  161.      * rare as one might think, since they are conceivable at least for ARTIST comments
  162.      * in the situation where a track is a duet.
  163.      *
  164.      * @access  public
  165.      * @param   string  $commentTitle   Comment title to search for, e.g. TITLE.
  166.      * @param   string  $separator      String to separate multiple values.
  167.      * @return  string 
  168.      */
  169.     function getField($commentTitle$separator ", ")
  170.     {
  171.     if (isset($this->_comments[$commentTitle])) {
  172.         if (is_array($this->_comments[$commentTitle]))
  173.             return (implode($separator$this->_comments[$commentTitle]));
  174.         else
  175.             return ($this->_comments[$commentTitle]);
  176.     else
  177.         // The comment doesn't exist in this file.  The user should've called getCommentList first.
  178.         return ("");
  179.     }
  180.     
  181.     /**
  182.      * Get the entire comments array.
  183.      * May return an empty array if the bitstream does not support comments.
  184.      *
  185.      * @access  public
  186.      * @return  array 
  187.      */
  188.     function getComments({
  189.         return $this->_comments;
  190.     }
  191.  
  192.     /**
  193.      * Vendor of software used to encode this stream.
  194.      *
  195.      * Gives the vendor string for the software used to encode this stream.
  196.      * It is common to find libVorbis here.  The majority of encoders appear
  197.      * to use libvorbis from Xiph.org.
  198.      *
  199.      * @access  public
  200.      * @return  string 
  201.      */
  202.     function getVendor()
  203.     {
  204.         return ($this->_vendor);
  205.     }
  206.  
  207.     /**
  208.      * Get an associative array containing header information about the stream
  209.      * @access  public
  210.      * @return  array 
  211.      */
  212.     function getHeader(
  213.     {
  214.         return array();
  215.     }
  216.  
  217.     /**
  218.      * Get the length of the stream in seconds
  219.      * @return float 
  220.      */
  221.     function getLength()
  222.     {
  223.         return $this->_streamLength;
  224.     }
  225.     /**
  226.      * Get the start offset of the stream in seconds
  227.      *
  228.      * @return float 
  229.      */
  230.     function getStartOffset(){
  231.         return $this->_startOffset;
  232.     }
  233. }

Documentation generated on Mon, 11 Mar 2019 15:38:25 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.