Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements for filesystem provider #1844

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions music_assistant/controllers/players.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
CONF_TTS_PRE_ANNOUNCE,
)
from music_assistant.helpers.api import api_command
from music_assistant.helpers.tags import parse_tags
from music_assistant.helpers.tags import async_parse_tags
from music_assistant.helpers.throttle_retry import Throttler
from music_assistant.helpers.uri import parse_uri
from music_assistant.helpers.util import TaskManager, get_changed_values
Expand Down Expand Up @@ -1309,7 +1309,7 @@ async def _play_announcement(
await self.wait_for_state(player, PlayerState.PLAYING, 10, minimal_time=0.1)
# wait for the player to stop playing
if not announcement.duration:
media_info = await parse_tags(announcement.custom_data["url"])
media_info = await async_parse_tags(announcement.custom_data["url"])
announcement.duration = media_info.duration or 60
media_info.duration += 2
await self.wait_for_state(
Expand Down
18 changes: 12 additions & 6 deletions music_assistant/helpers/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import logging
import os
import subprocess
from collections.abc import Iterable
from dataclasses import dataclass
from json import JSONDecodeError
Expand Down Expand Up @@ -380,9 +381,14 @@ def get(self, key: str, default=None) -> Any:
return self.tags.get(key, default)


async def parse_tags(input_file: str, file_size: int | None = None) -> AudioTags:
async def async_parse_tags(input_file: str, file_size: int | None = None) -> AudioTags:
"""Parse tags from a media file (or URL). Async friendly."""
return await asyncio.to_thread(parse_tags, input_file, file_size)


def parse_tags(input_file: str, file_size: int | None = None) -> AudioTags:
"""
Parse tags from a media file (or URL).
Parse tags from a media file (or URL). NOT Async friendly.

Input_file may be a (local) filename or URL accessible by ffmpeg.
"""
Expand All @@ -402,9 +408,8 @@ async def parse_tags(input_file: str, file_size: int | None = None) -> AudioTags
"-i",
input_file,
)
async with AsyncProcess(args, stdin=False, stdout=True) as ffmpeg:
res = await ffmpeg.read(-1)
try:
res = subprocess.check_output(args) # noqa: S603
data = json.loads(res)
if error := data.get("error"):
raise InvalidDataError(error["string"])
Expand All @@ -424,12 +429,13 @@ async def parse_tags(input_file: str, file_size: int | None = None) -> AudioTags
not input_file.startswith("http")
and input_file.endswith(".mp3")
and "musicbrainzrecordingid" not in tags.tags
and await asyncio.to_thread(os.path.isfile, input_file)
and os.path.isfile(input_file)
):
# eyed3 is able to extract the musicbrainzrecordingid from the unique file id
# this is actually a bug in ffmpeg/ffprobe which does not expose this tag
# so we use this as alternative approach for mp3 files
audiofile = await asyncio.to_thread(eyed3.load, input_file)
# TODO: Convert all the tag reading to Mutagen!
audiofile = eyed3.load(input_file)
if audiofile is not None and audiofile.tag is not None:
for uf_id in audiofile.tag.unique_file_ids:
if uf_id.owner_id == b"http://musicbrainz.org" and uf_id.uniq_id:
Expand Down
4 changes: 2 additions & 2 deletions music_assistant/providers/builtin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from music_assistant_models.streamdetails import StreamDetails

from music_assistant.constants import MASS_LOGO, VARIOUS_ARTISTS_FANART
from music_assistant.helpers.tags import AudioTags, parse_tags
from music_assistant.helpers.tags import AudioTags, async_parse_tags
from music_assistant.helpers.uri import parse_uri
from music_assistant.models.music_provider import MusicProvider

Expand Down Expand Up @@ -533,7 +533,7 @@ async def _get_media_info(self, url: str, force_refresh: bool = False) -> AudioT
if cached_info and not force_refresh:
return AudioTags.parse(cached_info)
# parse info with ffprobe (and store in cache)
media_info = await parse_tags(url)
media_info = await async_parse_tags(url)
if "authSig" in url:
media_info.has_cover_image = False
await self.mass.cache.set(
Expand Down
Loading
Loading