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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
-- README:
-- original version by Zehkul https://gist.github.com/Zehkul/25ea7ae77b30af959be0
-- needs: yad, libnotify (and --ytdl if you want to encode from streams)
-- press any of alt + w, g or x to set the start frame
-- press again to set the end frame and use
-- alt + w to make a webm
-- alt + g to make a gif
-- alt + x to make a x264 encoded mkv
-- Note: encoding a webm to a specific filesize will only work if audio is disabled
-- and including subs will make them lose their styling
local msg = require 'mp.msg'
local opt = require 'mp.options'
local mputils = require 'mp.utils'
-- default options, convert_script.conf is read
local options = {
bitrate_multiplier = 0.975, -- to make sure the file won’t go over the target file size, set it to 1 if you don’t care
}
read_options(options, "convert_script_webm")
read_options(options, "convert_script_gif")
read_options(options, "convert_script_x264")
-----------------
-- Main script --
-----------------
function set_timepos(func)
if timepos1 then
timepos2 = mp.get_property("time-pos")
timepos2_humanreadable = mp.get_property_osd("time-pos")
if tonumber(timepos1) > tonumber(timepos2) then
length = timepos1-timepos2
start = timepos2
start_humanreadable = timepos2_humanreadable
end_humanreadable = timepos1_humanreadable
msg.info("End frame set")
elseif tonumber(timepos2) > tonumber(timepos1) then
length = timepos2-timepos1
start = timepos1
start_humanreadable = timepos1_humanreadable
end_humanreadable = timepos2_humanreadable
msg.info("End frame set")
else
msg.error("Both frames are the same, ignoring the second one")
mp.osd_message("Both frames are the same, ignoring the second one")
timepos2 = nil
return
end
timepos1 = nil
func.call_gui()
else
timepos1 = mp.get_property("time-pos")
timepos1_humanreadable = mp.get_property_osd("time-pos")
msg.info("Start frame set")
mp.osd_message("Start frame set")
end
end
function convert_script_hotkey_call_webm ()
func = {
call_gui = call_gui_webm
}
set_timepos(func)
end
function convert_script_hotkey_call_gif ()
func = {
call_gui = call_gui_gif
}
set_timepos(func)
end
function convert_script_hotkey_call_x264 ()
func = {
call_gui = call_gui_x264
}
set_timepos(func)
end
------------
-- Encode --
------------
function generate_filenames(ext)
local filename_ext = mp.get_property_osd("filename")
filename_ext = string.gsub(filename_ext, "'", "'\\''")
local filename = string.gsub(filename_ext, "%....$","")
if string.len(filename) > 230 then
filename = mp.get_property("options/title")
if filename == 'mpv - ${media-title}' or string.len(filename) > 230 then
filename = 'output'
end
end
local path = mp.get_property("path", "")
local dir, fil = mputils.split_path(path)
file_in = dir .. fil
file_out = dir .. filename .. "-out." .. ext
file_in = string.gsub(file_in, "'", "'\\''")
file_out = string.gsub(file_out, "'", "'\\''")
if string.sub(file_in,1,string.len("http")) == "http" then
file_in = mp.get_property("stream-open-filename")
file_out = os.getenv("HOME") .. "/" .. mp.get_property("media-title") .. "-out." .. ext
file_out = string.gsub(file_out, "'", "'\\''")
end
end
function encode_webm ()
generate_filenames("webm")
local full_command = '(ffmpeg -ss ' .. start .. ' -i \'' .. file_in .. '\' -t ' .. length .. ' -b:v ' .. bitrate .. bitsize .. ' -vf scale=' .. scale .. ':-1 -quality good -cpu-used 0 -pass 1 ' .. audio .. ' ' .. subs .. ' -f webm /dev/null -y'
full_command = full_command .. ' && ffmpeg -ss ' .. start .. ' -i \'' .. file_in .. '\' -t ' .. length .. ' -b:v ' .. bitrate .. bitsize .. ' -vf scale=' .. scale .. ':-1 -quality good -cpu-used 0 -pass 2 ' .. audio .. ' ' .. subs .. ' -f webm \'' .. file_out .. '\' -y'
full_command = full_command .. ') && notify-send "Encode done!"'
msg.info(full_command)
os.execute(full_command)
end
function encode_gif ()
generate_filenames("gif")
local full_command = '(ffmpeg -ss ' .. start .. ' -i \'' .. file_in .. '\' -t ' .. length .. ' -vf scale=' .. scale .. ':-1 -r ' .. fps .. ' \'' .. file_out .. '\' -y'
full_command = full_command .. ') && notify-send "Encode done!"'
msg.info(full_command)
os.execute(full_command)
end
function encode_x264 ()
generate_filenames("mkv")
local full_command = '(ffmpeg -ss ' .. start .. ' -i \'' .. file_in .. '\' -t ' .. length .. ' -c:v libx264 -vf scale=' .. sheight .. ':' .. swidth .. ' -preset slow -qp ' .. quality .. ' -c:a copy -c:s copy -map 0 \'' .. file_out .. '\' -y'
full_command = full_command .. ') && notify-send "Encode done! (' .. quality .. ')"'
msg.info(full_command)
os.execute(full_command)
end
---------
-- GUI --
---------
function call_gui_webm ()
mp.resume_all()
local handle = io.popen('yad --title="Convert Script" --center --form --separator="\n" --field="Resize to:NUM" "720" --field="Don’t resize at all:CHK" "false" --field="Include audio:CHK" "false" --field="Include subs:CHK" "false" --field="Bitrate/Filesize (M):NUM" "3" --field="Filesize > Bitrate:CHK" "true" --button="gtk-cancel:2" --button="gtk-ok:0" && echo "$?"')
local yad = handle:read("*a")
handle:close()
if yad == "" then
return
end
local yad_table = {}
local i = 0
for k in string.gmatch(yad, "[%a%d]+") do
yad_table[i] = k
i = i + 1
end
if (yad_table[2] == "FALSE") then
scale = yad_table[0]
else
scale = "-1"
end
if yad_table[3] == "FALSE" then
audio = '-an'
else
audio = ""
end
if yad_table[4] == "FALSE" then
subs = '-sn'
else
subs = ""
end
if yad_table[7] == "TRUE" then
bitrate = math.floor(yad_table[5]*1024*8/length*options.bitrate_multiplier)
bitsize = "K"
else
bitrate = yad_table[5]
bitsize = "M"
end
if yad_table[8] == "0" then
encode_webm()
end
end
function call_gui_gif ()
mp.resume_all()
local handle = io.popen('yad --title="Convert Script" --center --form --separator="\n" --field="Resize to:NUM" "320" --field="Don’t resize at all:CHK" "false" --field="FPS:NUM" "24" --button="gtk-cancel:2" --button="gtk-ok:0" && echo "$?"')
local yad = handle:read("*a")
handle:close()
if yad == "" then
return
end
local yad_table = {}
local i = 0
for k in string.gmatch(yad, "[%a%d]+") do
yad_table[i] = k
i = i + 1
end
if (yad_table[2] == "FALSE") then
scale = yad_table[0]
else
scale = "-1"
end
fps = yad_table[3]
if yad_table[5] == "0" then
encode_gif()
end
end
function call_gui_x264 ()
mp.resume_all()
local handle = io.popen('yad --title="Convert Script" --center --form --separator="\n" --field="Resize height:NUM" "720" --field="Resize width:NUM" "480" --field="Don’t resize at all:CHK" "true" --field="Quality (0-51):NUM" "18" --button="gtk-cancel:2" --button="gtk-ok:0" && echo "$?"')
local yad = handle:read("*a")
handle:close()
if yad == "" then
return
end
local yad_table = {}
local i = 0
for k in string.gmatch(yad, "[%a%d]+") do
yad_table[i] = k
i = i + 1
end
if (yad_table[4] == "FALSE") then
sheight = yad_table[0]
else
sheight = "-1"
end
if (yad_table[4] == "FALSE") then
swidth = yad_table[2]
else
swidth = "-1"
end
if ((sheight ~= "-1" and swidth ~= "-1") and (tonumber(sheight)%2 ~= 0 or tonumber(swidth)%2 ~= 0)) then
os.execute('notify-send "' .. sheight ..':' .. swidth .. '"')
return
end
quality = yad_table[5];
if yad_table[7] == "0" then
encode_x264()
end
end
mp.add_key_binding("alt+w", "convert_script_webm", convert_script_hotkey_call_webm)
mp.add_key_binding("alt+g", "convert_script_gif", convert_script_hotkey_call_gif)
mp.add_key_binding("alt+x", "convert_script_x264", convert_script_hotkey_call_x264)
|