Advertisement
Guest User

animecardslinux

a guest
Sep 17th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.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
  5. -- -- Tab 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.  
  9. local utils = require 'mp.utils'
  10. local msg = require 'mp.msg'
  11. local subs = {}
  12. local enable_subs_to_clip = true
  13.  
  14. -- User Config
  15. local FRONT_FIELD = "Front"
  16. local SENTENCE_AUDIO_FIELD = "Sentence-Audio"
  17. local SENTENCE_FIELD = "Sentence"
  18. local IMAGE_FIELD = "Image"
  19. local FFMPEG_PATH = [[D:\ffmpeg\bin\ffmpeg]]
  20. local prefix = os.getenv("HOME") .. '/.local/share/Anki2/User 1/collection.media/'
  21. local AUDIO_CLIP_FADE = 0.2
  22. local AUDIO_CLIP_PADDING = 0.75
  23.  
  24. local function trim(s)
  25. return string.gsub(s, "^%s*(.-)%s*$", "%1")
  26. end
  27.  
  28. local function get_name(s, e)
  29. return mp.get_property("filename"):gsub('%W','').. tostring(s) .. tostring(e)
  30. end
  31.  
  32. local function record_sub(name, text)
  33. if text and mp.get_property_number('sub-start') and mp.get_property_number('sub-end') then
  34. local sub_delay = mp.get_property_native("sub-delay")
  35. local newtext = string.gsub(text, "\n", "")
  36. newtext = string.gsub(newtext, "\r", "")
  37. newtext = string.gsub(newtext, '"', "”")
  38. if newtext == '' then
  39. return
  40. end
  41.  
  42. subs[newtext] = { mp.get_property_number('sub-start') + sub_delay, mp.get_property_number('sub-end') + sub_delay }
  43. if enable_subs_to_clip then
  44. local cmd = 'echo ' .. newtext .. ' | clip'
  45. mp.command("run cmd /D /C @echo off | chcp 65001 | " .. cmd)
  46. end
  47. end
  48. end
  49.  
  50. local function create_audio(s, e)
  51.  
  52. if s == nil or e == nil then
  53. return
  54. end
  55.  
  56. local ff_aid = 0
  57. local tracks_count = mp.get_property_number("track-list/count")
  58. for i = 1, tracks_count do
  59. local track_type = mp.get_property(string.format("track-list/%d/type", i))
  60. local track_index = mp.get_property_number(string.format("track-list/%d/ff-index", i))
  61. local track_selected = mp.get_property(string.format("track-list/%d/selected", i))
  62.  
  63. if track_type == "audio" and track_selected == "yes" then
  64. ff_aid = track_index - 1
  65. break
  66. end
  67. end
  68.  
  69. local name = get_name(s, e)
  70. s = s - AUDIO_CLIP_PADDING
  71. local t = e - s + AUDIO_CLIP_PADDING
  72. local source = mp.get_property("path")
  73. local destination = prefix .. name .. '.mp3'
  74. local args = {
  75. FFMPEG_PATH,
  76. "-y",
  77. "-ss", tostring(s),
  78. "-i", source,
  79. "-t", string.format("%.3f", t),
  80. "-map", string.format("0:a:%d", ff_aid),
  81. "-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),
  82. destination
  83. }
  84. utils.subprocess_detached({ args=args })
  85. end
  86.  
  87. function get_clipboard()
  88. local command = 'xclip -o'
  89. local handle = io.popen(command)
  90. local result = handle:read("*a")
  91. handle:close()
  92. return result
  93. end
  94.  
  95. local function create_screenshot(s, e)
  96. local source = mp.get_property("path")
  97. local img = prefix .. get_name(s,e) .. '.webp'
  98. local args = {
  99. FFMPEG_PATH,
  100. "-y",
  101. "-ss", tostring(mp.get_property_number("time-pos")),
  102. "-i", source,
  103. "-vframes", "1",
  104. "-vf", "scale=-2:480",
  105. "-vcodec", "libwebp",
  106. "-compression_level", "6",
  107. "-preset", "drawing",
  108. img
  109. }
  110. utils.subprocess_detached({ args=args })
  111. end
  112.  
  113. local function get(request)
  114. local args = {
  115. "curl",
  116. "localhost:8765",
  117. "-X",
  118. "GET",
  119. "-d",
  120. request
  121. }
  122.  
  123. local result =
  124. utils.subprocess(
  125. {
  126. args = args,
  127. cancellable = true
  128. }
  129. )
  130.  
  131. return utils.parse_json(result.stdout)
  132. end
  133.  
  134. local function add_to_last_added(ifield, afield, tfield)
  135. local noteid = math.max(unpack(get('{"action": "findNotes", "params": {"query": "added:1"}, "version": 6}')["result"]))
  136. local note = get('{"action": "notesInfo", "params": {"notes": ['.. noteid.. ']}, "version": 6}')
  137.  
  138. ifield = string.gsub(ifield, "\"", "\\\"")
  139. afield = string.gsub(afield, "\"", "\\\"")
  140. tfield = string.gsub(tfield, "\"", "\\\"")
  141.  
  142. if note ~= nil then
  143. get('{"action": "updateNoteFields", "version": 6, "params": {"note": {"id": ' .. noteid.. ', "fields": {"' .. SENTENCE_AUDIO_FIELD .. '": "' .. afield .. '","' .. SENTENCE_FIELD .. '": "' .. tfield .. '","' .. IMAGE_FIELD .. '": "' .. ifield .. '"} } } }')
  144.  
  145. mp.osd_message("Updated note: " .. note["result"][1]["fields"][FRONT_FIELD]["value"], 3)
  146. msg.info("Updated note: " .. note["result"][1]["fields"][FRONT_FIELD]["value"])
  147. end
  148. end
  149.  
  150. local function get_extract()
  151. local lines = get_clipboard()
  152. local e = 0
  153. local s = 0
  154. for line in lines:gmatch("[^\r\n]+") do
  155. line = trim(line)
  156. if subs[line]~= nil then
  157. msg.info(lines)
  158. if subs[line][1] ~= nil and subs[line][2] ~= nil then
  159. msg.info(lines)
  160. if s == 0 then
  161. s = subs[line][1]
  162. else
  163. s = math.min(s, subs[line][1])
  164. end
  165. e = math.max(e, subs[line][2])
  166. end
  167. end
  168. end
  169. if e ~= 0 then
  170. create_screenshot(s, e)
  171. create_audio(s, e)
  172. local ifield = '<img src='.. get_name(s,e) ..'.webp>'
  173. local afield = "[sound:".. get_name(s,e) .. ".mp3]"
  174. local tfield = string.gsub(string.gsub(lines,"\n+", "<br />"), "\r", "")
  175. add_to_last_added(ifield, afield, tfield)
  176. end
  177. end
  178.  
  179. local function ex()
  180. pcall(get_extract)
  181. end
  182.  
  183. local function rec(...)
  184. pcall(record_sub, ...)
  185. end
  186.  
  187. local function toggle_sub_to_clipboard()
  188. enable_subs_to_clip = not enable_subs_to_clip
  189. mp.osd_message("Clipboard inserter " .. (enable_subs_to_clip and "activated" or "deactived"), 3)
  190. end
  191.  
  192. mp.observe_property("sub-text", 'string', rec)
  193. mp.add_key_binding("ctrl+v", ex)
  194. mp.add_key_binding("ctrl+t", toggle_sub_to_clipboard)
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement