This commit is contained in:
2026-04-03 11:33:51 +02:00
parent 64922e1ae3
commit 0ed904319d
57 changed files with 2935 additions and 1377 deletions
+33 -81
View File
@@ -208,31 +208,6 @@ function create_select_tracklist_type_menu_opener(opts)
return tonumber(mp.get_property(opts.prop)), snd and tonumber(mp.get_property(snd.prop)) or nil
end
local function escape_codec(str)
if not str or str == '' then return '' end
local codec_map = {
mpeg2 = "mpeg2",
dvvideo = "dv",
pcm = "pcm",
pgs = "pgs",
subrip = "srt",
vtt = "vtt",
dvd_sub = "vob",
dvb_sub = "dvb",
dvb_tele = "teletext",
arib = "arib"
}
for key, value in pairs(codec_map) do
if str:find(key) then
return value
end
end
return str
end
local function serialize_tracklist(tracklist)
local items = {}
@@ -285,15 +260,14 @@ function create_select_tracklist_type_menu_opener(opts)
if track['demux-h'] then
h(track['demux-w'] and (track['demux-w'] .. 'x' .. track['demux-h']) or (track['demux-h'] .. 'p'))
end
if track['demux-fps'] then h(string.format('%.5g fps', track['demux-fps'])) end
if track['codec'] then h(escape_codec(track.codec)) end
if track['demux-fps'] then h(string.format('%.5gfps', track['demux-fps'])) end
h(track.codec)
if track['audio-channels'] then
h(track['audio-channels'] == 1
and t('%s channel', track['audio-channels'])
or t('%s channels', track['audio-channels']))
end
if track['demux-samplerate'] then h(string.format('%.3g kHz', track['demux-samplerate'] / 1000)) end
if track['demux-bitrate'] then h(string.format('%.0f kbps', track['demux-bitrate'] / 1000)) end
if track['demux-samplerate'] then h(string.format('%.3gkHz', track['demux-samplerate'] / 1000)) end
if track.forced then h(t('forced')) end
if track.default then h(t('default')) end
if track.external then
@@ -920,7 +894,8 @@ function open_subtitle_downloader()
return
end
local search_suggestion, destination_directory = '', nil
local search_suggestion, file_path, destination_directory = '', nil, nil
local credentials = {'--api-key', config.open_subtitles_api_key, '--agent', config.open_subtitles_agent}
if state.path then
if is_protocol(state.path) then
@@ -930,6 +905,7 @@ function open_subtitle_downloader()
local serialized_path = serialize_path(state.path)
if serialized_path then
search_suggestion = serialized_path.filename
file_path = state.path
destination_directory = serialized_path.dirname
end
end
@@ -942,7 +918,6 @@ function open_subtitle_downloader()
end
local handle_download, handle_search
local url = 'https://api.opensubtitles.com/api/v1'
-- Checks if there an error, or data is invalid. If true, reports the error,
-- updates menu to inform about it, and returns true.
@@ -991,49 +966,16 @@ function open_subtitle_downloader()
end
end)
local download_url = url .. '/download'
local args = itable_join({'download-subtitles'}, credentials, {
'--file-id', tostring(data.id),
'--destination', destination_directory,
})
local headers = {
['Accept'] = 'application/json',
['Api-Key'] = config.open_subtitles_api_key,
['Content-Type'] = 'application/json',
['User-Agent'] = config.open_subtitles_agent,
}
local body = {
file_id = data.id
}
http_request_async('POST', download_url, headers, body, function(error, data)
call_ziggy_async(args, function(error, data)
if not menu:is_alive() then return end
if data and data.link then
local file_path = utils.join_path(destination_directory, data.file_name)
local arg = {
'curl',
'-sL',
'--user-agent', config.open_subtitles_agent,
'-o', file_path,
data.link
}
if should_abort(error, data, function(data) return type(data.file) == 'string' end) then return end
mp.command_native({
name = 'subprocess',
capture_stdout = true,
capture_stderr = true,
playback_only = false,
args = arg
})
end
local function check_is_valid(data)
local path = data and utils.join_path(destination_directory, data.file_name) or nil
local meta = path and utils.file_info(path) or nil
return meta and meta.is_file
end
if should_abort(error, data, check_is_valid) then return end
load_track('sub', utils.join_path(destination_directory, data.file_name))
load_track('sub', data.file)
menu:update_items({
{
@@ -1043,7 +985,7 @@ function open_subtitle_downloader()
selectable = false,
},
{
title = t('Remaining downloads today: %s', data.remaining),
title = t('Remaining downloads today: %s', data.remaining .. '/' .. data.total),
italic = true,
muted = true,
icon = 'file_download',
@@ -1068,22 +1010,32 @@ function open_subtitle_downloader()
menu:update_items({{icon = 'spinner', align = 'center', selectable = false, muted = true}})
local args = itable_join({'search-subtitles'}, credentials)
local languages = itable_filter(get_languages(), function(lang) return lang:match('.json$') == nil end)
args[#args + 1] = '--languages'
args[#args + 1] = table.concat(table_keys(create_set(languages)), ',') -- deduplicates stuff like `en,eng,en`
local search_url = string.format('%s/subtitles?query=%s&languages=%s&page=%s', url, url_encode(query),
table.concat(table_keys(create_set(languages)), ','), tostring(page))
args[#args + 1] = '--page'
args[#args + 1] = tostring(page)
local headers = {
['Api-Key'] = config.open_subtitles_api_key,
['User-Agent'] = config.open_subtitles_agent,
}
if file_path then
args[#args + 1] = '--hash'
args[#args + 1] = file_path
end
http_request_async('GET', search_url, headers, nil, function(error, data)
if query and #query > 0 then
args[#args + 1] = '--query'
args[#args + 1] = query
end
call_ziggy_async(args, function(error, data)
if not menu:is_alive() then return end
local function check_is_valid(data)
return data and type(data.data) == 'table' and data.page and data.total_pages
return type(data.data) == 'table' and data.page and data.total_pages
end
if should_abort(error, data, check_is_valid) then return end
local subs = itable_filter(data.data, function(sub)
@@ -1183,4 +1135,4 @@ function open_subtitle_downloader()
end
end
)
end
end