aboutsummaryrefslogtreecommitdiffstats
path: root/chapters.lua
blob: fa817144f839dd851eb7c675215af885a4897880 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
script_name = "Chapter Generator"
script_description = "makes chapters"
script_author = "garret"
script_version = "2021-04-25"

language = "eng"
language_ietf = "en"

function ms_to_human(start) -- From Significance
    timecode=math.floor(start/1000)
    tc1=math.floor(timecode/60)
    tc2=timecode%60
    tc3=start%1000
    tc4="00"
    if tc2==60 then tc2=0 tc1=tc1+1 end
    if tc1>119 then tc1=tc1-120 tc4="02" end
    if tc1>59 then tc1=tc1-60 tc4="01" end
    if tc1<10 then tc1="0"..tc1 end
    if tc2<10 then tc2="0"..tc2 end
    if tc3<100 then tc3="0"..tc3 end
    linetime=tc4..":"..tc1..":"..tc2.."."..tc3
    if linetime=="00:00:00.00" then linetime="00:00:00.033" end
    return linetime
end

function get_sane_path()
    script_path = aegisub.decode_path("?script")
    audio_path = aegisub.decode_path("?audio")
    video_path = aegisub.decode_path("?video")
    if script_path ~= "?script" then
        return script_path
    elseif video_path ~= "?video" then
        return video_path
    elseif audio_path ~= "?audio" then
        return audio_path
    else
        return nil
    end
end

function main(sub)
    local times = {}
    local names = {}
    for i=1,#sub do
		local line = sub[i]
        if line.class == "dialogue" then
            if line.effect == "chapter" then
                line.comment = true
                table.insert(times, line.start_time)
                table.insert(names, line.text)
                sub[i] = line
            end
        end
	end
	aegisub.set_undo_point(script_name)
    local chapters = "<?xml version=\"1.0\"?>\n<!-- <!DOCTYPE Chapters SYSTEM \"matroskachapters.dtd\"> -->\n<!-- generated by garret's chapter script -->\n<Chapters>\n  <EditionEntry>\n"
    for j, k in ipairs(times) do
        local humantime = ms_to_human(k)
        local name = names[j]
        chapters = chapters.."    <ChapterAtom>\n      <ChapterTimeStart>"..humantime.."</ChapterTimeStart>\n      <ChapterDisplay>\n        <ChapterString>"..name.."</ChapterString>\n        <ChapLanguageIETF>"..language_ietf.."</ChapLanguageIETF>\n        <ChapterLanguage>"..language.."</ChapterLanguage>\n      </ChapterDisplay>\n    </ChapterAtom>\n"
    end
    chapters = chapters.."  </EditionEntry>\n</Chapters>"
    path = get_sane_path()
    if path ~= nil then
        local chapfile = io.open(path.."/chapters.xml", "w")
	    chapfile:write(chapters)
        chapfile:close()
        aegisub.log("saved to "..path.."/chapters.xml")
    else
        aegisub.log("<!-- couldn't find anywhere to save chapters, logging here - select everything below this message and copy+paste into a .xml file -->\n\n")
        aegisub.log(chapters)
    end
end

aegisub.register_macro(script_name, script_description, main)