forked from Ajatt-Tools/mpvacious
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subs2srs.lua
1479 lines (1258 loc) · 44.8 KB
/
subs2srs.lua
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
--[[
Copyright (C) 2020 Ren Tatsumoto
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Requirements:
* mpv >= 0.32.0
* AnkiConnect
* curl
* xclip
Usage:
1. Change `config` according to your needs
* Options can be changed right in this file or in a separate config file.
* Config path: ~/.config/mpv/script-opts/subs2srs.conf
* Config file isn't created automatically.
2. Open a video
3. Use key bindings to manipulate the script
* Open mpvacious menu - `a`
* Create a note from the current subtitle line - `Ctrl + e`
For complete usage guide, see <https://github.com/Ajatt-Tools/mpvacious/blob/master/README.md>
]]
local config = {
-- Common
autoclip = false, -- enable copying subs to the clipboard when mpv starts
nuke_spaces = true, -- remove all spaces from exported anki cards
clipboard_trim_enabled = true, -- remove unnecessary characters from strings before copying to the clipboard
snapshot_format = "webp", -- webp or jpg
snapshot_quality = 15, -- from 0=lowest to 100=highest
snapshot_width = -2, -- a positive integer or -2 for auto
snapshot_height = 200, -- same
audio_format = "opus", -- opus or mp3
audio_bitrate = "18k", -- from 16k to 32k
audio_padding = 0.12, -- Set a pad to the dialog timings. 0.5 = audio is padded by .5 seconds. 0 = disable.
tie_volumes = false, -- if set to true, the volume of the outputted audio file depends on the volume of the player at the time of export
menu_font_size = 25,
-- Anki
deck_name = "Learning", -- the deck will be created if needed
model_name = "Japanese sentences", -- Tools -> Manage note types
sentence_field = "SentKanji",
audio_field = "SentAudio",
image_field = "Image",
note_tag = "subs2srs", -- the tag that is added to new notes. change to "" to disable tagging
-- Forvo support
use_forvo = "yes", -- 'yes', 'no', 'always'
vocab_field = "VocabKanji", -- target word field
vocab_audio_field = "VocabAudio", -- target word audio
}
local utils = require('mp.utils')
local msg = require('mp.msg')
local mpopt = require('mp.options')
mpopt.read_options(config, "subs2srs")
-- namespaces
local subs
local clip_autocopy
local encoder
local ankiconnect
local menu
local platform
local append_forvo_pronunciation
-- classes
local Subtitle
local OSD
------------------------------------------------------------
-- utility functions
---Returns true if table contains element. Returns false otherwise.
---@param table table
---@param element any
---@return boolean
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
---Returns the largest numeric index.
---@param table table
---@return number
function table.max_num(table)
local max = table[1]
for _, value in ipairs(table) do
if value > max then
max = value
end
end
return max
end
---Returns a value for the given key. If key is not available then returns default value 'nil'.
---@param table table
---@param key string
---@param default any
---@return any
function table.get(table, key, default)
if table[key] == nil then
return default or 'nil'
else
return table[key]
end
end
local function is_empty(var)
return var == nil or var == '' or (type(var) == 'table' and next(var) == nil)
end
local function is_running_windows()
return mp.get_property('options/vo-mmcss-profile') ~= nil
end
local function is_running_macOS()
return mp.get_property('options/cocoa-force-dedicated-gpu') ~= nil
end
local function contains_non_latin_letters(str)
return str:match("[^%c%p%s%w]")
end
local function capitalize_first_letter(string)
return string:gsub("^%l", string.upper)
end
local function notify(message, level, duration)
level = level or 'info'
duration = duration or 1
msg[level](message)
mp.osd_message(message, duration)
end
local escape_special_characters
do
local entities = {
['&'] = '&',
['"'] = '"',
["'"] = ''',
['<'] = '<',
['>'] = '>',
}
escape_special_characters = function(s)
return s:gsub('[&"\'<>]', entities)
end
end
local function remove_extension(filename)
return filename:gsub('%.%w+$', '')
end
local function remove_special_characters(str)
return str:gsub('[%c%p%s]', ''):gsub(' ', '')
end
local function remove_text_in_brackets(str)
return str:gsub('%b[]', ''):gsub('【.-】', '')
end
local function remove_text_in_parentheses(str)
-- Remove text like (泣き声) or (ドアの開く音)
-- No deletion is performed if there's no text after the parentheses.
-- Note: the modifier `-´ matches zero or more occurrences.
-- However, instead of matching the longest sequence, it matches the shortest one.
return str:gsub('(%b())(.)', '%2'):gsub('((.-))(.)', '%2')
end
local function remove_newlines(str)
return str:gsub('[\n\r]+', ' ')
end
local function remove_leading_trailing_spaces(str)
return str:gsub('^%s*(.-)%s*$', '%1')
end
local function remove_all_spaces(str)
return str:gsub('%s*', '')
end
local function remove_spaces(str)
if config.nuke_spaces == true and contains_non_latin_letters(str) then
return remove_all_spaces(str)
else
return remove_leading_trailing_spaces(str)
end
end
local function trim(str)
str = remove_spaces(str)
str = remove_text_in_parentheses(str)
str = remove_newlines(str)
return str
end
local function copy_to_clipboard(_, text)
if not is_empty(text) then
text = config.clipboard_trim_enabled and trim(text) or remove_newlines(text)
platform.copy_to_clipboard(text)
end
end
local function copy_sub_to_clipboard()
copy_to_clipboard("copy-on-demand", mp.get_property("sub-text"))
end
local function human_readable_time(seconds)
if type(seconds) ~= 'number' or seconds < 0 then
return 'empty'
end
local parts = {
h = math.floor(seconds / 3600),
m = math.floor(seconds / 60) % 60,
s = math.floor(seconds % 60),
ms = math.floor((seconds * 1000) % 1000),
}
local ret = string.format("%02dm%02ds%03dms", parts.m, parts.s, parts.ms)
if parts.h > 0 then
ret = string.format('%dh%s', parts.h, ret)
end
return ret
end
local function subprocess(args, completion_fn)
-- if `completion_fn` is passed, the command is ran asynchronously,
-- and upon completion, `completion_fn` is called to process the results.
local command_native = type(completion_fn) == 'function' and mp.command_native_async or mp.command_native
local command_table = {
name = "subprocess",
playback_only = false,
capture_stdout = true,
args = args
}
return command_native(command_table, completion_fn)
end
local function construct_note_fields(sub_text, snapshot_filename, audio_filename)
return {
[config.sentence_field] = sub_text,
[config.image_field] = string.format('<img alt="snapshot" src="%s">', snapshot_filename),
[config.audio_field] = string.format('[sound:%s]', audio_filename),
}
end
local function minutes_ago(m)
return (os.time() - 60 * m) * 1000
end
local function join_media_fields(new_data, stored_data)
for _, field in pairs { config.audio_field, config.image_field } do
new_data[field] = table.get(stored_data, field, "") .. table.get(new_data, field, "")
end
return new_data
end
local validate_config
do
local function is_webp_supported()
local ret = subprocess { 'mpv', '--ovc=help' }
return ret.status == 0 and ret.stdout:match('--ovc=libwebp')
end
local function is_opus_supported()
local ret = subprocess { 'mpv', '--oac=help' }
return ret.status == 0 and ret.stdout:match('--oac=libopus')
end
local function set_audio_format()
if config.audio_format == 'opus' and is_opus_supported() then
config.audio_codec = 'libopus'
config.audio_extension = '.ogg'
else
config.audio_codec = 'libmp3lame'
config.audio_extension = '.mp3'
end
end
local function set_video_format()
if config.snapshot_format == 'webp' and is_webp_supported() then
config.snapshot_extension = '.webp'
config.snapshot_codec = 'libwebp'
else
config.snapshot_extension = '.jpg'
config.snapshot_codec = 'mjpeg'
end
end
local function ensure_in_range(dimension)
config[dimension] = config[dimension] < 42 and -2 or config[dimension]
config[dimension] = config[dimension] > 640 and 640 or config[dimension]
end
local function conditionally_set_defaults(width, height, quality)
if config[width] < 1 and config[height] < 1 then
config[width] = -2
config[height] = 200
end
if config[quality] < 0 or config[quality] > 100 then
config[quality] = 15
end
end
local function check_image_settings()
ensure_in_range('snapshot_width')
ensure_in_range('snapshot_height')
conditionally_set_defaults('snapshot_width', 'snapshot_height', 'snapshot_quality')
end
validate_config = function()
set_audio_format()
set_video_format()
check_image_settings()
end
end
local function update_sentence(new_data, stored_data)
-- adds support for TSCs
-- https://tatsumoto-ren.github.io/blog/discussing-various-card-templates.html#targeted-sentence-cards-or-mpvacious-cards
-- if the target word was marked by yomichan, this function makes sure that the highlighting doesn't get erased.
local _, target, _ = stored_data[config.sentence_field]:match('^(.-)<b>(.-)</b>(.-)$')
if target then
local prefix, _, suffix = new_data[config.sentence_field]:match(table.concat { '^(.-)(', target, ')(.-)$' })
if prefix and suffix then
new_data[config.sentence_field] = table.concat { prefix, '<b>', target, '</b>', suffix }
end
end
return new_data
end
------------------------------------------------------------
-- utility classes
local function new_timings()
local self = { ['start'] = -1, ['end'] = -1, }
local is_set = function(position)
return self[position] >= 0
end
local set = function(position)
self[position] = mp.get_property_number('time-pos')
end
local get = function(position)
return self[position]
end
return {
is_set = is_set,
set = set,
get = get,
}
end
local function new_sub_list()
local subs_list = {}
local _is_empty = function()
return next(subs_list) == nil
end
local find_i = function(sub)
for i, v in ipairs(subs_list) do
if sub < v then
return i
end
end
return #subs_list + 1
end
local get_time = function(position)
local i = position == 'start' and 1 or #subs_list
return subs_list[i][position]
end
local get_text = function()
local speech = {}
for _, sub in ipairs(subs_list) do
table.insert(speech, sub['text'])
end
return table.concat(speech, ' ')
end
local insert = function(sub)
if sub ~= nil and not table.contains(subs_list, sub) then
table.insert(subs_list, find_i(sub), sub)
return true
end
return false
end
return {
get_time = get_time,
get_text = get_text,
is_empty = _is_empty,
insert = insert
}
end
local function make_switch(states)
local self = {
states = states,
current_state = 1
}
local bump = function()
self.current_state = self.current_state + 1
if self.current_state > #self.states then
self.current_state = 1
end
end
local get = function()
return self.states[self.current_state]
end
return {
bump = bump,
get = get
}
end
local filename_factory = (function()
local filename
local anki_compatible_length = (function()
-- Anki forcibly mutilates all filenames longer than 119 bytes when you run `Tools->Check Media...`.
local allowed_bytes = 119
local timestamp_bytes = #'_99h99m99s999ms-99h99m99s999ms.webp'
return function(str, timestamp)
-- if timestamp provided, recalculate limit_bytes
local limit_bytes = allowed_bytes - (timestamp and #timestamp or timestamp_bytes)
if #str <= limit_bytes then
return str
end
local bytes_per_char = contains_non_latin_letters(str) and #'車' or #'z'
local limit_chars = math.floor(limit_bytes / bytes_per_char)
if limit_chars == limit_bytes then
return str:sub(1, limit_bytes)
end
local ret = subprocess {
'awk',
'-v', string.format('str=%s', str),
'-v', string.format('limit=%d', limit_chars),
'BEGIN{print substr(str, 1, limit); exit}'
}
if ret.status == 0 then
ret.stdout = remove_newlines(ret.stdout)
ret.stdout = remove_leading_trailing_spaces(ret.stdout)
return ret.stdout
else
return 'subs2srs_' .. os.time()
end
end
end)()
local make_media_filename = function()
filename = mp.get_property("filename") -- filename without path
filename = remove_extension(filename)
filename = remove_text_in_brackets(filename)
filename = remove_special_characters(filename)
end
local make_audio_filename = function(speech_start, speech_end)
local filename_timestamp = string.format(
'_%s-%s%s',
human_readable_time(speech_start),
human_readable_time(speech_end),
config.audio_extension
)
return anki_compatible_length(filename, filename_timestamp) .. filename_timestamp
end
local make_snapshot_filename = function(timestamp)
local filename_timestamp = string.format(
'_%s%s',
human_readable_time(timestamp),
config.snapshot_extension
)
return anki_compatible_length(filename, filename_timestamp) .. filename_timestamp
end
mp.register_event("file-loaded", make_media_filename)
return {
make_audio_filename = make_audio_filename,
make_snapshot_filename = make_snapshot_filename,
}
end)()
------------------------------------------------------------
-- front for adding and updating notes
local function export_to_anki(gui)
local sub = subs.get()
if sub == nil then
notify("Nothing to export.", "warn", 1)
return
end
if not gui and is_empty(sub['text']) then
sub['text'] = string.format([[<span id="mpv%s">mpvacious wasn't able to grab subtitles</span>]], os.time())
end
local snapshot_timestamp = mp.get_property_number("time-pos", 0)
local snapshot_filename = filename_factory.make_snapshot_filename(snapshot_timestamp)
local audio_filename = filename_factory.make_audio_filename(sub['start'], sub['end'])
encoder.create_snapshot(snapshot_timestamp, snapshot_filename)
encoder.create_audio(sub['start'], sub['end'], audio_filename)
local note_fields = construct_note_fields(sub['text'], snapshot_filename, audio_filename)
ankiconnect.add_note(note_fields, gui)
subs.clear()
end
local function update_last_note(overwrite)
local sub = subs.get()
local last_note_id = ankiconnect.get_last_note_id()
if sub == nil or is_empty(sub['text']) then
notify("Nothing to export. Have you set the timings?", "warn", 2)
return
end
if last_note_id < minutes_ago(10) then
notify("Couldn't find the target note.", "warn", 2)
return
end
local snapshot_timestamp = mp.get_property_number("time-pos", 0)
local snapshot_filename = filename_factory.make_snapshot_filename(snapshot_timestamp)
local audio_filename = filename_factory.make_audio_filename(sub['start'], sub['end'])
local create_media = function()
encoder.create_snapshot(snapshot_timestamp, snapshot_filename)
encoder.create_audio(sub['start'], sub['end'], audio_filename)
end
local new_data = construct_note_fields(sub['text'], snapshot_filename, audio_filename)
local stored_data = ankiconnect.get_note_fields(last_note_id)
if stored_data then
new_data = append_forvo_pronunciation(new_data, stored_data)
new_data = update_sentence(new_data, stored_data)
if not overwrite then
new_data = join_media_fields(new_data, stored_data)
end
end
ankiconnect.append_media(last_note_id, new_data, create_media)
subs.clear()
end
------------------------------------------------------------
-- seeking: sub replay, sub seek, sub rewind
local function _(params)
local unpack = unpack and unpack or table.unpack
return function() return pcall(unpack(params)) end
end
local pause_timer = (function()
local stop_time = -1
local check_stop
local set_stop_time = function(time)
stop_time = time
end
local stop = function()
mp.unobserve_property(check_stop)
stop_time = -1
end
check_stop = function(_, time)
if time > stop_time then
stop()
mp.set_property("pause", "yes")
else
notify('Timer: ' .. human_readable_time(stop_time - time))
end
end
return {
set_stop_time = set_stop_time,
check_stop = check_stop,
stop = stop,
}
end)()
local function sub_replay()
local sub = subs.get_current()
pause_timer.set_stop_time(sub['end'] - 0.050)
mp.commandv('seek', sub['start'], 'absolute')
mp.set_property("pause", "no")
mp.observe_property("time-pos", "number", pause_timer.check_stop)
end
local function sub_seek(direction, pause)
mp.commandv("sub_seek", direction == 'backward' and '-1' or '1')
mp.commandv("seek", "0.015", "relative+exact")
if pause then
mp.set_property("pause", "yes")
end
pause_timer.stop()
end
local function sub_rewind()
mp.commandv('seek', subs.get_current()['start'] + 0.015, 'absolute')
pause_timer.stop()
end
------------------------------------------------------------
-- platform specific
local function init_platform_windows()
local self = {}
local curl_tmpfile_path = utils.join_path(os.getenv('TEMP'), 'curl_tmp.txt')
mp.register_event('shutdown', function() os.remove(curl_tmpfile_path) end)
self.tmp_dir = function()
return os.getenv('TEMP')
end
self.copy_to_clipboard = function(text)
text = text:gsub("&", "^^^&")
mp.commandv("run", "cmd.exe", "/d", "/c", string.format("@echo off & chcp 65001 & echo %s|clip", text))
end
self.curl_request = function(request_json, completion_fn)
local handle = io.open(curl_tmpfile_path, "w")
handle:write(request_json)
handle:close()
local args = {
'curl',
'-s',
'localhost:8765',
'-H',
'Content-Type: application/json; charset=UTF-8',
'-X',
'POST',
'--data-binary',
table.concat { '@', curl_tmpfile_path }
}
return subprocess(args, completion_fn)
end
self.windows = true
return self
end
local function init_platform_nix()
local self = {}
local clip = is_running_macOS() and 'LANG=en_US.UTF-8 pbcopy' or 'xclip -i -selection clipboard'
self.tmp_dir = function()
return '/tmp'
end
self.copy_to_clipboard = function(text)
local handle = io.popen(clip, 'w')
handle:write(text)
handle:close()
end
self.curl_request = function(request_json, completion_fn)
local args = { 'curl', '-s', 'localhost:8765', '-X', 'POST', '-d', request_json }
return subprocess(args, completion_fn)
end
return self
end
platform = is_running_windows() and init_platform_windows() or init_platform_nix()
------------------------------------------------------------
-- utils for downloading pronunciations from Forvo
do
local base64d -- http://lua-users.org/wiki/BaseSixtyFour
do
local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
base64d = function(data)
data = string.gsub(data, '[^'..b..'=]', '')
return (data:gsub('.', function(x)
if (x == '=') then return '' end
local r,f='',(b:find(x)-1)
for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
return r;
end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
if (#x ~= 8) then return '' end
local c=0
for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
return string.char(c)
end))
end
end
local function url_encode(url) -- https://gist.github.com/liukun/f9ce7d6d14fa45fe9b924a3eed5c3d99
local char_to_hex = function(c)
return string.format("%%%02X", string.byte(c))
end
if url == nil then
return
end
url = url:gsub("\n", "\r\n")
url = url:gsub("([^%w _%%%-%.~])", char_to_hex)
url = url:gsub(" ", "+")
return url
end
local function reencode(source_path, dest_path)
local args = {
'mpv',
source_path,
'--loop-file=no',
'--video=no',
'--no-ocopy-metadata',
'--no-sub',
'--audio-channels=mono',
'--oacopts-add=vbr=on',
'--oacopts-add=application=voip',
'--oacopts-add=compression_level=10',
'--af-append=silenceremove=1:0:-50dB',
table.concat { '--oac=', config.audio_codec },
table.concat { '--oacopts-add=b=', config.audio_bitrate },
table.concat { '-o=', dest_path }
}
return subprocess(args)
end
local function reencode_and_store(source_path, filename)
local reencoded_path = utils.join_path(platform.tmp_dir(), 'reencoded_' .. filename)
reencode(source_path, reencoded_path)
local result = ankiconnect.store_file(filename, reencoded_path)
os.remove(reencoded_path)
return result
end
local function curl_save(source_url, save_location)
local curl_args = { 'curl', source_url, '-s', '-L', '-o', save_location }
return subprocess(curl_args).status == 0
end
local function get_pronunciation_url(word)
local file_format = config.audio_extension:sub(2)
local forvo_page = subprocess { 'curl', '-s', string.format('https://forvo.com/search/%s/ja', url_encode(word)) }.stdout
local play_params = string.match(forvo_page, "Play%((.-)%);")
if play_params then
local iter = string.gmatch(play_params, "'(.-)'")
local formats = { mp3 = iter(), ogg = iter() }
return string.format('https://audio00.forvo.com/%s/%s', file_format, base64d(formats[file_format]))
end
end
local function make_forvo_filename(word)
return string.format('forvo_%s%s', platform.windows and os.time() or word, config.audio_extension)
end
local function get_forvo_pronunciation(word)
local audio_url = get_pronunciation_url(word)
if is_empty(audio_url) then
msg.warn(string.format("Seems like Forvo doesn't have audio for word %s.", word))
return
end
local filename = make_forvo_filename(word)
local tmp_filepath = utils.join_path(platform.tmp_dir(), filename)
local result
if curl_save(audio_url, tmp_filepath) and reencode_and_store(tmp_filepath, filename) then
result = string.format('[sound:%s]', filename)
else
msg.warn(string.format("Couldn't download audio for word %s from Forvo.", word))
end
os.remove(tmp_filepath)
return result
end
append_forvo_pronunciation = function(new_data, stored_data)
if config.use_forvo == 'no' then
-- forvo functionality was disabled in the config file
return new_data
end
if type(stored_data[config.vocab_audio_field]) ~= 'string' then
-- there is no field configured to store forvo pronunciation
return new_data
end
if is_empty(stored_data[config.vocab_field]) then
-- target word field is empty. can't continue.
return new_data
end
if config.use_forvo == 'always' or is_empty(stored_data[config.vocab_audio_field]) then
local forvo_pronunciation = get_forvo_pronunciation(stored_data[config.vocab_field])
if not is_empty(forvo_pronunciation) then
if config.vocab_audio_field == config.audio_field then
-- improperly configured fields. don't lose sentence audio
new_data[config.audio_field] = forvo_pronunciation .. new_data[config.audio_field]
else
new_data[config.vocab_audio_field] = forvo_pronunciation
end
end
end
return new_data
end
end
------------------------------------------------------------
-- provides interface for creating audio clips and snapshots
encoder = {}
encoder.pad_timings = function(start_time, end_time)
local video_duration = mp.get_property_number('duration')
if config.audio_padding == 0.0 or not video_duration then
return start_time, end_time
end
if subs.user_timings.is_set('start') or subs.user_timings.is_set('end') then
return start_time, end_time
end
start_time = start_time - config.audio_padding
end_time = end_time + config.audio_padding
if start_time < 0 then start_time = 0 end
if end_time > video_duration then end_time = video_duration end
return start_time, end_time
end
encoder.get_active_track = function(track_type)
local track_list = mp.get_property_native('track-list')
for _, track in pairs(track_list) do
if track.type == track_type and track.selected == true then
return track
end
end
return nil
end
encoder.create_snapshot = function(timestamp, filename)
local source_path = mp.get_property("path")
local output_path = utils.join_path(platform.tmp_dir(), filename)
local args = {
'mpv',
source_path,
'--loop-file=no',
'--audio=no',
'--no-ocopy-metadata',
'--no-sub',
'--frames=1',
'--ovcopts-add=lossless=0',
'--ovcopts-add=compression_level=6',
table.concat { '--ovc=', config.snapshot_codec },
table.concat { '-start=', timestamp },
table.concat { '--ovcopts-add=quality=', tostring(config.snapshot_quality) },
table.concat { '--vf-add=scale=', config.snapshot_width, ':', config.snapshot_height },
table.concat { '-o=', output_path }
}
local on_finish = function()
ankiconnect.store_file(filename, output_path)
os.remove(output_path)
end
subprocess(args, on_finish)
end
encoder.create_audio = function(start_timestamp, end_timestamp, filename)
local source_path = mp.get_property("path")
local audio_track = encoder.get_active_track('audio')
local audio_track_id = mp.get_property("aid")
local output_path = utils.join_path(platform.tmp_dir(), filename)
if audio_track and audio_track.external == true then
source_path = audio_track['external-filename']
audio_track_id = 'auto'
end
start_timestamp, end_timestamp = encoder.pad_timings(start_timestamp, end_timestamp)
local args = {
'mpv',
source_path,
'--loop-file=no',
'--video=no',
'--no-ocopy-metadata',
'--no-sub',
'--audio-channels=mono',
'--oacopts-add=vbr=on',
'--oacopts-add=application=voip',
'--oacopts-add=compression_level=10',
table.concat { '--oac=', config.audio_codec },
table.concat { '--start=', start_timestamp },
table.concat { '--end=', end_timestamp },
table.concat { '--aid=', audio_track_id },
table.concat { '--volume=', config.tie_volumes and mp.get_property('volume') or '100' },
table.concat { '--oacopts-add=b=', config.audio_bitrate },
table.concat { '-o=', output_path }
}
local on_finish = function()
ankiconnect.store_file(filename, output_path)
os.remove(output_path)
end
subprocess(args, on_finish)
end
------------------------------------------------------------
-- AnkiConnect requests
ankiconnect = {}
ankiconnect.execute = function(request, completion_fn)
-- utils.format_json returns a string
-- On error, request_json will contain "null", not nil.
local request_json, error = utils.format_json(request)
if error ~= nil or request_json == "null" then
return completion_fn and completion_fn()
else
return platform.curl_request(request_json, completion_fn)
end
end
ankiconnect.parse_result = function(curl_output)
-- there are two values that we actually care about: result and error
-- but we need to crawl inside to get them.
if curl_output == nil then
return nil, "Failed to format json or no args passed"
end
if curl_output.status ~= 0 then
return nil, "Ankiconnect isn't running"
end
local stdout_json = utils.parse_json(curl_output.stdout)
if stdout_json == nil then
return nil, "Fatal error from Ankiconnect"
end
if stdout_json.error ~= nil then
return nil, tostring(stdout_json.error)
end
return stdout_json.result, nil
end
ankiconnect.store_file = function(filename, file_path)
local args = {
action = "storeMediaFile",
version = 6,
params = {
filename = filename,
path = file_path
}
}
local ret = ankiconnect.execute(args)
local _, error = ankiconnect.parse_result(ret)
if not error then
msg.info(string.format("File stored: '%s'.", filename))
return true
else
msg.error(string.format("Couldn't store file '%s': %s", filename, error))
return false
end
end
ankiconnect.create_deck = function(deck_name)
local args = {
action = "changeDeck",
version = 6,
params = {
cards = {},
deck = deck_name
}
}
local result_notify = function(_, result, _)
local _, error = ankiconnect.parse_result(result)
if not error then
msg.info(string.format("Deck %s: check completed.", deck_name))