Skip to content

Instantly share code, notes, and snippets.

@Jzarecta
Created December 11, 2019 05:11
Show Gist options
  • Save Jzarecta/eef4ed87ce06d6ef4e7d6d7a9e7c0e4f to your computer and use it in GitHub Desktop.
Save Jzarecta/eef4ed87ce06d6ef4e7d6d7a9e7c0e4f to your computer and use it in GitHub Desktop.
PyChromecast broadcasting
#!/bin/env python
"""
YouTube Controller to use with pychromecast
you feed them a youtube URL and it will broadcast it to your device
Originally from the pychromecast examples
ToDo:
Support more youtube urls alias
"""
import pychromecast
from pychromecast.controllers.youtube import YouTubeController
import sys
from urllib.parse import urlparse, parse_qs
# State your Chromecast device
CAST_NAME = "Bedroom TV"
# Change to the video id of the YouTube video
# Use the urlparse and parse_qs from the urllib module
url_data = urlparse(sys.argv[1])
query = parse_qs(url_data.query)
VIDEO_ID = query["v"][0]
# Look for the chromecast device and search for the CAST_NAME
# Once it finds it it will send to YouTubeController
chromecasts = pychromecast.get_chromecasts()
cast = next(cc for cc in chromecasts if cc.device.friendly_name == CAST_NAME)
cast.wait()
yt = YouTubeController()
cast.register_handler(yt)
yt.play_video(VIDEO_ID)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment