Prevent overwriting of MidiRegion length and length_beats during session load.
[ardour.git] / scripts / export_mp4chaps.lua
1 ardour {
2         ["type"] = "EditorAction",
3         name = "Export markers as mp4chaps",
4         author = "Johannes Mueller",
5         description = [[
6 Exports MP4chaps of all markers except xruns. The markers are stored in the
7 export directory of the session in mp4 chapter marks format. The filename
8 is mp4chaps.txt
9
10 Note that there's always a chapter mark "Intro" at 00:00:00.000 as some
11 players can't live without it. If there are no exportable markers, the file
12 is not created.
13
14 This is a bit more convenient than the export option, as one does not
15 have to wait for the export.
16 ]],
17         license = "GPLv2"
18 }
19
20 function factory (unused_params) return function ()
21
22         fr = Session:frame_rate()
23         chaps = {}
24
25         for l in Session:locations():list():iter() do
26                 name = l:name()
27                 if not l:is_mark() or string.find(name, "^xrun%d*$") then
28                         goto next end
29
30                 t = l:start()
31                 h = math.floor(t / (3600*fr))
32                 r = t - (h*3600*fr)
33                 m = math.floor(r / (60*fr))
34                 r = r - m*60*fr
35                 s = math.floor(r / fr)
36                 r = r - s*fr
37                 ms = math.floor(r*1000/fr)
38                 table.insert(chaps, string.format("%02d:%02d:%02d.%03d %s\n", h, m, s, ms, name))
39                 ::next::
40         end
41
42         if next(chaps) == nil then
43                 goto out end
44
45         table.insert(chaps, "00:00:00.000 Intro\n")
46         table.sort(chaps)
47
48         file = io.open(ARDOUR.LuaAPI.build_filename (Session:path(), "export", "mp4chaps.txt"), "w")
49         for i, line in ipairs(chaps) do
50                 file:write(line)
51         end
52         file:close()
53
54         ::out::
55 end end