Skip to content

Commit

Permalink
support: Remove use of mediafile
Browse files Browse the repository at this point in the history
Relates to #2529
  • Loading branch information
postlund committed Nov 2, 2024
1 parent d2356c2 commit 056e394
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion base_versions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ chacha20poly1305-reuseable==0.13.2
ifaddr==0.1.7
mediafile==0.8.1
miniaudio==1.45
protobuf==5.28.0
protobuf==5.28.1
pydantic==1.10.10
requests==2.23.0
srptools==0.2.0
Expand Down
14 changes: 9 additions & 5 deletions pyatv/support/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import io
from typing import Union

from mediafile import MediaFile
from tinytag import TinyTag

from pyatv.interface import MediaMetadata

EMPTY_METADATA = MediaMetadata(None, None, None, None)


def _open_file(file: io.BufferedIOBase) -> MediaFile:
def _open_file(file: io.BufferedIOBase) -> TinyTag:
start_position = file.tell()
in_file = TinyTag.get(file_obj=file)
file.seek(start_position)
Expand All @@ -23,10 +22,15 @@ async def get_metadata(file: Union[str, io.BufferedIOBase]) -> MediaMetadata:
"""Extract metadata from a file and return it."""
loop = asyncio.get_event_loop()

if isinstance(file, io.BufferedIOBase):
tag = await loop.run_in_executor(None, _open_file, file)
else:
# TODO: TinyTag will always start by seeking to the end of a
# file, which isn't possible for streaming buffers. So this
# works as long as the entire file is in the buffer, otherwise
# it will fail. Hopefully this can be fixed by using mutagen
# directly, but will require some manual handling.
if isinstance(file, str):
tag = await loop.run_in_executor(None, TinyTag.get, file)
else:
tag = await loop.run_in_executor(None, _open_file, file)

return MediaMetadata(
title=tag.title,
Expand Down
21 changes: 14 additions & 7 deletions tests/support/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,27 @@
from tests.utils import data_path


@pytest.mark.parametrize(
"audio_file",
[(data_path("only_metadata.wav")), (Path(data_path("only_metadata.wav")))],
)
@pytest.mark.asyncio
async def test_get_metadata_from_file(audio_file):
metadata = await get_metadata(audio_file)
def assert_metadata(metadata: MediaMetadata) -> None:
assert metadata.artist == "postlund"
assert metadata.album == "raop"
assert metadata.title == "pyatv"
assert metadata.artwork is None
assert math.isclose(metadata.duration, 0.0)


@pytest.mark.asyncio
async def test_get_metadata_from_file():
with open(data_path("only_metadata.wav"), "rb") as fh:
metadata = await get_metadata(fh)
assert_metadata(metadata)


@pytest.mark.asyncio
async def test_get_metadata_from_buffer():
metadata = await get_metadata(data_path("only_metadata.wav"))
assert_metadata(metadata)


METADATA_FIELDS = list(MediaMetadata.__dataclass_fields__.keys())


Expand Down

0 comments on commit 056e394

Please sign in to comment.