Shiina-Mahiru commited on
Commit
f0f994f
·
verified ·
1 Parent(s): 2b4656c

Upload ffmpeg\_utils.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. ffmpeg//_utils.py +97 -0
ffmpeg//_utils.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import unicode_literals
2
+ from builtins import str
3
+ from past.builtins import basestring
4
+ import hashlib
5
+ import sys
6
+
7
+ if sys.version_info.major == 2:
8
+ # noinspection PyUnresolvedReferences,PyShadowingBuiltins
9
+ str = str
10
+
11
+
12
+ # `past.builtins.basestring` module can't be imported on Python3 in some environments (Ubuntu).
13
+ # This code is copy-pasted from it to avoid crashes.
14
+ class BaseBaseString(type):
15
+ def __instancecheck__(cls, instance):
16
+ return isinstance(instance, (bytes, str))
17
+
18
+ def __subclasshook__(cls, thing):
19
+ # TODO: What should go here?
20
+ raise NotImplemented
21
+
22
+
23
+ def with_metaclass(meta, *bases):
24
+ class metaclass(meta):
25
+ __call__ = type.__call__
26
+ __init__ = type.__init__
27
+
28
+ def __new__(cls, name, this_bases, d):
29
+ if this_bases is None:
30
+ return type.__new__(cls, name, (), d)
31
+ return meta(name, bases, d)
32
+
33
+ return metaclass('temporary_class', None, {})
34
+
35
+
36
+ if sys.version_info.major >= 3:
37
+
38
+ class basestring(with_metaclass(BaseBaseString)):
39
+ pass
40
+
41
+
42
+ else:
43
+ # noinspection PyUnresolvedReferences,PyCompatibility
44
+ from builtins import basestring
45
+
46
+
47
+ def _recursive_repr(item):
48
+ """Hack around python `repr` to deterministically represent dictionaries.
49
+
50
+ This is able to represent more things than json.dumps, since it does not require things to be JSON serializable
51
+ (e.g. datetimes).
52
+ """
53
+ if isinstance(item, basestring):
54
+ result = str(item)
55
+ elif isinstance(item, list):
56
+ result = '[{}]'.format(', '.join([_recursive_repr(x) for x in item]))
57
+ elif isinstance(item, dict):
58
+ kv_pairs = [
59
+ '{}: {}'.format(_recursive_repr(k), _recursive_repr(item[k]))
60
+ for k in sorted(item)
61
+ ]
62
+ result = '{' + ', '.join(kv_pairs) + '}'
63
+ else:
64
+ result = repr(item)
65
+ return result
66
+
67
+
68
+ def get_hash(item):
69
+ repr_ = _recursive_repr(item).encode('utf-8')
70
+ return hashlib.md5(repr_).hexdigest()
71
+
72
+
73
+ def get_hash_int(item):
74
+ return int(get_hash(item), base=16)
75
+
76
+
77
+ def escape_chars(text, chars):
78
+ """Helper function to escape uncomfortable characters."""
79
+ text = str(text)
80
+ chars = list(set(chars))
81
+ if '\\' in chars:
82
+ chars.remove('\\')
83
+ chars.insert(0, '\\')
84
+ for ch in chars:
85
+ text = text.replace(ch, '\\' + ch)
86
+ return text
87
+
88
+
89
+ def convert_kwargs_to_cmd_line_args(kwargs):
90
+ """Helper function to build command line arguments out of dict."""
91
+ args = []
92
+ for k in sorted(kwargs.keys()):
93
+ v = kwargs[k]
94
+ args.append('-{}'.format(k))
95
+ if v is not None:
96
+ args.append('{}'.format(v))
97
+ return args