forked from mpv-player/mpv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds script messages to select playlist entries, tracks, chapters and subtitle lines using the newly introduced mp.input.select(). This fully closes mpv-player#13964.
- Loading branch information
1 parent
708e05e
commit ad4699c
Showing
10 changed files
with
251 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
--[[ | ||
This file is part of mpv. | ||
mpv is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
mpv is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with mpv. If not, see <http://www.gnu.org/licenses/>. | ||
]] | ||
|
||
local input = require "mp.input" | ||
|
||
mp.add_forced_key_binding(nil, "select-playlist", function () | ||
local playlist = {} | ||
local default_item = 1 | ||
|
||
for i, entry in ipairs(mp.get_property_native("playlist")) do | ||
playlist[i] = select(2, require "mp.utils".split_path(entry.filename)) | ||
|
||
if entry.playing then | ||
default_item = i | ||
end | ||
end | ||
|
||
input.select({ | ||
prompt = "Select a playlist entry:", | ||
items = playlist, | ||
default_item = default_item, | ||
submit = function (index) | ||
mp.commandv("playlist-play-index", index - 1) | ||
input.terminate() | ||
end, | ||
}) | ||
end) | ||
|
||
mp.add_forced_key_binding(nil, "select-track", function () | ||
local tracks = {} | ||
|
||
for i, track in ipairs(mp.get_property_native("track-list")) do | ||
tracks[i] = track.type:sub(1, 1):upper() .. track.type:sub(2) .. ": " .. | ||
(track.selected and "●" or "○") .. | ||
(track.title and " " .. track.title or "") .. | ||
" (" .. ( | ||
(track.lang and track.lang .. " " or "") .. | ||
(track.codec and track.codec .. " " or "") .. | ||
(track["demux-w"] and track["demux-w"] .. "x" .. track["demux-h"] .. " " or "") .. | ||
(track["demux-fps"] and not track.image and string.format("%.3f", track["demux-fps"]) .. "FPS " or "") .. | ||
(track["demux-channel-count"] and track["demux-channel-count"] .. "ch " or "") .. | ||
(track["demux-samplerate"] and track["demux-samplerate"] / 1000 .. "kHz " or "") .. | ||
(track.external and "external " or "") | ||
):sub(1, -2) .. ")" | ||
end | ||
|
||
input.select({ | ||
prompt = "Select a track:", | ||
items = tracks, | ||
submit = function (id) | ||
local track = mp.get_property_native("track-list/" .. id - 1) | ||
if track then | ||
mp.set_property(track.type, track.selected and "no" or track.id) | ||
end | ||
input.terminate() | ||
end, | ||
}) | ||
end) | ||
|
||
local function show_error(message) | ||
mp.msg.error(message) | ||
if mp.get_property_native("vo-configured") then | ||
mp.osd_message(message) | ||
end | ||
end | ||
|
||
mp.add_forced_key_binding(nil, "select-secondary-sub", function () | ||
local subs = {} | ||
local sub_indexes = {} | ||
local default_item = 1 | ||
local secondary_sid = mp.get_property_native("secondary-sid") | ||
|
||
for i, track in ipairs(mp.get_property_native("track-list")) do | ||
if track.type == "sub" then | ||
subs[#subs + 1] = (track.selected and "●" or "○") .. | ||
(track.title and " " .. track.title or "") .. | ||
" (" .. ( | ||
(track.lang and track.lang .. " " or "") .. | ||
(track.codec and track.codec .. " " or "") .. | ||
(track.external and "external " or "") | ||
):sub(1, -2) .. ")" | ||
|
||
sub_indexes[#sub_indexes + 1] = i - 1 | ||
|
||
if track.id == secondary_sid then | ||
default_item = #subs | ||
end | ||
end | ||
end | ||
|
||
if #subs == 0 then | ||
show_error("No subtitle is loaded.") | ||
return | ||
end | ||
|
||
input.select({ | ||
prompt = "Select a secondary subtitle:", | ||
items = subs, | ||
default_item = default_item, | ||
submit = function (id) | ||
local sub = mp.get_property_native("track-list/" .. sub_indexes[id]) | ||
if sub then | ||
mp.set_property("secondary-sid", sub.selected and "no" or sub.id) | ||
end | ||
input.terminate() | ||
end, | ||
}) | ||
end) | ||
|
||
local function format_time(t) | ||
local h = math.floor(t / (60 * 60)) | ||
t = t - (h * 60 * 60) | ||
local m = math.floor(t / 60) | ||
local s = t - (m * 60) | ||
|
||
return string.format("%.2d:%.2d:%.2d", h, m, s) | ||
end | ||
|
||
mp.add_forced_key_binding(nil, "select-chapter", function () | ||
local chapters = {} | ||
local default_item = mp.get_property_native("chapter") | ||
|
||
if default_item == nil then | ||
show_error("No chapters are present.") | ||
return | ||
end | ||
|
||
for i, chapter in ipairs(mp.get_property_native("chapter-list")) do | ||
chapters[i] = format_time(chapter.time) .. " " .. chapter.title | ||
end | ||
|
||
input.select({ | ||
prompt = "Select a chapter:", | ||
items = chapters, | ||
default_item = default_item + 1, | ||
submit = function (chapter) | ||
mp.set_property("chapter", chapter - 1) | ||
input.terminate() | ||
end, | ||
}) | ||
end) | ||
|
||
mp.add_forced_key_binding(nil, "sub-seek", function () | ||
local sub = mp.get_property_native("current-tracks/sub") | ||
|
||
if sub == nil then | ||
show_error("No subtitle is loaded.") | ||
return | ||
end | ||
|
||
local r = mp.command_native({ | ||
name = "subprocess", | ||
capture_stdout = true, | ||
args = sub.external | ||
and {"ffmpeg", "-loglevel", "quiet", "-i", sub["external-filename"], "-f", "lrc", "-map_metadata", "-1", "-fflags", "+bitexact", "-"} | ||
or {"ffmpeg", "-loglevel", "quiet", "-i", mp.get_property("path"), "-map", "s:" .. sub["id"] - 1, "-f", "lrc", "-map_metadata", "-1", "-fflags", "+bitexact", "-"} | ||
}) | ||
|
||
if r.status < 0 then | ||
show_error("subprocess error: " .. r.error_string) | ||
return | ||
end | ||
|
||
if r.status > 0 then | ||
show_error("ffmpeg failed with code " .. r.status) | ||
return | ||
end | ||
|
||
local sub_lines = {} | ||
local default_item = 1 | ||
|
||
local sub_start = mp.get_property_native("sub-start", 0) | ||
local m = math.floor(sub_start / 60) | ||
local s = sub_start - m * 60 | ||
sub_start = string.format("%.2d:%05.2f", m, s) | ||
|
||
-- Strip HTML and ASS tags. | ||
for line in r.stdout:gsub("<.->", ""):gsub("{\\.-}", ""):gmatch("[^\n]+") do | ||
sub_lines[#sub_lines + 1] = line | ||
|
||
if line:find("^%[" .. sub_start) then | ||
default_item = #sub_lines | ||
end | ||
end | ||
|
||
input.select({ | ||
prompt = "Select a line to seek to:", | ||
items = sub_lines, | ||
default_item = default_item, | ||
submit = function (index) | ||
mp.commandv("seek", sub_lines[index]:match("[%d:%.]+"), "absolute") | ||
input.terminate() | ||
end, | ||
}) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters