-
Notifications
You must be signed in to change notification settings - Fork 0
/
archiver.py
64 lines (50 loc) · 2.12 KB
/
archiver.py
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
import os
from subprocess import Popen, PIPE
from numpy import average, std as stddev
class Archiver(object):
def __init__(self):
self.logname = "log"
def exists(self, path):
pass # TODO
def folder(self, path):
if path:
if path[-1] != '/': path += "/"
os.makedirs(os.path.dirname(path), exist_ok = True)
return path
else:
return None
def file(self, path, params = 'wb'):
self.folder('/'.join(path.split('/')[:-1]) + '/')
return open(path, params)
def execute(self, command):
p = Popen(command, shell = True, stdout= PIPE, stderr = PIPE)
out, err = p.communicate()
return {'out': out.decode(), 'err': err.decode()}
def log(self, string):
self.file(self.logname, 'a').write(string + '\n')
def copy(self, src, dst):
return self.execute("cp -r " + src + " " + dst)
def move(self, src, dst):
ret = self.copy(src, dst)
if not ret['err']:
return self.remove(src)
return ret
def remove(self, files):
return self.execute("rm -r " + files)
def zipToAnimation(self, sourcepath, destpath, filename, framedata):
destpath = self.folder(destpath)
workpath = "temp/" + filename
self.execute("unzip -o " + sourcepath + " -d " + workpath)
if stddev(framedata) < 1.0:
# assumed stable framerate
fr = 1 / round(average(framedata)) * 1000
self.execute("ffmpeg -framerate %d -i %s/%%6d.jpg -c:v copy %s.mkv" % (fr, workpath, destpath + filename))
else:
# probably needs variable framerate
print("\n stddev %.3f, %d frames:\n" % (stddev(framedata), len(framedata)), framedata)
cmd = "convert "
for i, d in enumerate(framedata):
cmd += "-delay %d %s/%06d.jpg " % (d / 10, workpath, i)
cmd += destpath + filename + ".gif"
self.execute(cmd) # we convert to gif and copy the zip too, because gif is trash
self.execute("cp " + sourcepath + " " + destpath + filename + ".zip")