#! /usr/bin/env pyhon
"""
AVI Fps module can be used to retrieve an AVI movie framerate.
"""

import struct
import sys

def getAVIFrameRate(fileName):
  "Opens an AVI movie and compute its framerate."

  # safe value
  framerate = 0

  try:
    # opens the file
    fsock = open(fileName, "rb", 0)
    # seek the 128th byte
    fsock.seek(128)

    # get scale value
    movieScale = struct.unpack("i", fsock.read(4))[0]
    # get rate value
    movieRate = struct.unpack("i", fsock.read(4))[0]
    # compute framerate
    framerate = movieRate / movieScale

    # close the file
    fsock.close()
  except IOError:
    print "Could not open", fileName

  return framerate

# main entry point
# allow use of the module as a stand-alone script
if __name__ == "__main__":
  if (len(sys.argv) == 1):
    print """
AVI Fps - Display AVI movie framerate

Usage: python aviFps.py movie.avi
"""
  else:
    print "AVI Fps - (C)2001 Romain Guy"
    print sys.argv[1], "framerate is", getAVIFrameRate(sys.argv[1]), "fps."
