-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.lua
290 lines (253 loc) · 7.5 KB
/
main.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
282
283
284
285
286
287
288
289
290
-- NOTE TO FUTURE SELF:
-- To view debug logs when launching through Finder,
-- use the ` key in mpv instead of viewing ~/Library/Logs/mpv.log.
mp.msg.info("MPV-CUT LOADED")
utils = require "mp.utils"
local function print(s)
mp.msg.info(s)
mp.osd_message(s)
end
local function table_to_str(o)
if type(o) == 'table' then
local s = ''
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. table_to_str(v) .. '\n'
end
return s
else
return tostring(o)
end
end
local result = mp.command_native({ name = "subprocess", args = {"ffmpeg"}, playback_only = false, capture_stdout = true, capture_stderr = true })
mp.msg.info("Your PATH: " .. os.getenv('PATH'))
if result.status ~= 1 then
mp.osd_message("FFmpeg failed to run, please press ` for debug info", 5)
mp.msg.error("FFmpeg failed to run:\n" .. table_to_str(result))
mp.msg.error("`which ffmpeg` output:\n" .. table_to_str(mp.command_native({ name = "subprocess", args = {"which", "ffmpeg"}, playback_only = false, capture_stdout = true, capture_stderr = true })))
end
local function to_hms(seconds)
local ms = math.floor((seconds - math.floor(seconds)) * 1000)
local secs = math.floor(seconds)
local mins = math.floor(secs / 60)
secs = secs % 60
local hours = math.floor(mins / 60)
mins = mins % 60
return string.format("%02d-%02d-%02d-%03d", hours, mins, secs, ms)
end
local function next_table_key(t, current)
local keys = {}
for k in pairs(t) do
keys[#keys + 1] = k
end
table.sort(keys)
for i = 1, #keys do
if keys[i] == current then
return keys[(i % #keys) + 1]
end
end
return keys[1]
end
ACTIONS = {}
ACTIONS.COPY = function(d)
local args = {
"ffmpeg",
"-nostdin", "-y",
"-loglevel", "error",
"-ss", d.start_time,
"-t", d.duration,
"-i", d.inpath,
"-c", "copy",
"-map", "0",
"-dn",
"-avoid_negative_ts", "make_zero",
utils.join_path(d.indir, "COPY_" .. d.channel .. "_" .. d.infile_noext .. "_FROM_" .. d.start_time_hms .. "_TO_" .. d.end_time_hms .. d.ext)
}
mp.command_native_async({
name = "subprocess",
args = args,
playback_only = false,
}, function() print("Done") end)
end
ACTIONS.ENCODE = function(d)
local args = {
"ffmpeg",
"-nostdin", "-y",
"-loglevel", "error",
"-ss", d.start_time,
"-t", d.duration,
"-i", d.inpath,
"-pix_fmt", "yuv420p",
"-crf", "16",
"-preset", "superfast",
utils.join_path(d.indir, "ENCODE_" .. d.channel .. "_" .. d.infile_noext .. "_FROM_" .. d.start_time_hms .. "_TO_" .. d.end_time_hms .. d.ext)
}
mp.command_native_async({
name = "subprocess",
args = args,
playback_only = false,
}, function() print("Done") end)
end
ACTIONS.LIST = function(d)
local inpath = mp.get_property("path")
local outpath = inpath .. ".list"
local file = io.open(outpath, "a")
if not file then print("Error writing to cut list") return end
local filesize = file:seek("end")
local s = "\n" .. d.channel
.. ":" .. d.start_time
.. ":" .. d.end_time
file:write(s)
local delta = file:seek("end") - filesize
io.close(file)
print("Δ " .. delta)
end
ACTION = "COPY"
CHANNEL = 1
CHANNEL_NAMES = {}
KEY_CUT = "c"
KEY_CANCEL_CUT = "C"
KEY_CYCLE_ACTION = "a"
KEY_BOOKMARK_ADD = "i"
KEY_CHANNEL_INC = "="
KEY_CHANNEL_DEC = "-"
home_config = mp.command_native({"expand-path", "~/.config/mpv-cut/config.lua"})
if pcall(require, "config") then
mp.msg.info("Loaded config file from script dir")
elseif pcall(dofile, home_config) then
mp.msg.info("Loaded config file from " .. home_config)
else
mp.msg.info("No config loaded")
end
for i, v in ipairs(CHANNEL_NAMES) do
CHANNEL_NAMES[i] = string.gsub(v, ":", "-")
end
if not ACTIONS[ACTION] then ACTION = next_table_key(ACTIONS, nil) end
START_TIME = nil
local function get_current_channel_name()
return CHANNEL_NAMES[CHANNEL] or tostring(CHANNEL)
end
local function get_data()
local d = {}
d.inpath = mp.get_property("path")
d.indir = utils.split_path(d.inpath)
d.infile = mp.get_property("filename")
d.infile_noext = mp.get_property("filename/no-ext")
d.ext = mp.get_property("filename"):match("^.+(%..+)$") or ".mp4"
d.channel = get_current_channel_name()
return d
end
local function get_times(start_time, end_time)
local d = {}
d.start_time = tostring(start_time)
d.end_time = tostring(end_time)
d.duration = tostring(end_time - start_time)
d.start_time_hms = tostring(to_hms(start_time))
d.end_time_hms = tostring(to_hms(end_time))
d.duration_hms = tostring(to_hms(end_time - start_time))
return d
end
text_overlay = mp.create_osd_overlay("ass-events")
text_overlay.hidden = true
text_overlay:update()
local function text_overlay_off()
-- https://github.com/mpv-player/mpv/issues/10227
text_overlay:update()
text_overlay.hidden = true
text_overlay:update()
end
local function text_overlay_on()
local channel = get_current_channel_name()
text_overlay.data = string.format("%s in %s from %s", ACTION, channel, START_TIME)
text_overlay.hidden = false
text_overlay:update()
end
local function print_or_update_text_overlay(content)
if START_TIME then text_overlay_on() else print(content) end
end
local function cycle_action()
ACTION = next_table_key(ACTIONS, ACTION)
print_or_update_text_overlay("ACTION: " .. ACTION)
end
local function cut(start_time, end_time)
local d = get_data()
local t = get_times(start_time, end_time)
for k, v in pairs(t) do d[k] = v end
mp.msg.info(ACTION)
mp.msg.info(table_to_str(d))
ACTIONS[ACTION](d)
end
local function put_time()
local time = mp.get_property_number("time-pos")
if not START_TIME then
START_TIME = time
text_overlay_on()
return
end
text_overlay_off()
if time > START_TIME then
cut(START_TIME, time)
START_TIME = nil
else
print("INVALID")
START_TIME = nil
end
end
local function cancel_cut()
text_overlay_off()
START_TIME = nil
print("CANCELLED CUT")
end
local function get_bookmark_file_path()
local d = get_data()
mp.msg.info(table_to_str(d))
local outfile = string.format("%s_%s.book", d.channel, d.infile)
return utils.join_path(d.indir, outfile)
end
local function bookmarks_load()
local inpath = get_bookmark_file_path()
local file = io.open(inpath, "r")
if not file then return end
local arr = {}
for line in file:lines() do
if tonumber(line) then
table.insert(arr, {
time = tonumber(line),
title = "chapter_" .. line
})
end
end
file:close()
table.sort(arr, function(a, b) return a.time < b.time end)
mp.set_property_native("chapter-list", arr)
end
local function bookmark_add()
local d = get_data()
local outpath = get_bookmark_file_path()
local file = io.open(outpath, "a")
if not file then print("Failed to open bookmark file for writing") return end
local out_string = mp.get_property_number("time-pos") .. "\n"
local filesize = file:seek("end")
file:write(out_string)
local delta = file:seek("end") - filesize
io.close(file)
bookmarks_load()
print(string.format("Δ %s, %s", delta, d.channel))
end
local function channel_inc()
CHANNEL = CHANNEL + 1
bookmarks_load()
print_or_update_text_overlay(get_current_channel_name())
end
local function channel_dec()
if CHANNEL >= 2 then CHANNEL = CHANNEL - 1 end
bookmarks_load()
print_or_update_text_overlay(get_current_channel_name())
end
mp.add_key_binding(KEY_CUT, "cut", put_time)
mp.add_key_binding(KEY_CANCEL_CUT, "cancel_cut", cancel_cut)
mp.add_key_binding(KEY_BOOKMARK_ADD, "bookmark_add", bookmark_add)
mp.add_key_binding(KEY_CHANNEL_INC, "channel_inc", channel_inc)
mp.add_key_binding(KEY_CHANNEL_DEC, "channel_dec", channel_dec)
mp.add_key_binding(KEY_CYCLE_ACTION, "cycle_action", cycle_action)
mp.register_event('file-loaded', bookmarks_load)