1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
--[[
Copyright: Ren Tatsumoto and contributors
License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/gpl.html
Creates image and audio filenames compatible with Anki.
]]
local mp = require('mp')
local h = require('helpers')
local filename
local anki_compatible_length = (function()
-- 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'
return function(str, timestamp)
-- if timestamp provided, recalculate limit_bytes
local limit_bytes = allowed_bytes - (timestamp and #timestamp or timestamp_bytes)
if #str <= limit_bytes then
return str
end
local bytes_per_char = h.contains_non_latin_letters(str) and #'車' or #'z'
local limit_chars = math.floor(limit_bytes / bytes_per_char)
if limit_chars == limit_bytes then
return str:sub(1, limit_bytes)
end
local ret = h.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 = h.remove_newlines(ret.stdout)
ret.stdout = h.remove_leading_trailing_spaces(ret.stdout)
return ret.stdout
else
return 'subs2srs_' .. os.time()
end
end
end)()
local make_media_filename = function()
filename = mp.get_property("filename") -- filename without path
filename = h.remove_extension(filename)
filename = h.remove_filename_text_in_parentheses(filename)
filename = h.remove_text_in_brackets(filename)
filename = h.remove_special_characters(filename)
end
local function timestamp_range(start_timestamp, end_timestamp, extension)
-- Generates a filename suffix of the form: _00h00m00s000ms-99h99m99s999ms.extension
-- Extension must already contain the dot.
return string.format(
'_%s_%s%s',
h.human_readable_time(start_timestamp),
h.human_readable_time(end_timestamp),
extension
)
end
local function timestamp_static(timestamp, extension)
-- Generates a filename suffix of the form: _00h00m00s000ms.extension
-- Extension must already contain the dot.
return string.format(
'_%s%s',
h.human_readable_time(timestamp),
extension
)
end
local make_filename = function(...)
local args = {...}
local timestamp = #args < 3 and timestamp_static(...) or timestamp_range(...)
return string.lower(anki_compatible_length(filename, timestamp) .. timestamp)
end
mp.register_event("file-loaded", make_media_filename)
return {
make_filename = make_filename,
}
|