« 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
looks sweet