tasks.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import time
  2. from subprocess import Popen,PIPE
  3. from rq import get_current_job
  4. import os.path
  5. def long_job(arg):
  6. for i in range(20):
  7. time.sleep(1)
  8. job = get_current_job()
  9. job.meta['progress'] = i
  10. job.save()
  11. #push to db, return id if ok. return error if not ok
  12. return "37"
  13. def upload_video(title,thumb_path,video_path):
  14. import youtube_upload.main
  15. j=get_current_job()
  16. return youtube_upload.main.main(title,thumb_path,video_path,job=j)
  17. def cut_video(in_fname,start_time,end_time,out_fname):
  18. if not os.path.isfile(in_fname):
  19. return { "status":"error",
  20. "error": "El archivo %s no existe" % in_fname
  21. }
  22. if os.path.isfile(out_fname):
  23. return { "status":"error",
  24. "error": "El archivo %s existe" % out_fname
  25. }
  26. process = [ 'ffmpeg',
  27. '-loglevel', 'error',
  28. '-stats',
  29. '-i', in_fname,
  30. '-ss', start_time,
  31. '-t', end_time,
  32. '-c', 'copy',
  33. '-movflags', '+faststart',
  34. out_fname
  35. ]
  36. out = run_process(process)
  37. if out == "":
  38. out = { "status": "Proceso finalizado" }
  39. return out
  40. def run_process(process = None):
  41. if process is None or type(process) is not list:
  42. return { "status": "error",
  43. "error": "Argumento process invalido"
  44. }
  45. proc=Popen(process, stderr=PIPE, universal_newlines=True)
  46. while True:
  47. line = proc.stderr.readline()
  48. if line != '' and line !=b'':
  49. job = get_current_job()
  50. job.meta['progress'] = line.rstrip()
  51. job.save()
  52. else:
  53. break