-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccastplayer.py
executable file
·479 lines (385 loc) · 14.2 KB
/
ccastplayer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/usr/bin/python3
#
# SPDX-License-Identifier: MIT
#
# Copyright (c) 2023-2024 Janne Snabb <snabb at epipe.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""A script for streaming a local or remote video file to Chromecast device."""
import argparse
from collections import namedtuple
from datetime import timedelta
from functools import partial
from http.server import BaseHTTPRequestHandler, HTTPStatus
import logging
import math
import mimetypes
import os
import re
import socketserver
import sys
import threading
from threading import Event
import time
from urllib.parse import urljoin
import pychromecast
from pychromecast.controllers.media import MediaStatus, MediaStatusListener
class MyMediaStatusListener(
MediaStatusListener
): # pylint: disable=too-few-public-methods
"""Class that receives media status reports and emits status line to
terminal."""
def new_media_status(self, status: MediaStatus):
if status.adjusted_current_time is not None:
current_time = str(
timedelta(seconds=math.floor(status.adjusted_current_time))
)
else:
current_time = "-:--:--"
if status.duration is not None:
duration = str(timedelta(seconds=math.floor(status.duration)))
else:
duration = "-:--:--"
# Refresh status line on terminal:
print(
f"{current_time}/{duration} {status.player_state} \r",
end="",
)
# Type for inventory entry of a local files that can be served:
File = namedtuple("File", ["local_path", "size", "mimetype"])
# Regex for parsing "range" HTTP header value:
BYTE_RANGE_RE = re.compile(r"bytes=(\d+)?-(\d+)?")
# Type for parsed "range" HTTP header value:
Range = namedtuple("Range", ["first", "last"])
class HTTPRequestHandler(BaseHTTPRequestHandler):
"""HTTP Request Handler class that serves local files to cast device.
Supports range requests."""
def __init__(self, *args, files, **kwargs):
self._files = files
self._file = None
self.range = Range(first=None, last=None)
super().__init__(*args, **kwargs)
def do_GET(self): # pylint: disable=invalid-name
"""Handle HTTP GET request."""
self.parse_range()
try:
success = self.send_head()
if not success:
return
with open(self._file.local_path, "rb") as rfile:
self.copyfile(rfile, self.wfile)
except (ConnectionResetError, BrokenPipeError):
pass
def do_HEAD(self): # pylint: disable=invalid-name
"""Handle HTTP HEAD request."""
try:
self.send_head()
except (ConnectionResetError, BrokenPipeError):
pass
def send_head(self):
"""Check HTTP request and send response headers."""
# Check if the request path can be found in our "inventory":
if self.path in self._files:
self._file = self._files[self.path]
else:
self.send_error(HTTPStatus.NOT_FOUND, "File not found")
return False
if self.range == Range(first=None, last=None):
# Normal HTTP request without "range":
self.send_response(HTTPStatus.OK)
self.send_header("Content-Length", self._file.size)
else:
# HTTP request for specific "range":
last_pos = self._file.size - 1
if self.range.first is None: # handle "bytes=-N" case
self.range = Range(
first=self._file.size - self.range.last, last=last_pos
)
elif self.range.last is None: # handle "bytes=N-" case
self.range = Range(first=self.range.first, last=last_pos)
if (
self.range.first > last_pos
or self.range.last > last_pos
or self.range.first > self.range.last
): # Return 416 response code in case the range was bad:
self.send_error(HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE)
self.send_header("Content-Range", f"bytes */{self._file.size}")
return False
self.send_response(HTTPStatus.PARTIAL_CONTENT)
self.send_header(
"Content-Length", str(self.range.last - self.range.first + 1)
)
self.send_header(
"Content-Range",
f"bytes {self.range.first}-{self.range.last}/{self._file.size}",
)
self.send_header("Content-Type", self._file.mimetype)
self.send_header("Accept-Ranges", "bytes")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
return True
def parse_range(self):
"""Parse range header. Supports only a single range."""
if not "Range" in self.headers:
return
match = BYTE_RANGE_RE.fullmatch(self.headers["Range"])
if not match:
return
str1 = match.group(1)
first = int(str1) if str1 is not None else None
str2 = match.group(2)
last = int(str2) if str2 is not None else None
self.range = Range(first=first, last=last)
def copyfile(self, rfile, wfile, bufsize=64 * 1024):
"""Copy file contents (all or range) from rfile to wfile."""
first, last = self.range
if first is None:
first = 0
if last is None:
last = self._file.size - 1
remaining = last - first + 1
if first > 0:
rfile.seek(first)
while remaining > 0:
buf = rfile.read(min(remaining, bufsize))
if buf == "":
# File got shorter while running?
break
wfile.write(buf)
remaining -= len(buf)
def log_request(self, code="-", size="-"):
"""Override log_request function to suppress log output."""
return
def start_httpd(server_address, files):
"""Start HTTP server for serving local files."""
# Use "partial" from "functools" to pass local file directory
# to the request handler:
handler = partial(HTTPRequestHandler, files=files)
# Use a threading server variant so that we can serve multiple
# HTTP requests simultaneously:
httpd = socketserver.ThreadingTCPServer(
server_address, handler, bind_and_activate=False
)
httpd.allow_reuse_address = True
httpd.daemon_threads = True
httpd.server_bind()
httpd.server_activate()
# Start the HTTP server in a daemon thread:
httpd_thread = threading.Thread(target=httpd.serve_forever, daemon=True)
httpd_thread.start()
def play_video(
cast,
video_url,
video_mimetype,
subs_url=None,
subs_mimetype=None,
wait_timeout=10,
idle_timeout=10,
): # pylint: disable=too-many-arguments
"""Play video on cast device and display status in terminal."""
media_controller = cast.media_controller
# Register a status listener that emits a status line on the terminal:
media_status_listener = MyMediaStatusListener()
media_controller.register_status_listener(media_status_listener)
# Start playback:
media_controller.play_media(
video_url,
video_mimetype,
subtitles=subs_url,
subtitles_mime=subs_mimetype,
)
media_controller.block_until_active(timeout=wait_timeout)
try:
idle_since = None
while True:
# Get status update from the cast device every second.
# As a side effect this will cause a status line refresh
# to be emitted to terminal.
media_controller.update_status()
if media_controller.status.player_is_idle:
if idle_since is None:
idle_since = time.monotonic()
else:
if time.monotonic() - idle_since > idle_timeout:
# Bail out if the player has been idle for 10 seconds.
# The video probably ended.
break
else:
idle_since = None
time.sleep(1)
except KeyboardInterrupt:
# Pressing ctrl-C stops playback.
pass
print("\nExiting")
media_controller.stop()
media_controller.tear_down()
cast.quit_app()
def prepare_source(source, mimetype, local_ip, local_port, url_path):
"""Prepare source file or URL for streaming."""
if mimetype is None:
mimetype = mimetypes.guess_type(source)[0]
if source.startswith("http://") or source.startswith("https://"):
# Remote URL
url = source
local_files = {}
else:
# Local file
url = urljoin(f"http://{local_ip}:{local_port}/", url_path)
local_files = {
url_path: File(
local_path=source,
size=os.path.getsize(source),
mimetype=mimetype,
)
}
return url, mimetype, local_files
def handle_args():
"""Handle command-line arguments."""
parser = argparse.ArgumentParser()
parser.add_argument("video_source", help="video source (local file or URL)")
parser.add_argument("--chromecast-name", help="Name of cast device")
parser.add_argument(
"--chromecast-ip",
help="Add known cast device IP, can be used multiple times",
action="append",
)
parser.add_argument(
"--discovery-timeout",
help="Cast device discovery timeout seconds (default: 10)",
type=int,
default=10,
)
parser.add_argument(
"--idle-timeout",
help="Cast device idle timeout seconds (default: 10)",
type=int,
default=10,
)
parser.add_argument(
"--wait-timeout",
help="Cast device wait timeout seconds (default: 10)",
type=int,
default=10,
)
parser.add_argument(
"--local-ip",
help="Local IP for serving video (default: autodetect)",
default="",
)
parser.add_argument(
"--local-port",
type=int,
help="Local port for serving video (default: 8080)",
default=8080,
)
parser.add_argument(
"--video-mimetype", help="Video source mimetype (default: autodetect)"
)
parser.add_argument("--subs", help="Subtitles source (local file or URL)")
parser.add_argument(
"--subs-mimetype", help="Subtitles source mimetype (default: autodetect)"
)
parser.add_argument("--debug", help="Enable debug output", action="store_true")
args = parser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
return args
def discover_cast(args):
"""Discover and/or select cast device."""
if args.chromecast_name:
# Find a named cast device:
chromecasts = pychromecast.get_listed_chromecasts(
friendly_names=[args.chromecast_name],
timeout=args.discovery_timeout,
known_hosts=args.chromecast_ip,
)[0]
else:
chromecasts = []
discover_complete = Event()
def found_device(cast):
chromecasts.append(cast)
discover_complete.set()
# Find one cast device (the first one to respond):
browser = pychromecast.get_chromecasts(
timeout=args.discovery_timeout,
known_hosts=args.chromecast_ip,
blocking=False,
callback=found_device,
)
discover_complete.wait(args.discovery_timeout)
if not chromecasts:
print("Could not find cast device")
sys.exit(1)
print(f"Discovered devices: {[cast.name for cast in chromecasts]}")
# Pick the first device if there are many:
cast = chromecasts[0]
print(f"Casting to: {cast.name}")
return cast
def main():
"""Main."""
args = handle_args()
cast = discover_cast(args)
cast.wait(timeout=args.wait_timeout)
# Determine our local IP address that the cast device should use when
# doing a reverse connection back to us:
if args.local_ip:
local_ip = args.local_ip
else:
local_ip = cast.socket_client.socket.getsockname()[0]
# Prepare video file or URL:
video_url, video_mimetype, local_files = prepare_source(
args.video_source,
args.video_mimetype,
local_ip,
args.local_port,
"/video",
)
# Prepare subtitles file or URL:
if args.subs is not None:
subs_url, subs_mimetype, subs_local_files = prepare_source(
args.subs,
args.subs_mimetype,
local_ip,
args.local_port,
"/subtitles",
)
local_files |= subs_local_files
else:
subs_url = None
subs_mimetype = None
# Start local HTTP server in case we need to serve local files
# to the cast device:
if local_files:
start_httpd((args.local_ip, args.local_port), local_files)
print(f"Video URL: {video_url} ({video_mimetype})")
if subs_url:
print(f"Subtitles URL: {subs_url} ({subs_mimetype})")
# Video playback:
play_video(
cast,
video_url,
video_mimetype,
subs_url=subs_url,
subs_mimetype=subs_mimetype,
wait_timeout=args.wait_timeout,
idle_timeout=args.idle_timeout,
)
cast.disconnect(timeout=args.wait_timeout)
if __name__ == "__main__":
main()