Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- -- open clipboard inserter
- -- -- Wait for unknown word and add it to anki through yomichan
- -- -- Select all the subtitle lines to add to the card.
- -- -- Ctrl + c
- -- -- Tab back to MPV and Ctrl + v
- -- -- Done. The lines, their respective Audio and the current paused image
- -- -- will be added to the back of the card
- local utils = require 'mp.utils'
- local msg = require 'mp.msg'
- local subs = {}
- local enable_subs_to_clip = true
- -- User Config
- local FRONT_FIELD = "Front"
- local SENTENCE_AUDIO_FIELD = "Sentence-Audio"
- local SENTENCE_FIELD = "Sentence"
- local IMAGE_FIELD = "Image"
- local FFMPEG_PATH = [[D:\ffmpeg\bin\ffmpeg]]
- local prefix = os.getenv("HOME") .. '/.local/share/Anki2/User 1/collection.media/'
- local AUDIO_CLIP_FADE = 0.2
- local AUDIO_CLIP_PADDING = 0.75
- local function trim(s)
- return string.gsub(s, "^%s*(.-)%s*$", "%1")
- end
- local function get_name(s, e)
- return mp.get_property("filename"):gsub('%W','').. tostring(s) .. tostring(e)
- end
- local function record_sub(name, text)
- if text and mp.get_property_number('sub-start') and mp.get_property_number('sub-end') then
- local sub_delay = mp.get_property_native("sub-delay")
- local newtext = string.gsub(text, "\n", "")
- newtext = string.gsub(newtext, "\r", "")
- newtext = string.gsub(newtext, '"', "”")
- if newtext == '' then
- return
- end
- subs[newtext] = { mp.get_property_number('sub-start') + sub_delay, mp.get_property_number('sub-end') + sub_delay }
- if enable_subs_to_clip then
- local cmd = 'echo ' .. newtext .. ' | clip'
- mp.command("run cmd /D /C @echo off | chcp 65001 | " .. cmd)
- end
- end
- end
- local function create_audio(s, e)
- if s == nil or e == nil then
- return
- end
- local ff_aid = 0
- local tracks_count = mp.get_property_number("track-list/count")
- for i = 1, tracks_count do
- local track_type = mp.get_property(string.format("track-list/%d/type", i))
- local track_index = mp.get_property_number(string.format("track-list/%d/ff-index", i))
- local track_selected = mp.get_property(string.format("track-list/%d/selected", i))
- if track_type == "audio" and track_selected == "yes" then
- ff_aid = track_index - 1
- break
- end
- end
- local name = get_name(s, e)
- s = s - AUDIO_CLIP_PADDING
- local t = e - s + AUDIO_CLIP_PADDING
- local source = mp.get_property("path")
- local destination = prefix .. name .. '.mp3'
- local args = {
- FFMPEG_PATH,
- "-y",
- "-ss", tostring(s),
- "-i", source,
- "-t", string.format("%.3f", t),
- "-map", string.format("0:a:%d", ff_aid),
- "-af", string.format("afade=t=in:curve=ipar:st=%.3f:d=%.3f,afade=t=out:curve=ipar:st=%.3f:d=%.3f", 0, AUDIO_CLIP_FADE, t - AUDIO_CLIP_FADE, AUDIO_CLIP_FADE),
- destination
- }
- utils.subprocess_detached({ args=args })
- end
- function get_clipboard()
- local command = 'xclip -o'
- local handle = io.popen(command)
- local result = handle:read("*a")
- handle:close()
- return result
- end
- local function create_screenshot(s, e)
- local source = mp.get_property("path")
- local img = prefix .. get_name(s,e) .. '.webp'
- local args = {
- FFMPEG_PATH,
- "-y",
- "-ss", tostring(mp.get_property_number("time-pos")),
- "-i", source,
- "-vframes", "1",
- "-vf", "scale=-2:480",
- "-vcodec", "libwebp",
- "-compression_level", "6",
- "-preset", "drawing",
- img
- }
- utils.subprocess_detached({ args=args })
- end
- local function get(request)
- local args = {
- "curl",
- "localhost:8765",
- "-X",
- "GET",
- "-d",
- request
- }
- local result =
- utils.subprocess(
- {
- args = args,
- cancellable = true
- }
- )
- return utils.parse_json(result.stdout)
- end
- local function add_to_last_added(ifield, afield, tfield)
- local noteid = math.max(unpack(get('{"action": "findNotes", "params": {"query": "added:1"}, "version": 6}')["result"]))
- local note = get('{"action": "notesInfo", "params": {"notes": ['.. noteid.. ']}, "version": 6}')
- ifield = string.gsub(ifield, "\"", "\\\"")
- afield = string.gsub(afield, "\"", "\\\"")
- tfield = string.gsub(tfield, "\"", "\\\"")
- if note ~= nil then
- get('{"action": "updateNoteFields", "version": 6, "params": {"note": {"id": ' .. noteid.. ', "fields": {"' .. SENTENCE_AUDIO_FIELD .. '": "' .. afield .. '","' .. SENTENCE_FIELD .. '": "' .. tfield .. '","' .. IMAGE_FIELD .. '": "' .. ifield .. '"} } } }')
- mp.osd_message("Updated note: " .. note["result"][1]["fields"][FRONT_FIELD]["value"], 3)
- msg.info("Updated note: " .. note["result"][1]["fields"][FRONT_FIELD]["value"])
- end
- end
- local function get_extract()
- local lines = get_clipboard()
- local e = 0
- local s = 0
- for line in lines:gmatch("[^\r\n]+") do
- line = trim(line)
- if subs[line]~= nil then
- msg.info(lines)
- if subs[line][1] ~= nil and subs[line][2] ~= nil then
- msg.info(lines)
- if s == 0 then
- s = subs[line][1]
- else
- s = math.min(s, subs[line][1])
- end
- e = math.max(e, subs[line][2])
- end
- end
- end
- if e ~= 0 then
- create_screenshot(s, e)
- create_audio(s, e)
- local ifield = '<img src='.. get_name(s,e) ..'.webp>'
- local afield = "[sound:".. get_name(s,e) .. ".mp3]"
- local tfield = string.gsub(string.gsub(lines,"\n+", "<br />"), "\r", "")
- add_to_last_added(ifield, afield, tfield)
- end
- end
- local function ex()
- pcall(get_extract)
- end
- local function rec(...)
- pcall(record_sub, ...)
- end
- local function toggle_sub_to_clipboard()
- enable_subs_to_clip = not enable_subs_to_clip
- mp.osd_message("Clipboard inserter " .. (enable_subs_to_clip and "activated" or "deactived"), 3)
- end
- mp.observe_property("sub-text", 'string', rec)
- mp.add_key_binding("ctrl+v", ex)
- mp.add_key_binding("ctrl+t", toggle_sub_to_clipboard)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement