-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-sequence.lua
64 lines (49 loc) · 1.6 KB
/
file-sequence.lua
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
function getFileNameWithSequence(seq)
return seq .. ".png"
end
function isFileSeq(path, seq)
return app.fs.isFile(app.fs.joinPath(path, getFileNameWithSequence(seq)))
end
function getPivotPoint(left, right)
return left + math.floor((right - left) / 2)
end
--[[ Finds the next sequence available ]]
function getNextSequence(path)
local step = 100
local maxSeq = 1000000
local seq = 0
-- coarse grain phase
for i = 0, maxSeq, step do
if not isFileSeq(path, i) then
seq = i
break
end
end
-- fine grain, binary search
local left = math.max(0, seq - step) -- left is a file that does exist
local right = seq -- right is a file that does not exist
while right - left > 1 do
local pivot = getPivotPoint(left, right)
if isFileSeq(path, pivot) then
left = pivot
else
right = pivot
end
end
return right
end
--[[ Obtains the next file name available in the snapshot sequence ]]
function getTimeLapseFileName(fullFileName, seq)
if fullFileName and app.fs.isFile(fullFileName) then
local path = app.fs.filePath(fullFileName)
local title = app.fs.fileTitle(fullFileName)
local timestamp = os.date("%Y%m%d%H%M%S")
local timelapsePath = app.fs.joinPath(path, title .. "-timelapse")
app.fs.makeAllDirectories(timelapsePath) -- this is like `mkdir -p`
if not seq then
seq = getNextSequence(timelapsePath)
end
return app.fs.joinPath(timelapsePath, getFileNameWithSequence(seq))
end
return nil
end