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: d105b7acf46a3e381ee31ca534a6dafc86df2dcf
Parent: fe5a6153c429b43415ff80096741b5ceef770483
Author: Ren Tatsumoto
Date:   Thu, 29 Oct 2020 19:02:03 +0300

deal with long filenames smarter

Diffstat:
Msubs2srs.lua | 49++++++++++++++++++++++++++++---------------------
1 file changed, 28 insertions(+), 21 deletions(-)

diff --git a/subs2srs.lua b/subs2srs.lua @@ -217,24 +217,34 @@ local function subprocess(args) } end -local function anki_compatible_length(str) - -- anki forcibly mutilates all filenames longer than 64 characters - -- leave 25 characters for the filename - -- the rest is reserved for the timestamp, which is added later - - local ret = subprocess { - 'awk', - '-v', string.format('str=%s', str), - '-v', 'limit=25', - 'BEGIN{print substr(str, 1, limit); exit}' - } +local anki_compatible_length +do + -- Anki forcibly mutilates all filenames longer than 119 bytes when you run `Tools->Check Media...`. + local allowed_bytes = 119 + local timestamp_bytes = #'_(99h99m99s999ms-99h99m99s999ms).webp' + local limit_bytes = allowed_bytes - timestamp_bytes + + anki_compatible_length = function(str) + if #str <= limit_bytes then + return str + end - if ret.status == 0 then - ret.stdout = remove_newlines(ret.stdout) - ret.stdout = remove_leading_trailing_spaces(ret.stdout) - return ret.stdout - else - return 'subs2srs_' .. os.time() + local bytes_per_char = contains_non_latin_letters(str) and #'車' or #'z' + local limit_chars = math.floor(limit_bytes / bytes_per_char) + local ret = subprocess { + 'awk', + '-v', string.format('str=%s', str), + '-v', string.format('limit=%d', limit_chars), + 'BEGIN{print substr(str, 1, limit); exit}' + } + + if ret.status == 0 then + ret.stdout = remove_newlines(ret.stdout) + ret.stdout = remove_leading_trailing_spaces(ret.stdout) + return ret.stdout + else + return 'subs2srs_' .. os.time() + end end end @@ -244,10 +254,7 @@ local function construct_media_filenames(sub) filename = remove_extension(filename) filename = remove_text_in_brackets(filename) filename = remove_special_characters(filename) - - if contains_non_latin_letters(filename) then - filename = anki_compatible_length(filename) - end + filename = anki_compatible_length(filename) filename = string.format( '%s_(%s-%s)',