diff --git a/ffmpeg/_build.py b/ffmpeg/_build.py index 3b6192c..8e2521c 100644 --- a/ffmpeg/_build.py +++ b/ffmpeg/_build.py @@ -17,45 +17,51 @@ parser.add_argument( help='The path to the ffmpeg execuatble') -VERSION_RE = re.compile(r' version (?P[^ ]+) ') +VERSION = dict( + RE=re.compile(r' version (?P[^ ]+) ')) -MUXER_RE = re.compile( - r'^ (?P[D ])(?P[E ]) ' - r'(?P[^ ]+) +(?P.+)$', - re.M) -MUXER_FLAGS = dict(demuxing='D', muxing='E') +MUXER = dict( + RE=re.compile( + r'^ (?P[D ])(?P[E ]) ' + r'(?P[^ ]+) +(?P.+)$', + re.M), + FLAGS=dict(demuxing='D', muxing='E')) -CODEC_RE = re.compile( - r'^ (?P[D.])(?P[E.])' - r'(?P[VAS.])(?P[I.])' - r'(?P[L.])(?P[S.]) ' - r'(?P[^ ]+) +(?P.+)$', - re.M) -CODEC_FLAGS = dict( - decoding='D', encoding='E', - stream=dict(video='V', audio='A', subtitle='S'), - intra_frame='I', lossy='L', lossless='S') -CODEC_DESCRIPTION_RE = re.compile( - r'^(?P.+?) \((de|en)coders: [^)]+ \)') -CODEC_CODERS_RE = re.compile( - r' \((?P(de|en)coders): (?P[^)]+) \)') +CODEC = dict( + RE=re.compile( + r'^ (?P[D.])(?P[E.])' + r'(?P[VAS.])(?P[I.])' + r'(?P[L.])(?P[S.]) ' + r'(?P[^ ]+) +(?P.+)$', + re.M), + FLAGS=dict( + decoding='D', encoding='E', + stream=dict(video='V', audio='A', subtitle='S'), + intra_frame='I', lossy='L', lossless='S'), + DESCRIPTION_RE=re.compile( + r'^(?P.+?) \((de|en)coders: [^)]+ \)'), + CODERS_RE=re.compile( + r' \((?P(de|en)coders): (?P[^)]+) \)')) -HWACCEL_SYNONYMS = dict(cuvid=['nvenc', 'nvdec', 'cuda']) +HWACCEL = dict( + SYNONYMS=dict(cuvid=['nvenc', 'nvdec', 'cuda'])) -FILTER_RE = re.compile( - r'^ (?P[T.])(?P[S.])(?P[C.]) ' - r'(?P[^ ]+) +(?P[^ ]+) +(?P.+)$', - re.M) -FILTER_FLAGS = dict(timeline='T', slice='S', command='C') +FILTER = dict( + RE=re.compile( + r'^ (?P[T.])(?P[S.])(?P[C.]) ' + r'(?P[^ ]+) +(?P[^ ]+) +(?P.+)$', + re.M), + FLAGS=dict(timeline='T', slice='S', command='C')) -PIX_FMT_RE = re.compile( - r'^(?P[I.])(?P[O.])(?P[H.])' - r'(?P[P.])(?P[B.]) ' - r'(?P[^ ]+) +(?P[0-9]+) +(?P[0-9]+)$', - re.M) -PIX_FMT_FLAGS = dict( - input='I', output='O', accelerated='H', palleted='P', bitstream='B') -PIX_FMT_INT_FIELDS = {'components', 'bits'} +PIX_FMT = dict( + RE=re.compile( + r'^(?P[I.])(?P[O.])(?P[H.])' + r'(?P[P.])(?P[B.]) ' + r'(?P[^ ]+) +(?P[0-9]+) +(?P[0-9]+)$', + re.M), + FLAGS=dict( + input='I', output='O', accelerated='H', palleted='P', bitstream='B'), + INT_FIELDS={'components', 'bits'}) def _run(args): @@ -107,7 +113,7 @@ def get_version(cmd='ffmpeg'): Extract the version of the ffmpeg build. """ stdout = _run([cmd, '-version']) - match = VERSION_RE.search(stdout.split('\n')[0]) + match = VERSION['RE'].search(stdout.split('\n')[0]) return match.group('version') @@ -116,7 +122,7 @@ def get_formats(cmd='ffmpeg'): Extract the formats of the ffmpeg build. """ stdout = _run([cmd, '-formats']) - return _get_line_fields(stdout, 4, MUXER_RE, MUXER_FLAGS) + return _get_line_fields(stdout, 4, MUXER['RE'], MUXER['FLAGS']) def get_demuxers(cmd='ffmpeg'): @@ -124,7 +130,7 @@ def get_demuxers(cmd='ffmpeg'): Extract the demuxers of the ffmpeg build. """ stdout = _run([cmd, '-demuxers']) - return _get_line_fields(stdout, 4, MUXER_RE, MUXER_FLAGS) + return _get_line_fields(stdout, 4, MUXER['RE'], MUXER['FLAGS']) def get_muxers(cmd='ffmpeg'): @@ -132,7 +138,7 @@ def get_muxers(cmd='ffmpeg'): Extract the muxers of the ffmpeg build. """ stdout = _run([cmd, '-muxers']) - return _get_line_fields(stdout, 4, MUXER_RE, MUXER_FLAGS) + return _get_line_fields(stdout, 4, MUXER['RE'], MUXER['FLAGS']) def get_codecs(cmd='ffmpeg'): @@ -140,13 +146,14 @@ def get_codecs(cmd='ffmpeg'): Extract the codecs of the ffmpeg build. """ stdout = _run([cmd, '-codecs']) - codecs = _get_line_fields(stdout, 10, CODEC_RE, CODEC_FLAGS) + codecs = _get_line_fields(stdout, 10, CODEC['RE'], CODEC['FLAGS']) for codec in codecs.values(): - for coders_match in CODEC_CODERS_RE.finditer(codec['description']): + for coders_match in CODEC['CODERS_RE'].finditer(codec['description']): coders = coders_match.group(3).split() if coders: codec[coders_match.group(1)] = coders - description_match = CODEC_DESCRIPTION_RE.search(codec['description']) + description_match = CODEC['DESCRIPTION_RE'].search( + codec['description']) if description_match is not None: codec['description'] = description_match.group('description') return codecs @@ -179,7 +186,7 @@ def get_filters(cmd='ffmpeg'): Extract the filters of the ffmpeg build. """ stdout = _run([cmd, '-filters']) - return _get_line_fields(stdout, 8, FILTER_RE, FILTER_FLAGS) + return _get_line_fields(stdout, 8, FILTER['RE'], FILTER['FLAGS']) def get_pix_fmts(cmd='ffmpeg'): @@ -188,7 +195,7 @@ def get_pix_fmts(cmd='ffmpeg'): """ stdout = _run([cmd, '-pix_fmts']) return _get_line_fields( - stdout, 8, PIX_FMT_RE, PIX_FMT_FLAGS, PIX_FMT_INT_FIELDS) + stdout, 8, PIX_FMT['RE'], PIX_FMT['FLAGS'], PIX_FMT['INT_FIELDS']) def get_sample_fmts(cmd='ffmpeg'): @@ -238,7 +245,7 @@ def get_devices(cmd='ffmpeg'): Extract the devices of the ffmpeg build. """ stdout = _run([cmd, '-devices']) - return _get_line_fields(stdout, 4, MUXER_RE, MUXER_FLAGS) + return _get_line_fields(stdout, 4, MUXER['RE'], MUXER['FLAGS']) def get_hw_devices(cmd='ffmpeg'): @@ -272,7 +279,7 @@ def get_hwaccels(cmd='ffmpeg'): for coder in codec.get(coders_key, []): for synonym in ( [hwaccel_name] + - HWACCEL_SYNONYMS.get(hwaccel_name, [])): + HWACCEL['SYNONYMS'].get(hwaccel_name, [])): if ( coder == synonym or '_' + synonym in coder or diff --git a/ffmpeg/_detect.py b/ffmpeg/_detect.py index e2e8316..84d048c 100644 --- a/ffmpeg/_detect.py +++ b/ffmpeg/_detect.py @@ -43,23 +43,25 @@ parser.add_argument( # Separators to divide a range of models within a line MODEL_RANGE_SEPARATORS = ['-', '>'] -# List `hwaccel` options by order of expected performance when available. -HWACCELS_BY_PERFORMANCE = [ - # NVidia - 'nvdec', 'cuvid', 'cuda', - # AMD - 'amf', - # Windows - 'qsv', 'd3d11va', 'dxva2', - # Linux - 'vaapi', 'vdpau', 'drm'] -HWACCEL_OUTPUT_FORMATS = { - 'nvdec': 'cuda', - 'vaapi': 'vaapi'} +HWACCEL = dict( + # List `hwaccel` options by order of expected performance when available. + BY_PERFORMANCE=[ + # NVidia + 'nvdec', 'cuvid', 'cuda', + # AMD + 'amf', + # Windows + 'qsv', 'd3d11va', 'dxva2', + # Linux + 'vaapi', 'vdpau', 'drm'], + OUTPUT_FORMATS={ + 'nvdec': 'cuda', + 'vaapi': 'vaapi'}) -GPU_PRODUCT_RE = re.compile(r'(?P[^[]+)(\[(?P[^]]+)\]|)') -GPU_WMI_PROPERTIES = collections.OrderedDict( - vendor='AdapterCompatibility', board='VideoProcessor') +GPU = dict( + PRODUCT_RE=re.compile(r'(?P[^[]+)(\[(?P[^]]+)\]|)'), + WMI_PROPERTIES=collections.OrderedDict( + vendor='AdapterCompatibility', board='VideoProcessor')) # Loaded from JSON DATA = None @@ -88,7 +90,7 @@ def detect_gpus(): # TODO get multiple GPUs from lshw gpus.append(gpu) - product_match = GPU_PRODUCT_RE.search(display_data['product']) + product_match = GPU['PRODUCT_RE'].search(display_data['product']) if product_match: gpu.update(**product_match.groupdict()) if not gpu['board']: @@ -98,7 +100,7 @@ def detect_gpus(): import wmi for controller in wmi.WMI().Win32_VideoController(): gpu = collections.OrderedDict() - for key, wmi_prop in GPU_WMI_PROPERTIES.items(): + for key, wmi_prop in GPU['WMI_PROPERTIES'].items(): value = controller.wmi_property(wmi_prop).value if value: gpu[key] = value @@ -192,11 +194,11 @@ def detect_hwaccels(hwaccels=None, cmd='ffmpeg'): hwaccels.sort(key=lambda hwaccel: ( # Sort unranked hwaccels last, but in the order given by ffmpeg - hwaccel['name'] in HWACCELS_BY_PERFORMANCE and 1 or 0, + hwaccel['name'] in HWACCEL['BY_PERFORMANCE'] and 1 or 0, ( # Sort ranked hwaccels per the constant - hwaccel['name'] in HWACCELS_BY_PERFORMANCE and - HWACCELS_BY_PERFORMANCE.index(hwaccel['name'])))) + hwaccel['name'] in HWACCEL['BY_PERFORMANCE'] and + HWACCEL['BY_PERFORMANCE'].index(hwaccel['name'])))) hwaccels_data['hwaccels'] = hwaccels return hwaccels_data @@ -230,9 +232,9 @@ def detect_codecs(decoder, encoder, hwaccels=None, cmd='ffmpeg'): hwaccel_kwargs = collections.OrderedDict( input=collections.OrderedDict(hwaccel=hwaccel['name']), output=collections.OrderedDict(codec=hwaccel_encoder)) - if hwaccel['name'] in HWACCEL_OUTPUT_FORMATS: + if hwaccel['name'] in HWACCEL['OUTPUT_FORMATS']: hwaccel_kwargs['input']['hwaccel_output_format'] = ( - HWACCEL_OUTPUT_FORMATS[hwaccel['name']]) + HWACCEL['OUTPUT_FORMATS'][hwaccel['name']]) codecs_kwargs.append(hwaccel_kwargs) for hwaccel_decoder in hwaccel['codecs'].get( decoder, {}).get('decoders', []):