simojenki / bonob

sonos SMAPI implementation allowing integrating different music sources with sonos.
GNU General Public License v3.0
221 stars 15 forks source link

transcoding to mp3 gives the song is not encoded correctly errors #52

Closed zombiehoffa closed 2 years ago

zombiehoffa commented 3 years ago

Hi, using default mp3 audio settings for navidrome transcoding results in no songs being able to play because of an encoding error from sonos. Encoded songs play fine in other players and if I turn off transcoding the portion of my collection that is flac and mp3 in a supported bitrate play fine through bonob to my sonos system.

Looking at the logs I get output in navidrome for both transcoding and non transcoding bonob requests ie: transcoding: time="2021-09-11T15:27:18Z" level=info msg="Streaming file" artist="Charles Bradley" bitRate=192 cached=true format=mp3 originalBitRate=865 originalFormat=flac title="Dusty Blue" transcoding=true user=chris

time="2021-09-11T15:27:18Z" level=info msg="Now Playing" artist="Charles Bradley" player="bonob [bonob]" title="Dusty Blue" user=chris

non transcoding: time="2021-09-11T15:24:49Z" level=info msg="Streaming file" artist="Charles Bradley" bitRate=0 cached=false format=raw originalBitRate=975 originalFormat=flac title="Strictly Reserved for You" transcoding=false user=chris

time="2021-09-11T15:24:49Z" level=info msg="Now Playing" artist="Charles Bradley" player="bonob [bonob]" title="Strictly Reserved for You" user=chris

but log output in bonob only incremements for non transcoding (there's a bunch of tokens in here so I will only clean this up and post it if really needed).

simojenki commented 3 years ago

I think this is because when transcoding is enabled ND reports the content-length as "0". Documentation at sonos indicates that content-length is a required response field; https://developer.sonos.com/build/content-service-add-features/streaming-basics/

I've previously tried reading the entire stream in bonob so as to get the length, however on some tracks that causes a delay whilst the server caches the response, which in turn causes the sonos device to assume that the track is not going to play, and so it moves onto the next track in the playlist.

zombiehoffa commented 3 years ago

Can we just lie to sonos? Is there a setting.I should change in navidrome to change the behaviour? I know I can briefly get it to play a stream if I click play in sinks fast enough after changing the transcoding setting in navidrome. It definitely is ouputting transcoded mp3 and will play once before breaking.

simojenki commented 3 years ago

You can try using different players in ND by media type. See "BONOB_NAVIDROME_CUSTOM_CLIENTS" setting on README. That allows different transcoding settings by file type.

"will play once before breaking" <- the entire track, or just the first part of the track?

What formats are you trying to play/transcode? Sonos supports these -> https://developer.sonos.com/build/content-service-add-features/supported-audio-formats/. So mp3, and flac do not require transcoding.

I've noticed that transcoding; flac -> mp3 breaks sonos/bonob/ND because sonos tries to use range queries for the mp3, which ND does not support. flac -> flac (ie. downsampling) does work, as sonos does not seem to try and perform a range query.

So it would seem that behaviour can also be different depending on file types themselves.

zombiehoffa commented 3 years ago

Mostly it's for dealing with some higher quality flac files that sample above what sonos supports. I didn't realize I could be so granular with bonob though, so Iw as just transcoding everything to mp3 for sonos. I will play around with it and see if I can get it to work. You wouldn't happen to have your suggest flac to flac transcoding command handy would you?

simojenki commented 3 years ago

In that case I would set BONOB_NAVIDROME_CUSTOM_CLIENTS="audio/flac", for the default bonob player i would disable transcoding so that the raw files (mp3, etc) just get passed straight through to sonos. flac -> flac with downsample, could try something like this -> https://stackoverflow.com/questions/41420391/ffmpeg-flac-24-bit-96khz-to-16-bit-48khz

zombiehoffa commented 3 years ago

For anyone that sees this later with the same problem, this is the transcoding command that started working on my sonos system after creating the custom navidrome client as above. I don't know if it's the best one, it's just the one that finally started playing after fiddling with it for a half hour (I ended up having to nuke the cached transcodes because some of the lines I tried created real flac cache files that didn't work, so maybe all this isn't totally necessary and the cache messing up playback led me down a more complex rabbit hole than necessary): ffmpeg -i %s -af aresample=resampler=soxr:out_sample_fmt=s16:out_sample_rate=48000 -f flac -

I don't fully understand what all the stuff is doing other than the sampl

simojenki commented 3 years ago

I have documented this on the README for anyone else that requires downsampling of flacs

padenj commented 2 years ago

I have a new installation of Navidrome and Bonob and I am getting the "song is not encoded correctly" error. I have tried following the steps in this issue and the notes on transcoding in the readme. I am not trying to do any specific transcoding, just trying to play my media through an older Sonos device (Play 5, gen 1) which requires the S1 app. Unfortunately it doesn't appear Bonob/Navidrome work out-of-the-box for S1.

I have installed Navidrom / Bonob on docker running on Debian 11. My Sonos devices are on the same network.

Here's where I am so far:

I am running the latest Sonos S1 v11.5. Most of my media are mp3 files.

I'm not sure what else to try. If anyone has steps for configuring an instance of Navidrome / Bonob from scratch and connecting to it through Sonos S1, that would be appreciated!

zombiehoffa commented 2 years ago

I dunno man, I haven't changed anything and i used mine with s1 and now s2. It's been solid for me even through moving the whole thing to a different server. You need that custom player to pop into navidrome so you can tell it to use specific transcoding above. Try playing a song through your sonos from bonob that doesn't need transcoding (like load in an mp3 album or something), it might need to actaully play to show up. I have BONOB_NAVIDROME_CUSTOM_CLIENTS in my docker-compose for bonob.

simojenki commented 2 years ago

Sonos S1 should play mp3 and flac without transcoding (https://support.sonos.com/s/article/79?language=en_US). You should only need to use the transcoder if your content is in a format that the S1 cannot understand, ie. HD flacs.

The correct env far to set for custom clients is now, 'BNB_SUBSONIC_CUSTOM_CLIENTS'. 'BONOB_NAVIDROME_CUSTOM_CLIENTS' will still work, it is however deprecated.

To start with I would try testing without transcoding, play some mp3s and from there try some flacs that you know are 16bit so that the S1 can play them without transcoding.

If that works however you have some content that the S1 cannot support, then you will need to look into transcoding.