db.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import time
  2. import re
  3. import os
  4. import subprocess
  5. from bson.objectid import ObjectId
  6. from constants import RAW_PATH
  7. from pymongo import MongoClient, errors, DESCENDING, ASCENDING
  8. class db():
  9. DB = "videos"
  10. def __init__(self):
  11. self.client = MongoClient('localhost', 27017)
  12. self.raw_files = self.client[self.DB]["raw"]
  13. self.cut_files = self.client[self.DB]["cut"]
  14. self.ul_files = self.client[self.DB]["ul"]
  15. def raw(self, _id=None):
  16. ret = None
  17. if _id:
  18. ret = jsonable(self.raw_files.find_one({"_id": ObjectId(_id)}))
  19. else:
  20. ret = [ jsonable(r) for r in self.raw_files.find() ]
  21. return ret
  22. def insert_raw(self, rawfile):
  23. try:
  24. self.raw_files.insert_one(rawfile)
  25. return True
  26. except errors.DuplicateKeyError:
  27. print("Dup")
  28. return False
  29. def insert_raw_list(self):
  30. for fname in os.listdir(RAW_PATH):
  31. if fname.endswith("mp4"):
  32. self.insert_raw(file_info(os.path.join(RAW_PATH,fname)))
  33. def get_video_length(fname):
  34. process="ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -sexagesimal %s" % fname
  35. process=process.split(" ")
  36. out,err= subprocess.Popen(process,stdout=subprocess.PIPE).communicate()
  37. out=out.decode('utf-8')
  38. if "." in out:
  39. out = out.split(".")[0]
  40. return out
  41. def file_info(fname):
  42. return {'file':fname,
  43. 'length': get_video_length(fname),
  44. 'date': time.ctime(os.path.getctime(fname))
  45. }
  46. def jsonable(el):
  47. if el is None or "_id" not in el:
  48. return el
  49. el["_id"] = str(el["_id"])
  50. return el