1 year ago
#132626
llionevans
Auto play audio CD on insert (external drive) and then cast
UPDATE BELOW
I've had a look and not seen similar posts (apologies if I've missed one). I had originally posted this on unix.stackexchange.com but not received any replies.
I'm aiming to use an external drive to cast audio to our smart speakers via chromecast. I feel that I'm very close (I can get it to work from the CL) but unable to get it running automatically on insert of the CD.
Here are the steps:
- Create a service by making the following file at /lib/systemd/system/castcd.service
[Unit]
Description=My Shell Script
[Service]
ExecStart=/home/pi/audio/start-cd.sh
[Install]
WantedBy=multi-user.target
- Create executable script at /home/pi/audio/start-cd.sh to start playing the CD with ffmpeg and cast via a python script
#!/bin/bash
killall ffmpeg
/usr/bin/ffmpeg -f libcdio -ss 0 -i /dev/sr0 -nostdin -acodec libmp3lame -ab 48k -bufsize 15 -ac 1 -content_type audio/mpeg -f mp3 icecast://source:PASSWORD@192.168.0.60:8000/raspi & /usr/bin/python3 /home/pi/audio/cast.py &
- Create python script which starts cast with pychromecast
import time
import pychromecast
import urllib.request
stream_url = 'http://192.168.0.60:8000/raspi'
chromecasts = pychromecast.get_chromecasts()
chromecasts, browser = pychromecast.get_listed_chromecasts(friendly_names=["Living Room Hub"])
[cc.cast_info.friendly_name for cc in chromecasts]
cast = chromecasts[0]
cast.wait()
print(cast.cast_info)
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.pause()
time.sleep(5)
mc.play()
pychromecast.discovery.stop_discovery(browser)
- Add the line below to /etc/udev/rules.d/99-cd-audio-processing.rules
SUBSYSTEM=="block", KERNEL=="sr0", ACTION=="change", ENV{SYSTEMD_WANTS}+="castcd.service"
- Then I ran the following
sudo systemctl daemon-reload
sudo systemctl enable castcd.service
sudo systemctl start castcd.service
sudo udevadm control --reload-rules && sudo udevadm trigger
This should run on insert of CD.
And that's it, I thought that would work. start-cd.sh runs when I insert a CD (I checked by adding a mkdir line to test). Equally, if I run the 'ffmpeg & cast.py' line from the CL then it starts playing the CD and casts it as desired. The only thing missing is getting this to happen automatically on a CD insert.
Any suggestions much appreciated.
UPDATE
Ok, I may have found the issue.
When this runs it does so as root, therefore it doesn't access the python modules for the pi user.
I thought that would give me a simple solution. However, regardless of what I try it still runs it as root.
I've tried each of these and it just returns 'root'.
sudo -u pi echo $(whoami)
su - pi -c "echo $(whoami)"
runuser - pi -c "echo $(whoami)"
linux
audio
systemd
chromecast
udev
0 Answers
Your Answer