Problems with coding censor extension for VLC Media Player using Lua

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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>
<code>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>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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
</code>
<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 </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!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật