Guest User

Untitled

a guest
Aug 27th, 2020
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. -- -- Open clipboard inserter
  2. -- -- Wait for unknown word and add it to anki through yomichan
  3. -- -- Select all the subtitle lines to add to the card.
  4. -- -- Ctrl + c [make sure it copies with paragraphs included, otherwise it wont work]
  5. -- -- Change back to MPV and Ctrl + v
  6. -- -- Done. The lines, their respective Audio and the current paused image
  7. -- will be added to the back of the card
  8. -- -- It's not too bad. You would need the prefix path to whatever it is on Windows. The clipboard command would need to be to changed to the windows equivalent (whatever that is). You would need ffmpeg and curl in your path. I'm not 100% that "\" works as an escape character in windows cmd in this way (maybe it does), so possibly you would need to change that. I don't see anything else though.
  9.  
  10.  
  11. local utils = require 'mp.utils'
  12. local msg = require 'mp.msg'
  13. local subs = {}
  14.  
  15. -- User Config
  16. local FRONT_FIELD = "Expression"
  17. local SENTENCE_AUDIO_FIELD = "Audio"
  18. local SENTENCE_FIELD = "Sentences"
  19. local IMAGE_FIELD = "Picture"
  20. local prefix = os.getenv("HOME") .. '/.local/share/Anki2/User 1/collection.media/'
  21.  
  22. function trim(s)
  23. return string.gsub(s, "^%s*(.-)%s*$", "%1")
  24. end
  25.  
  26. function get_name(s, e)
  27. return mp.get_property("filename"):gsub('%W','').. tostring(s) .. tostring(e)
  28. end
  29.  
  30. function clip(text)
  31. os.execute("echo \"" .. text .. "\" | xclip -selection clipboard &")
  32. end
  33.  
  34. function create_audio(s, e)
  35.  
  36. if s == nil or e == nil then
  37. return
  38. end
  39.  
  40. local t = tostring(e - s)
  41. local source = mp.get_property("path")
  42. local destination = prefix .. get_name(s, e) .. '.mp3'
  43. local cmd = "ffmpeg -y -i \"" .. source .. "\" -ss " .. tostring(s)
  44. .. " -t " .. t .. " -q:a 0 -map a \"".. destination.. "\" 2> /dev/null &"
  45. os.execute(cmd)
  46. end
  47.  
  48. function get_clipboard()
  49. local command = 'xclip -o'
  50. local handle = io.popen(command)
  51. local result = handle:read("*a")
  52. handle:close()
  53. return result
  54. end
  55.  
  56.  
  57. function set_clipboard(name, sub)
  58. if sub and mp.get_property_number('sub-start') then
  59. local sub_delay = mp.get_property_native("sub-delay")
  60. clip(sub)
  61. subs[sub] = { mp.get_property_number('sub-start') + sub_delay, mp.get_property_number('sub-end') + sub_delay }
  62. end
  63. end
  64.  
  65.  
  66. function create_screenshot(s, e)
  67. local img = prefix .. get_name(s,e) .. '.jpg'
  68. mp.commandv("raw", "no-osd", "screenshot-to-file", img)
  69. os.execute("mogrify -resize 640x480 -quality 85 -format jpg \"" .. img .."\"")
  70. end
  71.  
  72. function get_extract()
  73. local lines = get_clipboard()
  74. local e = 0
  75. local s = 0
  76. for line in lines:gmatch("([^\n]*)\n?") do
  77. line = trim(line)
  78. if subs[line]~= nil then
  79. msg.info(lines)
  80. if subs[line][1] ~= nil and subs[line][2] ~= nil then
  81. msg.info(lines)
  82. if s == 0 then
  83. s = subs[line][1]
  84. else
  85. s = math.min(s, subs[line][1])
  86. end
  87. e = math.max(e, subs[line][2])
  88. end
  89. end
  90. end
  91. if e ~= 0 then
  92. local ifield = create_screenshot(s, e)
  93. local afield = create_audio(s, e)
  94. local tfield = string.gsub(string.gsub(lines,"\n", "<br />"), "\r", "")
  95. ifield = '<img src=\"'.. get_name(s,e) ..'.jpg\">'
  96. afield = "[sound:".. get_name(s,e) .. ".mp3]"
  97. add_to_last_added(ifield, afield, tfield)
  98. end
  99. end
  100.  
  101. function get(request)
  102. local args = {
  103. "curl",
  104. "localhost:8765",
  105. "-X",
  106. "GET",
  107. "-d",
  108. request
  109. }
  110.  
  111. local result =
  112. utils.subprocess(
  113. {
  114. args = args,
  115. cancellable = true
  116. }
  117. )
  118.  
  119. return utils.parse_json(result.stdout)
  120. end
  121.  
  122. function add_to_last_added(ifield, afield, tfield)
  123. local noteid = math.max(unpack(get('{"action": "findNotes", "params": {"query": "added:1"}, "version": 6}')["result"]))
  124. local note = get('{"action": "notesInfo", "params": {"notes": ['.. noteid.. ']}, "version": 6}')
  125.  
  126. ifield = string.gsub(ifield, "\"", "\\\"")
  127. afield = string.gsub(afield, "\"", "\\\"")
  128. tfield = string.gsub(tfield, "\"", "\\\"")
  129.  
  130. if note ~= nil then
  131. get('{"action": "updateNoteFields", "version": 6, "params": {"note": {"id": ' .. noteid.. ', "fields": {
  132. "' .. SENTENCE_AUDIO_FIELD .. '": "' .. afield .. '",
  133. "' .. SENTENCE_FIELD .. '": "' .. tfield .. '",
  134. "' .. IMAGE_FIELD .. '": "' .. ifield .. '"
  135. } } } }')
  136.  
  137. mp.osd_message("Updated note: " .. note["result"][1]["fields"][FRONT_FIELD]["value"], 3)
  138. msg.info("Updated note: " .. note["result"][1]["fields"][FRONT_FIELD]["value"])
  139. end
  140. end
  141.  
  142. function ex()
  143. pcall(get_extract)
  144. end
  145.  
  146. mp.observe_property("sub-text", 'string', set_clipboard)
  147. mp.add_key_binding("ctrl+v", ex)
Add Comment
Please, Sign In to add comment