Didelot Maurice-Michel "Hacked SONOS and YouTube" to Build a Simple Stream Conversion Server
Taking advantage of support for web radio streams, Maurice-Michel has written a Python server for YouTube streaming — with apps to follow.
IT consultant Didelot Maurice-Michel was displeased with the inaccessibility of Google's YouTube video streaming platform on SONOS speaker systems — and set about fixing the problem by reverse engineering the SONOS desktop app.
"Even if YouTube videos are publicly available and their content are free," Maurice-Michel explains, "SONOS can't play them without the use of YouTube Music which is not cool. YouTube is a big place for finding music, especially old live concert, or forgotten songs. So, how do we play YouTube videos on a SONOS?"
The answer: Digging into the operation of the SONOS desktop application and building a system for pulling content down from YouTube in a suitable format and feeding it to SONOS for playback, all without having to touch YouTube Music — or using an iOS device to stream via Airplay, unavailable to those outside the Apple ecosystem.
"After some digging, I find a really interesting request that is linked to the Web-Radio feature of the device," Maurice-Michel explains. "Indeed, part of the SONOS feature is that it can stream Web-Radios by choosing them on a list from the app, or add your own! Note the custom URL protocol, that indicate that it try to play a MP3 Radio. But later tests will show that it works not only for MP3 but for [almost] every supported encoding."
Experiments, however, revealed that the problem wasn't going to be as simple as feeding a YouTube MP4 into the SONOS system. Nor was it possible to extract and use the AAC audio stream alone, with SONOS requiring an ADTS streaming container.
"My new plan [was] to build a web server that serve ADTS formatted YouTube video content," Maurice-Michel writes. "The SONOS ask a resource containing a YouTube video ID, then the server ask to YouTube the corresponding video. For each packet received: Parse MP4; Extract AAC; Format it to ADTS; Send it back to the SONOS."
Having picked apart the MP4 format to parse its metadata and atomic data, Maurice-Michel was able to build a system which could extract the AAC audio and reformat it as ADTS. Written in Python with Flask and pytube, a Python tool for interfacing with YouTube, the resulting server successfully handles the conversion and streaming with a simple cat and pipe — and is in the process of being rewritten as a fully-featured SONOS-compatible streaming app for Android and iOS devices.
Maurice-Michel's full write-up is available on deadf00d.com, along with the source code under an unspecified open-source license.