Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2007
    Location
    Atlanta, GA
    Posts
    79

    PHP parseing video files

    I'm building a website where people can upload videos for Multi-Editor Projects (or MEP). what I want the site to be capable of performing is that it can read an uploaded video and parse the video's Header Information so that I can store that information into a database. it would also allow me to automatically check a video to see if it meets the required specifications.

    most MEPs have strict rules such that submitted videos must meet:
    • a specific frame resolution
    • FPS
    • time limitations
    • file size
    • a small list of accepted codecs.


    what I want PHP to be able to do is that once a submitter uploads a video, PHP can can read the header information of the file and automatically determine whether an uploaded video meets the requirements.

    I've made a function that can read the Header information of an AVI file as shown below:

    Code:
    <?php
    // a very big thanks to these websites as they are a big help in getting me to parse an avi file:
    //http://www.fastgraph.com/help/avi_header_format.html
    //http://www.alexander-noe.com/video/documentation/avi.pdf
    // and of course www.php.net
    
    function AVIHandle ($file)
    {
      if(file_exists($file))
      {
        if(strtolower(substr($file,-3))=='avi')
        {
          $handle = fopen($file, "r");
          $contents = fread($handle, filesize($file));
          // the header information resides in the first 88 bytes of the an avi file.
          // the first 32 bytes are not part of the header so in actuality, the header is 56 bytes, 32 bytes into the file.
          // you might notice that I'm still reading the entire file instead of the first 88 bytes. thats because the Codec
          // information resides further in the file, not in the header
          fclose($handle);
          // now chop off the first 32 bytes  and all the bytes  after the 56th so that I only have the header information
          $header=substr($contents,32,56);  
    
    
    
          $fps=            unpack("l",substr($header,0,4)); 
          //fps is a misnomer, this is actually the delay between frames in microseconds
          $fps[1]=(1000000/$fps[1]);
          //also note that this will produce a relatively accurate fps. I later plan to add in a script that will compare the 
          //project's listed fps and the vids fps to be true if the numbers are within a floating threashold
    
          $frames=         unpack("l",substr($header,16,4));
          // number of video frames
          $secondsLength = $frames[1]/$fps[1];
          $length= date("i:s",$secondsLength);
          // this is the caluated length of a video in min:sec
    
          $width=          unpack("i",(substr($header,32,4)));
          $height=         unpack("i",(substr($header,36,4)));
          // the Video's resolution
    
          //right now the function is just outputting the information to the webpage. When the function is finished
          // I'll have the information returned in an array.
    
          echo "file name: $file<br />
          File size:".filesize($file)."<br />
          fps:$fps[1]<br />
          frames:$frames[1]<br />
          length:$length <br />
          width:$width[1]<br />
          height:$height[1]<br />";
          
    
          //determine codec information from the streams
          $streams=substr($contents,88);
          //since the fcctype (referenced from page 11 of the pdf link listed at the top of this php file) turns out to be in ASCII,
          // I can do a simple search to find the vids stream without needing to unpack. when I find it I know the next 4
          // characters, which are also ASCII will be the video's FourCC.
          preg_match('/(vids){1}.{4}/',$streams,$vids);
          echo "vidCC:".substr($vids[0],4,4)."<br />";
    
    
          // finding the Audio codec isn't as easy. as using the same trick doesn't yield correct results
        }
        else
        {
          return "not an avi!!!";
        }
      }
      else
      {
        return "file doesn't exist!";
      }
    }
    the function isn't finished since I can't seem to figure out how to find the audio codec. also this handler function only works on AVI files. I also want to make a function for mkv. The reason is because the H264 codec is commonly used and MKV's use of the h.264(AVC) codec is better than avi (at least compression wise). but the problem with mkv is that it's file format is vastly different from AVI.

    www.matroska.org states that an mkv file uses an EBML format. for the life of me I can't seems to make sense as to how to parse an EBML file.

    so I guess the purpose of this thread is to ask if anyone has knowledge as to how to parse an mkv file using php, how to find the audio codec in an AVI file using php, and any comments relating to the subject

  2. #2
    Join Date
    Feb 2007
    Location
    Atlanta, GA
    Posts
    79
    in my googling I came across a page that had an open-source php project with functions that perform the exact things that I wanted and more.

    http://getid3.sourceforge.net/

    it works like a dream with avi and many other formatshowever it still doesn't support the parsing of mkvs so thats still up in the air.

    so I'm still looking for a way to parse an EBML file in php if anyone has any clue.

  3. #3
    Join Date
    Mar 2006
    Location
    South Australia
    Posts
    4,521
    Why mkvs for h.264? MP4 and AVI handle them just fine.

    EDIT: The getid3 homepage says it handles matroska files just fine.
    Last edited by mr_charisma; 12-07-2008 at 09:51 PM.

  4. #4
    Join Date
    Feb 2007
    Location
    Atlanta, GA
    Posts
    79
    getid3 does say that it can parse matroska and it does has a module named module.audio-video.matroska.php (the latest stable release anyway. in the beta and future versions the module is removed)but the module doesn't do anything, just an error. if you read the readme.txt file it mentions that the ability to parse mkvs is a future plan.

    the reason I was looking forward to using mkv is that mkv is better at h264 compression. so it was merely wanted to assist in bandwidth

    however today I was going over the specs of how the site will work. and we finally decided to only accept avi files for a couple of reasons. seeing that these videos will be imported into an editor which likely won't support the mkv format and that our webmaster is getting up another server where bandwidth won't be a problem. that and the fact that I still can't parse mkv in php i think I'll stay with avi

  5. #5
    Join Date
    Mar 2006
    Location
    South Australia
    Posts
    4,521
    But MKV and AVI are both container formats - they don't have anything to do with the compression.

  6. #6
    Join Date
    Feb 2007
    Location
    Atlanta, GA
    Posts
    79
    its that mkv can store b frames. when a video is stored in mkv with h.264, the codec can better compress the video if it uses b frames. the AVI format can't store bframes, so the avi format can't take full advantage of the codec's compression capabilities

    if you have vdub and an uncompressed (or huffy or similar) video compress the video in H.264 into both mkv and avi. quality wise you likely won't notice a difference but the filesizes will be different especially if you use b frames for mkv. and the difference in size grows the longer the video is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •