Untitled conversation
- Usercode
- Assistant
-- 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,
restore_speed = 1.0,
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 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(config.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
config.restore_speed = current_speed
update_playback_speed(config.silence_speed)
end
else
update_playback_speed(config.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),
config.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)
This Lua script integrates auto-editor with mpv, allowing for automated video/audio editing directly within the mpv media player. Here's a detailed breakdown of the script, its functionalities, and some recommendations for improvements:
local utils = require "mp.utils"
local msg = require "mp.msg"
local options = require "mp.options"
local AUTO_EDITOR_BIN = "auto-editor"
mp.utils: Provides utility functions for interacting with the system and files.mp.msg: Used for logging messages in mpv's console.mp.options: Handles script configuration options.AUTO_EDITOR_BIN: Specifies the auto-editor executable. Make sure it's in your system's PATH or provide the full path.local config = {
enabled = false,
restore_speed = 1.0,
silence_speed = 2.5,
threshold = "4%",
margin = "0.1s,0.2s",
mincut = "1s",
}
options.read_options(config)
enabled: Toggles the auto-editor integration. Default is false.restore_speed: Normal playback speed after playback speed adjustments.silence_speed: Speed to set during silent segments.threshold: Audio silence threshold.margin: Margin around cuts to provide smooth transitions.mincut: Minimum duration for cuts.local time_observer = nil
local cmd_in_progress = false
time_observer: Observes playback time to adjust speed based on analysis.cmd_in_progress: Indicates if the auto-editor analysis is currently running to prevent multiple concurrent analyses.Update Playback Speed
local function update_playback_speed(speed)
mp.set_property("speed", speed)
msg.info("Speed set to: " .. speed)
end
Adjusts the current playback speed and logs the change.
Check if File is Local
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
Determines if the current media is a local file to avoid processing network streams.
Load and Process Segments
local function load_segments(json)
-- [Function details...]
end
silence_speed during silences and restores it after.Execute Auto-Editor
local function execute_auto_editor()
-- [Function details...]
end
Auto-Start Analysis on File Load
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
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)
E: Triggers the auto-editor analysis manually.Ctrl+E: Displays the current auto-editor settings.file-loaded: Automatically starts analysis when a new file is loaded, if enabled.Enable by Default (Optional)
enabled = true by default.Handle Different Operating Systems
auto-editor executable might be auto-editor.exe. Modify the AUTO_EDITOR_BIN accordingly or detect the OS to set it dynamically.
local is_windows = package.config:sub(1,1) == "\\"
local AUTO_EDITOR_BIN = is_windows and "auto-editor.exe" or "auto-editor"
Provide Full Path to auto-editor (Optional)
auto-editor isn't in the system's PATH, specify the absolute path in the configuration or detect its location.Cache Analysis Results
Error Handling for Missing Dependencies
auto-editor is available. Provide a user-friendly message if it's missing.
local function is_auto_editor_available()
local res = utils.subprocess({args = {"which", AUTO_EDITOR_BIN}})
return res.status == 0
end
execute_auto_editor to verify availability.Support for Multiple Audio Streams
Configuration Options Expansion
auto-editor parameters through the config table for greater flexibility.Optimize Time Observer
Audio Stream Indexing
local audio_stream_index = (mp.get_property_number("aid") or 1) - 1
1, so subtracting 1 might lead to incorrect indexing, especially if aid is 1.auto-editor. Adjust accordingly without assuming zero-based indexing unless confirmed.JSON Parsing and Structure
local segments = parsed_content["chunks"]
auto-editor matches this expectation. If auto-editor updates its output format, this could break.Handling Special Characters in File Paths
Performance on Large Files
Thread Safety and Concurrency
cmd_in_progress, concurrency issues might arise if multiple events are triggered rapidly.Install auto-editor
pip install auto-editor
Save the Script
~/.config/mpv/scripts/%APPDATA%\mpv\scripts\Configure as Needed
config table within the script to suit your preferences. For example, enable auto-run by setting enabled = true.Load a Local File
Manual Controls
E: Manually trigger the auto-editor analysis.Ctrl+E: Display current auto-editor settings.This script provides a seamless integration between mpv and auto-editor, enhancing the playback experience by automatically speeding through silent segments. By addressing the potential issues and implementing the recommended improvements, you can ensure a robust and efficient setup tailored to your workflow.
If you encounter any specific issues or have further questions about customizing this script, feel free to ask!
Not yet included in a published release.
Automatically initiates auto-editor analysis when a new file is loaded, provided the integration is enabled and the file is local.
Display Current Settings
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),
config.restore_speed,
config.silence_speed,
config.threshold,
config.margin
)
mp.osd_message(settings_str, 5)
msg.info(settings_str)
end
Displays the current auto-editor settings both on-screen and in the console.
Feedback During Analysis
Logging Enhancements