« WebSphere flex plugin | Home | Flex app featured in PCWorld »

August 30, 2007

ffmpeg - Creating a watch folder

I've seen so many times questions on how to create a watch folder that together with ffmpeg will convert videos from one format to the flv.

So today i felt like trying to create one in Python using inotify.

Inotify is linux kernel only subsystem that provides file system notification. It's a replacement of dnotify which was a pain in you know where to work with.

I worked with inotify last week doing an image optimizer daemon. I'm writing this in my head and haven't tested it since i'm at home with no access to a linux box but it should work fine.

You need python, pyinotify, a kernel 2.6.13+ and ofcourse ffmpeg :)

# @author funciton communications <info@funciton.com> # @version 1.0.0 # Imports import inotify import logging import logging.handlers import sys from os import system # Mask __flags = inotify.ATTRIB # Init if __name__ == "__main__": path = sys.argv[1] if len(sys.argv) == 1: print >> sys.stderr, "Usage: " + sys.argv[0] + " path" sys.exit(0) logger = logging.getLogger('watchfolder') logger.addHandler(logging.handlers.RotatingFileHandler('/var/log/watchfolder.log')) watcher = inotify.Watcher() if not watcher.is_watching(path): print >> sys.stderr, "watching "+ path watcher.watch(path, __flags) while (True): try: event = watcher.get_next_event() except Exception: print "re-scanning" continue try: if event & inotify.ATTRIB: filename, ext = os.path.splitext(os.path.basename(image)) if ext != ".flv": # execute ffmpeg system("ffmpeg -i " + filename + ext + " " + filename + ".flv") except Exception, er: print er.strerror continue

To test it out:
python watchFolder.py /path/to/folder

I'll test it tomorrow at work if i get better (i'm sick at home) so let me know right away if you find smth which isn't working fine.

-- fernando

1 Comment

About this Entry

This page contains a single entry by fernando published on August 30, 2007 5:36 PM.

WebSphere flex plugin was the previous entry in this blog.

Flex app featured in PCWorld is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.