def play(filename, transcode=False, transcoder=None, transcode_options=None, device_name=None, server_port=None): """ play a local file on the chromecast """ if os.path.isfile(filename): filename = os.path.abspath(filename) else: sys.exit("media file %s not found" % filename) cast = CCMediaController(device_name=device_name) kill_old_pid(cast.host) save_pid(cast.host) print "Playing:", filename transcoder_cmd, probe_cmd = get_transcoder_cmds(preferred_transcoder=transcoder) mimetype = get_mimetype(filename, probe_cmd) status = cast.get_status() webserver_ip = status['client'][0] print "my ip address:", webserver_ip req_handler = RequestHandler if transcode: if transcoder_cmd == "ffmpeg": req_handler = TranscodingRequestHandler req_handler.transcoder_command = FFMPEG elif transcoder_cmd == "avconv": req_handler = TranscodingRequestHandler req_handler.transcoder_command = AVCONV if transcode_options is not None: req_handler.transcode_options = transcode_options else: req_handler.content_type = mimetype # create a webserver to handle a single request on a free port or a specific port if passed in the parameter port = 0 if server_port is not None: port = int(server_port) server = BaseHTTPServer.HTTPServer((webserver_ip, port), req_handler) thread = Thread(target=server.handle_request) thread.start() url = "http://%s:%s%s" % (webserver_ip, str(server.server_port), urllib.quote_plus(filename, "/")) print "URL & content-type: ", url, req_handler.content_type load(cast, url, req_handler.content_type)
def playurl(url, device_name=None): """ play a remote HTTP resource on the chromecast """ print_ident() url_parsed = urlparse.urlparse(url) scheme = url_parsed.scheme host = url_parsed.netloc path = url.split(host, 1)[-1] conn = None if scheme == "https": conn = httplib.HTTPSConnection(host) else: conn = httplib.HTTPConnection(host) conn.request("HEAD", path) resp = conn.getresponse() if resp.status != 200: sys.exit("HTTP error:" + resp.status + " - " + resp.reason) print "Found HTTP resource" headers = resp.getheaders() mimetype = None for header in headers: if len(header) > 1: if header[0].lower() == "content-type": mimetype = header[1] if mimetype != None: print "content-type:", mimetype else: mimetype = "video/mp4" print "resource does not specify mimetype - using default:", mimetype cast = CCMediaController(device_name=device_name) load(cast, url, mimetype)
def set_volume(v, device_name=None): """ set the volume to level between 0 and 1 """ CCMediaController(device_name=device_name).set_volume(v)
def volume_down(device_name=None): """ lower the volume by 0.1 """ CCMediaController(device_name=device_name).set_volume_down()
def volume_up(device_name=None): """ raise the volume by 0.1 """ CCMediaController(device_name=device_name).set_volume_up()
def get_status(device_name=None): """ print the status of the chromecast device """ print CCMediaController(device_name=device_name).get_status()
def stop(device_name=None): """ stop playback and quit the media player app on the chromecast """ CCMediaController(device_name=device_name).stop()
def unpause(device_name=None): """ continue playback """ CCMediaController(device_name=device_name).play()
def pause(device_name=None): """ pause playback """ CCMediaController(device_name=device_name).pause()
def playurl(url, device_name=None): """ play a remote HTTP resource on the chromecast """ print_ident() def get_resp(url): url_parsed = urlparse.urlparse(url) scheme = url_parsed.scheme host = url_parsed.netloc path = url.split(host, 1)[-1] conn = None if scheme == "https": conn = httplib.HTTPSConnection(host) else: conn = httplib.HTTPConnection(host) conn.request("HEAD", path) resp = conn.getresponse() return resp def get_full_url(url, location): url_parsed = urlparse.urlparse(url) scheme = url_parsed.scheme host = url_parsed.netloc if location.startswith("/") is False: path = url.split(host, 1)[-1] if path.endswith("/"): path = path.rsplit("/", 2)[0] else: path = path.rsplit("/", 1)[0] + "/" location = path + location full_url = scheme + "://" + host + location return full_url resp = get_resp(url) if resp.status != 200: redirect_codes = [301, 302, 303, 307, 308] if resp.status in redirect_codes: redirects = 0 while resp.status in redirect_codes: redirects += 1 if redirects > 9: sys.exit("HTTP Error: Too many redirects") headers = resp.getheaders() for header in headers: if len(header) > 1: if header[0].lower() == "location": redirect_location = header[1] if redirect_location.startswith("http") is False: redirect_location = get_full_url(url, redirect_location) print "Redirecting to " + redirect_location resp = get_resp(redirect_location) if resp.status != 200: sys.exit("HTTP error:" + str(resp.status) + " - " + resp.reason) else: sys.exit("HTTP error:" + str(resp.status) + " - " + resp.reason) print "Found HTTP resource" headers = resp.getheaders() mimetype = None for header in headers: if len(header) > 1: if header[0].lower() == "content-type": mimetype = header[1] if mimetype != None: print "content-type:", mimetype else: mimetype = "video/mp4" print "resource does not specify mimetype - using default:", mimetype cast = CCMediaController(device_name=device_name) load(cast, url, mimetype)
def play(filename, transcode=False, transcoder=None, transcode_options=None, transcode_input_options=None, transcode_bufsize=0, device_name=None, server_ip=None, server_port=None, server_external_port=None, subtitles=None, subtitles_port=None, subtitles_language=None): """ play a local file or transcode from a file or URL and stream to the chromecast """ print_ident() cast = CCMediaController(device_name=device_name) kill_old_pid(cast.host) save_pid(cast.host) if os.path.isfile(filename): filename = os.path.abspath(filename) print "source is file: %s" % filename else: if transcode and (filename.lower().startswith("http://") or filename.lower().startswith("https://") or filename.lower().startswith("rtsp://")): print "source is URL: %s" % filename else: sys.exit("media file %s not found" % filename) transcoder_cmd, probe_cmd = get_transcoder_cmds( preferred_transcoder=transcoder) status = cast.get_status() webserver_ip = status['client'][0] if server_ip is None: server_ip = webserver_ip print "local ip address:", webserver_ip req_handler = RequestHandler if transcode: if transcoder_cmd in ("ffmpeg", "avconv"): req_handler = TranscodingRequestHandler if transcoder_cmd == "ffmpeg": req_handler.transcoder_command = FFMPEG else: req_handler.transcoder_command = AVCONV if transcode_options is not None: req_handler.transcode_options = transcode_options if transcode_input_options is not None: req_handler.transcode_input_options = transcode_input_options req_handler.bufsize = transcode_bufsize else: print "No transcoder is installed. Attempting standard playback" if req_handler == RequestHandler: req_handler.content_type = get_mimetype(filename, probe_cmd) # create a webserver to handle a single request for the media file on either a free port or on a specific port if passed in the port parameter port = 0 if server_port is not None: port = int(server_port) server = BaseHTTPServer.HTTPServer((webserver_ip, port), req_handler) if server_external_port is None: server_external_port = server.server_port thread = Thread(target=server.handle_request) thread.start() url = "http://%s:%s?%s" % (server_ip, str(server_external_port), urllib.quote_plus(filename, "/")) print "URL & content-type: ", url, req_handler.content_type # create another webserver to handle a request for the subtitles file, if specified in the subtitles parameter sub = None if subtitles: if os.path.isfile(subtitles): sub_port = 0 if subtitles_port is not None: sub_port = int(subtitles_port) sub_server = BaseHTTPServer.HTTPServer((webserver_ip, sub_port), SubRequestHandler) thread2 = Thread(target=sub_server.handle_request) thread2.start() sub = "http://%s:%s?%s" % (webserver_ip, str( sub_server.server_port), urllib.quote_plus(subtitles, "/")) print "sub URL: ", sub else: print "Subtitles file %s not found" % subtitles load(cast, url, req_handler.content_type, sub, subtitles_language)
def play(filename, transcode=False, transcoder=None, transcode_options=None, transcode_input_options=None, transcode_bufsize=0, device_name=None, server_port=None, subtitles=None, subtitles_port=None, subtitles_language=None): """ play a local file or transcode from a file or URL and stream to the chromecast """ print_ident() cast = CCMediaController(device_name=device_name) kill_old_pid(cast.host) save_pid(cast.host) if os.path.isfile(filename): filename = os.path.abspath(filename) print("source is file: %s" % filename) else: if transcode and (filename.lower().startswith("http://") or filename.lower().startswith("https://") or filename.lower().startswith("rtsp://")): print("source is URL: %s" % filename) else: sys.exit("media file %s not found" % filename) transcoder_cmd, probe_cmd = get_transcoder_cmds(preferred_transcoder=transcoder) status = cast.get_status() webserver_ip = status['client'][0] print("local ip address: " + webserver_ip) req_handler = RequestHandler if transcode: if transcoder_cmd in ("ffmpeg", "avconv"): req_handler = TranscodingRequestHandler if transcoder_cmd == "ffmpeg": req_handler.transcoder_command = FFMPEG else: req_handler.transcoder_command = AVCONV if transcode_options is not None: req_handler.transcode_options = transcode_options if transcode_input_options is not None: req_handler.transcode_input_options = transcode_input_options req_handler.bufsize = transcode_bufsize else: print("No transcoder is installed. Attempting standard playback") if req_handler == RequestHandler: req_handler.content_type = get_mimetype(filename, probe_cmd) # create a webserver to handle a single request for the media file on either a free port or on a specific port if passed in the port parameter port = 0 if server_port is not None: port = int(server_port) server = BaseHTTPServer.HTTPServer((webserver_ip, port), req_handler) thread = Thread(target=server.handle_request) thread.start() url = "http://%s:%s?%s" % (webserver_ip, str(server.server_port), quote_plus(filename, "/")) print("URL & content-type: " + url + " " + req_handler.content_type) # create another webserver to handle a request for the subtitles file, if specified in the subtitles parameter sub = None if subtitles: if os.path.isfile(subtitles): sub_port = 0 #convert srt to vtt, case needed if subtitles[-3:] == 'srt': print("Converting subtitle to WebVTT") with open(subtitles, 'r') as srtfile: content = srtfile.read() content = re.sub(r'([\d]+)\,([\d]+)', r'\1.\2', content) subtitles = subtitles.replace('.srt', '.vtt') with open(subtitles, 'w') as vttfile: vttfile.write("WEBVTT\n\n" + content) if subtitles_port is not None: sub_port = int(subtitles_port) sub_server = BaseHTTPServer.HTTPServer((webserver_ip, sub_port), SubRequestHandler) thread2 = Thread(target=sub_server.handle_request) thread2.start() sub = "http://%s:%s?%s" % (webserver_ip, str(sub_server.server_port), quote_plus(subtitles, "/")) print("sub URL: " + sub) else: print("Subtitles file %s not found" % subtitles) load(cast, url, req_handler.content_type, sub, subtitles_language)