| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- 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"]
- def raw(self, _id=None):
- ret = None
- if _id:
- ret = jsonable(self.raw_files.find_one({"_id": ObjectId(_id)}))
- else:
- ret = [ jsonable(r) for r in self.raw_files.find() ]
- return ret
- 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("flv"):
- self.insert_raw(file_info(os.path.join(RAW_PATH,fname)))
- 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
|