Skip to content

Commit 24bb4d4

Browse files
committed
tests: adjusted to updated probe module
1 parent 28e83b2 commit 24bb4d4

4 files changed

Lines changed: 71 additions & 42 deletions

File tree

src/ffmpegio/probe.py

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from typing import BinaryIO, Any, TypeAlias, Literal
4+
from numbers import Number
45
from collections.abc import Sequence
56
import json, re
67
from fractions import Fraction
@@ -251,11 +252,11 @@ def full_details(
251252
show_streams: bool | None = True,
252253
show_programs: bool | None = False,
253254
show_chapters: bool | None = False,
254-
select_streams: bool | None = None,
255+
select_streams: str | int | None = None,
255256
keep_str_values: bool | None = False,
256257
cache_output: bool | None = False,
257258
sp_kwargs: dict[str, Any] | None = None,
258-
):
259+
) -> dict[str, str | Number | Fraction]:
259260
"""Retrieve full details of a media file or stream
260261
261262
:param url: URL of the media file/stream
@@ -268,8 +269,8 @@ def full_details(
268269
:type show_programs: bool, optional
269270
:param show_chapters: True to return chapter info, defaults to False
270271
:type show_chapters: bool, optional
271-
:param select_streams: Indices of streams to get info of, defaults to None
272-
:type select_streams: seq of int, optional
272+
:param select_streams: Stream specifier of the streams to get info of, defaults to None to retrieve all
273+
:type select_streams: str, int, optional
273274
:param keep_str_values: True to keep all field values as str,
274275
defaults to False to convert numeric values
275276
:type keep_str_values: bool, optional
@@ -329,13 +330,25 @@ def format_basic(
329330
keep_str_values: bool | None = False,
330331
cache_output: bool | None = False,
331332
sp_kwargs: dict[str, Any] | None = None,
332-
):
333+
) -> dict[str, str | Number | Fraction]:
333334
"""Retrieve basic media format info
334335
335336
:param url: URL of the media file/stream
336337
:type url: str or seekable file-like object or bytes-like object
337338
:param entries: specify to narrow which information entries to retrieve. Default to None, to return all entries
338339
:type entries: seq of str
340+
:param keep_optional_fields: True to return a missing optional field in the
341+
returned dict with None or "N/A" (if keep_str_values
342+
is True) as its value
343+
:type keep_optional_fields: bool, optional
344+
:param keep_str_values: True to keep all field values as str,
345+
defaults to False to convert numeric values
346+
:type keep_str_values: bool, optional
347+
:param cache_output: True to cache FFprobe output, defaults to False
348+
:type cache_output: bool, optional
349+
:param sp_kwargs: Additional keyword arguments for :py:func:`subprocess.run`,
350+
default to None
351+
:type sp_kwargs: dict[str, Any], optional
339352
:return: set of media format information.
340353
:rtype: dict
341354
@@ -380,13 +393,25 @@ def streams_basic(
380393
keep_str_values: bool | None = False,
381394
cache_output: bool | None = False,
382395
sp_kwargs: dict[str, Any] | None = None,
383-
):
396+
) -> dict[str, str | Number | Fraction]:
384397
"""Retrieve basic info of media streams
385398
386399
:param url: URL of the media file/stream
387400
:type url: str or seekable file-like object or bytes-like object
388401
:param entries: specify to narrow which stream entries to retrieve. Default to None, returning all entries
389402
:type entries: seq of str, optional
403+
:param keep_optional_fields: True to return a missing optional field in the
404+
returned dict with None or "N/A" (if keep_str_values
405+
is True) as its value
406+
:type keep_optional_fields: bool, optional
407+
:param keep_str_values: True to keep all field values as str,
408+
defaults to False to convert numeric values
409+
:type keep_str_values: bool, optional
410+
:param cache_output: True to cache FFprobe output, defaults to False
411+
:type cache_output: bool, optional
412+
:param sp_kwargs: Additional keyword arguments for :py:func:`subprocess.run`,
413+
default to None
414+
:type sp_kwargs: dict[str, Any], optional
390415
:return: List of media stream information.
391416
:rtype: list of dict
392417
@@ -423,7 +448,7 @@ def video_streams_basic(
423448
keep_str_values: bool | None = False,
424449
cache_output: bool | None = False,
425450
sp_kwargs: dict[str, Any] | None = None,
426-
):
451+
) -> dict[str, str | Number | Fraction]:
427452
"""Retrieve basic info of video streams
428453
429454
:param url: URL of the media file/stream
@@ -432,6 +457,18 @@ def video_streams_basic(
432457
:type index: int, optional
433458
:param entries: specify to narrow which information entries to retrieve. Default to None, to return all entries
434459
:type entries: seq of str
460+
:param keep_optional_fields: True to return a missing optional field in the
461+
returned dict with None or "N/A" (if keep_str_values
462+
is True) as its value
463+
:type keep_optional_fields: bool, optional
464+
:param keep_str_values: True to keep all field values as str,
465+
defaults to False to convert numeric values
466+
:type keep_str_values: bool, optional
467+
:param cache_output: True to cache FFprobe output, defaults to False
468+
:type cache_output: bool, optional
469+
:param sp_kwargs: Additional keyword arguments for :py:func:`subprocess.run`,
470+
default to None
471+
:type sp_kwargs: dict[str, Any], optional
435472
:return: List of video stream information.
436473
:rtype: list of dict
437474
@@ -528,7 +565,7 @@ def audio_streams_basic(
528565
keep_str_values: bool | None = False,
529566
cache_output: bool | None = False,
530567
sp_kwargs: dict[str, Any] | None = None,
531-
):
568+
) -> dict[str, str | Number | Fraction]:
532569
"""Retrieve basic info of audio streams
533570
534571
:param url: URL of the media file/stream
@@ -537,6 +574,18 @@ def audio_streams_basic(
537574
:type index: int, optional
538575
:param entries: specify to narrow which information entries to retrieve. Default to None, to return all entries
539576
:type entries: seq of str
577+
:param keep_optional_fields: True to return a missing optional field in the
578+
returned dict with None or "N/A" (if keep_str_values
579+
is True) as its value
580+
:type keep_optional_fields: bool, optional
581+
:param keep_str_values: True to keep all field values as str,
582+
defaults to False to convert numeric values
583+
:type keep_str_values: bool, optional
584+
:param cache_output: True to cache FFprobe output, defaults to False
585+
:type cache_output: bool, optional
586+
:param sp_kwargs: Additional keyword arguments for :py:func:`subprocess.run`,
587+
default to None
588+
:type sp_kwargs: dict[str, Any], optional
540589
:return: List of audio stream information.
541590
:rtype: list of dict
542591
@@ -622,14 +671,14 @@ def query(
622671
keep_str_values: bool | None = False,
623672
cache_output: bool | None = False,
624673
sp_kwargs: dict[str, Any] | None = None,
625-
):
674+
) -> dict[str, Any] | Sequence[dict[str, Any]]:
626675
"""Query specific fields of media format or stream
627676
628677
:param url: URL of the media file/stream
629678
:type url: str or seekable file-like object or bytes-like object
630679
:param streams: stream specifier, defaults to None to get format
631680
:type streams: str, int, bool, optional
632-
:param fields: info, defaults to None
681+
:param fields: list of format/stream fields to retrieve, defaults to None (all fields)
633682
:type fields: sequence of str, optional
634683
:param keep_optional_fields: True to return a missing optional field in the
635684
returned dict with None or "N/A" (if keep_str_values
@@ -755,24 +804,6 @@ def frames(
755804
if entries is str (i.e., a single entry)
756805
:rtype: list[dict] or list[str|int|float]
757806
758-
``intervals`` argument
759-
----------------------
760-
761-
intervals argument can be specified in multiple ways to form the ``-read_intervals`` ffprobe option:
762-
763-
1) ``str`` - pass through the argument as-is to ffprobe
764-
2) ``int`` - read this numbers of packets to read from the beginning of the file
765-
3) ``float`` - read packets over this duration in seconds from the beginning of the file
766-
4) ``seq[str|float, str|int|float]`` - sets start and end points
767-
- start: str = as-is, float=starting time in seconds
768-
- end: str = as-is, int=offset in # of packets, float=offset in seconds
769-
5) ``dict`` - specifies start and end points with the following keys:
770-
- 'start' - (str|float) start time
771-
- 'start_offset' - (str|float) start time offset from the previous read. Ignored if 'start' is present.
772-
- 'end' - (str|float) end time
773-
- 'end_offset' - (str|float|int) end time offset from the start time. Ignored if 'end' is present.
774-
6) - ``seq[dict]`` - specify multiple intervals
775-
776807
"""
777808

778809
is_single = isinstance(entries, str)

tests/test_audio.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ def test_read_write():
7171
url = "tests/assets/testaudio-1m.mp3"
7272
outext = ".flac"
7373

74-
info = probe.audio_streams_basic(
75-
url, index=0, entries=("sample_rate", "sample_fmt", "channels")
76-
)[0]
77-
7874
fs, x = audio.read(url)
7975

8076
with tempfile.TemporaryDirectory() as tmpdirname:

tests/test_ffmpegprocess.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ def test_popen():
9191

9292
def test_popen_progress():
9393
url = "tests/assets/testvideo-1m.mp4"
94-
info = probe.video_streams_basic(url, 0)[0]
94+
info = probe.video_streams_basic(
95+
url, 0, ["pix_fmt", "width", "height", "frame_rate"]
96+
)
9597
pix_fmt_in = info["pix_fmt"]
9698
s_in = (info["width"], info["height"])
9799
r_in = info["frame_rate"]

tests/test_probe.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ def test_query():
5656
)
5757

5858
assert all(
59-
st["bad_filed"] is None
59+
st["max_bit_rate"] is None
6060
for st in probe.query(
61-
url, "a", fields=("duration", "bad_filed"), return_none=True
61+
url, "a", fields=("duration", "max_bit_rate"), keep_optional_fields=True
6262
)
6363
)
6464

6565
assert (
66-
probe.query(url, "v:0", fields=("duration", "bad_filed"), return_none=True)[
67-
"bad_filed"
68-
]
66+
probe.query(
67+
url, "v:0", fields=("duration", "max_bit_rate"), keep_optional_fields=True
68+
)["max_bit_rate"]
6969
is None
7070
)
7171

@@ -79,7 +79,7 @@ def test_frames():
7979
info = probe.frames(
8080
url,
8181
streams="a:0",
82-
intervals=10
82+
intervals=10,
8383
# intervals='%+#20,30%+#15'
8484
# intervals=[{"end_offset": 20}, {"start": 30, "end_offset": 12}],
8585
)
@@ -89,7 +89,7 @@ def test_frames():
8989
url,
9090
"key_frame",
9191
"a:0",
92-
intervals=10
92+
intervals=10,
9393
# intervals='%+#20,30%+#15'
9494
# intervals=[{"end_offset": 20}, {"start": 30, "end_offset": 12}],
9595
)
@@ -99,7 +99,7 @@ def test_frames():
9999
"key_frame",
100100
"a:0",
101101
intervals=10,
102-
accurate_time=True
102+
accurate_time=True,
103103
# intervals='%+#20,30%+#15'
104104
# intervals=[{"end_offset": 20}, {"start": 30, "end_offset": 12}],
105105
)
@@ -110,7 +110,7 @@ def test_frames():
110110
url,
111111
streams="a:0",
112112
intervals=(23.4, 1),
113-
accurate_time=True
113+
accurate_time=True,
114114
# intervals='%+#20,30%+#15'
115115
# intervals=[{"end_offset": 20}, {"start": 30, "end_offset": 12}],
116116
)

0 commit comments

Comments
 (0)