Skip to content

Commit

Permalink
Added utf8 support and force align
Browse files Browse the repository at this point in the history
  • Loading branch information
tmoroney committed Dec 8, 2024
1 parent 3802453 commit 3bdd3ce
Show file tree
Hide file tree
Showing 6 changed files with 3,014 additions and 19 deletions.
34 changes: 28 additions & 6 deletions AutoSubs-App/src-tauri/resources/AutoSubs V2.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local ffi = require("ffi")
local socket = require("ljsocket")
local json = require("dkjson")
local utf8 = require("utf8")

-- Detect the operating system
local os_name = ffi.os
Expand Down Expand Up @@ -87,7 +88,7 @@ end
-- UTILS

function lstrip(str)
return string.gsub(str, "^%s+", "")
return utf8.gsub(str, "^%s+", "")
end

-- Convert hex color to RGB (Davinci Resolve uses 0-1 range)
Expand Down Expand Up @@ -304,8 +305,25 @@ function GetTemplateItem(folder, templateName)
end
end

-- remove sensitive words from the text and replace some letters with asterisks
local function RemoveSensitiveWords(input_string, censor_list)
-- Iterate through the list of words to censor
for _, word in ipairs(censor_list) do
-- Create a pattern to match the word (case-insensitive)
local pattern = word:gsub("(%W)", "%%%1") -- Escape special characters
pattern = utf8.lower(pattern) -- Ensure the pattern matches in lower case
pattern = "%f[%a]" .. pattern .. "%f[%A]" -- Match whole words only

-- Replace the word with asterisks
input_string = utf8.gsub(input_string, pattern, function(match)
return string.rep("*", utf8.len(match))
end)
end
return input_string
end

-- Add subtitles to the timeline using the specified template
function AddSubtitles(filePath, trackIndex, templateName, textFormat, removePunctuation)
function AddSubtitles(filePath, trackIndex, templateName, textFormat, removePunctuation, sensitiveWords)
local timeline = project:GetCurrentTimeline()

if trackIndex == "0" or trackIndex == "" then
Expand Down Expand Up @@ -392,16 +410,20 @@ function AddSubtitles(filePath, trackIndex, templateName, textFormat, removePunc

-- Remove punctuation if specified
if removePunctuation then
subtitleText = subtitleText:gsub("[%p%c]", "")
subtitleText = utf8.gsub(subtitleText, "[%p%c]", "")
end

-- Apply text formatting
if textFormat == "uppercase" then
subtitleText = string.upper(subtitleText)
subtitleText = utf8.upper(subtitleText)
end

if textFormat == "lowercase" then
subtitleText = string.lower(subtitleText)
subtitleText = utf8.lower(subtitleText)
end

if #sensitiveWords > 0 then
subtitleText = RemoveSensitiveWords(subtitleText, sensitiveWords)
end

-- Skip if text is not compatible
Expand Down Expand Up @@ -544,7 +566,7 @@ while not quitServer do
elseif data.func == "AddSubtitles" then
print("[AutoSubs Server] Adding subtitles to timeline...")
AddSubtitles(data.filePath, data.trackIndex, data.templateName, data.textFormat,
data.removePunctuation)
data.removePunctuation, data.sensitiveWords)
body = json.encode({
message = "Job completed"
})
Expand Down
Loading

0 comments on commit 3bdd3ce

Please sign in to comment.