Skip to content

Commit fb1cbca

Browse files
committed
fixed a bug in utils.parse_stream_spec()
- in the file_index=True logic path
1 parent 2523d33 commit fb1cbca

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

src/ffmpegio/utils/__init__.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# import sys
88
# sys.byteorder
99

10+
1011
def escape(txt):
1112
"""apply FFmpeg single quote escaping
1213
@@ -83,10 +84,25 @@ def unescape(txt):
8384

8485

8586
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+
86102
if isinstance(spec, str):
87103
out = {}
88104
if file_index:
89-
m = re.match(r"(\d+):")
105+
m = re.match(r"(\d+):", spec)
90106
if m:
91107
out["file_index"] = int(m[1])
92108
spec = spec[m.end() :]
@@ -119,7 +135,27 @@ def parse_stream_spec(spec, file_index=False):
119135
out["usable"] = True
120136
return out
121137
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
123159

124160

125161
def stream_spec(
@@ -262,7 +298,7 @@ def get_pixel_config(input_pix_fmt, pix_fmt=None):
262298

263299
if pix_fmt == input_pix_fmt:
264300
n_out = n_in
265-
elif n_in==1 and pix_fmt=='gray16le':
301+
elif n_in == 1 and pix_fmt == "gray16le":
266302
# sub-16-bit pixel format, use the input format
267303
pix_fmt = input_pix_fmt
268304
n_out = n_in

tests/test_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from ffmpegio import utils
33
import pytest
44

5+
56
def test_string_escaping():
67
raw = "Crime d'Amour"
78
esc = utils.escape(raw)
@@ -50,6 +51,9 @@ def test_parse_stream_spec():
5051
assert utils.parse_stream_spec("m:key:value") == {"tag": ("key", "value")}
5152
assert utils.parse_stream_spec("u") == {"usable": True}
5253

54+
assert utils.parse_stream_spec("0:1", True) == {"index": 1, "file_index": 0}
55+
assert utils.parse_stream_spec([0, 1], True) == {"index": 1, "file_index": 0}
56+
5357

5458
def test_stream_spec():
5559
assert utils.stream_spec() == ""
@@ -111,4 +115,3 @@ def test_get_audio_format():
111115

112116
if __name__ == "__main__":
113117
import re
114-

0 commit comments

Comments
 (0)