Chromecast stream player

Small script for playing any stream URL on a Chromecast Audio, for example from MPD, VLC, or a web radio. Supports automatic discovery and direct connections.

One of the main use-cases of a Chromecast Audio is the ability to “headlessly” play externally provided HTTP streams on a regular HiFi setup. These could for example come from MPD, VLC, or any other arbitrary Web radio or stream in general.

The small script chromecast-stream does exactly this: It lets a Chromecast play a certain stream URL. First, it tries to auto-discover the Chromecast with the given name via mDNS. As this can be unreliable, a direct connection to the provided IP address is supported as fallback. All this is merely a thin wrapper on top of the pychromecast python library.

Usage & Installation

The script expects the Chromecast to use and the URL to play.

usage: chromecast-stream [-h] [--debug] [--name NAME] [--host HOST] [--port PORT] [--volume VOLUME] [--ctype CTYPE] URL

Play streams on a Chromecast Audio, using PyChromecast.

positional arguments:
  URL              stream url to play

optional arguments:
  -h, --help       show this help message and exit
  --debug          enable debug log output (default: False)
  --name NAME      chromecast friendly name for discovery (default: None)
  --host HOST      chromecast ip address for direct connection (default: None)
  --port PORT      chromecast port for direct connection (default: 8009)
  --volume VOLUME  volume to set before streaming, 0-100 (default: None)
  --ctype CTYPE    stream content type, if not mp3 (default: audio/mpeg)

Of course, at least either a Chromecast name or IP address should be provided – best would be both, if available. Optionally, the volume can be set to the given value before playing.

Exiting via Ctrl^C stops the playback.

Installation

The pychromecast python library needs to be installed beforehand. This can for example be done either via

(sudo) pip install pychromecast

or

(sudo) apt-get install python3-pychromecast

Afterwards, the script can be directly invoked locally (./chromecast-stream.py) or optionally installed to /usr/local/bin/chromecast-stream via

(sudo) make install

Development

The Makefile can install all needed requirements to a local virtual environment. Basic linting via mypy and flake8 will validate the source code:

make check

Running chromecast-stream.py directly with the local dependencies:

. venv/bin/activate ; python3 -m chromecast-stream -h

Usecase: MPD HTTP Stream to Chromecast Audio

While there is often no explicit output plugin for Chromecasts, players such as VLC or in especially MPD can easily provide HTTP streams. The Chromecast in turn can then be instructed to play such a source – which this script automates. Also note that most players can then provide other streams in a unified way, too.

To enable HTTP streaming, a corresponding mpd.conf could look like:

audio_output {
    type            "httpd"
    name            "My MPD Stream"
    bind_to_address "0.0.0.0"
    port            "5000"
    encoder         "lame"
    format          "48000:24:2"
    bitrate         "192"
    mixer_type      "none"
    always_on       "yes"
}

The playback of http://192.168.1.XXX:5000/ can then be started from everywhere the Chromecast is reachable. For automatically starting conditionally, a systemd unit can be used:

[Unit]
Description=Chromecast Stream to Port 5000
After=mpd.service
BindsTo=mpd.service
StopWhenUnneeded=true

[Service]
Type=exec
User=mpd
KillMode=process
KillSignal=SIGINT
TimeoutStopSec=10
Restart=always
RestartSec=10
Environment=PYTHONUNBUFFERED=TRUE
ExecStart=/usr/local/bin/chromecast-stream --name MyCCA --host 192.168.1.XXX http://192.168.1.XXX:5000/

[Install]
WantedBy=mpd.service

The service file should for example be copied to /etc/systemd/system/chromecast-stream.service and enabled with systemctl enable chromecast-stream.

Alternative: Mkchromecast

An alternative for streaming audio to a chromecast device is mkchromecast, which also works from commandline and additionally provides a PyQt5 system tray menu. When configuring MPD for alsa output, mkchromecast can transform this to an HTTP stream and instruct the chromecast to play it. Here, it relies on the pychromecast library, too.

However, this possibly complicated configuration and transcoding step on the same machine is not needed, as MPD can directly provide an HTTP stream. The audio process can be patched out though, using mkchromecast merely for the purpose of discovery and starting playback – the usecase of this project.

While it tries to do too much not strictly related to controlling the player, the discovery and connection check of mkchromecast proved unstable and is silent, crashes or hangs were common. Also automation, proper logging, and error-checking was hard, for example as the way of program termination under almost any condition is a kill -9 on itself. But the underlying pychromecast seems in a better shape – thus the motivation for this little project.

Alternative: HifiBerry

A small HiFiBerryOS extension similarly allows to start and stop arbitrary streams on-demand.

Code & Download