def main(): try: files = [] dbfn = "/home/chris/Videos/kmedia/tvh/tvh.db" fffn = "/home/chris/Videos/kmedia/TV/mpeg-todo" db = tvheadend.tvhdb.TVHDb(dbfn) sql = "insert into files (name,size,hash) values (?, ?, ?)" with open(fffn, "r") as fn: lines = fn.readlines() for line in lines: files.append(line.strip()) log.info("Found {} files to insert".format(len(files))) cn = 0 for fn in files: if FUT.fileExists(fn): log.info("obtaining details of {}".format(fn)) fhash, fsize = FUT.getFileHash(fn, blocksize=10485760) log.info("{} {}".format(fhash, FUT.sizeof_fmt(fsize))) log.info("inserting into db") db.doInsertSql(sql, [fn, fsize, fhash]) cn += 1 log.info("inserted {} files into DB".format(cn)) db.doInsertSql(sql, fvals) sql = "select count(*) as cn from files" row = db.doSql(sql, one=1) log.info("there are {} files currently in the db".format(row["cn"])) except Exception as e: fname = sys._getframe().f_code.co_name errorExit(fname, e)
def convertToMkv(fqfn): if FUT.fileExists(fqfn): cmd = ["/home/chris/bin/convert-ts-to-mkv.sh", "{}".format(fqfn)] proc = subprocess.run(cmd) if proc.returncode == 0: log.info("Converted {} to mkv".format(fqfn)) else: log.info("Error muxing {}".format(fqfn))
def channelLogo(channel, url): if len(url) > 0: imgs = "/home/chris/Pictures" imgpath = imgs + "/{}.png".format(channel) if not FUT.fileExists(imgpath): print("Getting logo for {}".format(channel)) r = requests.get(url) if r.status_code == 200: print("Logo retrieved ok") with open(imgpath, "wb") as ifn: ifn.write(r.content)
def processFiles(db): try: ret = True if FUT.fileExists(stopnext) else False cn = filesWaiting(db) while cn > 0: log.info("{} files to process.".format(cn)) frow = getFirstFile(db) if FUT.fileExists(frow["name"]): log.info("Converting {}: {}".format( os.path.basename(frow["name"]), FUT.sizeof_fmt(frow["size"]))) rc = FF.convert(frow["name"]) log.info("updating DB, deleting {}".format( os.path.basename(frow["name"]))) deleteFirstFile(db, frow["name"], frow["hash"]) if FUT.fileExists(stopnext): ret = True break cn = filesWaiting(db) return ret except Exception as e: fname = sys._getframe().f_code.co_name errorRaise(fname, e)
def checkRemoveOutputFile(ofn): try: if FUT.fileExists(ofn): size = FUT.fileSize(ofn) if size > 0: msg = f"Destination file '{ofn}' exists: {FUT.sizeof_fmt(size)}, not converting" log.info(msg) raise ConvertFailure(msg) else: msg = "Deleting existing zero length destination file '{ofn}'" log.info(msg) os.remove(ofn) except Exception as e: errorNotify("checkRemoveOutputFile", e)
def aquireLock(self): try: cn = 0 tempfn = self.lockfn while FUT.fileExists(tempfn): time.sleep(1) cn += 1 if cn > self.locktime: raise DBLockError("Timeout waiting for locked DB") tempfn = "wibble, there is a bug, that doesn't seem to notice the file is missing, if the string is the same" tempfn = self.lockfn FUT.fileTouch(self.lockfn) except Exception as e: fname = sys._getframe().f_code.co_name errorRaise(fname, e)
def tidy(rc, fqfn, ofn, sizecheck=True): """ tidies up after ffmpeg. rc = ffmpeg returncode """ global olines global thebin bname = os.path.basename(fqfn) olog = thebin + bname + "-tvhf.log" if rc == 0: log.info("Conversion was successful") insize = FUT.fileSize(fqfn) sinsize = FUT.sizeof_fmt(insize) outsize = FUT.fileSize(ofn) soutsize = FUT.sizeof_fmt(outsize) log.info(f"Input size: {sinsize}, output size: {soutsize}") if sizecheck: if outsize > insize: log.info( "input size is smaller than output size, keeping input file" ) obname = os.path.basename(ofn) destfn = thebin + bname log.info(f"Deleting '{ofn}' file (to {thebin})") FUT.rename(ofn, destfn) else: destfn = thebin + bname log.info(f"Deleting '{fqfn}' file (to {thebin})") FUT.rename(fqfn, destfn) else: destfn = thebin + bname log.info(f"Deleting '{fqfn}' file (to {thebin})") FUT.rename(fqfn, destfn) with open(olog, "w") as olfn: olfn.writelines(olines) log.info(f"ffmpeg output in {olog}") olines = [] else: olog = thebin + bname + "-tvhf.log" with open(olog, "w") as olfn: olfn.writelines(olines) olines = [] msg = f"Conversion of {fqfn} to {ofn} failed, output in {olog}" log.error(msg) if FUT.fileExists(ofn): os.remove(ofn) raise ConvertFailure(msg)
def fileInfo(fqfn): try: finfo = None try: if FUT.fileExists(fqfn): cmd = [ "ffprobe", "-loglevel", "quiet", "-of", "json", "-show_streams", fqfn, ] proc = subprocess.run(cmd, capture_output=True) if proc.returncode == 0: xstr = proc.stdout.decode("utf-8") # print(xstr) finfo = json.loads(xstr) except Exception as e: errorNotify("fileInfo", e) return finfo except Exception as e: errorNotify("fileInfo", e)
def moveShow(show, config): try: then = time.time() tvhstat = os.stat(show["filename"]) log.info("{}: {}".format(show["opbase"], FUT.sizeof_fmt(tvhstat.st_size))) if "year" in show: if show["title"].startswith("The "): letter = show["title"][4:5].upper() else: letter = show["title"][0:1].upper() opdir = "/".join([config["filmhome"], letter, show["opbase"]]) snfo = NFO.makeFilmNfo(show) else: opdir = "/".join( [config["videohome"], show["category"], show["title"]]) snfo = NFO.makeProgNfo(show) basefn = "/".join([opdir, show["opbase"]]) opfn = basefn + ".mpg" mkopfn = basefn + ".mkv" existingfile = None if FUT.fileExists(opfn): existingfile = opfn elif FUT.fileExists(mkopfn): existingfile = mkopfn if existingfile is not None: log.info("kodi file already exists, not copying {}".format( existingfile)) log.info("deleting from tvheadend") TVH.deleteRecording(show["uuid"]) else: log.info("making directory {}".format(opdir)) FUT.makePath(opdir) nfofn = basefn + ".nfo" log.info("writing nfo to {}".format(nfofn)) with open(nfofn, "w") as nfn: nfn.write(snfo) log.info("copying {} to {}".format(show["filename"], opfn)) shutil.copy2(show["filename"], opfn) if FUT.fileExists(opfn): cstat = os.stat(opfn) if cstat.st_size == tvhstat.st_size: log.info("copying {} took: {}".format( FUT.sizeof_fmt(cstat.st_size), NFO.hmsDisplay(int(time.time() - then)), )) log.info("show copied to {} OK.".format(opfn)) fhash, fsize = FUT.getFileHash(show["filename"]) log.info("deleting from tvheadend") TVH.deleteRecording(show["uuid"]) log.info("updating DB") db = tvheadend.tvhdb.TVHDb(dbfn) # sql = "insert into files (name,size,hash) values (" # sql += "'{}',{},'{}')".format(opfn, fsize, fhash) # db.doSql(sql) sql = "insert into files (name,size,hash) values (?, ?, ?)" db.doInsertSql(sql, ( opfn, fsize, fhash, )) # it is safe to run removeFromYear for all shows # as it tests whether this is a movie or not removeFromYear(show, config) # log.info("\n") # if show["channelname"].endswith("HD"): # log.info("Not converting HD programme {}".format(show["title"])) # else: # log.info("converting {} to mkv".format(show["title"])) # convertToMkv(opfn) # FFMPEG.convert(opfn) else: raise (CopyFailure("Failed to copy {} to {}".format( show["filename"], opfn))) except Exception as e: fname = sys._getframe().f_code.co_name errorExit(fname, e)
def moveShow(self, show): try: then = time.time() tvhstat = os.stat(show["filename"]) self.log.info("{}: {}".format(show["opbase"], FUT.sizeof_fmt(tvhstat.st_size))) if "year" in show: if show["title"].startswith("The "): letter = show["title"][4:5].upper() else: letter = show["title"][0:1].upper() opdir = "/".join([tvheadend.filmhome, letter, show["opbase"]]) snfo = NFO.makeFilmNfo(show) else: category = show["category"] if category == "drama": category = "Drama" opdir = "/".join( [tvheadend.videohome, category, show["title"]]) snfo = NFO.makeProgNfo(show) basefn = "/".join([opdir, show["opbase"]]) opfn = basefn + ".mpg" mkopfn = basefn + ".mkv" existingfile = None if FUT.fileExists(opfn): existingfile = opfn elif FUT.fileExists(mkopfn): existingfile = mkopfn if existingfile is not None: self.log.info( "kodi file already exists, not copying {}".format( existingfile)) self.log.info("deleting from tvheadend") TVH.deleteRecording(show["uuid"]) else: self.log.info("making directory {}".format(opdir)) FUT.makePath(opdir) nfofn = basefn + ".nfo" self.log.info("writing nfo to {}".format(nfofn)) with open(nfofn, "w") as nfn: nfn.write(snfo) self.log.info("copying {} to {}".format( show["filename"], opfn)) t = threading.Thread(target=copyFile, args=(show["filename"], opfn)) t.start() while t.is_alive(): Gtk.main_iteration() # time.sleep(1) # wait for thread to complete t.join() # shutil.copy2(show["filename"], opfn) if FUT.fileExists(opfn): cstat = os.stat(opfn) if cstat.st_size == tvhstat.st_size: self.log.info("copying {} took: {}".format( FUT.sizeof_fmt(cstat.st_size), NFO.hmsDisplay(int(time.time() - then)), )) self.log.info("show copied to {} OK.".format(opfn)) fhash, fsize = FUT.getFileHash(show["filename"]) self.log.info("deleting from tvheadend") TVH.deleteRecording(show["uuid"]) self.log.info("updating DB") db = tvheadend.tvhdb.TVHDb(tvheadend.dbfn) sql = "insert into files (name,size,hash) values (?, ?, ?)" return db.doInsertSql(sql, ( opfn, fsize, fhash, )) else: raise (CopyFailure("Failed to copy {} to {}".format( show["filename"], opfn))) return False except Exception as e: fname = sys._getframe().f_code.co_name errorExit(fname, e)