Wednesday, February 26, 2020

Playing Local Network Media on a Chromecast with Python

I have several Chromecast devices in my house.  Recently I was playing around with the Python module pychromecast.  It has given me a much better understanding of what the Chromecast does.  The module allows you to find devices on your network and play videos or other media on the device.



This is done simply by finding the device and giving it a url and mime-type for the media that you want to play.  After seeing the example it seemed like it should be possible to play media from a server on my local network.

The example code has this line:

mc.play_media('http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', 'video/mp4')
I found an .avi video file and quickly started a python web server in the folder with the video on my machine.  I added a server folder to the project with an index.html file and a test.avi file.  Then I navigated to the folder in Terminal and ran this.

python -m http.server

Then I could see my index.html file here: http:/localhost:8000

After I had the server working I modified the example code to send the url to the video file using my local network IP address.  This almost worked.  I got sound, but no video and I got an error on my web server when I tried to pause it.  I think some of the requests the Chromecast was making to the server were not supported by the python web server.  This may not be an issue if I used Apache or some other standard web server.

Right now I have a Plex media server setup, but there are some limitations with that when casting from the Android app with a free account.  At some point maybe I will try to build something that uses this to play my movies.  I'll have to experiment a bit more and see why I wasn't getting video.  It may have just had something to do with the video encoding of the file I was testing with.  It is possible it is also a web server related issue.

The code I used is on Github here: https://github.com/mattdyer/chromecastcontrol

No comments:

Post a Comment

Coding

  I'm not sure where I heard this, and it wasn't worded this way, but it helps to think about coding this way. Basically, any progra...