def connect_with_vfr(self,avs,label=None): """ Connects templates.py with vfr.py, enabling its use outside of vfr.py. Uses the same quirks as AMkvC but only for 24 and 30 fps. Ex: inputfps=30 is understood as being '30*1000/1001' """ from vfr import parse_trims, fmt_time # compensate for amkvc's fps assumption if self.fps in ('24','30'): fps = self.fps + '/1.001' else: self.fps = str(fps) if self.ofps and self.ofps in ('24','30'): ofps = self.ofps + '/1.001' else: ofps = str(self.ofps) Trims2, Trims2ts = parse_trims(avs, fps, ofps, label=label)[2:4] Trims2ts = [(fmt_time(i[0]),fmt_time(i[1]) if i[1] != 0 else None) for i in Trims2ts] self.trims = Trims2ts self.kframes = Trims2
def connect_with_vfr(self, avs, label=None, clip=None): """ Connects templates.py with vfr.py, enabling its use outside of vfr.py. Uses the same quirks as AMkvC but only for 24 and 30 fps. Ex: inputfps=30 is understood as being '30*1000/1001' """ from vfr import parse_trims, fmt_time # compensate for amkvc's fps assumption if self.fps in ('24', '30'): fps = self.fps + '/1.001' else: fps = str(self.fps) if self.ofps and self.ofps in ('24', '30'): ofps = self.ofps + '/1.001' else: ofps = str(self.ofps) Trims2, Trims2ts = parse_trims(avs, fps, ofps, label=label, clip=clip)[2:4] Trims2ts = [(fmt_time(i[0]), fmt_time(i[1]) if i[1] != 0 else None) for i in Trims2ts] self.trims = Trims2ts self.kframes = Trims2
def __init__(self, templatefile, output=None, avs=None, trims=None, kframes=None, uid=None, label=None, ifps=None, clip=None, idr=False): try: import configparser except ImportError: import ConfigParser as configparser from io import open # Init config config = configparser.ConfigParser() template = open(templatefile, encoding='utf-8') # Read template config.readfp(template) template.close() # Template defaults self = self.Template() self.editions = [] self.uid = uid if uid else self.uid # Set mkvinfo path from vfr import mkvmerge, parse_with_mkvmerge, fmt_time from os.path import dirname, join, isfile # Set placeholder for mkvinfo output mkv_globbed = False mkvinfo = {} for k, v in config.items('info'): if k == 'lang': self.lang = v.split(',') elif k == 'country': self.country = v.split(',') elif k == 'inputfps': self.fps = v elif k == 'outputfps': self.ofps = v elif k == 'createqpfile': self.qpf = v elif k == 'uid': self.uid = int(v) elif k == 'editions': self.num_editions = int(v) if avs and not ifps: self.connect_with_vfr(avs, label, clip) elif trims: self.trims = trims self.kframes = kframes else: self.trims = False self.idr = idr for i in range(self.num_editions): from re import compile ed = self.Edition() ed.uid = self.uid * 100 self.uid += 1 cuid = ed.uid ed.num = i + 1 ed.chapters = [] stuff = {} for k, v in config.items('edition{0:d}'.format(ed.num)): if k == 'default': ed.default = int(v) elif k == 'name': ed.name = v.split(',') elif k == 'ordered': ed.ordered = int(v) elif k == 'hidden': ed.hidden = int(v) elif k == 'chapters': ed.num_chapters = int(v) for i in range(ed.num_chapters): stuff[i + 1] = [] elif k == 'uid': ed.uid = int(v) else: opt_re = compile('(\d+)(\w+)') ret = opt_re.search(k) if ret: stuff[int(ret.group(1))].append((ret.group(2), v)) for j in range(ed.num_chapters): ch = self.Chapter() cuid += 1 ch.uid = cuid ch.num = j + 1 for k, v in stuff[j + 1]: if k == 'name': ch.name = v.split(',') elif k == 'chapter': ch.chapter = int(v) elif k == 'start': ch.start = v elif k == 'end': ch.end = v elif k == 'suid': ch.suid = v.strip() if ret else 0 elif k == 'hidden': ch.hidden = int(v) elif k == 'enabled': ch.enabled = int(v) if ch.suid and not isfile(ch.suid): ch.suid = ch.suid.replace('0x', '').lower().replace(' ', '') if ch.chapter and not (ch.start and ch.end): ch.start, ch.end = self.trims[ch.chapter - 1] if self.trims else ( ch.start, ch.end) elif ch.suid: mkvfiles = [] if isfile(ch.suid): mkvfiles = [ch.suid] elif not mkv_globbed: from glob import glob mkvfiles = glob('*.mkv') + glob( join(dirname(avs), '*.mkv')) mkv_globbed = True if mkvfiles: if parse_with_mkvmerge: from subprocess import check_output import json for file in mkvfiles: info = check_output([ mkvmerge, '-i', '-F', 'json', '--output-charset', 'utf-8', file ]).decode('utf-8') try: props = json.loads(info).get( "container", {}).get("properties", {}) ch.suid = props.get("segment_uid", 0) duration = props.get("duration", 0) mkvinfo[ch.suid] = { 'file': file, 'duration': fmt_time(duration * 10**6) if duration else 0 } except Exception: pass else: for file in mkvfiles: ch.suid, duration = self.parse_mkv(file) mkvinfo[ch.suid] = { 'file': file, 'duration': fmt_time(duration * 10**6) if duration else 0 } if not (ch.start or ch.end): ch.start = fmt_time(0) if not ch.start else ch.start ch.end = mkvinfo[ ch.suid]['duration'] if not ch.end and ( ch.suid in mkvinfo) else ch.end ed.chapters.append(ch) self.editions.append(ed) if output: self.toxml(output)
def __init__(self, templatefile, output=None, avs=None, trims=None, kframes=None, uid=None, label=None, ifps=None, clip=None, idr=False): try: import configparser except ImportError: import ConfigParser as configparser from io import open # Init config config = configparser.ConfigParser() template = open(templatefile, encoding='utf-8') # Read template config.readfp(template) template.close() # Template defaults self = self.Template() self.editions = [] self.uid = uid if uid else self.uid # Set mkvinfo path from vfr import mkvmerge, parse_with_mkvinfo, fmt_time from os.path import dirname, join, isfile if parse_with_mkvinfo: mkvinfo_path = join(dirname(mkvmerge), 'mkvinfo') if not isfile(mkvinfo_path) and not isfile(mkvinfo_path + '.exe'): import os from subprocess import check_call, CalledProcessError which = 'where' if os.name == 'nt' else 'which' try: check_call([which, 'mkvinfo']) except CalledProcessError: parse_with_mkvinfo = False # Set placeholder for mkvinfo output mkv_globbed = False mkvinfo = {} for k, v in config.items('info'): if k == 'lang': self.lang = v.split(',') elif k == 'country': self.country = v.split(',') elif k == 'inputfps': self.fps = v elif k == 'outputfps': self.ofps = v elif k == 'createqpfile': self.qpf = v elif k == 'uid': self.uid = int(v) elif k == 'editions': self.num_editions = int(v) if avs and not ifps: self.connect_with_vfr(avs, label, clip) elif trims: self.trims = trims self.kframes = kframes else: self.trims = False self.idr = idr for i in range(self.num_editions): from re import compile ed = self.Edition() ed.uid = self.uid * 100 self.uid += 1 cuid = ed.uid ed.num = i+1 ed.chapters = [] stuff = {} for k, v in config.items('edition{0:d}'.format(ed.num)): if k == 'default': ed.default = int(v) elif k == 'name': ed.name = v.split(',') elif k == 'ordered': ed.ordered = int(v) elif k == 'hidden': ed.hidden = int(v) elif k == 'chapters': ed.num_chapters = int(v) for i in range(ed.num_chapters): stuff[i+1] = [] elif k == 'uid': ed.uid = int(v) else: opt_re = compile('(\d+)(\w+)') ret = opt_re.search(k) if ret: stuff[int(ret.group(1))].append((ret.group(2),v)) for j in range(ed.num_chapters): ch = self.Chapter() cuid += 1 ch.uid = cuid ch.num = j+1 for k, v in stuff[j+1]: if k == 'name': ch.name = v.split(',') elif k == 'chapter': ch.chapter = int(v) elif k == 'start': ch.start = v elif k == 'end': ch.end = v elif k == 'suid': ch.suid = v.strip() if ret else 0 elif k == 'hidden': ch.hidden = int(v) elif k == 'enabled': ch.enabled = int(v) if ch.suid and not isfile(ch.suid): ch.suid = ch.suid.replace('0x','').lower().replace(' ','') if ch.chapter and not (ch.start and ch.end): ch.start, ch.end = self.trims[ch.chapter-1] if self.trims else (ch.start, ch.end) elif ch.suid: mkvfiles = [] if isfile(ch.suid): mkvfiles = [ch.suid] elif not mkv_globbed: from glob import glob mkvfiles = glob('*.mkv') + glob(join(dirname(avs),'*.mkv')) mkv_globbed = True if mkvfiles: if parse_with_mkvinfo: from subprocess import check_output suid_re = compile('^\| \+ Segment UID:(.*)(?m)') duration_re = compile('^\| \+ Duration: \d+\.\d*s \((\d+:\d+:\d+.\d+)\)(?m)') for file in mkvfiles: info = check_output([mkvinfo_path, '--ui-language', 'en', '--output-charset', 'utf-8', file]).decode('utf-8') ret = suid_re.search(info) ch.suid = ret.group(1).lower().strip().replace('0x','').replace(' ','') if ret else 0 ret = duration_re.search(info) duration = ret.group(1) if ret else 0 mkvinfo[ch.suid] = {'file': file, 'duration': duration} else: for file in mkvfiles: ch.suid, duration = self.parse_mkv(file) mkvinfo[ch.suid] = {'file': file, 'duration': fmt_time(duration * 10**6) if duration else 0} if not (ch.start or ch.end): ch.start = fmt_time(0) if not ch.start else ch.start ch.end = mkvinfo[ch.suid]['duration'] if not ch.end and (ch.suid in mkvinfo) else ch.end ed.chapters.append(ch) self.editions.append(ed) if output: self.toxml(output)