-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmpv2anki.lua
454 lines (368 loc) · 11.2 KB
/
mpv2anki.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
local util = require('mp.utils')
local msg = require('mp.msg')
local mpopt = require('mp.options')
-- either modify your options here or create a config file in ~/.config/mpv/script-opts/mpv2anki.conf
local options = {
-- absolute path to where anki saves its media on linux this is usually the form of
-- '/home/$user/.local/share/Anki2/$ankiprofile/collection.media/'
-- relative paths (e.g. ~ for home dir) do NOT work.
media_path = '',
-- The anki deck where to put the cards
deckname = 'mpv2anki',
-- The note type
modelName = 'mpv2anki',
-- You can use these options to remap the fields
field_audio = 'audio',
field_snapshot = 'snapshot',
field_subtitle1 = 'subtitle1',
field_subtitle2 = 'subtitle2',
field_start_time = 'start_time',
field_end_time = 'end_time',
field_snapshot_time = 'snapshot_time',
field_title = 'title',
-- The font size used in the menu.
font_size = 20,
shortcut = 'shift+f',
-- In case you changed it
anki_url = 'localhost:8765',
audio_bitrate = '128k',
snapshot_height = '480',
}
mpopt.read_options(options)
local overlay = mp.create_osd_overlay('ass-events')
local ctx = {
start_time = -1,
end_time = -1,
snapshot_time = -1,
sub = {'',''},
}
------------------------------------------------------------
-- utility functions
local function time_to_string(seconds)
if seconds < 0 then
return 'empty'
end
local time = string.format('.%03d', seconds * 1000 % 1000);
time = string.format('%02d:%02d%s',
seconds / 60 % 60,
seconds % 60,
time)
if seconds > 3600 then
time = string.format('%02d:%s', seconds / 3600, time)
end
return time
end
local function time_to_string2(seconds)
return string.format('%02dh%02dM%02ds%03dm',
seconds / 3600,
seconds / 60 % 60,
seconds % 60,
seconds * 1000 % 1000)
end
------------------------------------------------------------
-- anki functions
-- All anki requests are of the form:
-- {
-- "action" : @action
-- "version" : 6
-- "params" : @t_params (optional)
-- }
-- and return a lua table with 2 keys, "result" and "error"
local function anki_connect(action, t_params, url)
local url = url or 'localhost:8765'
local request = {
action = action,
version = 6,
}
if t_params ~= nil then
request.params = t_params
end
local json = util.format_json(request)
local command = {
'curl', url, '-X', 'POST', '-d', json
}
local result = mp.command_native({
name = 'subprocess',
args = command,
capture_stdout = true,
capture_stderr = true,
})
result = util.parse_json(result.stdout)
return result.result, result.error
end
------------------------------------------------------------
-- creating media and anki note
function generate_media_name()
local name = mp.get_property('filename/no-ext')
name = string.gsub(name, '[%[%]]', '')
return 'mpv2anki_' .. name
end
-- Creates an audio fragment with @start_time and @end_time in @media_path.
-- Returns a string containing the name to the audio file
-- or an empty string on error
function create_audio(
media_path,
filename_prefix,
start_time,
end_time,
bitrate)
if (start_time < 0 or end_time < 0) or
(start_time == end_time) then
return ''
end
if start_time > end_time then
local t = start_time
start_time = end_time
end_time = t
end
local filename = string.format(
'%s_(%s-%s).mp3',
filename_prefix,
time_to_string2(start_time),
time_to_string2(end_time))
local encode_args = {
'mpv', mp.get_property('path'),
'--start=' .. start_time,
'--end=' .. end_time,
'--aid=' .. mp.get_property("aid"),
'--vid=no',
'--loop-file=no',
'--oacopts=b=' .. bitrate,
'-o=' .. media_path .. filename
}
local result = mp.command_native({
name = 'subprocess',
args = encode_args,
capture_stdout = true,
capture_stderr = true,
})
return filename
end
-- Takes a snapshot at @snapshot_time and writes it to @media_path
-- Returns a string containing the name to the snapshot
-- or an empty string on error
function create_snapshot(media_path, filename_prefix, snapshot_time, height)
if (snapshot_time <= 0) then
return ''
end
local filename = string.format(
'%s_(%s).jpg',
filename_prefix,
time_to_string2(snapshot_time)
)
-- Sadly the screenshot command does not allow us to create screenshots on specific
-- times nor resize images. So we have to encode to a single image instead
local encode_args = {
'mpv', mp.get_property('path'),
'-start=' .. snapshot_time,
'--frames=1',
'--no-audio',
'--no-sub',
'--vf-add=scale=-2:' .. height,
-- See https://github.com/mpv-player/mpv/issues/6088
-- This is the equivalent of --ovcopts=qscale=2
'--ovcopts=global_quality=2*QP2LAMBDA,flags=+qscale',
'--loop-file=no',
'-o=' .. media_path .. filename
}
local result = mp.command_native({
name = 'subprocess',
args = encode_args,
capture_stdout = true,
capture_stderr = true,
})
return filename
end
function create_anki_note(gui)
local filename_prefix = generate_media_name()
local filename_audio = create_audio(
options.media_path,
filename_prefix,
ctx.start_time,
ctx.end_time,
options.audio_bitrate)
local filename_snapshot = create_snapshot(
options.media_path,
filename_prefix,
ctx.snapshot_time,
options.snapshot_height
)
-- Start filling the fields
local fields = {}
if #filename_audio > 0 then
fields[options.field_audio] = '[sound:'..filename_audio..']'
fields[options.field_start_time] = time_to_string(ctx.start_time)
fields[options.field_end_time] = time_to_string(ctx.end_time)
end
if #filename_snapshot > 0 then
fields[options.field_snapshot] = '<img src="' .. filename_snapshot ..'">'
fields[options.field_snapshot_time] = time_to_string(ctx.snapshot_time)
end
if #ctx.sub[1] > 0 then
fields[options.field_subtitle1] = ctx.sub[1]
end
if #ctx.sub[2] > 0 then
fields[options.field_subtitle2] = ctx.sub[2]
end
fields[options.field_title] = mp.get_property('filename/no-ext')
local param = {
note = {
deckName = options.deckname,
modelName = options.modelName,
fields = fields,
tags = {
'mpv2anki'
}
}
}
local action;
if gui then
action = 'guiAddCards'
else
action = 'addNote'
end
anki_connect(action, param, options.anki_url)
end
------------------------------------------------------------
-- main menu
local menu_keybinds = {
{ key = '1', fn = function() menu_set_time('start_time', 'time-pos') end },
{ key = '2', fn = function() menu_set_time('end_time', 'time-pos') end },
{ key = '3', fn = function() menu_set_time('snapshot_time', 'time-pos') end },
{ key = '4', fn = function() menu_set_subs(1) end },
{ key = '5', fn = function() menu_set_subs(2) end },
{ key = 'd', fn = function() menu_set_to_subs() end },
{ key = 'D', fn = function() menu_clear_all() end },
{ key = 'S', fn = function() menu_set_time('end_time', 'sub-end') end },
{ key = 's', fn = function() menu_set_time('start_time', 'sub-start') end },
{ key = 'a', fn = function() menu_append(1) end },
{ key = 'A', fn = function() menu_append(2) end },
{ key = 'e', fn = function() create_anki_note(true) end },
{ key = 'shift+e', fn = function() create_anki_note(false) end },
{ key = 'ESC', fn = function() menu_close() end },
}
function menu_append(n)
local subs = mp.get_property('sub-text'):gsub('\n', '')
ctx.sub[n] = ctx.sub[n] .. ' ' .. subs
menu_update()
end
function menu_set_to_subs()
local start_time = mp.get_property_number('sub-start')
local end_time = mp.get_property_number('sub-end')
if start_time == nil then
start_time = -1
end
if end_time == nil then
end_time = -1
end
ctx.start_time = start_time
ctx.end_time = end_time
ctx.sub[1] = mp.get_property('sub-text'):gsub('\n', '')
menu_update()
end
function menu_set_time(field, prop)
local time = mp.get_property_number(prop)
if time == nil or time == ctx[field] then
ctx[field] = -1
else
ctx[field] = time
end
menu_update()
end
function menu_set_subs(n)
local subs = mp.get_property('sub-text'):gsub('\n', '')
if subs == ctx.sub[n] then
ctx.sub[n] = ''
else
ctx.sub[n] = subs
end
menu_update()
end
function menu_clear_all()
ctx.start_time = -1
ctx.end_time = -1
ctx.snapshot_time = -1
ctx.sub[1] = ''
ctx.sub[2] = ''
menu_update()
end
function menu_update()
local ass = ASS.new():s(options.font_size):b('MPV2Anki'):nl():nl()
-- Media type
ass:b('Audio fragment'):nl()
ass:tab():b('1: ')
:a('Set start time (' .. time_to_string(ctx.start_time) .. ')'):nl()
ass:tab():b('2: ')
:a('Set end time (' .. time_to_string(ctx.end_time) .. ')'):nl()
:nl()
-- snapshot
ass:b('Snapshot'):nl()
ass:tab():b('3: ')
:a('Set snapshot (' .. time_to_string(ctx.snapshot_time) .. ')'):nl()
:nl()
-- subtitle
ass:b('Subtitles'):nl()
local start_key = 4
for i, sub in pairs(ctx.sub) do
ass:tab():b(start_key .. ': ')
:a('sub ' .. i)
if sub == '' then sub = 'empty' end
ass:a(' (' .. sub .. ')'):nl()
start_key = start_key + 1
end
-- menu options
ass:nl()
:b('d: '):a('Set to current subtitle ('):b('D: '):a('clear all)'):nl()
:b('s: '):a('Set end time to sub ('):b('S: '):a('begin time)'):nl()
:b('a: '):a('Append sub 1 ('):b('A: '):a('sub 2)'):nl()
:b('e: '):a('Create card ('):b('E: '):a('without GUI)'):nl()
:b('ESC: '):a('Close'):nl()
ass:draw()
end
function menu_close()
for _, val in pairs(menu_keybinds) do
mp.remove_key_binding(val.key)
end
overlay:remove()
end
function menu_open()
for _, val in pairs(menu_keybinds) do
mp.add_key_binding(val.key, val.key, val.fn)
end
menu_update()
end
------------------------------------------------------------
-- Helper functions for styling ASS messages
ASS = {}
ASS.__index = ASS
function ASS.new()
return setmetatable({text=''}, ASS)
end
-- append
function ASS:a(s)
self.text = self.text .. s
return self
end
-- bold
function ASS:b(s)
return self:a('{\\b1}' .. s .. '{\\b0}')
end
-- new line
function ASS:nl()
return self:a('\\N')
end
-- 4 space tab
function ASS:tab()
return self:a('\\h\\h\\h\\h')
end
-- size
function ASS:s(size)
return self:a('{\\fs' .. size .. '}')
end
function ASS:draw()
overlay.data = self.text
overlay:update()
end
------------------------------------------------------------
-- Finally, set an 'entry point' in mpv
mp.add_key_binding(options.shortcut, options.shortcut, menu_open)