mpv2oboeru

mpv helpers to create flashcards from movies and TV shows
git clone anongit@rnpnr.xyz:mpv2oboeru.git
Log | Files | Refs | Feed | README | LICENSE

Commit: ec8e5d848cdf4f6e5686c7a4db78e5eddb1dbc11
Parent: c8a8e735c85326b02bae4bd6eb866629f5623cd8
Author: Randy Palamar
Date:   Wed, 25 May 2022 08:55:02 -0600

remove support for fetching from forvo

this had limited use since it could only fetch the first pronunciation
which is usually not the best one.

Diffstat:
Msubs2srs.lua | 152-------------------------------------------------------------------------------
1 file changed, 0 insertions(+), 152 deletions(-)

diff --git a/subs2srs.lua b/subs2srs.lua @@ -17,7 +17,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. Requirements: * mpv >= 0.32.0 -* curl (for forvo) * xclip (when running X11) * wl-copy (when running Wayland) @@ -59,11 +58,6 @@ local config = { sentence_field = "SentKanji", audio_field = "SentAudio", create_image = true, - - -- Forvo support - use_forvo = "yes", -- 'yes', 'no', 'always' - vocab_field = "VocabKanji", -- target word field - vocab_audio_field = "VocabAudio", -- target word audio } -- Defines config profiles @@ -86,10 +80,8 @@ local Menu = require('menu') -- namespaces local subs local clip_autocopy -local ankiconnect local menu local platform -local append_forvo_pronunciation -- classes local Subtitle @@ -602,156 +594,12 @@ local function init_platform_nix() 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 = 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) - helpers.notify(string.format("Reencoded: '%s'", 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 helpers.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 helpers.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 helpers.is_empty(stored_data[config.vocab_audio_field]) then - local forvo_pronunciation = get_forvo_pronunciation(stored_data[config.vocab_field]) - if not helpers.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 - ------------------------------------------------------------- -- subtitles and timings subs = {