db.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from pymongo import MongoClient, errors, DESCENDING, ASCENDING
  2. from bson.objectid import ObjectId
  3. import time
  4. import re
  5. import os
  6. import subprocess
  7. RAW_DIRECTORY="/home/david"
  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 jsonable(self, el):
  16. if el is None or "_id" not in el:
  17. return el
  18. el["_id"] = str(el["_id"])
  19. return el
  20. def raw(self, _id=None):
  21. ret = None
  22. if _id:
  23. ret = self.raw_files.find_one({"_id": ObjectId(id)})
  24. else:
  25. ret = [ self.jsonable(r) for r in self.raw_files.find() ]
  26. return ret
  27. def insert_raw(self, rawfile):
  28. try:
  29. self.raw_files.insert_one(rawfile)
  30. return True
  31. except errors.DuplicateKeyError:
  32. print("Dup")
  33. return False
  34. def insert_raw_list(self):
  35. for fname in os.listdir(RAW_DIRECTORY):
  36. if fname.endswith("flv"):
  37. self.insert_raw(file_info(os.path.join(RAW_DIRECTORY,fname)))
  38. def get_video_length(fname):
  39. process="ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -sexagesimal %s" % fname
  40. process=process.split(" ")
  41. out,err= subprocess.Popen(process,stdout=subprocess.PIPE).communicate()
  42. out=out.decode('utf-8')
  43. if "." in out:
  44. out = out.split(".")[0]
  45. return out
  46. def file_info(fname):
  47. return {'file':fname,
  48. 'length': get_video_length(fname),
  49. 'date': time.ctime(os.path.getctime(fname))
  50. }