Skip to content

Commit

Permalink
feat: attach codec used in the input file to the streamdetails
Browse files Browse the repository at this point in the history
  • Loading branch information
maximmaxim345 committed Jan 30, 2025
1 parent 6a49f0f commit 337e7bb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions music_assistant/controllers/player_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from music_assistant_models.enums import (
CacheCategory,
ConfigEntryType,
ContentType,
EventType,
MediaType,
PlayerState,
Expand Down Expand Up @@ -108,6 +109,7 @@ class CompareState(TypedDict):
elapsed_time: int
stream_title: str | None
content_type: str | None
codec_type: ContentType | None
output_formats: list[str] | None


Expand Down Expand Up @@ -992,6 +994,9 @@ def on_player_update(
content_type=queue.current_item.streamdetails.audio_format.output_format_str
if queue.current_item and queue.current_item.streamdetails
else None,
codec_type=queue.current_item.streamdetails.audio_format.codec_type
if queue.current_item and queue.current_item.streamdetails
else None,
output_formats=output_formats,
)
changed_keys = get_changed_keys(prev_state, new_state)
Expand Down
5 changes: 5 additions & 0 deletions music_assistant/helpers/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ async def get_media_stream(
bytes_sent += len(chunk)
continue

if chunk_number == 0:
# At this point ffmpeg has started and should now know the codec used
# for encoding the audio.
streamdetails.audio_format.codec_type = ffmpeg_proc.input_format.codec_type

chunk_number += 1
# determine buffer size dynamically
if chunk_number < 5 and strip_silence_begin:
Expand Down
9 changes: 7 additions & 2 deletions music_assistant/helpers/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(
self.log_history: deque[str] = deque(maxlen=100)
self._stdin_task: asyncio.Task | None = None
self._logger_task: asyncio.Task | None = None
self._input_codec_parsed = False
super().__init__(
ffmpeg_args,
stdin=True if isinstance(audio_input, str | AsyncGenerator) else audio_input,
Expand Down Expand Up @@ -117,15 +118,19 @@ async def _log_reader_task(self) -> None:

# if streamdetails contenttype is unknown, try parse it from the ffmpeg log
if line.startswith("Stream #") and ": Audio: " in line:
if self.input_format.content_type == ContentType.UNKNOWN:
if not self._input_codec_parsed:
content_type_raw = line.split(": Audio: ")[1].split(" ")[0]
content_type_raw = content_type_raw.split(",")[0]
content_type = ContentType.try_parse(content_type_raw)
self.logger.debug(
"Detected (input) content type: %s (%s)",
content_type,
content_type_raw,
)
self.input_format.content_type = content_type
if self.input_format.content_type == ContentType.UNKNOWN:
self.input_format.content_type = content_type
self.input_format.codec_type = content_type
self._input_codec_parsed = True
del line

async def _feed_stdin(self) -> None:
Expand Down

0 comments on commit 337e7bb

Please sign in to comment.