-
Notifications
You must be signed in to change notification settings - Fork 0
/
pypopro.py
executable file
·296 lines (235 loc) · 9.46 KB
/
pypopro.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python3
"""
Copyright (C) 2014 Boris Grozev <[email protected]>
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
import os
import json
import time
import pypopro
import Participants
def main():
if len(sys.argv) < 3:
print("Usage: {} <input_dir> <output_dir>".format(sys.argv[0]))
return
#read args
global input_dir, output_dir, config, debug
input_dir = sys.argv[1]
if input_dir[-1] != '/':
input_dir += '/'
if not os.path.isdir(input_dir):
print(
'Input directory {} does not exists (or is not a directory)'.format(
input_dir))
return
output_dir = sys.argv[2]
if output_dir[-1] != '/':
output_dir += '/'
os.makedirs(output_dir, mode=0o755, exist_ok=True)
print("input_dir={}\noutput_dir={}".format(input_dir, output_dir))
#load configuration
config = json.load(open('config.json'))
config['frameDuration'] = int(1000 / config['outputFramerate'])
print("Configuration: ", config)
print()
debug = parse_bool(config.get('debug'))
#read metadata
events = json.load(open(input_dir + config['metadataFilename']))
if config['processAudio']:
#get this before we change the events' instants
audio_start = events.get('audio')[0]['instant']
audio_file = do_audio(events.get('audio'))
add_timing('Audio complete')
if config['processVideo']:
#get this before we change the events' instants
video_start = events.get('video')[0]['instant']
video_file = do_video(events.get('video'))
add_timing('Video complete')
if config['processAudio'] and config['processVideo']:
#merge audio and video
audio_offset, video_offset = 0, 0
if (audio_start > video_start):
audio_offset = audio_start - video_start
else:
video_offset = video_start - audio_start
print('audio start ', audio_start, ' video start ', video_start)
print('audio offset ', audio_offset, ' video offset ', video_offset)
merge(audio_file, audio_offset,
video_file, video_offset,
output_dir + config['outputFilename'])
add_timing('Merge audio and video')
print()
for s in timing_strings:
print(s)
def do_audio(events):
normalize_instants(events)
os.system('mkdir -p {}audio_tmp'.format(output_dir))
mix_command = 'sox --combine mix-power'
for e in events:
#TODO: check if file exists and it's min length
in_file = input_dir + e['filename'];
if not os.path.isfile(in_file) or os.stat(in_file).st_size < 10000:
print('Skipping audio file', in_file,
'because it does not exist or is too short.')
continue
decode_audio(in_file,
output_dir + 'audio_tmp/' + e['filename'] + '.wav',
e['instant'])
mix_command += ' {}.wav'.format(
output_dir + 'audio_tmp/' + e['filename'])
filename = output_dir + 'out.wav'
mix_command += ' ' + filename
add_timing('Audio padded')
os.system(mix_command)
add_timing('Audio mixed')
os.system('rm -rf {}audio_tmp'.format(output_dir))
return filename
def do_video(events):
events = preprocess_video_events(events)
add_timing('Video events pre-processed (extracted durations)')
if debug:
print('Pre-processed video events:')
for e in events:
print(e)
last_instant = events[-1]['instant']
index = -1
participants = Participants.Participants(config, input_dir)
overlayer = pypopro.overlayer_init()
filename = output_dir + config['outputFilename'] + '.ivf'
encoder = pypopro.encoder_init(filename)
print()
for ms in range(0, last_instant, config['frameDuration']):
#read events in (0, ms] and update participants
added = False
while events[index + 1]['instant'] <= ms:
event = events[index + 1]
if debug:
print('\nEvent {} at {} handled at ms={}'.format(event['type'],
event['instant'],
ms))
participants.add_event(event)
index += 1
added = True
# if debug and added:
# print('ms={} participants={} active={}'.format(ms, participants,
# active))
if not participants.active:
print('\nno active speaker left, ending at ms=', ms)
participants.print_stats()
break
if 0 < config['maxVideoDurationMs'] < ms:
break
#read frames for all participants and overlay them
f, w, h, x, y = participants.get_frames(ms)
#print("overlay: " + str(overlayer) + ' ' + str(f) + "; " + str(
# w) + "; " + str(h) + '; ' + str(x) + '; ' + str(y))
#overlay currently disabled, just use the 'active' frame
#frame = pypopro.overlayer_overlay(overlayer,
# f, w, h, x, y)
frame = f[0]
#encode the frame and write it to disk
#print('encode: ' + str(encoder) + " " + str(frame) + " " + str(ms))
pypopro.encoder_add_frame(encoder, frame, ms)
participants.frames_encoded += 1
print('\rProcessing {} / {}ms'.format(str(ms).zfill(len(str(last_instant))),
last_instant),
end='')
participants.print_stats()
pypopro.encoder_close(encoder)
pypopro.overlayer_close(overlayer)
return filename
def merge(audio_file, audio_offset, video_file, video_offset, output_file):
print(
'Merging {}({}) and {}({})'.format(audio_file, audio_offset, video_file,
video_offset))
os.system(
'ffmpeg -y -itsoffset {} -i {} -itsoffset {} -i {} -vcodec copy {}'.format(
audio_offset, audio_file, video_offset, video_file, output_file))
def preprocess_video_events(events):
#remove SPEAKER_CHANGED events from the beginning
for i in range(len(events)):
if events[i]['type'] == 'SPEAKER_CHANGED':
del events[i]
else:
break
events = [i for i in events if i['type'] != 'RECORDING_ENDED']
#insert RECORDING_ENDED events
#TODO: cleaner syntax for this?
ended = []
for event in events:
if event['type'] == 'RECORDING_STARTED':
e = dict(filename=event['filename'],
mediaType='video',
type='RECORDING_ENDED',
ssrc=event['ssrc'],
instant=event['instant'] + get_duration(
input_dir + event['filename']))
ended.append(e)
events += ended
events.sort(key=lambda x: x['instant'])
return normalize_instants(events)
def normalize_instants(events):
"""
Decreases the 'instance' field of each element of 'events' by the
'instance' of the first event (making the first event's instance '0').
Assumes 'events' is ordered by 'instance'.
"""
assert events
first_instant = events[0]['instant']
for e in events:
e['instant'] -= first_instant
return events
def get_duration(filename):
"""
Gets the duration of the webm file 'filename' in milliseconds.
"""
proc = os.popen(
'mkvinfo -s -v ' + filename + " | tail -n 1 | awk '{print $6;}'")
return2 = int(proc.read())
print('duration {} {}'.format(filename, return2))
return return2
def decode_audio(in_name, out_name, padding):
"""
Converts 'in_name' to 'out_name' using 'sox', adding 'padding'
milliseconds of padding in the beginning.
"""
os.system("sox {} {} pad {} > /dev/null 2>&1".format(in_name,
out_name,
millis_to_seconds(
padding)))
def millis_to_seconds(millis):
"""
Converts 'millis' to seconds and return a string representation with
three digits after the decimal point.
"""
return "{0:.3f}".format(millis / 1000)
def add_timing(s):
global last_time, timing_strings
cur = int(round(time.time() * 1000))
timing_strings.append(
"[TIME] {}s (total {}s): {}".format(millis_to_seconds(cur - last_time),
millis_to_seconds(cur - first_time),
s))
last_time = cur
def parse_bool(v):
if v is None:
return False
return str(v).lower() in ("yes", "true", "t", "1")
last_time = int(round(time.time() * 1000))
first_time = last_time
timing_strings = []
input_dir = None
output_dir = None
config = None
debug = None
main()