Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update submerger.lua #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions submerger.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#! /usr/bin/env lua
--#! /usr/bin/env lua

-- Original script from https://github.com/daxliar/submerger
--9.08.2019 - set_basetime support added by Oli

-- Default colors to use when writing the merged values
-- they have to be valid html color codes as used inside the <font> tag
Expand Down Expand Up @@ -84,8 +87,10 @@ function import_srt( filename )
table.insert(srts, generate_entry(current_start_time, current_end_time, current_text ) )
else
if current_text == "" then
aegisub.debug.out("\n=="..tostring(current_index)..":"..tostring(trimmed_text))
current_text = trimmed_text
else
aegisub.debug.out("\n=="..tostring(current_index).."+"..tostring(trimmed_text))
current_text = current_text .. " " .. trimmed_text
end
end
Expand Down Expand Up @@ -212,15 +217,36 @@ function merge_srts( srt_a, srt_b )
return srts
end

function setContains(set, key)
return set[key] ~= nil
end

function hasprop(line,prop)
return (setContains(line,prop))
end

-- This function should be called before seconds_to_str_timestamp/ str_timestamp_to_seconds
function set_basetime(srt)
local basetime=1 -- Normaly basetime for srt is the second
if(#srt) then
line=srt[1]
if(hasprop(line,"layer")) then --but for the line object of a selected line in aegisub
basetime=1000 --times are in ms so we have to adjust basetime
end
end
return basetime
end

-- Writes srt data to a file
function write_srt( filename, srt )
if srt ~= nil then
local file = io.open(filename, "w")
if file ~= nil then
local basetime=set_basetime(srt)
for k,v in pairs(srt) do
file:write(string.format( "%s\n", tostring(k)))
local start_time = seconds_to_str_timestamp(v["start_time"])
local end_time = seconds_to_str_timestamp(v["end_time"])
local start_time = seconds_to_str_timestamp(v["start_time"]/basetime)
local end_time = seconds_to_str_timestamp(v["end_time"]/basetime)
file:write(string.format( "%s --> %s\n", start_time, end_time))
file:write(string.format( "%s\n\n", v["text"] ))
end
Expand All @@ -235,17 +261,17 @@ function write_srt( filename, srt )
end

-- Check if running as library or as a program
if pcall(debug.getlocal, 4, 1) then
print("You are using " .. arg[0] .. " as a library")
else
local num_args = #arg
if num_args >= 3 or num_args <= 5 then

default_color_a = arg[4] or default_color_a
default_color_b = arg[5] or default_color_b

write_srt( arg[3], merge_srts( import_srt(arg[1]), import_srt(arg[2]) ) )
else
print( "Usage: " .. arg[0] .. " <input srt file 1> <input srt file 2> <output srt file> [html color code 1] [html color code 2]")
end
end
--if pcall(debug.getlocal, 4, 1) then
-- print("You are using " .. arg[0] .. " as a library")
--else
-- local num_args = #arg
-- if num_args >= 3 or num_args <= 5 then
--
-- default_color_a = arg[4] or default_color_a
-- default_color_b = arg[5] or default_color_b
--
-- write_srt( arg[3], merge_srts( import_srt(arg[1]), import_srt(arg[2]) ) )
-- else
-- print( "Usage: " .. arg[0] .. " <input srt file 1> <input srt file 2> <output srt file> [html color code 1] [html color code 2]")
-- end
--end