watch.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python3
  2. #
  3. # Usage:
  4. # ./autocompile.py path ext1
  5. #
  6. import subprocess
  7. import sys
  8. import os
  9. import pyinotify
  10. import audio_timings
  11. from db import db,file_info
  12. from constants import RAW_PATH
  13. from subprocess import Popen,PIPE
  14. class OnWriteHandler(pyinotify.ProcessEvent):
  15. def my_init(self, cwd, extension):
  16. self.cwd = cwd
  17. self.extension = extension
  18. def _run_cmd(self,path):
  19. print('==> Modification detected')
  20. d=db()
  21. out_fname=os.path.join(RAW_PATH, os.path.basename(path).replace('flv','mp4'))
  22. in_fname=os.path.join(self.cwd, path)
  23. if os.path.isfile(out_fname):
  24. #exists
  25. return
  26. process=[ 'ffmpeg',
  27. '-loglevel', 'error',
  28. '-i', in_fname,
  29. '-c', 'copy',
  30. '-movflags', '+faststart',
  31. '-f','mp4',
  32. out_fname
  33. ]
  34. print(" ".join(process))
  35. proc=Popen(process, universal_newlines=True)
  36. proc.wait()
  37. rc = proc.returncode
  38. if rc != 0:
  39. print("Error?")
  40. return
  41. info = file_info(out_fname)
  42. info['timings'] = audio_timings.parse_file(out_fname)
  43. d.insert_raw(info)
  44. def process_IN_CLOSE_WRITE(self, event):
  45. if not event.pathname.endswith(self.extension):
  46. return
  47. print(event.mask, event.maskname, event.pathname)
  48. self._run_cmd(event.pathname)
  49. def auto_compile(path, extension):
  50. wm = pyinotify.WatchManager()
  51. handler = OnWriteHandler(cwd=path, extension=extension)
  52. notifier = pyinotify.Notifier(wm, default_proc_fun=handler)
  53. wm.add_watch(path, pyinotify.ALL_EVENTS, rec=True, auto_add=True)
  54. print('==> Start monitoring %s' % path)
  55. notifier.loop()
  56. if __name__ == '__main__':
  57. path = '/home/nginx/processed/'
  58. extension = 'flv'
  59. auto_compile(path, extension)