|
7 | 7 | # import sys |
8 | 8 | # sys.byteorder |
9 | 9 |
|
| 10 | + |
10 | 11 | def escape(txt): |
11 | 12 | """apply FFmpeg single quote escaping |
12 | 13 |
|
@@ -83,10 +84,25 @@ def unescape(txt): |
83 | 84 |
|
84 | 85 |
|
85 | 86 | def parse_stream_spec(spec, file_index=False): |
| 87 | + """Parse stream specifier string |
| 88 | +
|
| 89 | + :param spec: stream specifier string. If file_index=False and given an int |
| 90 | + value, it specifies the stream index. If file_index=True and given |
| 91 | + a 2-element sequence, it specifies the file index in spec[0] and |
| 92 | + stream index in spec[1]. |
| 93 | + :type spec: str or int or [int,int] |
| 94 | + :param file_index: True to expect spec to start with a file index, defaults to False |
| 95 | + :type file_index: bool, optional |
| 96 | + :return: stream spec dict |
| 97 | + :rtype: dict |
| 98 | +
|
| 99 | + The reverse of `stream_spec()` |
| 100 | + """ |
| 101 | + |
86 | 102 | if isinstance(spec, str): |
87 | 103 | out = {} |
88 | 104 | if file_index: |
89 | | - m = re.match(r"(\d+):") |
| 105 | + m = re.match(r"(\d+):", spec) |
90 | 106 | if m: |
91 | 107 | out["file_index"] = int(m[1]) |
92 | 108 | spec = spec[m.end() :] |
@@ -119,7 +135,27 @@ def parse_stream_spec(spec, file_index=False): |
119 | 135 | out["usable"] = True |
120 | 136 | return out |
121 | 137 | else: |
122 | | - return {"index": int(spec)} |
| 138 | + if file_index: |
| 139 | + return {"file_index": int(spec[0]), "index": int(spec[1])} |
| 140 | + else: |
| 141 | + return {"index": int(spec)} |
| 142 | + |
| 143 | + |
| 144 | +def is_stream_spec(spec, file_index=False): |
| 145 | + """True if valid stream specifier string |
| 146 | +
|
| 147 | + :param spec: stream specifier string to be tested |
| 148 | + :type spec: str |
| 149 | + :param file_index: True if spec starts with a file index, defaults to False |
| 150 | + :type file_index: bool, optional |
| 151 | + :return: True if valid stream specifier |
| 152 | + :rtype: bool |
| 153 | + """ |
| 154 | + try: |
| 155 | + parse_stream_spec(spec, file_index) |
| 156 | + return True |
| 157 | + except: |
| 158 | + return False |
123 | 159 |
|
124 | 160 |
|
125 | 161 | def stream_spec( |
@@ -262,7 +298,7 @@ def get_pixel_config(input_pix_fmt, pix_fmt=None): |
262 | 298 |
|
263 | 299 | if pix_fmt == input_pix_fmt: |
264 | 300 | n_out = n_in |
265 | | - elif n_in==1 and pix_fmt=='gray16le': |
| 301 | + elif n_in == 1 and pix_fmt == "gray16le": |
266 | 302 | # sub-16-bit pixel format, use the input format |
267 | 303 | pix_fmt = input_pix_fmt |
268 | 304 | n_out = n_in |
|
0 commit comments