#!/usr/bin/env python3 # # Usage: # ./autocompile.py path ext1 # import subprocess import sys import os import pyinotify import audio_timings from db import db,file_info from constants import RAW_PATH from subprocess import Popen,PIPE class OnWriteHandler(pyinotify.ProcessEvent): def my_init(self, cwd, extension): self.cwd = cwd self.extension = extension def _run_cmd(self,path): print('==> Modification detected') d=db() out_fname=os.path.join(RAW_PATH, os.path.basename(path).replace('flv','mp4')) in_fname=os.path.join(self.cwd, path) if os.path.isfile(out_fname): #exists return process=[ 'ffmpeg', '-loglevel', 'error', '-i', in_fname, '-c', 'copy', '-movflags', '+faststart', '-f','mp4', out_fname ] print(" ".join(process)) proc=Popen(process, universal_newlines=True) proc.wait() rc = proc.returncode if rc != 0: print("Error?") return info = file_info(out_fname) info['timings'] = audio_timings.parse_file(out_fname) d.insert_raw(info) def process_IN_CLOSE_WRITE(self, event): if not event.pathname.endswith(self.extension): return print(event.mask, event.maskname, event.pathname) self._run_cmd(event.pathname) def auto_compile(path, extension): wm = pyinotify.WatchManager() handler = OnWriteHandler(cwd=path, extension=extension) notifier = pyinotify.Notifier(wm, default_proc_fun=handler) wm.add_watch(path, pyinotify.ALL_EVENTS, rec=True, auto_add=True) print('==> Start monitoring %s' % path) notifier.loop() if __name__ == '__main__': path = '/home/nginx/processed/' extension = 'flv' auto_compile(path, extension)