Python Web Proxy Convinces Sonos To Stream YouTube

[Maurice-Michel Didelot] owns a Sonos smart speaker, and was lamenting the devices inability (or plain unwillingness) to stream music from online sources without using a subscription service. YouTube Music will work, but being a subscription product there is a monthly fee, which sucks since you can listen to plenty of content on YouTube for free. [Maurice] decided that the way forward was to dig into how the Sonos firmware accesses ‘web radio’ sources, and see if that could be leveraged to stream audio from YouTube via some kind of on-the-fly stream conversion process.

What? No MP4 support for web radio? Curses!

So let’s dig in to how [Maurice] chose to approach this. The smart speaker can be configured to add various streaming audio sources, and allows you add custom sources for those. The Sonos firmware supports a variety of audio codecs, besides MP3, but YouTube uses the MP4 format. Sonos won’t handle that from a web radio source, so what was there to do, but make a custom converter?

After a little digging, it was determined that Sonos supports AAC encoding (which is how MP4 encodes audio) but needs it wrapped in an ADTS (Audio Data Transport Stream) container. By building a reverse web-proxy application, in python using Flask, it was straightforward enough to grab the YouTube video ID from the web radio request, forward a request to YouTube using a modified version of pytube tweaked to not download the video, but stream it. Pytube enabled [Maurice] to extract the AAC audio ‘atoms’ from the MP4 container, and then wrap them up with ADTS and forward them onto the Sonos device, which happily thinks it’s just a plain old MP3 radio stream, even if it isn’t.

Sonos doesn’t have the best reputation, let’s say, but you can’t deny that there’s some pretty slick tech going on inside. Here’s a neat hack we covered last year, adding Sonos support to an old school speaker, and a nice teardown of a IKEA Sonos-compatible unit, which uses some neat design hacks.

Thanks [mip] for the tip!

Featured image by Charles Deluvio on Unsplash.

22 thoughts on “Python Web Proxy Convinces Sonos To Stream YouTube

    1. Yes it is, and I think it’s a great article. thanks for sharing the details. I’ve no experience how to do this from scratch and since I have Sonos 5’s i’ll be trying to emulate your results. I was so impressed when I got the speakers and replaced my medicore system and enjoyed the interface with its limitations until they changed it and my ux went to sh#t bcos basic things like creating groups kept failing or failure to play iradio as my alarm.

      1. Get a raspberry pi and install Logitech Media Server (LMS), this will control the alarms and play the play the music from any source. You can use an app such as Squeezer on your phone to manage the lot. It also allows you to sync multiple speakers. There are several guides online on how to pair a Sonos speaker to LMS.

  1. Yeah this is cool. But would be way better if it grabbed the audio stream from the PC. Then it would play anything you could want(probably with some lag).
    Currently I have a Sonos sound bar at my desk which accepts optical inputs. Then a converter to convert analog to optical. Works great and no noticeable latency

      1. You can ditch the Five and just use the TuneBlade app on your PC to stream audio over the network. Best $10 I ever spent. If you set the buffer to zero milliseconds, it’s pretty lag free. I watch video on my PC and the audio syncs well when streaming to my Sonos.

  2. I’ve found Logitech Media Server (LMS) running on a raspberry pi a pretty useful tool in this regard. There’s a YouTube plugin amongst others. The upnp/dlna bridge plugin takes care of the Sonos side of things and most other devices too. The material skin plugin gives it a nice modern look too.

      1. The beam works very well doing this. No need for extra audio input. Also a great app for YouTube on Sonos without a TV is the Sonotube on the official app-/playstore. Trial and then one payment of somewhere around $5-10 and you good to go

  3. Can Anyone here please break this down for the dummies like me who have ZERO idea how to actually DO this! No PC here just android phone and ipad and a sonos Play 1. Make it clear and you might help someone less blessed with um “tech-telligence” cheers… Much obliged

  4. One can also use Streamlink with streamlinksrv or liveproxy working on many Python capable machines (computer, OpenWRT router, Enigma2 set top box, etc.) to get a constant stream URL for websites, streams that use variable stream URL addresses (including tokens, expiration time, etc.).

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.