def _parse_input(self, line): ''' >>> p = FFmpegOutputParser() not matched: >>> p._parse_input('') is None True flv >>> p._parse_input("Input #0, flv, from '1.flv':") {'type': 'flv'} mp4 >>> p._parse_input("Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1.mp4':") {'type': 'mp4'} mpeg >>> p._parse_input("Input #0, mpeg, from '2.mpg':") {'type': 'mpeg'} unknown type >>> p._parse_input("Input #0, image2, from 'logo.png':") {'type': 'n/a'} ''' m = re.match('Input #0, (.*), from .*', line) if m is None: return None types = m.group(1) known_types = ['asf', 'avi', 'flv', 'mp4', 'mpeg', 'rm'] for t in known_types: if t in types: return Dict(type=t) else: return Dict(type='n/a')
def row2dict(row): if row is None: return None d = Dict() for c in row.__table__.columns: d[c.name] = getattr(row, c.name) return d
def _hook(d): o = Dict() for k, v in d.items(): if is_str and isinstance(v, unicode): v = str(v) o[str(k)] = v return o
def _parse_output(self, stdout): keyframes = [] for line in stdout.readlines(): line = line.rstrip('\r\n') ss = line.split(',') time = long(float(ss[4]) * 1000) bytes = long(ss[7]) keyframes.append(Dict(time=time, bytes=bytes)) return keyframes
def page(q, start, length, process_data=True): data = slice_query(q, start, length).all() count = q.count() d = Dict() if process_data: d.data = rs2dict(data) else: d.data = data d.count = count d.start = start d.len = length return d
def _validate(self, d): d = Dict(d) try: value = eval(self._expression, d) except KeyError: raise ArgMissingError(self._expression) except AttributeError: raise ArgMissingError(self._expression) except: raise ArgError( 'VALIDATION_FAILED', 'Failed to evaluate expression "%s".' % self._expression) self._validate_value(value)
def _call(self, api, **fields): resp_headers = Dict.get_and_del_from_dict(fields, 'resp_headers', {}) if api.startswith('public/'): # should use GET url = self.url + api + '?' + urllib.urlencode(fields) fields = None else: # should use POST url = self.url + api headers = self._get_token_header(api) resp = httputil.curl(url, fields, headers, use_json=True, resp_headers=resp_headers) self._check_error(resp) return resp
def _evaluate(self, vars): ''' >>> LogDecorator('aaa')._evaluate({}) (True, 'aaa') >>> LogDecorator('aaa', '1 == 2')._evaluate({}) (False, None) ''' vars = Dict(vars) vars.os = os condition = eval(self._condition, vars) if condition: return True, self._evaluate_message(vars) else: return False, None
def parse(self, output): lines = output.split('\n') meta = Dict(videoStream=None, audioStream=None) for line in lines: line = line.strip() input = self._parse_input(line) if input is not None: meta.update(input) duration = self._parse_duration(line, meta) if duration is not None: meta.update(duration) if meta.videoStream is None: meta.videoStream = self._parse_vs(line) if meta.audioStream is None: meta.audioStream = self._parse_as(line) return meta
def _load_ftp_sites(): types = set([ k.split('.')[2] for k in _props.keys() if k.startswith('distribution.ftp.') ]) sites = {} for type in types: site = Dict() site.host = _props.get('distribution.ftp.%s.host' % type) site.user = _props.get('distribution.ftp.%s.userName' % type) site.passwd = _props.get('distribution.ftp.%s.password' % type) site.root_dir = _props.get('distribution.ftp.%s.rootDir' % type) site.dest_root_dir = _props.get('distribution.ftp.%s.destRootDir' % type) sites[type] = site return sites
def list_processes(cmd, match_end=True): p = subprocess.Popen('ps ax -o pid,cmd|grep "%s"' % cmd, shell=True, stdout=subprocess.PIPE) stdout, stderr = p.communicate() #@UnusedVariable log.info(u'ps ax out: %s, err: %s' % (stdout, stderr)) processes = [] for line in stdout.splitlines(): pid, command = line.strip().split(' ', 1) if match_end: if command != cmd: continue else: if not command.startswith(cmd): continue processes.append(Dict(pid=int(pid), cmd=command)) p = subprocess.Popen('id', shell=True, stdout=subprocess.PIPE) stdout, stderr = p.communicate() log.info(u'id out: %s, err: %s' % (stdout, stderr)) return processes
def _parse_flvtool_output(output): in_bytes = False in_time = False bytes_list = [] time_list = [] for line in output.splitlines(): line = line.strip() if line == 'filepositions: {': in_bytes = True elif line == 'times: {': in_time = True elif line == '}': in_bytes = False in_time = False elif in_bytes: bytes_list.append(long(float(line))) elif in_time: time_list.append(long(float(line) * 1000)) return [ Dict(bytes=bytes_list[i], time=time_list[i]) for i in range(len(bytes_list)) ]
def _parse_response(lines): ''' >>> r = _parse_response(['exitcode:', '0', 'stdout:', 'output1\\noutput2', 'stderr:', 'error1\\nerror2']) >>> r.exitcode 0 >>> r.stdout 'output1\\noutput2' >>> r.stderr 'error1\\nerror2' >>> r = _parse_response(['exitcode:', '0', 'stdout:', 'stderr:']) >>> r.stdout '' >>> r.stderr '' ''' r = Dict() stdout_index = lines.index('stdout:') stderr_index = lines.index('stderr:') r.exitcode = int(lines[1]) r.stdout = '\n'.join(lines[stdout_index + 1:stderr_index]) r.stderr = '\n'.join(lines[stderr_index + 1:]) return r
def dict(self): return Dict([(k, v) for k, v in self.__dict__.items() if not k.startswith('_')])
def _parse_as(self, line): ''' >>> p = FFmpegOutputParser() not matched: >>> s = p._parse_as('') >>> s is None True normal situation: >>> s = p._parse_as('Stream #0.0: Audio: wmav2, 44100 Hz, 2 channels, s16, 64 kb/s') >>> s.type 'wmav2' >>> s.channels 2 >>> s.kbps 64 >>> s.sampleRate 44100 mono channel >>> s = p._parse_as('Stream #0.1: Audio: aac, 44100 Hz, mono, s16, 64 kb/s') >>> s.channels 1 2 channels >>> s = p._parse_as('Stream #0.1: Audio: aac, 44100 Hz, 2 channels (FC), s16, 32 kb/s') >>> s.channels 2 stereo channels >>> s = p._parse_as('Stream #0.0: Audio: wmav2, 44100 Hz, 2 channels, s16, 64 kb/s') >>> s.channels 2 5.1 channels >>> s = p._parse_as('Stream #0.1: Audio: ac3, 48000 Hz, 5.1, s16, 384 kb/s') >>> s.channels 6 channels with braces >>> s = p._parse_as('Stream #0.1: Audio: aac, 44100 Hz, 2 channels (FC), s16, 47 kb/s') >>> s.channels 2 no kbps >>> s = p._parse_as('Stream #0.1(eng): Audio: mp3, 11025 Hz, 1 channels, s16') >>> s.kbps 0 invalid audio stream caused by flvtool++ >>> s = p._parse_as('Stream #0.0: Audio: [0][0][0][0] / 0x0000, 0 channels') >>> s is None True default audio stream >>> s = p._parse_as('Stream #0.1(eng): Audio: dca (DTS), 48000 Hz, 5.1, s16, 1536 kb/s (default)') >>> s.kbps 1536 ''' m = re.match('Stream #.*?: Audio: (.+)', line) if m is None: return None fields = m.group(1) if '0 channels' in fields: # invalid metadata caused by flvtool++ return None fields = fields.split(', ') s = Dict() s.type = fields[0] s.sampleRate = int(fields[1][:-3]) # 44100 Hz if len(fields) == 5: end = fields[4].find(' kb/s') s.kbps = int(fields[4][:end]) else: s.kbps = 0 channels = fields[2] if channels == 'mono': s.channels = 1 elif channels == 'stereo': s.channels = 2 elif channels.find(' channel') != (-1): s.channels = int(channels[:1]) elif re.match('\\d\\.\\d', channels): s.channels = int(channels[:1]) + int(channels[2:3]) else: logging.warn( 'Failed to get audio channels from source video metadata.') raise TranscodeError( 'Failed to get audio channels from source video metadata.') return s
def _parse_vs(self, line): ''' >>> p = FFmpegOutputParser() not matched: >>> vs = p._parse_vs('') >>> vs is None True normal situation: >>> vs = p._parse_vs('Stream #0.1: Video: rv30, yuv420p, 200x164, 36 kb/s, 12.05 fps, 12 tbr, 1k tbn, 12 tbc') >>> vs.type 'rv30' >>> vs.width 200 >>> vs.height 164 >>> vs.kbps 36 >>> vs.frameRate == 12.05 True no frame rate >>> vs = p._parse_vs('Stream #0.1(chi): Video: wmv3, yuv420p, 320x240, 65 kb/s, 15 tbr, 1k tbn, 1k tbc') >>> vs.frameRate 0 video type has space and braces >>> vs = p._parse_vs('Stream #0.0(und): Video: h264 (Constrained Baseline), yuv420p, 480x320, 252 kb/s, 24.01 fps, 24 tbr, 30k tbn, 59.94 tbc') >>> vs.type 'h264' video type has 0x1 which may be mistaken as wi/hi >>> vs = p._parse_vs('Stream #0.0[0x1e0]: Video: mpeg1video, yuv420p, 352x288 [PAR 178:163 DAR 1958:1467], 1150 kb/s, 31.52 fps, 25 tbr, 90k tbn, 25 tbc') >>> vs.type 'mpeg1video' >>> vs.width 352 >>> vs.height 288 ''' m = re.match('Stream #.*?: Video: (?P<type>.+?), (?P<remain>.+)', line) if m is None: return None vs = Dict() # type may contain spaces and braces, e.g., "h264 (Constrained Baseline)" vs.type = m.group('type').split(' ')[0] remain = m.group('remain') m = re.search(', (\\d+)x(\\d+)[, ]', remain) vs.width = int(m.group(1)) vs.height = int(m.group(2)) m = re.search(', (\\d+) kb/s,', remain) vs.kbps = int(m.group(1)) if m else 0 m = re.search(', ([\\.\\d]+) fps,', remain) vs.frameRate = 0 if m is None else float(m.group(1)) return vs