How to listen an audio cd to your chromecast installation?
April 3, 2019Using a raspberry, an usb cd reader, a wifi dongle… Stream an audio cd to a multi-chromecasts room !
How to ?
What you need
- A raspberry pi with working wifi
- An usb cd or dvd reader
- Of course, one or more chromecast…
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:
<authentication>
<!-- Sources log in with username 'source' -->
<source-password>YOUR_PASSWORD</source-password>
<!-- Relays log in with username 'relay' -->
<relay-password>YOUR_PASSWORD</relay-password>
<!-- Admin logs in with the username given below -->
<admin-user>admin</admin-user>
<admin-password>YOUR_PASSWORD</admin-password>
</authentication>
And the hostname and socket part:
<hostname>localhost</hostname>
<!-- You may have multiple <listener> elements -->
<listen-socket>
<port>8000</port>
<!-- <bind-address>127.0.0.1</bind-address> -->
<!-- <shoutcast-mount>/stream</shoutcast-mount> -->
</listen-socket>
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@192.168.0.31:8000/raspi
(Don’t forget to replace 192.168.0.31 by your raspberry pi local ip)
You have now a feed readable from http://192.168.0.31:8000/raspi
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 = 'http://192.168.0.31:8000/raspi'
chromecasts = pychromecast.get_chromecasts()
cast = next(cc for cc in chromecasts if cc.device.friendly_name == "Salon")
cast.wait()
print(cast.device)
print(cast.status)
mc = cast.media_controller
while urllib.request.urlopen(stream_url).getcode() != 200:
print('Waiting for stream up')
time.sleep(1)
mc.play_media(stream_url, 'audio/mp3')
mc.block_until_active()
print(mc.status)
mc.play()
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@192.168.0.31:8000/raspi & /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 ¯\(ツ)/¯
You May Also Enjoy
How to earn money with your raspberry pi
How to earn money with a raspbery pi? It’s simple, with earnapp.
Functional tests with puppeteer and gherkin scenarios
For my custom needs, I was looking for puppeteer functional testing suite. As a php developer, I ordinary use behat to write and play my functional tests...
Make sound volume presets for your chromecasts
Sometimes, I tell to Google “Up the volume”, or “Down the volume” on one or several chromecast devices. But the next day, when I want to listen music aga...
Retropie with a gameboy advance as controller
You have a gameboy advance. You have an amazing retropie installation. So… You have already your perfect controller…Inside Gadgets did it!I bought this w...
Make your own alarm system with your raspberry pi
A konyks senso with a raspberry pi and… You have now your diy alarm! What you need ...
How to mount put.io as a drive using python?
Mount put.io as a drive using python.Retrieving a token1) Go to https://put.io/oauth/apps/new , create an app (fill something in all fields, it does not ...
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.