-
Notifications
You must be signed in to change notification settings - Fork 51
/
gnomecast.py
1583 lines (1399 loc) · 58.1 KB
/
gnomecast.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import contextlib, os, re, signal, socket, subprocess, sys, tempfile, threading, time, traceback, urllib
DEPS_MET = True
try:
import pychromecast
import bottle
import html5lib.treebuilders
# hack fixing pycaption needing an old version of html5lib
if not hasattr(html5lib.treebuilders, '_base'):
html5lib.treebuilders._base = html5lib.treebuilders.base
import pycaption
except Exception as e:
traceback.print_exc()
print(e)
DEPS_MET = False
DBUS_AVAILABLE = False
try:
import dbus
DBUS_AVAILABLE = True
except Exception as e:
print(e)
try:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GLib, GdkPixbuf, Gio
except ImportError:
line = "-"*70
ERROR_MESSAGE = """
{}
Python package "gi" (for building the GU not found.\n
If on Debian or Ubuntu, please run:
$ sudo apt-get install python3-gi\n
For other distributions please look up the equivalent package.\n
If this doesn't work, please report the error here:
https://github.com/keredson/gnomecast\n
Thanks! - Gnomecast
{}
"""
print(ERROR_MESSAGE.format(line,line))
sys.exit(1)
__version__ = '1.9.11'
if DEPS_MET:
pycaption.WebVTTWriter._encode = lambda self, s: s
class Device:
def __init__(self, h265=None, ac3=None):
self.h265 = h265
self.ac3 = ac3
HARDWARE = {
('Unknown manufacturer','Chromecast'): Device(h265=False, ac3=False),
('Unknown manufacturer','Chromecast Ultra'): Device(h265=True, ac3=True),
('Unknown manufacturer','Google Home Mini'): Device(h265=False, ac3=False),
('Unknown manufacturer','Google Home'): Device(h265=False, ac3=False),
('VIZIO','P75-F1'): Device(h265=True, ac3=True),
}
def throttle(seconds=2):
def decorator(f):
timer = None
lastest_args, latest_kwargs = None, None
def run_f():
nonlocal timer, lastest_args, latest_kwargs
ret = f(*lastest_args, **latest_kwargs)
timer = None
return ret
def wrapper(*args, **kwargs):
nonlocal timer, lastest_args, latest_kwargs
lastest_args, latest_kwargs = args, kwargs
if timer == None:
timer = threading.Timer(seconds, run_f)
timer.start()
return wrapper
return decorator
def find_screensaver_dbus_iface(bus):
""" Searches the DBus names for Screensaver and returns correct Interface"""
if not DBUS_AVAILABLE: return None
for path, name in [('org.freedesktop.ScreenSaver', '/ScreenSaver'), ('org.mate.ScreenSaver', '/ScreenSaver')]:
try:
saver = bus.get_object(path, name)
return dbus.Interface(saver, dbus_interface=path)
except dbus.exceptions.DBusException as e:
# wrong path, try next one
print(e)
return None
AUDIO_EXTS = ('aac', 'mp3', 'wav')
def parse_ffmpeg_time(time_s):
"""
Converts ffmpeg's time string to number of seconds
:param time_s:
:return: number of seconds
"""
hours, minutes, seconds = (float(s) for s in time_s.split(':'))
return hours * 60 * 60 + minutes * 60 + seconds
class StreamMetadata:
def __init__(self, index, codec, title=None):
self.index = index
self.codec = codec
self.title = title
def __repr__(self):
fields = ['%s:%s'%(k,v) for k,v in self.__dict__.items() if v is not None and not k.startswith('_')]
return '%s(%s)' % (self.__class__.__name__, ', '.join(fields))
class AudioMetadata(StreamMetadata):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.channels = 2
def details(self):
if self.channels == 1: channels = 'mono'
elif self.channels == 2: channels = 'stereo'
elif self.channels == 6: channels = '5.1'
elif self.channels == 8: channels = '7.1'
else: channels = str(self.channels)
return '%s (%s/%s)' % (self.title, self.codec, channels)
class FileMetadata(object):
def __init__(self, fn, callback=None, _ffmpeg_output=None):
self.fn = fn
self.ready = False
def parse():
self.thumbnail_fn = None
thumbnail_fn = tempfile.mkstemp(suffix='.jpg', prefix='gnomecast_pid%i_thumbnail_' % os.getpid())[1]
os.remove(thumbnail_fn)
self._ffmpeg_output = _ffmpeg_output if _ffmpeg_output else subprocess.check_output(
['ffmpeg', '-i', fn, '-f', 'ffmetadata', '-', '-f', 'mjpeg', '-vframes', '1', '-ss', '27', '-vf', 'scale=600:-1', thumbnail_fn],
stderr=subprocess.STDOUT
).decode()
_important_ffmpeg = []
if os.path.isfile(thumbnail_fn):
self.thumbnail_fn = thumbnail_fn
output = self._ffmpeg_output.split('\n')
self.container = fn.lower().split(".")[-1]
self.video_streams = []
self.audio_streams = []
self.subtitles = []
stream = None
for line in output:
line = line.strip()
if line.startswith('ffmpeg version'):
_important_ffmpeg.append(line)
if line.startswith('Stream') and 'Video' in line:
_important_ffmpeg.append(line)
id = line.split()[1].strip('#').strip(':')
title = 'Video #%i' % (len(self.video_streams)+1)
if '(' in id:
title = id[id.index('(')+1:id.index(')')]
id = id[:id.index('(')]
video_codec = line.split()[3]
stream = StreamMetadata(id, video_codec, title=title)
self.video_streams.append(stream)
elif line.startswith('Stream') and 'Audio' in line:
_important_ffmpeg.append(line)
title = 'Audio #%i' % (len(self.audio_streams)+1)
id = line.split()[1].strip('#').strip(':')
if '(' in id:
title = id[id.index('(')+1:id.index(')')]
id = id[:id.index('(')]
audio_codec = line.split()[3].strip(',')
stream = AudioMetadata(id, audio_codec, title=title)
if ', stereo, ' in line: stream.channels = 1
if ', stereo, ' in line: stream.channels = 2
if ', 5.1' in line: stream.channels = 6
if ', 7.1' in line: stream.channels = 8
self.audio_streams.append(stream)
elif line.startswith('Stream') and 'Subtitle' in line:
_important_ffmpeg.append(line)
id = line.split()[1].strip('#').strip(':')
print(line, id)
if '(' in id:
title = id[id.index('(')+1:id.index(')')]
id = id[:id.index('(')]
stream = StreamMetadata(id, None, title=title)
self.subtitles.append(stream)
elif stream and line.startswith('title'):
_important_ffmpeg.append(line)
stream.title = line.split()[2]
elif line.startswith('Output'):
break
self._important_ffmpeg = '\n'.join(_important_ffmpeg)
if not _ffmpeg_output:
self.load_subtitles()
self.ready = True
if callback: callback(self)
threading.Thread(target=parse).start()
def wait(self):
while not self.ready:
time.sleep(1)
def load_subtitles(self):
if not self.subtitles: return
cmd = ['ffmpeg', '-y', '-i', self.fn, '-vn', '-an',]
files = []
for stream in self.subtitles:
srt_fn = tempfile.mkstemp(suffix='.srt', prefix='gnomecast_pid%i_subtitles_' % os.getpid())[1]
files.append(srt_fn)
cmd += ['-map', stream.index, '-codec', 'srt', srt_fn]
print(cmd)
try:
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
for stream, srt_fn in zip(self.subtitles, files):
try:
with open(srt_fn) as f:
caps = f.read()
#print('caps', caps)
converter = pycaption.CaptionConverter()
converter.read(caps, pycaption.detect_format(caps)())
stream._subtitles = converter.write(pycaption.WebVTTWriter())
except Exception as e:
traceback.print_exc()
finally:
os.remove(srt_fn)
except subprocess.CalledProcessError as e:
print('ERROR processing subtitles:', e)
self.subtitles = []
def __repr__(self):
fields = ['%s:%s'%(k,v) for k,v in self.__dict__.items() if not k.startswith('_')]
return 'FileMetadata(%s)' % ', '.join(fields)
def details(self):
fields = [
'File: %s' % os.path.basename(self.fn),
'Video: %s' % ', '.join(['%s (%s)' % (s.title, s.codec) for s in self.video_streams]),
'Audio: %s' % ', '.join([s.details() for s in self.audio_streams]),
'Subtitles: %s' % ', '.join([s.title for s in self.subtitles]),
]
return '\n'.join(fields)
class Transcoder(object):
def __init__(self, cast, fmd, video_stream, audio_stream, done_callback, error_callback, prev_transcoder=None, force_audio=False, force_video=False, fake=False):
self.fmd = fmd
self.video_stream = video_stream
self.audio_stream = audio_stream
fn = fmd.fn
self.cast = cast
self.source_fn = fn
self.p = None
if prev_transcoder:
prev_transcoder.destroy()
print('Transcoder', fn)
transcode_container = fmd.container not in ('mp4', 'aac', 'mp3', 'wav')
self.transcode_video = force_video or not self.can_play_video_codec(video_stream.codec)
self.transcode_audio = force_audio or fmd.container not in AUDIO_EXTS or not self.can_play_audio_stream(self.audio_stream)
self.transcode = transcode_container or self.transcode_video or self.transcode_audio
self.trans_fn = None
self.progress_bytes = 0
self.progress_seconds = 0
self.done_callback = done_callback
self.error_callback = error_callback
print('transcode, transcode_video, transcode_audio', self.transcode, self.transcode_video, self.transcode_audio)
if self.transcode:
self.done = False
dir = '/var/tmp' if os.path.isdir('/var/tmp') else None
self.trans_fn = tempfile.mkstemp(suffix='.mp4', prefix='gnomecast_pid%i_transcode_' % os.getpid(), dir=dir)[1]
os.remove(self.trans_fn)
device_info = HARDWARE.get((self.cast.cast_info.manufacturer, self.cast.cast_info.model_name))
ac3 = device_info.ac3 if device_info else None
transcode_audio_to = 'ac3' if (ac3 or ac3 is None) and audio_stream and audio_stream.channels > 2 else 'mp3'
self.transcode_cmd = ['ffmpeg', '-i', self.source_fn, '-map', self.video_stream.index]
if self.audio_stream:
self.transcode_cmd += ['-map', self.audio_stream.index, '-c:a', transcode_audio_to if self.transcode_audio else 'copy'] + (['-b:a', '256k'] if self.transcode_audio else [])
self.transcode_cmd += ['-c:v', 'h264' if self.transcode_video else 'copy'] # '-movflags', 'faststart'
self.transcode_cmd += [self.trans_fn]
print(' '.join(["'%s'"%s if ' ' in s else s for s in self.transcode_cmd]))
if fake:
self.p = None
self.monitor()
else:
print('---------------------')
print(' starting ffmpeg at:')
print('---------------------')
traceback.print_stack()
self.p = subprocess.Popen(self.transcode_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
t = threading.Thread(target=self.monitor)
t.daemon = True
t.start()
else:
self.done = True
self.done_callback()
@property
def fn(self):
return self.trans_fn if self.transcode else self.source_fn
def can_play_video_codec(self, video_codec):
h265 = True
if self.cast.cast_info.cast_type == 'audio': h265 = False
device_info = HARDWARE.get((self.cast.cast_info.manufacturer, self.cast.cast_info.model_name))
if device_info and device_info.h265 is not None:
h265 = device_info.h265
if h265:
return video_codec in ('h264', 'h265', 'hevc')
else:
return video_codec in ('h264',)
def can_play_audio_stream(self, stream):
if not stream: return True
device_info = HARDWARE.get((self.cast.cast_info.manufacturer, self.cast.cast_info.model_name))
ac3 = device_info.ac3 if device_info else None
if ac3:
return stream.codec in ('aac', 'mp3', 'ac3')
else:
return stream.codec in ('aac', 'mp3')
def wait_for_byte(self, offset, buffer=128 * 1024 * 1024):
if self.done: return
if self.source_fn.lower().split(".")[-1] == 'mp4':
while offset > self.progress_bytes + buffer:
print('waiting for', offset, 'at', self.progress_bytes + buffer)
time.sleep(2)
else:
while not self.done:
print('waiting for transcode to finish')
time.sleep(2)
print('done waiting')
def monitor(self):
line = b''
r = re.compile(r'=\s+')
total_output = b''
while self.p:
byte = self.p.stdout.read(1)
total_output += byte
if byte == b'' and self.p.poll() != None:
break
if byte != b'':
line += byte
if byte == b'\r':
# frame=92578 fps=3937 q=-1.0 size= 1142542kB time=01:04:21.14 bitrate=2424.1kbits/s speed= 164x
line = line.decode()
line = r.sub('=', line)
items = [s.split('=') for s in line.split()]
d = dict([x for x in items if len(x) == 2])
print(d)
self.progress_bytes = int(d.get('size', '0kb')[:-2]) * 1024
self.progress_seconds = parse_ffmpeg_time(d.get('time', '00:00:00'))
line = b''
if self.p:
self.p.stdout.close()
if self.p.returncode:
print('--== transcode error ==--')
print(total_output)
self.error_callback(total_output.decode())
return
self.done = True
if self.done_callback:
self.done_callback(did_transcode=True)
def destroy(self):
if self.p and self.p.poll() is None:
self.p.terminate()
if self.trans_fn and os.path.isfile(self.trans_fn):
os.remove(self.trans_fn)
def __del__(self):
self.destroy()
class Gnomecast(object):
def __init__(self):
self.ip = (([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")] or [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) + [None])[0]
if 'GNOMECAST_HTTP_PORT' in os.environ:
self.port = int(os.environ['GNOMECAST_HTTP_PORT'])
else:
with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(('0.0.0.0', 0))
self.port = s.getsockname()[1]
self.app = bottle.Bottle()
self.cast = None
self.last_known_player_state = None
self.last_known_current_time = None
self.last_time_current_time = None
self.fn = None
self.video_stream = None
self.audio_stream = None
self.last_fn_played = None
self.transcoder = None
self.duration = None
self.subtitles = None
self.seeking = False
self.last_known_volume_level = None
bus = dbus.SessionBus() if DBUS_AVAILABLE else None
self.saver_interface = find_screensaver_dbus_iface(bus)
self.inhibit_screensaver_cookie = None
self.autoplay = False
def run(self, fn=None, device=None, subtitles=None):
self.build_gui()
self.init_casts(device=device)
threading.Thread(target=self.check_ffmpeg).start()
t = threading.Thread(target=self.start_server)
t.daemon = True
t.start()
t = threading.Thread(target=self.monitor_cast)
t.daemon = True
t.start()
if fn:
self.queue_files([fn])
if subtitles:
self.select_subtitles_file(subtitles)
if fn and subtitles:
self.autoplay = True
Gtk.main()
def check_ffmpeg(self):
time.sleep(1)
ffmpeg_available = True
print('check_ffmpeg')
try:
print(subprocess.check_output(['which', 'ffmpeg']))
except Exception as e:
print(e, e.output)
ffmpeg_available = False
if not ffmpeg_available:
def f():
dialog = Gtk.MessageDialog(self.win, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, "FFMPEG not Found")
dialog.format_secondary_text("Could not find ffmpeg. Please run 'sudo apt-get install ffmpeg'.")
dialog.run()
dialog.destroy()
# TODO: there's a weird pause here closing the dialog. why?
sys.exit(1)
GLib.idle_add(f)
def start_server(self):
app = self.app
@app.route('/subtitles.vtt')
def subtitles():
# response = bottle.static_file(self.subtitles_fn, root='/', mimetype='text/vtt')
response = bottle.response
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, HEAD'
response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
response.headers['Content-Type'] = 'text/vtt'
return self.subtitles
@app.get('/media/<id>.<ext>')
def video(id, ext):
print(list(bottle.request.headers.items()))
ranges = list(bottle.parse_range_header(bottle.request.environ['HTTP_RANGE'], 1000000000000))
print('ranges', ranges)
offset, end = ranges[0]
self.transcoder.wait_for_byte(offset)
response = bottle.static_file(self.transcoder.fn, root='/')
if 'Last-Modified' in response.headers:
del response.headers['Last-Modified']
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, HEAD'
response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
return response
# app.run(host=self.ip, port=self.port, server='paste', daemon=True)
from paste import httpserver
from paste.translogger import TransLogger
handler = TransLogger(app, setup_console_handler=True)
httpserver.serve(handler, host=self.ip, port=str(self.port), daemon_threads=True)
def update_status(self, did_transcode=False):
if did_transcode:
self.update_button_visible()
self.prep_next_transcode()
# if self.last_known_player_state and self.last_known_player_state!='UNKNOWN':
# notes.append('Cast: %s' % self.last_known_player_state)
def f():
for row in self.files_store:
duration = row[2]
transcoder = row[7]
if transcoder:
if duration:
if transcoder.done:
row[5] = 100
else:
row[5] = transcoder.progress_seconds*100 // duration
GLib.idle_add(f)
def monitor_cast(self):
while True:
time.sleep(1)
if not self.cast: continue
seeking = self.seeking
cast = self.cast
mc = cast.media_controller
if mc.status.player_state != self.last_known_player_state:
if mc.status.player_state=='PLAYING' and self.last_known_player_state=='BUFFERING' and seeking:
self.seeking = False
if mc.status.player_state=='IDLE' and self.last_known_player_state=='PLAYING':
self.check_for_next_in_queue()
if mc.status.player_state=='PLAYING':
self.inhibit_screensaver()
else:
self.restore_screensaver()
self.last_known_player_state = mc.status.player_state
def f():
self.update_media_button_states()
self.update_status()
GLib.idle_add(f)
elif self.transcoder and not self.transcoder.done:
def f():
self.update_status()
GLib.idle_add(f)
if self.last_known_current_time != mc.status.current_time:
self.last_known_current_time = mc.status.current_time
self.last_time_current_time = time.time()
if not seeking and mc.status.player_state=='PLAYING':
GLib.idle_add(lambda: self.scrubber_adj.set_value(mc.status.current_time + time.time() - self.last_time_current_time))
def init_casts(self, widget=None, device=None):
self.cast_store.clear()
self.cast_store.append([None, "Searching local network - please wait..."])
self.cast_combo.set_active(0)
threading.Thread(target=self.load_casts, kwargs={'device':device}).start()
def inhibit_screensaver(self):
if not self.saver_interface or self.inhibit_screensaver_cookie: return
self.inhibit_screensaver_cookie = self.saver_interface.Inhibit("Gnomecast", "Player is playing...")
print('disabled screensaver')
def restore_screensaver(self):
if self.saver_interface and self.inhibit_screensaver_cookie:
self.saver_interface.UnInhibit(self.inhibit_screensaver_cookie)
self.inhibit_screensaver_cookie = None
print('restored screensaver')
def load_casts(self, device=None):
chromecasts = pychromecast.get_chromecasts()
# workaround for https://github.com/home-assistant-libs/pychromecast/issues/398
if isinstance(chromecasts, tuple) and len(chromecasts)==2:
chromecasts = chromecasts[0]
def f():
self.cast_store.clear()
self.cast_store.append([None, "Select a cast device..."])
self.cast_store.append([-1, 'Add a non-local Chromecast...'])
for cc in chromecasts:
friendly_name = cc.cast_info.friendly_name
if cc.cast_type!='cast':
friendly_name = '%s (%s)' % (friendly_name, cc.cast_type)
self.cast_store.append([cc, friendly_name])
if device:
found = False
for i, cc in enumerate(chromecasts):
if device == cc.cast_info.friendly_name:
self.cast_combo.set_active(i+1)
found = True
if not found:
self.cast_combo.set_active(0)
dialog = Gtk.MessageDialog(self.win, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, "Chromecast Not Found")
dialog.format_secondary_text("The Chromecast '%s' wasn't found." % device)
dialog.run()
dialog.destroy()
else:
self.cast_combo.set_active(2 if len(chromecasts) == 1 else 0)
GLib.idle_add(f)
def update_media_button_states(self):
mc = self.cast.media_controller if self.cast else None
self.play_button.set_sensitive(bool(self.transcoder and self.cast and mc.status.player_state in ('BUFFERING','PLAYING','PAUSED','IDLE','UNKNOWN') and self.fn))
self.volume_button.set_sensitive(bool(self.cast))
self.stop_button.set_sensitive(bool(self.transcoder and self.cast and mc.status.player_state in ('BUFFERING','PLAYING','PAUSED')))
self.rewind_button.set_sensitive(bool(self.transcoder and self.cast and mc.status.player_state in ('BUFFERING','PLAYING','PAUSED')))
self.forward_button.set_sensitive(bool(self.transcoder and self.cast and mc.status.player_state in ('BUFFERING','PLAYING','PAUSED')))
self.play_button.set_image(Gtk.Image(stock=Gtk.STOCK_MEDIA_PAUSE) if self.cast and mc.status.player_state=='PLAYING' else Gtk.Image(stock=Gtk.STOCK_MEDIA_PLAY))
if self.transcoder and self.duration:
self.scrubber_adj.set_upper(self.duration)
self.scrubber.set_sensitive(True)
else:
self.scrubber.set_sensitive(False)
self.update_button_visible()
def build_gui(self):
self.win = win = Gtk.ApplicationWindow(title='Gnomecast v%s' % __version__)
win.set_border_width(0)
win.set_icon(self.get_logo_pixbuf(color='#000000'))
enforce_target = Gtk.TargetEntry.new('text/plain', Gtk.TargetFlags(4), 129)
win.drag_dest_set(Gtk.DestDefaults.ALL, [enforce_target], Gdk.DragAction.COPY)
win.connect("drag-data-received", self.on_drag_data_received)
self.cast_store = cast_store = Gtk.ListStore(object, str)
vbox_outer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=16)
self.thumbnail_image = Gtk.Image()
self.thumbnail_image.set_from_pixbuf(self.get_logo_pixbuf())
vbox_outer.pack_start(self.thumbnail_image, True, False, 0)
alignment = Gtk.Alignment(xscale=1, yscale=1)
alignment.add(vbox)
alignment.set_padding(16, 20, 16, 16)
vbox_outer.pack_start(alignment, False, False, 0)
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=8)
vbox.pack_start(hbox, False, False, 0)
self.cast_combo = cast_combo = Gtk.ComboBox.new_with_model(cast_store)
cast_combo.set_entry_text_column(1)
renderer_text = Gtk.CellRendererText()
cast_combo.pack_start(renderer_text, True)
cast_combo.add_attribute(renderer_text, "text", 1)
hbox.pack_start(cast_combo, True, True, 0)
refresh_button = Gtk.Button(None, image=Gtk.Image(stock=Gtk.STOCK_REFRESH))
refresh_button.connect("clicked", self.init_casts)
hbox.pack_start(refresh_button, False, False, 0)
win.add(vbox_outer)
# list of queued files
self.files_store = Gtk.ListStore(str, str, int, str, str, int, str, object, object) # name, path, duration, duration_str, thumbnail_fn, transcode_progress, status_icon, transcoder, file_metadata
self.files_store.connect("row-inserted", self.update_button_visible)
self.files_store.connect("row-deleted", self.update_button_visible)
self.files_view = Gtk.TreeView(self.files_store)
self.files_view.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE)
self.files_view.set_headers_visible(False)
self.files_view.set_rules_hint(True)
column = Gtk.TreeViewColumn("Name", Gtk.CellRendererText(), text=0)
column.set_expand(True)
self.files_view.append_column(column)
self.file_view_column_renderer = r = Gtk.CellRendererText()
r.props.xalign = 1.0
self.files_view.append_column(Gtk.TreeViewColumn("Duration", r, text=3))
self.files_view_progress_column = column_progress = Gtk.TreeViewColumn("Progress", Gtk.CellRendererProgress(), value=5)
self.files_view.append_column(column_progress)
column_pixbuf = Gtk.TreeViewColumn("Playing", Gtk.CellRendererPixbuf(), icon_name=6)
self.files_view.append_column(column_pixbuf)
select = self.files_view.get_selection()
select.connect("changed", self.on_files_view_selection_changed)
self.files_view.connect("row-activated", self.on_files_view_row_activated)
# contains the files list and the buttons to add/del
self.hbox = hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=8)
vbox.pack_start(hbox, False, False, 0)
self.scrolled_window = Gtk.ScrolledWindow()
self.scrolled_window.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
self.scrolled_window.add(self.files_view)
hbox.pack_start(self.scrolled_window, True, True, 0)
self.btn_vbox = btn_vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=8)
hbox.pack_start(btn_vbox, True, True, 0)
self.file_button = Gtk.Button(None, image=Gtk.Image(stock=Gtk.STOCK_ADD))
self.file_button.set_tooltip_text('Add one or more audio or video files...')
self.file_button.set_always_show_image(True)
self.file_button.connect("clicked", self.on_file_clicked)
btn_vbox.pack_start(self.file_button, True, True, 0)
self.remove_button = Gtk.Button(None, image=Gtk.Image(stock=Gtk.STOCK_REMOVE))
self.remove_button.set_tooltip_text('Overwrite original file with transcoded version.')
self.remove_button.connect("clicked", self.remove_files)
self.remove_button.set_sensitive(False)
btn_vbox.pack_start(self.remove_button, False, False, 0)
self.file_detail_row = hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=8)
vbox.pack_start(self.file_detail_row, False, False, 0)
# audio/video track selection
self.stream_store = Gtk.ListStore(str, object, object)
self.audio_combo = Gtk.ComboBox.new_with_model(self.stream_store)
self.audio_combo.connect("changed", self.on_audio_combo_changed)
self.audio_combo.set_entry_text_column(0)
renderer_text = Gtk.CellRendererText()
self.audio_combo.pack_start(renderer_text, True)
self.audio_combo.add_attribute(renderer_text, "text", 0)
self.file_detail_row.pack_start(self.audio_combo, True, True, 0)
# subtitle selection
self.subtitle_store = Gtk.ListStore(str, object, object) # title, stream, callback
self.subtitle_combo = Gtk.ComboBox.new_with_model(self.subtitle_store)
self.subtitle_combo.connect("changed", self.on_subtitle_combo_changed)
self.subtitle_combo.set_entry_text_column(0)
renderer_text = Gtk.CellRendererText()
self.subtitle_combo.pack_start(renderer_text, True)
self.subtitle_combo.add_attribute(renderer_text, "text", 0)
self.subtitle_combo.set_active(0)
self.file_detail_row.pack_start(self.subtitle_combo, True, True, 0)
file_info_button = Gtk.Button(None, image=Gtk.Image(stock=Gtk.STOCK_DIALOG_INFO))
file_info_button.connect("clicked", self.show_file_info)
self.file_detail_row.pack_start(file_info_button, False, False, 0)
self.scrubber_adj = Gtk.Adjustment(0, 0, 100, 15, 60, 0)
self.scrubber = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL, adjustment=self.scrubber_adj)
self.scrubber.set_digits(0)
def f(scale, s):
notes = [self.humanize_seconds(s)]
return ''.join(notes)
self.scrubber.connect("format-value", f)
self.scrubber.connect("change-value", self.scrubber_move_started)
self.scrubber.connect("change-value", self.scrubber_moved)
self.scrubber.set_sensitive(False)
vbox.pack_start(self.scrubber, False, False, 0)
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=16)
self.rewind_button = Gtk.Button(None, image=Gtk.Image(stock=Gtk.STOCK_MEDIA_REWIND))
self.rewind_button.connect("clicked", self.rewind_clicked)
self.rewind_button.set_sensitive(False)
self.rewind_button.set_relief(Gtk.ReliefStyle.NONE)
hbox.pack_start(self.rewind_button, True, False, 0)
self.play_button = Gtk.Button(None, image=Gtk.Image(stock=Gtk.STOCK_MEDIA_PLAY))
self.play_button.connect("clicked", self.play_clicked)
self.play_button.set_sensitive(False)
self.play_button.set_relief(Gtk.ReliefStyle.NONE)
hbox.pack_start(self.play_button, True, False, 0)
self.forward_button = Gtk.Button(None, image=Gtk.Image(stock=Gtk.STOCK_MEDIA_FORWARD))
self.forward_button.connect("clicked", self.forward_clicked)
self.forward_button.set_sensitive(False)
self.forward_button.set_relief(Gtk.ReliefStyle.NONE)
hbox.pack_start(self.forward_button, True, False, 0)
self.stop_button = Gtk.Button(None, image=Gtk.Image(stock=Gtk.STOCK_MEDIA_STOP))
self.stop_button.connect("clicked", self.stop_clicked)
self.stop_button.set_sensitive(False)
self.stop_button.set_relief(Gtk.ReliefStyle.NONE)
hbox.pack_start(self.stop_button, True, False, 0)
self.volume_button = Gtk.VolumeButton()
self.volume_button.set_value(1)
self.volume_button.connect("value-changed", self.volume_moved)
self.volume_button.set_sensitive(False)
hbox.pack_start(self.volume_button, True, False, 0)
vbox.pack_start(hbox, False, False, 0)
cast_combo.connect("changed", self.on_cast_combo_changed)
win.connect("delete-event", self.quit)
win.connect("key_press_event", self.on_key_press)
win.show_all()
self.update_button_visible()
win.resize(1,1)
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, self.quit)
def add_extra_subtitle_options(self):
self.subtitle_store.prepend(["No subtitles.", None, None])
self.subtitle_store.append(["Add subtitle file...", None, self.on_new_subtitle_clicked])
#self.subtitle_store.append(["Download...", None, self.on_download_subtitle_clicked])
self.subtitle_combo.set_active(0)
def on_drag_data_received(self, widget, drag_context, x,y, data,info, time):
fn = data.get_text()
if fn.startswith('file://'):
fn = urllib.parse.unquote(fn[len('file://'):]).strip()
self.queue_files([fn])
def update_button_visible(self, x=None, y=None, z=None):
print('update_button_visible')
count = len(self.files_store)
self.scrolled_window.set_visible(count)
self.remove_button.set_visible(count)
self.file_button.set_label('' if count else ' Add one or more audio or video files...')
self.file_button.get_child().set_padding(1,0,2,0) # w/ an empty label the + icon isn't quite centered
self.hbox.set_child_packing(self.btn_vbox, not count, not count, 0, Gtk.PackType.START)
self.file_detail_row.set_visible(bool(self.fn))
def scrubber_move_started(self, scale, scroll_type, seconds):
print('scrubber_move_started', seconds)
self.seeking = True
def on_files_view_selection_changed(self, selection):
model, treeiter = selection.get_selected_rows()
self.remove_button.set_sensitive(bool(treeiter))
def remove_files(self, w):
store, paths = self.files_view.get_selection().get_selected_rows()
for path in reversed(paths):
print('remove', path)
iterx = store.get_iter(path)
transcoder = store.get_value(iterx, 7)
if transcoder:
transcoder.destroy()
fn = store.get_value(iterx, 1)
store.remove(iterx)
if self.fn == fn:
self.unselect_file()
def on_files_view_row_activated(self, widget, row, col):
model = widget.get_model()
print('double-clicked', model[row][:])
fn = model[row][1]
self.unselect_file()
self.fn = fn
self.transcoder = model[row][7]
self.duration = model[row][2]
thumbnail_fn = model[row][4]
if thumbnail_fn and os.path.isfile(thumbnail_fn):
self.thumbnail_image.set_from_file(thumbnail_fn)
if self.cast:
self.cast.media_controller.stop()
def f():
self.win.resize(1,1)
self.scrubber_adj.set_value(0)
for row in self.files_store:
if self.fn == row[1]:
row[6] = 'video-x-generic'
else:
row[6] = None
self.update_button_visible()
self.update_media_button_states()
GLib.idle_add(f)
return True
def queue_files(self, files):
existing_files = set([row[1] for row in self.files_store])
files = [f for f in files if f not in existing_files]
for fn in files:
display = os.path.basename(fn)
MAX_LEN = 40
if len(display) > MAX_LEN:
display = display[:MAX_LEN-10] + '...' + display[-10:]
def callback(fmd):
print(fmd)
if os.path.isfile(fmd.thumbnail_fn):
for row in self.files_store:
if row[1]==fmd.fn:
row[4] = fmd.thumbnail_fn
def f():
if self.fn == fmd.fn and fmd.thumbnail_fn:
self.thumbnail_image.set_from_file(fmd.thumbnail_fn)
self.win.resize(1,1)
self.update_status()
GLib.idle_add(f)
fmd = FileMetadata(fn, callback)
self.files_store.append([display, fn, None, '...', None, None, None, None, fmd])
threading.Thread(target=self.get_info, args=[fn]).start()
self.scrolled_window.set_visible(True)
if len(files) and self.fn is None:
self.select_file(files[0])
path = Gtk.TreePath().new_first()
_1, _2, width, height = self.files_view_progress_column.cell_get_size()
height += self.file_view_column_renderer.get_padding().ypad*2
height += 2 # measured - row lines?
self.scrolled_window.set_min_content_height(height*min(len(self.files_store),6))
@throttle(seconds=1)
def volume_moved(self, button, volume):
if self.last_known_volume_level != volume:
self.last_known_volume_level = volume
self.cast.set_volume(volume)
print('setting volume', volume)
@throttle()
def scrubber_moved(self, scale, scroll_type, seconds):
print('scrubber_moved', seconds)
self.seeking = True
self.cast.media_controller.seek(seconds)
def humanize_seconds(self, s):
s = int(s)
hours = s // (60*60)
minutes = (s // 60) % 60
seconds = s % 60
if hours:
return '%ih %im %is' % (hours, minutes, seconds)
if minutes:
return '%im %is' % (minutes, seconds)
else:
return '%is' % (seconds)
def stop_clicked(self, widget):
if not self.cast: return
self.cast.media_controller.stop()
def get_logo_pixbuf(self, width=200, color=None):
svg = LOGO_SVG
if color:
svg = svg.replace('#aaaaaa', color)
f = Gio.MemoryInputStream.new_from_bytes(GLib.Bytes.new(svg.encode()))
preserve_aspect_ratio = True
pixbuf = GdkPixbuf.Pixbuf.new_from_stream(f, None)
return pixbuf
def quit(self, a=0, b=0):
for row in self.files_store:
transcoder = row[7]
if transcoder:
transcoder.destroy()
thumbnail_fn = row[4]
if thumbnail_fn and os.path.isfile(thumbnail_fn):
os.remove(thumbnail_fn)
self.restore_screensaver()
Gtk.main_quit()
def forward_clicked(self, widget):
self.seek_delta(30)
def rewind_clicked(self, widget):
self.seek_delta(-10)
def seek_delta(self, delta):
seconds = self.cast.media_controller.status.current_time + time.time() - self.last_time_current_time + delta
self.last_time_current_time = time.time()
self.cast.media_controller.status.current_time = seconds
self.scrubber_adj.set_value(seconds)
self.seeking = True
self.cast.media_controller.seek(seconds)
def play_clicked(self, widget):
if not self.cast:
print('no cast selected')
return
cast = self.cast
mc = cast.media_controller
print('mc.status.player_state', mc.status.player_state, self.fn, hash(self.fn))
if mc.status.player_state in ('IDLE','UNKNOWN') or self.last_fn_played != self.fn:
self.last_fn_played = self.fn
cast.wait()
mc = cast.media_controller
kwargs = {}
if self.subtitles:
kwargs['subtitles'] = 'http://%s:%s/subtitles.vtt' % (self.ip, self.port)
current_time = self.scrubber_adj.get_value()
if current_time:
kwargs['current_time'] = current_time
ext = self.fn.split('.')[-1]
ext = ''.join(ch for ch in ext if ch.isalnum()).lower()
mc.play_media('http://%s:%s/media/%s.%s' % (self.ip, self.port, hash(self.fn), ext), 'audio/%s'%ext if ext in AUDIO_EXTS else 'video/mp4', **kwargs)
print(cast.status)
print(mc.status)
self.prep_next_transcode()
elif mc.status.player_state=='PLAYING':
mc.pause()
elif mc.status.player_state=='PAUSED':
mc.play()
def on_file_clicked(self, widget):
dialog = Gtk.FileChooserDialog("Please choose an audio or video file...", self.win,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
dialog.set_select_multiple(True)
downloads_dir = os.path.expanduser('~/Downloads')
if os.path.isdir(downloads_dir):
dialog.set_current_folder(downloads_dir)
filter_py = Gtk.FileFilter()
filter_py.set_name("Videos")
filter_py.add_mime_type("video/*")
filter_py.add_mime_type("audio/*")
dialog.add_filter(filter_py)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("Open clicked")
print("File selected:", dialog.get_filenames())
self.queue_files(dialog.get_filenames())
#self.select_file(dialog.get_filename())
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
def on_new_subtitle_clicked(self):
dialog = Gtk.FileChooserDialog("Please choose a subtitle file...", self.win,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,