import os
import shutil
# from add_file import addFile

def isVideo(path):
  basename, ext = os.path.splitext(path)
  if ext.lower() in ['.vob', '.avi', '.mp4', '.mov']:
    return True
  else:
    return False  

def isAudio(path):
  basename, ext = os.path.splitext(path)
  if ext.lower() in ['.wav', '.mp3', '.ac3', '.wmv']:
    return True
  else:
    return False

def spider(path, fn, **kwargs):
#    path = self.archives()[archive]
    path = os.path.normpath(path)
    for dirpath, dirnames, filenames in os.walk(path):
        if filenames:
#            prefix = dirpath[len(path)+1:]
            for filename in filenames:
                if not filename.startswith('._') and not filename in ('.DS_Store', ):
#                    print dirpath  + " + " + filename
                    fn(dirpath, filename)

def mvFile(dirpath, filename):
  path = os.path.join(dirpath, filename)
  if isVideo(path) or isAudio(path):
    new_path = os.path.join("/srv/edgware/RawMedia/", filename)
    shutil.move(path, new_path)
  return True 
  
def cleanFile(dirpath, filename):
  if filename.startswith("~$"):
    path = os.path.join(dirpath, filename)
    os.remove(path) 

def encodeFile(dirpath, filename):
  path = os.path.join(dirpath, filename)
  if isVideo(path):
    return encodeVideo(path)
  if isAudio(path):
    return encodeAudio(path)
  else:
    return None

def encodeVideo(path):
#  print "Video: " + path
  cmd = 'ffmpeg2theora -p padma "%s"' % (path,)
  print cmd
  os.system(cmd)
  return True  

def encodeAudio(path):
  basename, ext = os.path.splitext(path)
  outpath = basename + ".ogg"
  cmd = 'ffmpeg2theora --novideo "%s" -o "%s"' % (path, outpath,)
  print cmd
  os.system(cmd)
  return True

      
