-
Notifications
You must be signed in to change notification settings - Fork 1
/
vlc_gif_maker.lua
281 lines (224 loc) · 8.72 KB
/
vlc_gif_maker.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
--[[
--------------------------------------------
Installation guide: put "vlc_gif_maker.lua" file in installation directory
suitable to your operating system:
* Windows (all users): %ProgramFiles%\VideoLAN\VLC\lua\extensions\
* Windows (current user): %APPDATA%\VLC\lua\extensions\
* Linux (all users): /usr/lib/vlc/lua/extensions/
* Linux (current user): ~/.local/share/vlc/lua/extensions/
* Mac OS X (all users): /Applications/VLC.app/Contents/MacOS/share/lua/extensions/
* Mac OS X (current user): /Users/%your_name%/Library/Application Support/org.videolan.vlc/lua/extensions/
--------------------------------------------
To open GIF Maker: View > VLC Gif Maker
--]]----------------------------------------
default_command = 'ffmpeg -ss {start_timestamp} -to {stop_timestamp} -i "{input_file}" -vf "fps={fps},scale=498:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop {loop} "{output_path}/{output_filename}.gif"'
command = false
output_path = false
function string_replace(target, substring, replacement, n)
return (target:gsub(substring:gsub("%p", "%%%0"), replacement:gsub("%%", "%%%%"), n))
end
function get_timestamp()
local microseconds = vlc.var.get(vlc.object.input(), "time")
local seconds_total = math.floor((microseconds/1000)/1000)
local hours = math.floor((seconds_total % 86400)/3600)
local minutes = math.floor((seconds_total % 3600)/60)
local seconds = math.floor((seconds_total % 60))
local miliseconds = math.floor((microseconds % 1000))
-- format() seems to not be supported :(
return hours .. ":" .. minutes .. ":" .. seconds .. "." .. miliseconds
end
-- seperate file for path so there are no issues and weird gimmicks with multi-line commands
-- this is a simple project after all, not nginx
function load_config(filename)
local config_path = vlc.config.configdir() .. "/gif_maker_" .. filename
local config_file, err = io.open(config_path, "r")
if config_file == nil then
if filename == 'command' then
value = default_command
elseif filename == 'output_path' then
value = vlc.config.homedir()
end
save_config(filename, value)
return value
end
value = config_file:read()
config_file:close()
if not value or value == "" then
if filename == 'command' then
value = default_command
elseif filename == 'output_path' then
value = vlc.config.homedir()
end
save_config(filename, value)
end
return value
end
function save_config(filename, value)
config_path = vlc.config.configdir() .. "/gif_maker_" .. filename
local file = assert(io.open(config_path, 'w+b'), 'Error while saving the ' .. filename)
file:write(value)
file:close()
end
function gui_create_ffmpeg_warning()
ffmpegDlg = vlc.dialog("GIF Maker")
-- col, row, col_span, row_span, width, height
ffmpegDlg:add_label("WARNING: ffmpeg couldn't be found! Make sure it's available globally (PATH for Windows users)", 1, 1)
ffmpegDlg:add_button("OK", startExtension, 1, 2)
end
function gui_create_main_dialog(command, output_path)
local input = vlc.object.input()
dlg = vlc.dialog("GIF Maker")
-- left side
-- col, row, col_span, row_span, width, height
dlg:add_label("Start time: ", 1, 1)
start_timestamp_input = dlg:add_text_input("", 2, 1)
dlg:add_button("Get", fill_start_timestamp, 3, 1)
dlg:add_label("End time: ", 1, 2)
stop_timestamp_input = dlg:add_text_input("", 2, 2)
dlg:add_button("Get", fill_stop_timestamp, 3, 2)
dlg:add_label("GIFs output path:", 1, 3)
output_path_input = dlg:add_text_input(output_path, 1, 4, 3)
dlg:add_label("Filename (leave empty to randomly generate):", 1, 5)
output_filename_input = dlg:add_text_input('', 1, 6, 3)
-- right side
separator_string = '|<br/>'
for var=0,17 do
separator_string = separator_string .. '|<br/>'
end
dlg:add_label("<div style='color:#000; line-height:80%;'>" .. separator_string .. "</div>", 4, 1, 1, 8)
dlg:add_label("<b>Advanced settings:</b>", 6, 1)
dlg:add_label("Command to execute:", 5, 2, 2)
dlg:add_button("Set to default", set_default_command, 7, 2)
command_input = dlg:add_text_input(command, 5, 3, 3)
dlg:add_label("FPS", 5, 4, 1)
fps_input = dlg:add_text_input("15", 5, 5, 1)
dlg:add_label("Looping", 6, 4, 1)
looping_input = dlg:add_dropdown(6, 5, 1)
looping_input:add_value("Loop GIF", 0)
looping_input:add_value("Only play once", -1)
--[[dlg:add_label("Resolution", 7, 4, 1)
resolution_input = dlg:add_dropdown(7, 5, 1)
resolution_input:add_value("498px x height", 1)
resolution_input:add_value("Don't scale (input video resolution)", 2)
resolution_input:add_value("Set height in pixels:", 3)
resolution_input:add_value("Set width in pixels:", 4)
resolution_input:add_value("Set height and width in pixels:", 5)]]
-- middle
dlg:add_button("Generate GIF", generate_gif, 3, 9, 3, 2)
end
function fill_start_timestamp()
start_timestamp_input:set_text(get_timestamp())
end
function fill_stop_timestamp()
stop_timestamp_input:set_text(get_timestamp())
end
function generateCommand(command, generalOptions, commandBuilder)
for optionName,optionValue in pairs(generalOptions) do
if vlc.win and string.sub(optionValue, 1,1) == '/' then optionValue = string.sub(optionValue, 2, -1) end
command = string_replace(command, optionName, tostring(optionValue))
end
if commandBuilder then
return generateCommand(command, commandBuilder)
end
command = string.gsub(command, '\\', '\\')
if vlc.win then command = string_replace(command, '/', '\\') end
command = string_replace(command, '%5B', '[')
command = string_replace(command, '%5D', ']')
vlc.msg.info(command)
return command
end
function generate_gif()
local start_timestamp = start_timestamp_input:get_text()
local stop_timestamp = stop_timestamp_input:get_text()
local command = command_input:get_text()
local fps = fps_input:get_text()
local item = vlc.input.item()
local uri = item:uri()
local media_path
if vlc.win then
media_path = string.gsub(uri, '^file:///', '')
else
media_path = string.gsub(uri, '^file://', '')
end
local output_path = output_path_input:get_text()
local output_filename = output_filename_input:get_text()
if output_filename == '' then
output_filename = 'g' .. os.time()
else
output_filename = output_filename:match('([^.]+)') -- remove extension
end
save_config('command', command)
save_config('output_path', output_path)
local generalOptions = {}
generalOptions['{start_timestamp}'] = start_timestamp
generalOptions['{stop_timestamp}'] = stop_timestamp
generalOptions['{input_file}'] = media_path
generalOptions['{output_path}'] = output_path
generalOptions['{output_filename}'] = output_filename
local commandBuilder = {}
commandBuilder['{fps}'] = fps
commandBuilder['{loop}'] = looping_input:get_value()
command = generateCommand(command, generalOptions, commandBuilder)
os.execute(command)
vlc.osd.message("GIF created! Saved to " .. output_path .. "/" .. output_filename, 1, "top", 3000000) -- why, tf, is vlc's osd msg duration in MICROseconds??
end
function set_default_command()
command_input:set_text(default_command)
save_config('command', default_command)
end
function check_ffmpeg_status()
status_code = os.execute("ffmpeg -version")
if status_code then
startExtension(command, output_path)
else
gui_create_ffmpeg_warning()
end
end
function startExtension()
if ffmpegDlg then
ffmpegDlg:delete()
ffmpegDlg = nil
startExtension()
end
gui_create_main_dialog(command, output_path)
end
-- functions below are called by VLC
function descriptor()
return {
title = "VLC GIF maker";
version = "0.0.5";
author = "Piotr Zdolski";
url = "https://github.com/Dante383/VLC_GIF_Maker";
description = [[
This extension allows you to quickly make GIFs.
]];
capabilities = {"menu"};
}
end
function activate()
command = load_config('command')
output_path = load_config('output_path')
-- is ffmpeg even used in the current command?
if string.find(command, "ffmpeg") then
check_ffmpeg_status()
else
startExtension()
end
end
function deactivate()
save_config('command', command)
end
function close()
if dlg then
dlg:delete()
dlg = nil
end
if ffmpegDlg then
ffmpegDlg:delete()
ffmpegDlg = nil
startExtension()
end
end
function menu()
return {"GIF maker","Settings"}
end