-
Notifications
You must be signed in to change notification settings - Fork 8
/
credit-skipper.lua
214 lines (170 loc) · 5.68 KB
/
credit-skipper.lua
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
--[[
-- Install script to ~/.local/share/vlc/lua/extensions/credit-skipper.lua
-- Profiles saved to: ~/.config/vlc/credit-skipper.conf
]]
function descriptor()
return {
title = "Credit Skipper",
version = "1.0.0",
author = "Michael Bull",
url = "https://github.com/michaelbull/vlc-credit-skipper",
shortdesc = "Skip Intro/Outro Credits",
description = "Automatically skip intro/outro credit sequences in VLC.",
capabilities = {}
}
end
function activate()
profiles = {}
config_file = vlc.config.configdir() .. "/credit-skipper.conf"
if (file_exists(config_file)) then
load_all_profiles()
end
open_dialog()
end
function deactivate()
dialog:delete()
end
function close()
vlc.deactivate()
end
function meta_changed()
end
function open_dialog()
dialog = vlc.dialog(descriptor().title)
dialog:add_label("<center><h3>Profile</h3></center>", 1, 1, 2, 1)
dialog:add_button("Load", populate_profile_fields, 1, 3, 1, 1)
dialog:add_button("Delete", delete_profile, 2, 3, 1, 1)
dialog:add_label("", 1, 4, 2, 1)
dialog:add_label("<center><h3>Settings</h3></center>", 1, 5, 2, 1)
dialog:add_label("Profile name:", 1, 6, 1, 1)
profile_name_input = dialog:add_text_input("", 2, 6, 1, 1)
dialog:add_label("Intro duration (s):", 1, 7, 1, 1)
start_time_input = dialog:add_text_input("", 2, 7, 1, 1)
dialog:add_label("Outro duration (s):", 1, 8, 1, 1)
finish_time_input = dialog:add_text_input("", 2, 8, 1, 1)
dialog:add_button("Save", save_profile, 1, 9, 2, 1)
dialog:add_label("", 1, 10, 2, 1)
dialog:add_label("<center><strong>Ensure your playlist is queued<br/>before pressing start.</strong></center>", 1, 11, 2, 1)
dialog:add_button("Start Playlist", start_playlist, 1, 12, 2, 1)
populate_profile_dropdown()
populate_profile_fields()
end
function populate_profile_dropdown()
profile_dropdown = dialog:add_dropdown(1, 2, 2, 1)
for i, profile in pairs(profiles) do
profile_dropdown:add_value(profile.name, i)
end
end
function populate_profile_fields()
local profile = profiles[profile_dropdown:get_value()]
if profile then
profile_name_input:set_text(profile.name)
start_time_input:set_text(profile.start_time)
finish_time_input:set_text(profile.finish_time)
end
end
function delete_profile()
local dropdown_value = profile_dropdown:get_value()
if profiles[dropdown_value] then
profiles[dropdown_value] = nil
save_all_profiles()
end
end
function save_profile()
if profile_name_input:get_text() == "" then return end
if start_time_input:get_text() == "" then start_time_input:set_text("0") end
if finish_time_input:get_text() == "" then finish_time_input:set_text("0") end
local updated_existing = false
for _, profile in pairs(profiles) do
if profile.name == profile_name_input:get_text() then
profile.start_time = tonumber(start_time_input:get_text())
profile.finish_time = tonumber(finish_time_input:get_text())
updated_existing = true
end
end
if not updated_existing then
table.insert(profiles, {
name = profile_name_input:get_text(),
start_time = tonumber(start_time_input:get_text()),
finish_time = tonumber(finish_time_input:get_text())
})
end
save_all_profiles()
end
function start_playlist()
if start_time_input:get_text() == "" then return end
if finish_time_input:get_text() == "" then return end
local playlist = vlc.playlist.get("playlist", false)
local children = {}
for _, child in pairs(playlist.children) do
if child.duration ~= -1 then
table.insert(children, {
path = child.path,
name = child.name,
duration = child.duration
})
end
end
vlc.playlist.clear()
local skip_start = tonumber(start_time_input:get_text())
local skip_finish = tonumber(finish_time_input:get_text())
for _, child in pairs(children) do
local options = {}
if (child.duration - skip_start - skip_finish) > 0 then
if skip_start > 0 then
table.insert(options, "start-time=" .. skip_start)
end
if skip_finish > 0 then
table.insert(options, "stop-time=" .. (child.duration - skip_finish))
end
end
vlc.playlist.enqueue({
{
path = child.path,
name = child.name,
duration = child.duration,
options = options
}
})
end
dialog:hide()
vlc.playlist.play()
end
function save_all_profiles()
io.output(config_file)
for _, profile in pairs(profiles) do
io.write(profile.name)
io.write("=")
io.write(profile.start_time)
io.write(",")
io.write(profile.finish_time)
io.write("\n")
end
io.close()
dialog:del_widget(profile_dropdown)
populate_profile_dropdown()
end
function load_all_profiles()
local lines = lines_from(config_file)
for _, line in pairs(lines) do
for name, start_time, finish_time in string.gmatch(line, "(.+)=(%d+),(%d+)") do
table.insert(profiles, {
name = name,
start_time = start_time,
finish_time = finish_time
})
end
end
end
function file_exists(file)
local f = io.open(file, "rb")
if f then f:close() end
return f ~= nil
end
function lines_from(file)
local lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
return lines
end