jellyfin / jellyfin-kodi

Jellyfin Plugin for Kodi
https://jellyfin.org
GNU General Public License v3.0
860 stars 113 forks source link

No sound when playing transcoded Video with FLAC audio stream #907

Open b-m-f opened 3 months ago

b-m-f commented 3 months ago

It seems to be related to https://github.com/jellyfin/jellyfin/issues/8224#issuecomment-2289766898.

Playing any video file - transcoded - will result in no audio when the FLAC track is selected.

This is not an issue when direct-playing the video file. All audio codecs work fine then. Also all other clients tested by me:

Work without the audio dropping.

xingoxu commented 3 months ago

Same here. I've enabled forced transcoding and chose the aac / ac3 for audio codec but I see ffmpeg command still uses flac.

jellyfin-1  | [17:45:51] [INF] [78] MediaBrowser.MediaEncoding.Transcoding.TranscodeManager: /usr/lib/jellyfin-ffmpeg/ffmpeg -analyzeduration 200M -probesize 1G -init_hw_device vaapi=va:,kernel_driver=i915,driver=iHD -init_hw_device qsv=qs@va -filter_hw_device qs -noautorotate -i file:"/data/Anime/Buddy Complex/Season 1/S01E01.mkv" -map_metadata -1 -map_chapters -1 -threads 0 -map 0:0 -map 0:1 -map -0:s -codec:v:0 h264_qsv -preset veryfast -look_ahead 0 -b:v 12021144 -maxrate 12021144 -bufsize 24042288 -profile:v:0 high -level 50 -g:v:0 72 -keyint_min:v:0 72 -vf "setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709,scale=trunc(min(max(iw\,ih*a)\,min(2841\,1344*a))/2)*2:trunc(min(max(iw/a\,ih)\,min(2841/a\,1344))/2)*2,format=nv12" -codec:a:0 flac -ac 2 -ar 48000 -copyts -avoid_negative_ts disabled -max_muxing_queue_size 2048 -f hls -max_delay 5000000 -hls_time 3 -hls_segment_type mpegts -start_number 0 -hls_segment_filename "/cache/transcode/39d9628a7c81be3da744e6f9b0d35989%d.ts" -hls_playlist_type vod -hls_list_size 0 -y "/cache/transcode/39d9628a7c81be3da744e6f9b0d35989.m3u8"
Nocifer commented 2 months ago

Gah, this issue has been my single personal annoyance with Jellyfin for years now. I was under the impression that it's a Jellyfin server issue, but the people over at Jellyfin server have recently stated that this should not be an issue on their end (which is supported by the fact that other clients do not have this issue), so I guess the problem is with the Jellyfin Kodi plugin after all.

Skimming through the code for a whole of 5 minutes I stumbled on this (in helper/playutils.py):

    def get_transcoding_audio_codec(self):
        codecs = ["aac", "mp3", "ac3", "opus", "flac", "vorbis"]

...followed by this:

    "TranscodingProfiles": [
                {
                    "Type": "Video",
                    "Container": "m3u8",
                    "AudioCodec": self.get_transcoding_audio_codec(),
                    ...

Now, if I'm getting this right (and I could very well be way off, seeing as I've pretty much only read this part of the code) it seems like the plugin advertises the audio formats it supports for transcoding into as the above codecs array, which includes FLAC. And if my understanding is right, then during transcoding we should never be attempting to use FLAC as the audio codec, even if the device supports it natively during direct play.

So perhaps if FLAC were to be removed from this array, the issue could be fixed?

oddstr13 commented 2 months ago

https://github.com/jellyfin/jellyfin-kodi/blob/6140ea809c511ee67e81c919ba3f9ee3e6ed311b/jellyfin_kodi/helper/playutils.py#L406-L413 Removing flac from that list would likely cause it to get transcoded into aac (by default – audioPreferredCodec setting)

Video codecs have a few settings to force transcoding of mp2, hevc, av1 and vc1 Adding similar settings for audio codecs would be feasible

https://github.com/jellyfin/jellyfin-kodi/blob/6140ea809c511ee67e81c919ba3f9ee3e6ed311b/jellyfin_kodi/helper/playutils.py#L385-L404

Nocifer commented 2 months ago

Yeah, so it works more or less as I Imagined. If everything stays at the default state, then during transcoding the audio will get transcoded to the widely compatible AAC by default (with a list of further possible candidates), just like video will get transcoded to the widely compatible H.264 by default (with, again, a list of further candidates).

For the video part, the user can opt to select something else in the settings as the preferred video codec to transcode into, and the same functionality could (and should) be added to the audio part, because it's very useful (for example, some specific audio format could be supported for pass-through on some specific sound device, although that's often enough the case with the default AAC).

But aside from all that, this specific issue arises from the fact that FLAC is also included in that list of possible format candidates, so when the system tries to transcode a media file which contains FLAC audio, the audio part is considered as already compatible and so is left untouched, and is then (attempted to be) muxed into the TS container with the transcoded video stream, but the TS container doesn't support FLAC audio, which leads to the issue.

So yes, I think the proper solution here should be to remove FLAC from the possible transcode candidates and so let FLAC audio streams be transcoded to AAC as they should.