I am relatively new to Lua, and I am encountering a problem when I try to compile my code in VLC.
Please try the code that I have attached and get back to me. I have tried everything that I can think of but so far I cant get past this problem. I am sorry for the messy code!
This is the error that it gives:
lua info: Activated Addon!
lua info: Preparing Subtitles
lua info: Subtitle file opened successfully
lua info: Keyword found at 52.943 seconds.
lua info: Keyword found at 7337.018 seconds.
lua info: Successfully processed subtitles for exclusion times.
lua info: Printing exclusion times...
lua info: Exclusion Time 1: Start = 51.943, End = 53.943
lua info: Exclusion Time 2: Start = 7336.018, End = 7338.018
lua error: Could not activate extension!
Code:
-- Define descriptor function for the VLC addon
function descriptor()
return {
title = "Censor Extension",
version = "1.0",
author = "Deacon Griffiths",
capabilities = {}
}
end
-- Global table to store exclusion times
exclusion_times = {}
old_time = 0
-- For storing initial volume
prev_vol = vlc.volume.get()
-- Function to convert time from HH:MM:SS,ms to seconds
function timeToSeconds(time_str)
local hours, minutes, seconds, milliseconds = time_str:match("(%d+):(%d+):(%d+),(%d+)")
return tonumber(hours) * 3600 + tonumber(minutes) * 60 + tonumber(seconds) + tonumber(milliseconds) / 1000
end
-- Function to process subtitle file and populate exclusion_times
function prepareSubtitles()
vlc.msg.info("Preparing Subtitles")
local subtitle_file_path = "C:\Program Files (x86)\VideoLAN\VLC\lua\extensions\Subtitles.srt"
local subtitle_file = io.open(subtitle_file_path, "r")
if not subtitle_file then
vlc.msg.error("Failed to open subtitle file at " .. subtitle_file_path)
return
end
vlc.msg.info("Subtitle file opened successfully")
local current_time = 0
local in_block = false
for line in subtitle_file:lines() do
-- Detect the time block
local start_time_str, end_time_str = line:match("(%d+:%d+:%d+,%d+)%s*-->%s*(%d+:%d+:%d+,%d+)")
if start_time_str and end_time_str then
current_time = timeToSeconds(start_time_str)
in_block = true
elseif in_block and line ~= "" then
-- Check for the keyword in subtitle text
if string.find(line:lower(), "keyword") then
-- Ensure start_time does not go negative
local start_exclusion = math.max(current_time - 1, 0)
local end_exclusion = current_time + 1
table.insert(exclusion_times, {start_time = start_exclusion, end_time = end_exclusion})
vlc.msg.info("Keyword found at " .. current_time .. " seconds.")
end
elseif line == "" then
-- End of a subtitle block
in_block = false
end
end
subtitle_file:close()
vlc.msg.info("Successfully processed subtitles for exclusion times.")
end
-- Function to check if current time is within exclusion times
function isInExclusionTimes(current_time)
for _, range in ipairs(exclusion_times) do
if current_time >= range.start_time and current_time <= range.end_time then
return true
end
end
return false
end
-- Function to get the current playback time in seconds
function getCurrentTime()
local media_player = vlc.object.input()
local current_time = vlc.var.get(media_player, "time")
if current_time then
return current_time / 1000
else
return 0
end
end
-- Function to adjust volume based on current playback time
function adjustVolume()
local media_player = vlc.object.input()
local current_time = getCurrentTime()
if isInExclusionTimes(current_time) then
vlc.volume.set(0)
prev_vol = vlc.volume.get()
vlc.msg.info("Volume muted at time: " .. current_time)
else
vlc.volume.set(prev_vol)
vlc.msg.info("Volume set to 256 at time: " .. current_time)
end
end
function poll_time()
vlc.msg.info("Poll time")
local cur_time = getCurrentTime()
vlc.msg.info("Got current_time")
if time ~= old_time then
adjustVolume()
old_time = time
end
vlc.timer(100, poll_time)
end
-- Function called when the addon is activated
function activate()
vlc.msg.info("Activated Addon!")
prepareSubtitles()
-- Print exclusion_times for verification
vlc.msg.info("Printing exclusion times...")
for i, entry in ipairs(exclusion_times) do
vlc.msg.info("Exclusion Time " .. i .. ": Start = " .. entry.start_time .. ", End = " .. entry.end_time)
end
-- Subscribe to time change event
local media_player = vlc.object.input()
vlc.msg.info("Created local variable media_player")
if media_player then
vlc.msg.info("Inside If statement")
adjustVolume()
vlc.msg.info("Timer added for time change event.")
poll_time()
else
vlc.msg.info("Failed to get media player object.")
end
end
The code starts by accessing a subtitle file named Subtitle.srt
that contains the now playing movies subtitles. It then checks the subtitle file for bad language (Which is a hard coded one word for now) and stores the times of occurances in a global table variable named exclusion_times
, which stores the time one second before the occurance, and one second after the occurance.
That is what is working. Now this is where the problem comes in:
When it has processed the subtitles, it is supposed to check for a time change every 10 milliseconds, and when the time changes, it runs a function which is supposed to check if the current_time
is within any of the exclusion times. If so, it mutes audio. When it is outside of any of those times, it unmutes the audio to the original volume. It is not working.
Please help!