Создание субтитров отдельно для каждого персонажа в Aegisub

Lua-скрипт для создания отдельных субтитров для каждого персонажа в кодировке windows-1251:

script_name = "Export by actor"
script_description = "Create separate subtitles for each actor."
script_author = "Lyosha Kartoshin"
script_version = "0.72"

include("unicode.lua")

function actor_macro(subtitles)
	subtable = {}

	for i = 1, #subtitles do
		local s = subtitles[i]

		if s.class == "dialogue" then

			-- changes in text. Replace '\N' with new line
			s.text = string.gsub(s.text, "\\N", "\n")
			local text = ""
			for schar in unicode.chars(s.text) do
				text = text .. string.char(reversecp(schar) or "?")
			end

			-- changes in actor name. Replace \:*?"<>| with '-'
			replace = { "\\", ":", "*", "?", "\"", "<", ">", "|" }
			for r = 1, #replace do
				s.actor = string.gsub(s.actor, replace[r], "-")
			end

			sep = "/"
			actors = {}
			actors = explode(sep, s.actor)

			for a = 1, #actors do
				if subtable[actors[a]] == nil then
					subtable[actors[a]] = {}
				end
				table.insert(subtable[actors[a]], string.format("%s --> %s\n%s", ms_to_time(s.start_time), ms_to_time(s.end_time), text))
			end
		end
	end

	for actor,timeandtext in pairs(subtable) do
		local file = io.open(string.format("output - %s.srt", actor), "w")

		for index,value in ipairs(timeandtext) do
			file:write(string.format("%s\n%s\n\n", index, value))
		end
		io.close(file)
	end
end

function ms_to_time(ms_time)
    local h, m, s, ms

	ms = ms_time % 1000
	ms_time = ms_time - ms
	ms_time = ms_time / 1000

	h = (ms_time - (ms_time % 3600)) / 3600
	ms_time = ms_time % 3600

	s = ms_time % 60
	ms_time = ms_time - s
	m = ms_time / 60

	return string.format("%02d:%02d:%02d,%03d", h, m, s, ms)
end

-- Get ASCII-code by given codepoint
function reversecp(s)
	-- Basic case, ASCII
	local b = s:byte(1)
	if s:byte(1) < 128 then
		return s:byte(1)
	end

	local c = 63 -- '?'
	local cptable = { [1026] = 128, [1027] = 129, [1107] = 131, [8222] = 132, [8230] = 133, [8224] = 134, [8225] = 135, [8364] = 136, [8240] = 137, [1033] = 138, [8249] = 139, [1034] = 140, [1036] = 141, [1035] = 142, [1039] = 143, [1106] = 144, [8216] = 145, [8217] = 146, [8220] = 147, [8221] = 148, [8226] = 149, [8211] = 150, [8212] = 151, [152] = 152, [8482] = 153, [1113] = 154, [8250] = 155, [1114] = 156, [1116] = 157, [1115] = 158, [1119] = 159, [160] = 160, [1038] = 161, [1118] = 162, [1032] = 163, [164] = 164, [1168] = 165, [166] = 166, [167] = 167, [1025] = 168, [169] = 169, [1028] = 170, [171] = 171, [172] = 172, [173] = 173, [174] = 174, [1031] = 175, [176] = 176, [177] = 177, [1030] = 178, [1110] = 179, [1169] = 180, [181] = 181, [182] = 182, [183] = 183, [1105] = 184, [8470] = 185, [1108] = 186, [187] = 187, [1112] = 188, [1029] = 189, [1109] = 190, [1111] = 191 }
	local cp = unicode.codepoint(s)

    if b < 224 then
	--c = codepoint(s or "А") - 848
		if cp >= 1040 and cp <= 1103 then
			c = cp - 848
		else
			if cptable[cp] ~= nil then
				c = cptable[cp]
			end
		end
	elseif b < 240 then
		if cptable[cp] ~= nil then
			c = cptable[cp]
		end
	end

	return c
end

-- explode(seperator, string) (from http://lua-users.org/wiki/SplitJoin)
function explode(d,p)
  local t, ll
  t={}
  ll=0
  if(#p == 1) then return p end
    while true do
      l=string.find(p,d,ll+1,true) -- find the next d in the string
      if l~=nil then -- if "not not" found then..
        table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array.
        ll=l+1 -- save just after where we found it for searching next time.
      else
        table.insert(t, string.sub(p,ll)) -- Save what's left in our array.
        break -- Break at end, as it should be, according to the lua manual.
      end
    end
  return t
end

aegisub.register_macro(script_name, script_description, actor_macro)

Сохранить, к примеру, как actor-subs.lua и сложить в C:\Program Files\Aegisub\automation\autoload. В Ubuntu скрипты лежат примерно здесь: /usr/local/share/aegisub/2.1/automation/autoload.

Если сначала запускается aegisub, и там создаются или открываются субтитры, то субтитры, разбитые по персонажам будут создаваться в C:\Program Files\Aegisub. Если же стоит ассоциация по умолчанию открывать субтитры в aegisub, то субтитры будут создавать в директории, из которой были запущены субтитры. Впрочем, можно прописать свой путь перед output – %s.srt.

Если несколько персонажей произносит одну фразу одновременно, то можно указать через ‘/’: Actor1/Actor2, и эта фраза появится в субтитрах обоих персонажей. Символы \, :, *, ?, «, <, >, | в имени персонажа будут заменены на ‘‘. Символ \N в диалоге будет заменён переходом на новую строку.

Символы не присутствующие в кодировке windows-1251 будут заменены вопросами.

Оставьте комментарий