Skip to content

Commit 77488e0

Browse files
committed
probe: fixed cache_output=True operation
1 parent 4d2cc6b commit 77488e0

1 file changed

Lines changed: 30 additions & 14 deletions

File tree

src/ffmpegio/probe.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,14 @@ def _add_select_streams(args, stream_specifier):
5252
return args
5353

5454

55-
def _add_show_entries(args, entries: dict[str, bool | Sequence[str]]):
55+
def _compose_entries(entries: dict[str, bool | Sequence[str]]) -> str:
5656
arg = []
5757
for key, val in entries.items():
5858
if isinstance(val, Sequence):
5959
arg.append(f"{key}={','.join(val)}")
6060
elif val is not False:
6161
arg.append(key)
62-
63-
args.append("-show_entries")
64-
args.append(":".join(arg))
65-
return args
62+
return ":".join(arg)
6663

6764

6865
IntervalSpec: TypeAlias = (
@@ -164,22 +161,22 @@ def compose_dict(intv):
164161

165162
def _exec(
166163
url: str | BinaryIO | memoryview,
167-
entries: dict[str, bool | Sequence[str]],
164+
entries: str,
165+
sp_kwargs: tuple[tuple[str, Any]] | None = None,
168166
streams: str | int | None = None,
169167
intervals: IntervalSpec | Sequence[IntervalSpec] | None = None,
170168
count_frames: bool | None = False,
171169
count_packets: bool | None = False,
172170
keep_optional_fields: bool | None = None,
173-
sp_kwargs: dict[str, Any] | None = None,
174171
) -> dict[str, str]:
175172
"""execute ffprobe and return stdout as dict"""
176173

177174
sp_opts = {"stdout": PIPE, "stderr": PIPE}
178175

179176
if sp_kwargs is not None:
180-
sp_opts = {**sp_kwargs, **sp_opts}
177+
sp_opts = {**dict(sp_kwargs), **sp_opts}
181178

182-
args = ["-hide_banner", "-of", "json"]
179+
args = ["-hide_banner", "-of", "json", "-show_entries", entries]
183180

184181
if streams is not None:
185182
_add_select_streams(args, streams)
@@ -195,8 +192,6 @@ def _exec(
195192
args.append("-count_packets")
196193
# returns "nb_read_packets" item in each stream
197194

198-
_add_show_entries(args, entries)
199-
200195
if keep_optional_fields is not None:
201196
args.extend(
202197
["-show_optional_fields", "always" if keep_optional_fields else "never"]
@@ -232,9 +227,24 @@ def _exec_cached(*args, **kwargs) -> dict[str, str]:
232227
return _exec(*args, **kwargs)
233228

234229

235-
def _run(*args, cache_output: bool | None = False, **kwargs) -> dict[str, str]:
230+
def _run(
231+
url: str | BinaryIO | memoryview,
232+
entries: dict[str, bool | Sequence[str]],
233+
*args,
234+
cache_output: bool | None = False,
235+
sp_kwargs: dict[str, Any] | None = None,
236+
**kwargs,
237+
) -> dict[str, str]:
236238
"""execute ffprobe, return stdout as dict, and cache its output"""
237-
return _exec_cached(*args, **kwargs) if cache_output else _exec(*args, **kwargs)
239+
240+
entries = _compose_entries(entries)
241+
if sp_kwargs is not None:
242+
sp_kwargs = tuple(sp_kwargs.items())
243+
return (
244+
_exec_cached(url, entries, sp_kwargs, *args, **kwargs)
245+
if cache_output
246+
else _exec(url, entries, sp_kwargs, *args, **kwargs)
247+
)
238248

239249

240250
def full_details(
@@ -735,7 +745,13 @@ def frames(
735745
if accurate_time and has_time:
736746
entries["stream"] = ["index", "time_base"]
737747

738-
res = _exec(url, entries, streams=streams, intervals=intervals, sp_kwargs=sp_kwargs)
748+
res = _exec(
749+
url,
750+
_compose_entries(entries),
751+
sp_kwargs and tuple(sp_kwargs.items()),
752+
streams=streams,
753+
intervals=intervals,
754+
)
739755

740756
out = [_items_to_numeric(d) for d in res["frames"]]
741757

0 commit comments

Comments
 (0)