gkdv - geek articles
GKDV - Useless proofs of concepts ¯\_(ツ)_/¯

Useless proofs of concepts ¯\_(ツ)_/¯

Articles geek en français Articles in english

How to listen an audio cd to your chromecast installation?

April 3, 2019

Using a raspberry, an usb cd reader, a wifi dongle… Stream an audio cd to a multi-chromecasts room !

How to ?

What you need

What will we use

  • An icecast2 server
  • ffmpeg
  • python3 with pychromecast
  • udev rule

Global instructions

We will use an icecast server, to install it:

$ sudo apt-get install icecast2  

Icecast2 server will broadcast the audio feed

And we need ffmpeg, which will stream audio cd to icecast2 feed:

$ sudo apt-get install ffmpeg  

Some configuration, for icecast2: edit the /etc/icecast2/icecast.xml file, and check the authentication part:

        <!-- Sources log in with username 'source' -->  
        <!-- Relays log in with username 'relay' -->  
        <!-- Admin logs in with the username given below -->  

And the hostname and socket part:

<!-- You may have multiple <listener> elements -->  
    <!-- <bind-address></bind-address> -->  
    <!-- <shoutcast-mount>/stream</shoutcast-mount> -->  

You can see the full config file in the Chromecast-audio-cd-caster repo

To stream using ffmpeg, simply use:

/usr/bin/ffmpeg  -f libcdio -ss 0 -i /dev/cdrom -acodec libmp3lame -ab 48k -bufsize 15 -ac 1 -content_type audio/mpeg -f mp3  icecast://source:YOUR_PASSWORD@  

(Don’t forget to replace by your raspberry pi local ip)

You have now a feed readable from

How to cast?

To cast what you want to a chromecast, have a look on Pychromecast
Of course, you need python3:

$ sudo apt-get install python3  

And a tiny python script:

import time  
import pychromecast  
import urllib.request  
stream_url = ''  
chromecasts = pychromecast.get_chromecasts()  
cast = next(cc for cc in chromecasts if cc.device.friendly_name == "Salon")  
mc = cast.media_controller  
while urllib.request.urlopen(stream_url).getcode() != 200:  
    print('Waiting for stream up')  
mc.play_media(stream_url, 'audio/mp3')  

And … it’s a kind of magic:

$ /usr/bin/ffmpeg  -f libcdio -ss 0 -i /dev/cdrom -acodec libmp3lame -ab 48k -bufsize 15 -ac 1 -content_type audio/mpeg -f mp3  icecast://source:SOURCE_PASSWORD@ & /usr/bin/python3 /home/pi/audio/cast.py  

Bonus: autostart on cd insertion

We will use an udev rule, for example in a /etc/udev/rules.d/99-cd-audio-processing.rules rule:

SUBSYSTEM=="block", KERNEL=="sr0", ACTION=="change", RUN+="/home/pi/audio/start-cd.sh &"  

Reboot, and you’re ready :)

See sources on Chromecast-audio-cd-caster repo

To conclude…

However, it’s a bit long to start reading, and all my music is already uploaded on my google music account, so … useless ¯\(ツ)

Let's twitt about it!

When you click on links to various merchants on this site and make a purchase, this can result in this site earning a commission.
Affiliate programs and affiliations include, but are not limited to, the eBay Partner Network and Amazon.