-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
executable file
·136 lines (96 loc) · 4.02 KB
/
build.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
import os, sys, getopt, glob, configparser
class Build:
odir = ''
index = ''
name = ''
config = None
def parse_argv(self, argv):
try:
opts, args = getopt.getopt(argv, 'hd:i:n:', ['directory=', 'index=',
'name='])
except:
print('./build.py -d <output-directory> -i <index> -n <name>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('./build.py -d <output-directory> -i <index> -n <name>')
sys.exit()
elif opt in ('-d', '--directory'):
self.odir = arg
elif opt in ('-i', '--index'):
self.index = arg
elif opt in ('-n', '--name'):
self.name = arg
def read_config(self):
self.config = configparser.ConfigParser()
self.config.read(self.index + '/frames.cfg')
def read_duration(self, frame):
return self.config['frames']['frame_' + str(frame)]
def read_delay(self):
return int(self.config['frames']['delay']) * 1000
def gen_pdf(self):
cmd_dir = 'mkdir -p {}/{}'.format(self.odir, self.index)
cmd_base = ('pdflatex -halt-on-error -output-directory={}/{} {}/'
).format(self.odir, self.index, self.index)
cmd_cover = cmd_base + 'cover.tex'
cmd_text = cmd_base + self.name
os.system(cmd_dir)
os.system(cmd_cover)
os.system(cmd_text)
os.system(cmd_cover)
os.system(cmd_text)
def gen_frames(self):
cmd = 'pdftoppm -png {}/{}/{}.pdf {}/{}/frame'.format(self.odir,
self.index, self.name, self.odir, self.index)
os.system(cmd)
def gen_video(self):
path = '{}/{}/playlist.txt'.format(self.odir, self.index)
with open(path, 'w') as playlist:
count = len(glob.glob1(self.odir + '/' + self.index, '*.png'))
for i in range(count):
j = i + 1
if count < 9:
record = 'file frame-' + str(j) + '.mp4\n'
else:
record = 'file frame-' + str(j).zfill(2) + '.mp4\n'
playlist.write(record)
if count < 9:
cmd = 'ffmpeg -loop 1 -i {}/{}/frame-{}.png' \
' -t {} {}/{}/frame-{}.mp4'.format(self.odir,
self.index, j, self.read_duration(j), self.odir,
self.index, j)
else:
cmd = 'ffmpeg -loop 1 -i {}/{}/frame-{}.png' \
' -t {} {}/{}/frame-{}.mp4'.format(self.odir,
self.index, str(j).zfill(2), self.read_duration(j),
self.odir, self.index, str(j).zfill(2))
os.system(cmd)
playlist.write('file logo.mp4\n')
cmd = 'ffmpeg -loop 1 -i img/logo.png -t 3 {}/{}/logo.mp4'.format(
self.odir, self.index)
os.system(cmd)
cmd = 'ffmpeg -f concat -i {}/{}/playlist.txt' \
' -c copy {}/{}/tmp.mp4'.format(self.odir, self.index, self.odir,
self.index)
os.system(cmd)
def mix_audio(self):
cmd = 'ffmpeg -i {}/media/{}.mp3 -af "adelay={}|{}"' \
' {}/{}/tmp.mp3'.format(self.index, self.name, self.read_delay(),
self.read_delay(), self.odir, self.index)
os.system(cmd)
cmd = ('ffmpeg -i {}/{}/tmp.mp3 -af "apad=pad_dur=3" {}/{}/tmp2.mp3'
).format(self.odir, self.index, self.odir, self.index)
os.system(cmd)
cmd = 'ffmpeg -i {}/{}/tmp.mp4 -i {}/{}/tmp2.mp3 -map 0 -map 1:a' \
' -c:v copy -shortest {}/{}/{}.mp4'.format(self.odir, self.index,
self.odir, self.index, self.odir, self.index, self.name)
os.system(cmd)
if __name__ == '__main__':
b = Build()
b.parse_argv(sys.argv[1:])
b.read_config()
b.gen_pdf()
b.gen_frames()
b.gen_video()
b.mix_audio()