[PYTHON] Randomly play the movie on ChromeCast for a certain period of time

ChromeCast will randomly show the photos stored in Google Photos, so I'm happy to have a memory story with my family. However, since only the photos are shown, I was wondering if I could somehow play the movie, but orz does not add any functions at all. Even if you cast a movie with a smartphone app, it's only a function to randomly play all the movies from the Youtube playlist, and if a long movie (such as a movie of a playground) starts to flow in these apps, it will be the last , It doesn't end until all of it is played, so I feel that this is not the case. It seemed that I could easily play mp4 using pychromecast, so I made it quickly.

Advance preparation

--Linux environment (I use WSL) --HTTP server (I use apache)

Video preparation

Put the movie in a suitable folder that can be seen on WSL. I think I put about 1,000. These movies need to be in a format that can be played directly on ChromeCast. Convert unsupported movies with ffmpeg and align the time stamps. (The following is a movie with the extension .MOV converted and saved using the extension .m4v.)

for i in *.MOV; do ffmpeg -y -i $i -s 1920x1080 -vcodec libx264 -r 30 -b 1024k -acodec aac -ac 2 -ar 44100 -ab 128k -f mp4 `basename $i .MOV`.m4v; done

for i in *.MOV; do day=`date '+%Y%m%d%H%M' -r $i`; touch -t $day `basename $i .MOV`.m4v; done

Set up so that those movie files can be displayed on the http server. For apache, you can create a symbolic link under/var/www/html.

A program that randomly selects a movie and starts playback from a random time for 20 seconds

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
import time
import pychromecast
import random
import os
import datetime
from datetime import datetime as dt

waittime=3 #The time this program will wait for the time it takes to switch videos on ChromeCast
moviefolder="/mnt/e/movie/" #Where the movie is stored on the server
httpurl="" #The location of the movie on the server as seen from ChromeCast

# List chromecasts on the network, but don't connect
services, browser = pychromecast.discovery.discover_chromecasts()
# Shut down discovery

# Discover and connect to chromecasts named Living Room
chromecasts, browser = pychromecast.get_listed_chromecasts(friendly_names=["Living"])
[cc.device.friendly_name for cc in chromecasts]

cast = chromecasts[0]
# Start worker thread and wait for cast device to be ready

mc = cast.media_controller

while stopflag == 0:
  filepath = moviefolder+tempfile
  dt = datetime.datetime.fromtimestamp(os.stat(filepath).st_mtime)
  key = dt.strftime('%Y/%m/%d  %H:%M:%S')
  parser = createParser(filepath)
  meta = extractMetadata(parser)
  duration = meta.get("duration")
  if starttime < 0:
    starttime = 0
  mc.play_media(httpurl+tempfile, 'video/mp4', current_time=starttime)
  if duration.seconds < playtime:

After that, if you open the terminal and run this program, it will automatically play the randomly selected movie for 20 seconds. The children looked nostalgic. Occasionally, wedding movies come out and I think it's hard to see without such an app. It seems that the manner of pychromecast will change considerably, so it seems better to look at Honke HP. Actually, I kick it from PHP and start it, automatically reload the page in 20 seconds, and play it only while the browser is open on the smartphone.

Referenced site


Recommended Posts

Randomly play the movie on ChromeCast for a certain period of time
Put the process to sleep for a certain period of time (seconds) or more in Python
[Python] Create a list of date and time (datetime type) for a certain period
How to use the Slack API using Python to delete messages that have passed a certain period of time for a specific user on a specific channel
On Linux, the time stamp of a file is a little past.
The story of releasing a Python text check tool on GitHub x CircleCI for the first time
I measured the run queue wait time of a process on Linux
Delete files that have passed a certain period of time with Raspberry PI
Experiment to collect tweets for a long period of time (aggregation & content confirmation)
Make a histogram for the time being (matplotlib)
Run yolov4 "for the time being" on windows
A record of the time it took to deploy mysql on Cloud9 + Rails
Calculate the probability of outliers on a boxplot
At the time of python update on ubuntu
Until you run a Flask application on Google App Engine for the time being
I tried Python on Mac for the first time.
Register a task in cron for the first time
Create a QR code for the URL on Linux
I tried python on heroku for the first time
A memo of installing Chainer 1.5 for GPU on Windows
A Study on Visualization of the Scope of Prediction Models
Create a shape on the trajectory of an object
The story of creating a "spirit and time chat room" exclusively for engineers in the company
Avoiding the pitfalls of using a Mac (for Linux users?)
A note on the default behavior of collate_fn in PyTorch
I tried running PIFuHD on Windows for the time being
Let's execute the command on time with the bot of discord
[Python] Create a date and time list for a specified period
Summary of stumbling blocks in Django for the first time
One liner that randomly rewrites the startup time of cron
I want to create a Dockerfile for the time being.
Kaggle for the first time (kaggle ①)
Kaguru for the first time
For the time being, the one who creates a homepage with Django at the speed of a second and publishes it on Heroku (Windows compatible)
A memorandum of understanding for the Python package management tool ez_setup
A memo that reproduces the slide show (gadget) of Windows 7 on Windows 10.
Build a Selenium environment on Amazon Linux 2 in the shortest time
The story of making a standard driver for db with python.
A function that measures the processing time of a method in python
[Python3] Define a decorator to measure the execution time of a function
Find the rank of a matrix in the XOR world (rank of a matrix on F2)
A command to easily check the speed of the network on the console
Neighborhood search for a set of (geographical coordinates) points on a sphere
Host the network library Mirror for Unity on a Linux server
Get the number of readers of a treatise on Mendeley in Python
[For beginners] I want to get the index of an element that satisfies a certain conditional expression