core: Jellyfin Media Source generating audio URLs that result in 500 errors and no audio

The problem

Using Jellyfin as a media source doesn’t play properly on media players. It does not seem to account for supported codecs of the target media player, causing a 500 response from Jellyfin when the audio is requested by the media player and no playable audio.

When it requests a media URL, it does not supply any codec parameter. This results in an URL that may have to transcode the audio but use of the copy audio codec (default in Jellyfin maybe?) that may not work with the transcode target container. E.g. FLAC -> MP3 using the copy acodec.

What version of Home Assistant Core has the issue?

core-2023.5.0b8

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Jellyfin

Link to integration documentation on our website

https://www.home-assistant.io/integrations/jellyfin/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

Test situation:

  1. Source file format FLAC
  2. Target media player devices tested: Home-Assistant Browser media player (in Firefox) ESPHome media player
  3. Media Source URL example (no AudioCodec parameter):
https://<host>/Audio/<media-id>/universal?UserId=<uid>&DeviceId=<devid>&MaxStreamingBitrate=140000000&api_key=<key>
  1. Jellyfin transcode operation based on request URI:
/usr/lib/jellyfin-ffmpeg/ffmpeg -v info -analyzeduration 200M  -i file:"/media/test.flac" -threads 0 -vn -ab 256000 -ac 2 -acodec copy -ar 48000 -id3v2_version 3 -write_id3v1 1 -y "/transcode/<id>.mp3"

(use of copy, possibly hard-coded default in Jellyfin itself?)
5) Command failure with exit code 1, seemingly due to use of copy codec into mp3 container

[mp3 @ 0x55c9fb402400] Invalid audio stream. Exactly one MP3 audio stream is required.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
Error initializing output stream 0:0 --

(running the same without acodec or explicit mp3 codec results in an output file) 6) Result: Jellyfin sends 500 response to client, no audio played.

[ERR] [262] Jellyfin.Api.Helpers.TranscodingJobHelper: FFmpeg exited with code 1
[ERR] [20] Jellyfin.Server.Middleware.ExceptionMiddleware: Error processing request. URL GET /Audio/<media-id>/universal

I’ve not seen this behaviour outside of HomeAssistant so I posted the issue here but if this is an upstream problem and should be handled on the Jellyfin side, let me know.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 21 (2 by maintainers)

Commits related to this issue

Most upvoted comments

There hasn’t been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

Hmm if the plugin is not aware of what media player you requested it to play on (either because HA doesn’t pass that along in the first place or doesn’t record or expose supported codec information of a given media player) that does complicate things quite a bit…
\future HA media player infrastructure improvement? 😉

In relation to possible suggestions, the only things I could think of are:

  • Request the user config for the user configured in the plugin to check if the user is configured to allow (audio) transcoding or not (if Jellyfin exposes such information on an API endpoint). If (they do and) it’s enabled, a quick and sufficient fix for now would be to just append &AudioCodec=mp3 as additional query param (otherwise do nothing).
  • If Jellyfin does not expose (read-only) user configuration for oneself and you can’t know if transcoding is allowed or not in advance, check the constructed URL in the plugin first before returning it. I thought maybe using a HEAD request would work here it but it looks like that just returns a 200 OK without issue so that would need to be an actual GET request… (I feel like the response for both methods for a given URL should be the same but it’s not)… If that returns a 500 error, append &AudioCodec=mp3 before returning the URL.

Of course it would probably be much cleaner to specify the supported containers and such instead (so we don’t have to transcode if it’s allowed) but that would require the media players to report their supported codecs to HA (if they currently don’t) and for the plugin to get the media player information from HA when it’s requested to create a media source for a given media player. But I’m guessing with the current setup that would require a rework on the HA side of things.