| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import time
- import re
- import os
- import subprocess
- from bson.objectid import ObjectId
- from constants import RAW_PATH
- from pymongo import MongoClient, errors, DESCENDING, ASCENDING
- class db():
- DB = "videos"
- def __init__(self):
- self.client = MongoClient('localhost', 27017)
- self.raw_files = self.client[self.DB]["raw"]
- self.cut_files = self.client[self.DB]["cut"]
- self.ul_files = self.client[self.DB]["ul"]
- self.jobs = self.client[self.DB]["jobs"]
- def getJobs(self):
- ret = [ jsonable(r) for r in self.jobs.find() ]
- return ret
- def raw(self, _id=None):
- ret = None
- if _id:
- ret = jsonable(self.raw_files.find_one({"_id": ObjectId(_id)}))
- else:
- ret = [ jsonable(fix_date(r)) for r in self.raw_files.find() ]
- return ret
- def processed(self, _id=None):
- ret = None
- if _id:
- ret = jsonable(self.cut_files.find_one({"_id": ObjectId(_id)}))
- else:
- ret = [ jsonable(fix_date(c)) for c in self.cut_files.find() ]
- return ret
- def insert_processed(self, data):
- try:
- self.cut_files.insert_one(data)
- return True
- except errors.DuplicateKeyError:
- print("Dup")
- return False
-
- def insert_raw(self, rawfile):
- try:
- self.raw_files.insert_one(rawfile)
- return True
- except errors.DuplicateKeyError:
- print("Dup")
- return False
- def insert_raw_list(self):
- for fname in os.listdir(RAW_PATH):
- if fname.endswith("mp4"):
- self.insert_raw(file_info(os.path.join(RAW_PATH,fname)))
- def fix_date(obj):
- ret = obj
- #ret["date"]=ret["date"].strftime('%Y-%m-%dT%H:%M:%S')
- return ret
- def get_video_length(fname):
- process="ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -sexagesimal %s" % fname
- process=process.split(" ")
- out,err= subprocess.Popen(process,stdout=subprocess.PIPE).communicate()
- out=out.decode('utf-8')
- if "." in out:
- out = out.split(".")[0]
- return out
- def file_info(fname):
- return {'file':fname,
- 'length': get_video_length(fname),
- 'date': time.ctime(os.path.getctime(fname))
- }
- def jsonable(el):
- if el is None or "_id" not in el:
- return el
- el["_id"] = str(el["_id"])
- return el
|