-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoeditor.lua
164 lines (142 loc) · 4.7 KB
/
autoeditor.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
-- MPV integration with auto-editor (https://github.com/WyattBlue/auto-editor)
local utils = require "mp.utils"
local msg = require "mp.msg"
local options = require "mp.options"
local AUTO_EDITOR_BIN = "auto-editor"
local config = {
enabled = false,
silence_speed = 2.5,
threshold = "4%",
margin = "0.1s,0.2s",
mincut = "1s",
}
options.read_options(config)
local time_observer = nil
local cmd_in_progress = false
local restore_speed = 1.0
local function update_playback_speed(speed)
mp.set_property("speed", speed)
msg.info("Speed set to: " .. speed)
end
local function is_local_file(file_path)
if not file_path then
return false
end
local file_info = utils.file_info(file_path)
return file_info ~= nil and file_info.is_file
end
local function load_segments(json)
if time_observer then
mp.unobserve_property(time_observer)
time_observer = nil
update_playback_speed(restore_speed)
end
local parsed_content = utils.parse_json(json)
if not parsed_content then
msg.error("Failed to parse JSON.")
return
end
local segments = parsed_content["chunks"]
if not segments or #segments < 1 then
msg.warn("No segments found.")
return
end
mp.osd_message("Loaded " .. #segments .. " segments")
msg.info("Loaded " .. #segments .. " segments")
local current_segment = nil
time_observer = function(_, time)
local frame = mp.get_property_number("estimated-frame-number") or 0
if current_segment and frame >= current_segment[1] and frame < current_segment[2] then
return
end
for _, segment in ipairs(segments) do
if frame >= segment[1] and frame < segment[2] then
current_segment = segment
if segment[3] == 99999 then
local current_speed = mp.get_property_number("speed")
if current_speed < config.silence_speed then
restore_speed = current_speed
update_playback_speed(config.silence_speed)
end
else
update_playback_speed(restore_speed)
end
return
end
end
current_segment = nil
end
mp.observe_property("time-pos", "number", time_observer)
end
local function execute_auto_editor()
if cmd_in_progress then
mp.osd_message("Analysis is already in progress")
msg.info("Analysis is already in progress")
return
end
local file = mp.get_property("path")
if not is_local_file(file) then
mp.osd_message("auto-editor: disabled for network streams")
msg.info("Disabled for network streams")
return
end
local audio_stream_index = (mp.get_property_number("aid") or 1) - 1
local auto_editor_args = {
file,
"--export", "timeline:api=1",
"--quiet",
"--no-cache",
"--progress", "none",
"--margin", config.margin,
"--edit", string.format("audio:stream=%d,threshold=%s,mincut=%s", audio_stream_index, config.threshold, config.mincut)
}
local cmd = {
name = "subprocess",
playback_only = false,
capture_stdout = true,
args = {AUTO_EDITOR_BIN, unpack(auto_editor_args)}
}
mp.osd_message("Running analysis")
msg.info("Running analysis")
cmd_in_progress = true
mp.command_native_async(cmd, function(success, result, error)
cmd_in_progress = false
if success then
load_segments(result.stdout)
else
local err_msg = error or "Unknown error"
msg.error("Error: " .. err_msg)
mp.osd_message("Error: " .. err_msg)
end
end)
end
local function auto_start_analysis()
if config.enabled then
if is_local_file(mp.get_property("path")) then
execute_auto_editor()
else
mp.osd_message("auto-editor: disabled for network streams")
msg.info("Disabled for network streams")
end
end
end
local function display_settings()
local settings_str = string.format(
"Auto-editor settings:\n" ..
"Auto-run: %s\n" ..
"Restore speed: %.2f\n" ..
"Silence speed: %.2f\n" ..
"Threshold: %s\n" ..
"Margin: %s",
tostring(config.enabled),
restore_speed,
config.silence_speed,
config.threshold,
config.margin
)
mp.osd_message(settings_str, 5)
msg.info(settings_str)
end
mp.add_key_binding("E", "run-auto-editor", execute_auto_editor)
mp.add_key_binding("Ctrl+E", "print-auto-editor-settings", display_settings)
mp.register_event("file-loaded", auto_start_analysis)