-
Notifications
You must be signed in to change notification settings - Fork 5
/
migaku_mpv.py
841 lines (630 loc) · 24.1 KB
/
migaku_mpv.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
import sys
import os
import time
import shutil
import queue
import json
import subprocess
import collections
import psutil
import pathlib
import webbrowser
import threading
import traceback
import platform
import pysubs2
import codecs
import cchardet as chardet
import requests
import urllib.parse
import urllib.request
from utils.mpv_ipc import MpvIpc
from utils.server import HttpServer, HttpResponse
from utils.ankiexport import AnkiExporter
import utils.browser_support as browser_support
dev_mode = os.path.exists('./dev_flag')
plugin_is_packaged = getattr(sys, 'frozen', False) # if built with pyinstaller
if plugin_is_packaged:
plugin_dir = os.path.dirname(sys.executable)
else:
plugin_dir = os.path.dirname(os.path.abspath(__file__))
plugin_dir_name = os.path.basename(plugin_dir)
tmp_dir = os.path.join(plugin_dir, 'tmp')
mpv = None
host = '127.0.0.1'
port = None
media_path = None
audio_track = None
resx = 1920
resy = 1080
sub_font_name = 'Noto Sans CJK JP Regular'
sub_font_size = 55
sub_bottom_margin = 22
sub_outline_size = 3
sub_shadow_offset = 0
subs_json = '[]'
secondary_subs_json = '[]'
subs_delay = 0
anki_exporter = AnkiExporter()
data_queues = []
data_queues_lock = threading.Lock()
last_subs_request = 0
config = {}
webbrowser_name = None
reuse_last_tab = True
reuse_last_tab_timeout = 1.5
ffmpeg = 'ffmpeg'
ffsubsync = 'ffsubsync'
rubysubs = 'rubysubs'
mpv_external = None
skip_empty_subs = True
subtitle_export_timeout = 0
log_file = None
def path_clean(path):
if path.startswith('file:'):
uri_path = urllib.parse.urlparse(path).path
return urllib.request.url2pathname(uri_path)
return path
### Handlers for GET requests
def get_handler_subs(socket):
global last_subs_request
last_subs_request = time.time()
r = HttpResponse(content=subs_json.encode(), content_type='text/html')
r.send(socket)
def get_handler_secondary_subs(socket):
global last_subs_request
last_subs_request = time.time()
r = HttpResponse(content=secondary_subs_json.encode(), content_type='text/html')
r.send(socket)
def get_handler_data(socket):
r = HttpResponse(content_type='text/event-stream', headers={'Cache-Control': 'no-cache'})
r.send(socket)
q = queue.Queue()
data_queues_lock.acquire()
data_queues.append(q)
data_queues_lock.release()
keep_listening = True
while keep_listening:
data = q.get()
if len(data) < 1:
keep_listening = False
else:
cmd = data[0]
if cmd in ['s', 'r', 'e', 'l']:
send_msg = 'data: ' + data + '\r\n\r\n'
try:
socket.sendall(send_msg.encode())
except:
keep_listening = False
else:
keep_listening = False
q.task_done()
data_queues_lock.acquire()
data_queues.remove(q)
data_queues_lock.release()
### Handlers for POST requests
def post_handler_anki(socket, data):
r = HttpResponse()
r.send(socket)
if audio_track < 0:
mpv.show_text('Please select an audio track before opening Migaku MPV if you want to export Anki cards.')
return
json_data = data.decode()
cards = json.loads(json_data)
is_mass_export = len(cards) > 1
timestamp = time.time()
for i, card in enumerate(cards):
text = card['text']
translation_text = card['translation_text']
unknowns = card['unknowns']
# ms to seconds
start = card['start'] / 1000.0
end = card['end'] / 1000.0
try:
anki_exporter.export_card(media_path, audio_track, text, translation_text, start, end, unknowns, i, len(cards), timestamp)
except AnkiExporter.ExportError as e:
mpv.show_text('Exporting card failed:\n\n' + str(e), 8.0)
return
if is_mass_export:
mpv.show_text('%d/%d' % (i+1, len(cards)), 10.0)
if is_mass_export:
mpv.show_text('Card export finished.')
def post_handler_mpv_control(socket, data):
mpv.send_json_txt(data.decode())
r = HttpResponse()
r.send(socket)
def post_handler_set_subs(socket, data):
global subs_delay
r = HttpResponse()
r.send(socket)
if rubysubs is None:
mpv.show_text('Subtitle styling requires rubysubs to be located in the plugin directory.')
return
if data:
path = os.path.join(tmp_dir, 'migaku_parsed_%d.ass' % round(time.time() * 1000))
json_data = json.loads(data)
subs = pysubs2.SSAFile()
subs.info = {
'Title': 'Migaku Parsed',
'PlayResX': str(resx),
'PlayResY': str(resy),
# "ScriptType: v4.00+" automatically added
}
font_name = sub_font_name
font_size = sub_font_size
font_size = int((resy / 720) * font_size)
bottom_margin = sub_bottom_margin
bottom_margin = int((resy / 720) * bottom_margin)
outline_size = sub_outline_size
outline_size = int((resy / 720) * outline_size)
shadow_offset = sub_shadow_offset
shadow_offset = int((resy / 720) * shadow_offset)
subs.styles = {
'Default': pysubs2.SSAStyle(
fontname=font_name,
fontsize=font_size,
primarycolor=pysubs2.Color(255, 255, 255, 0),
secondarycolor=pysubs2.Color(255, 0, 0, 0),
outlinecolor=pysubs2.Color(0, 0, 0, 0),
backcolor=pysubs2.Color(0, 0, 0, 0),
bold=False,
italic=False,
underline=False,
strikeout=False,
scalex=100,
scaley=100,
spacing=0,
angle=0,
borderstyle=1,
outline=outline_size,
shadow=shadow_offset,
alignment=2,
marginl=0,
marginr=0,
marginv=bottom_margin
),
}
for (start, end, text) in json_data['subs']:
text = text.replace('\n', '\\N')
text = text.replace(' ', '\u00A0')
subs.events.append(pysubs2.SSAEvent(start=start, end=end, text=text))
subs.save(path)
r = subprocess.run([rubysubs, path, path, json_data['parser'], *json_data['parser_args']])
if r.returncode == 0:
mpv.command('sub-add', path)
mpv.command('set_property', 'sub-delay', 0)
subs_delay = 0
mpv.command('script-message', '@migakulua', 'remove_inactive_parsed_subs')
else:
mpv.show_text('Styling subtitles failed.')
### Managing data streams
def stop_get_data_handlers():
data_queues_lock.acquire()
for q in data_queues:
q.put('q')
data_queues_lock.release()
def send_subtitle_time(arg):
time_millis = (int(round(float(arg) * 1000)) + subs_delay) // 10 * 10
data_queues_lock.acquire()
for q in data_queues:
q.put('s' + str(time_millis))
data_queues_lock.release()
def browser_export_current():
data_queues_lock.acquire()
for q in data_queues:
q.put('e')
data_queues_lock.release()
def browser_lookup_current():
data_queues_lock.acquire()
for q in data_queues:
q.put('l')
data_queues_lock.release()
def open_webbrowser_new_tab():
url = 'http://' + str(host) + ':' + str(port)
try:
webbrowser.get(webbrowser_name).open(url, new=0, autoraise=True)
except:
mpv.show_text('Warning: Opening the subtitle browser with configured browser failed.\n\nPlease review your config.')
webbrowser.open(url, new=0, autoraise=True)
def tab_reload_timeout():
time.sleep(reuse_last_tab_timeout)
if last_subs_request < (time.time() - (reuse_last_tab_timeout + 0.25)):
print('BRS: Tab timed out.')
open_webbrowser_new_tab()
class SubtitleLoadError(Exception):
pass
def load_subs_from_info(sub_info):
sub_path = None
if '*' in sub_info:
internal_sub_info = sub_info.split('*')
if len(internal_sub_info) == 2:
ffmpeg_track = internal_sub_info[0]
sub_codec = internal_sub_info[1]
if sub_codec in ['subrip', 'ass']:
if not ffmpeg:
raise SubtitleLoadError('Using internal subtitles requires ffmpeg to be located in the plugin directory.')
mpv.show_text('Exporting internal subtitle track...', duration=150.0) # Next osd message will close it
if sub_codec == 'subrip':
sub_extension = 'srt'
else:
sub_extension = sub_codec
sub_path = tmp_dir + '/' + str(pathlib.Path(media_path).stem) + '.' + sub_extension
args = [ffmpeg, '-y', '-loglevel', 'error', '-i', media_path, '-map', '0:' + ffmpeg_track, sub_path]
try:
timeout = subtitle_export_timeout if subtitle_export_timeout > 0 else None
subprocess.run(args, timeout=timeout)
if not os.path.isfile(sub_path):
raise FileNotFoundError
except TimeoutError:
raise SubtitleLoadError('Exporting internal subtitle track timed out.')
except:
raise SubtitleLoadError('Exporting internal subtitle track failed.')
else:
raise SubtitleLoadError('Selected internal subtitle track is not supported.\n\nOnly SRT and ASS tracks are supported.\n\nSelected track is ' + sub_codec)
else:
sub_path = sub_info
if not sub_path: # Should not happen
raise SubtitleLoadError('Unkown error occured.')
# Support drag & drop subtitle files on some systems
sub_path = path_clean(sub_path)
# Web subtitle?
is_websub = False
if sub_path.startswith('edl://'):
i = sub_path.rfind('http')
if i >= 0:
url = sub_path[i:]
try:
response = requests.get(url)
tmp_sub_path = os.path.join(tmp_dir, 'websub_%d.vtt' % round(time.time() * 1000))
with open(tmp_sub_path, 'wb') as f:
f.write(response.content)
sub_path = tmp_sub_path
is_websub = True
except Exception:
raise SubtitleLoadError('Downloading web subtitles failed.')
elif sub_path.startswith('http'):
try:
response = requests.get(sub_path)
tmp_sub_path = os.path.join(tmp_dir, 'websub_%d' % round(time.time() * 1000))
with open(tmp_sub_path, 'wb') as f:
f.write(response.content)
sub_path = tmp_sub_path
except Exception:
raise SubtitleLoadError('Downloading web subtitles failed.')
if not os.path.isfile(sub_path):
print('SUBS Not found:', sub_path)
raise SubtitleLoadError('The subtitle file "%s" was not found.' % sub_path)
# Determine subs encoding
subs_encoding = 'utf-8'
try:
subs_f = open(sub_path, 'rb')
subs_data = subs_f.read()
subs_f.close()
boms_for_enc = [
('utf-32', (codecs.BOM_UTF32_LE, codecs.BOM_UTF32_BE)),
('utf-16', (codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE)),
('utf-8-sig', (codecs.BOM_UTF8,)),
]
for enc, boms in boms_for_enc:
if any(subs_data.startswith(bom) for bom in boms):
subs_encoding = enc
print('SUBS: Detected encoding (bom):', enc)
break
else:
chardet_ret = chardet.detect(subs_data)
subs_encoding = chardet_ret['encoding']
print('SUBS: Detected encoding (chardet):', chardet_ret)
except:
print('SUBS: Detecting encoding failed. Defaulting to utf-8')
# Parse subs and generate json for frontend
try:
with open(sub_path, encoding=subs_encoding, errors='replace') as fp:
subs = pysubs2.SSAFile.from_file(fp)
except:
raise SubtitleLoadError('Loading subtitle file "%s" failed.' % sub_path)
subs.sort()
subs_list = []
for s in subs:
text = s.plaintext.strip()
# Temporary to correct pysubs2 parsing mistakes
if is_websub:
text = text.split('\n\n')[0]
if not skip_empty_subs or text.strip():
sub_start = max(s.start + subs_delay, 0) // 10 * 10
sub_end = max(s.end + subs_delay, 0) // 10 * 10
subs_list.append( { 'text': text, 'start': sub_start, 'end': sub_end } )
return subs_list
### Called when user presses the migaku key in mpv, transmits info about playing environment
# TODO: Split this
def load_and_open_migaku(mpv_cwd, mpv_pid, mpv_media_path, mpv_audio_track, mpv_sub_info, mpv_secondary_sub_info, mpv_subs_delay, mpv_resx, mpv_resy):
global subs_json
global secondary_subs_json
global media_path
global audio_track
global subs_delay
global resx
global resy
if port is None:
mpv.show_text('Not ready...')
return
mpv_executable = psutil.Process(int(mpv_pid)).cmdline()[0]
if os.path.split(mpv_executable)[-1].lower() in ['mpv', 'mpv.exe', 'mpv-bundle']:
anki_exporter.mpv_cwd = mpv_cwd
anki_exporter.mpv_executable = mpv_executable
else:
print('Using external mpv')
if not mpv_external:
mpv.show_text('Please set mpv_external in the config file.')
return
anki_exporter.mpv_cwd = None
anki_exporter.mpv_executable = mpv_external
media_path = mpv_media_path
audio_track = int(mpv_audio_track)
subs_delay = int(round(float(mpv_subs_delay) * 1000))
resx = int(mpv_resx)
resy = int(mpv_resy)
if not mpv_sub_info:
mpv.show_text('Please select a subtitle track.')
return
if 'migaku_parsed' in mpv_sub_info:
mpv.show_text('Please select a subtitle track that was not created by Migaku.')
return
secondary_subs = []
if mpv_secondary_sub_info:
try:
secondary_subs = load_subs_from_info(mpv_secondary_sub_info)
except SubtitleLoadError as e:
pass
try:
subs = load_subs_from_info(mpv_sub_info)
except SubtitleLoadError as e:
mpv.show_text(str(e))
return
subs_json = json.dumps(subs)
secondary_subs_json = json.dumps(secondary_subs)
# Open or refresh frontend
mpv.show_text('Opening in Browser...', 2.0)
open_new_tab = False
data_queues_lock.acquire()
finalize_queues = data_queues
if reuse_last_tab and len(data_queues) > 0:
data_queues[-1].put('r')
finalize_queues = finalize_queues[:-1]
t = threading.Thread(target=tab_reload_timeout)
t.start()
else:
open_new_tab = True
for q in finalize_queues:
q.put('q')
data_queues_lock.release()
if open_new_tab:
open_webbrowser_new_tab()
def resync_subtitle(resync_sub_path, resync_reference_path, resync_reference_track):
if ffmpeg is None:
mpv.show_text('Subtitle syncing requires ffmpeg to be located in the plugin directory.')
return
# Support drag & drop subtitle files on some systems
resync_sub_path = path_clean(resync_sub_path)
mpv.show_text('Syncing subtitles to reference track. Please wait...', duration=150.0) # Next osd message will close it
path_ext_split = os.path.splitext(resync_sub_path) # [path_without_extension, extension_with_dot]
out_base_path = path_ext_split[0] + '-resynced' # Out path without index or extension
out_path = out_base_path + path_ext_split[1]
# If the out path already exists count up until free file is found
try_i = 1
while os.path.exists(out_path):
out_path = out_base_path + '-' + str(try_i) + path_ext_split[1]
try_i += 1
# Run actual syncing in thread
def sync_thread_func():
r = subprocess.run([ffsubsync, resync_reference_path, '-i', resync_sub_path, '-o', out_path, '--reftrack', resync_reference_track, '--ffmpeg-path', os.path.dirname(ffmpeg)])
if r.returncode == 0:
mpv.command('sub-add', out_path)
mpv.show_text('Syncing finished.')
else:
mpv.show_text('Syncing failed.')
t = threading.Thread(target=sync_thread_func)
t.start()
def exception_hook(exc_type, exc_value, exc_traceback):
print('--------------')
print('UNHANDLED EXCEPTION OCCURED:\n')
print('Platform:', platform.platform())
print('Python:', sys.version.replace('\n', ' '))
traceback_strs = traceback.format_exception(exc_type, exc_value, exc_traceback)
traceback_str = ''.join(traceback_strs)
print(traceback_str)
print('EXITING')
# What folllows is pretty dirty, but all threads need to die and I'm lazy right now
# TODO
try:
sys.stdout.flush()
sys.stderr.flush()
if log_file:
log_file.flush()
log_file.close()
except:
pass
os._exit(1)
def exception_hook_threads(args):
exception_hook(args.exc_type, args.exc_value, args.exc_traceback)
def install_except_hooks():
sys.excepthook = exception_hook
if hasattr(threading, 'excepthook'):
threading.excepthook = exception_hook_threads
else:
run_old = threading.Thread.run
LegacyExceptHookArgs = collections.namedtuple('LegacyExceptHookArgs', 'exc_type exc_value exc_traceback thread')
def run_new(*args, **kwargs):
try:
run_old(*args, **kwargs)
except:
exception_hook_threads(LegacyExceptHookArgs(*sys.exc_info(), None))
threading.Thread.run = run_new
def find_executable(name, config_name=None):
if config_name is None:
config_name = name
# Check if defined in config and exists
if config_name in config:
config_exe_path = config.get(config_name, config_name)
if os.path.isfile(config_exe_path):
return config_exe_path
check_paths = [
os.path.join(plugin_dir, name, name),
os.path.join(plugin_dir, name),
]
# On Windows also check for .exe files
if platform.system() == 'Windows':
for cp in check_paths.copy():
check_paths.append(cp + '.exe')
for cp in check_paths:
if os.path.isfile(cp):
return cp
return shutil.which(name) # Set to none when not found
def main():
global log_file
global mpv
global host
global port
global reuse_last_tab
global reuse_last_tab_timeout
global webbrowser_name
global ffmpeg
global ffsubsync
global rubysubs
global mpv_external
global skip_empty_subs
global sub_font_name
global sub_font_size
global sub_bottom_margin
global sub_outline_size
global sub_shadow_offset
global subtitle_export_timeout
install_except_hooks()
# Redirect stdout/stderr to log file if built for release
if not dev_mode:
print('Redirecting stout and stderr to log.txt...')
log_file = open(plugin_dir + '/log.txt', 'w', encoding='utf8')
sys.stdout = log_file
sys.stderr = log_file
print('ARGS:', sys.argv)
# Check command line args
if len(sys.argv) != 2 and len(sys.argv) != 3:
print('ARGS: Usage: %s mpv-ipc-handle [config path]' % sys.argv[0])
return
config_path = plugin_dir + '/migaku_mpv.cfg'
if len(sys.argv) >= 3:
config_path = sys.argv[2]
# Clear/create temp dir
shutil.rmtree(tmp_dir, ignore_errors=True)
os.makedirs(tmp_dir, exist_ok=True)
# Load config
config_f = open(config_path, 'r', encoding="utf-8")
for line in config_f:
line = line.strip()
if line.startswith('#'):
continue
equals_pos = line.find('=')
if equals_pos < 0:
continue
key = line[0:equals_pos].strip()
if key == '':
continue
value = line[equals_pos+1:].strip()
config[key] = value
config_f.close()
print('CFG:', config)
host = config.get('host', '127.0.0.1')
try:
try_port_min = int(config.get('port', '2222'))
except:
try_port_min = 2222
try:
try_port_max = int(config.get('port_max', '2233'))
if try_port_max < try_port_min:
try_port_max = try_port_min
except:
try_port_max = 2233
try_ports = range(try_port_min, try_port_max+1)
reuse_last_tab = config.get('reuse_last_tab', 'yes').lower() == 'yes'
try:
reuse_last_tab_timeout = float(config.get('reuse_last_tab_timeout', '1.5'))
except:
reuse_last_tab_timeout = 1.5
browser = config.get('browser', 'default')
if browser.lower() == 'default':
browser = None
else:
browser = browser_support.expand_browser_name(browser)
print('BRS:', browser)
webbrowser_name = browser
anki_w = None
anki_h = None
try:
anki_w = int(config.get('anki_image_width', 'auto'))
except:
pass
try:
anki_h = int(config.get('anki_image_height', 'auto'))
except:
pass
anki_exporter.image_width = anki_w
anki_exporter.image_height = anki_h
anki_exporter.image_format = config.get('anki_image_format', 'jpg')
anki_exporter.audio_format = config.get('anki_audio_format', 'wav')
print('ANKI:', vars(anki_exporter))
ffmpeg = find_executable('ffmpeg')
ffsubsync = find_executable('ffsubsync')
rubysubs = find_executable('rubysubs')
mpv_external = find_executable('mpv', 'mpv_external')
print('EXES:', { 'ffmpeg': ffmpeg, 'ffsubsync': ffsubsync, 'rubysubs': rubysubs, 'mpv_external': mpv_external })
anki_exporter.ffmpeg_executable = ffmpeg
skip_empty_subs = config.get('skip_empty_subs', 'yes').lower() == 'yes'
try:
subtitle_export_timeout = float(config.get('subtitle_export_timeout', '0'))
except:
subtitle_export_timeout = 0
sub_font_name = config.get('sub_font_name', 'Noto Sans CJK JP')
sub_font_size = int(config.get('sub_font_size', '55'))
sub_bottom_margin = int(config.get('sub_bottom_margin', '22'))
sub_outline_size = int(config.get('sub_outline_size', '3'))
sub_shadow_offset = int(config.get('sub_shadow_offset', '0'))
# Init mpv IPC
mpv = MpvIpc(sys.argv[1])
# Setup server
server = HttpServer(host, try_ports)
server.set_get_file_server('/', plugin_dir + '/migaku_mpv.html')
for path in ['/icons/migakufavicon.png', '/icons/anki.png', '/icons/bigsearch.png']:
server.set_get_file_server(path, plugin_dir + path)
server.set_get_handler('/subs', get_handler_subs)
server.set_get_handler('/secondary_subs', get_handler_secondary_subs)
server.set_get_handler('/data', get_handler_data)
server.set_post_handler('/anki', post_handler_anki)
server.set_post_handler('/mpv_control', post_handler_mpv_control)
server.set_post_handler('/set_subs', post_handler_set_subs)
server.open()
port = server.port
# Main loop, exits when IPC connection closes
for data in mpv.listen():
print('MPV:', data)
if ('event' in data) and (data['event'] == 'client-message'):
event_args = data.get('args', [])
if len(event_args) >= 2 and event_args[0] == '@migaku':
cmd = event_args[1]
if cmd == 'sub-start':
send_subtitle_time(event_args[2])
elif cmd == 'open':
load_and_open_migaku(*event_args[2:10+1])
elif cmd == 'resync':
resync_subtitle(*event_args[2:4+1])
elif cmd == 'export':
browser_export_current()
elif cmd == 'lookup':
browser_lookup_current()
# Close server
server.close()
stop_get_data_handlers()
# Close mpv IPC
mpv.close()
# Rempve temp dir
shutil.rmtree(tmp_dir, ignore_errors=True)
if __name__ == '__main__':
main()