-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_videos.py
239 lines (198 loc) · 8.62 KB
/
generate_videos.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
#!/usr/bin/env python
from __future__ import print_function
import argparse
import json
import os.path
import glob
import sys
import math
import statistics
import subprocess
from shutil import copyfile
from operator import itemgetter
global options
parser = argparse.ArgumentParser(
description="",
prog="generate_videos",
)
parser.add_argument(
"directory",
default="browsertime",
help="Directory to process",
)
parser.add_argument(
"-s", "--speedindex",
action="store_true",
default=False,
help="Use Median SpeedIndex",
)
parser.add_argument(
"-c", "--contentfulspeedindex",
action="store_true",
default=False,
help="Use Median ContentfulSpeedIndex",
)
parser.add_argument(
"-p", "--perceptualspeedindex",
action="store_true",
default=False,
help="Use Median PerceptualSpeedIndex",
)
parser.add_argument(
"-l", "--pageload",
action="store_true",
default=False,
help="Use Median pageload",
)
parser.add_argument(
"--videos",
action="store_true",
default=False,
help="Generate side-by-side videos",
)
parser.add_argument(
"-v", "--verbose",
action="store_true",
default=False,
help="Verbose output",
)
options = parser.parse_args()
os.chdir(options.directory);
files = glob.glob("*")
files_with_url = []
for url in files:
os.chdir(url)
sessions = glob.glob("*")
entry = {}
if options.verbose:
print(url)
print(sessions[0])
with open(sessions[0] + "/browsertime.json") as f:
data = json.load(f)
entry['url'] = data[0]['info']['url'].encode("utf8","ignore")
entry['dir'] = url
files_with_url.append(entry)
os.chdir('..')
if options.verbose:
print(files_with_url)
sortedFiles = sorted(files_with_url, key=itemgetter('url'))
sortedResults = []
for j,entry in enumerate(sortedFiles):
if options.verbose:
print(entry)
url=entry['dir']
os.chdir(url)
report = []
sessions = glob.glob("*")
skipURL=0
for k,session in enumerate(sessions):
with open(session + "/browsertime.json") as f:
data = json.load(f)
pageLoadTime = []
SpeedIndex = []
PerceptualSpeedIndex = []
ContentfulSpeedIndex = []
FirstVisualChange = []
VisualComplete85 = []
iterations=len(data[0]['browserScripts'])
for i in range(0,iterations):
pageLoadTime.append( data[0]['browserScripts'][i]['timings']['pageTimings']['pageLoadTime'] )
iterations=len(data[0]['visualMetrics'])
for i in range(0,iterations):
SpeedIndex.append( data[0]['visualMetrics'][i]['SpeedIndex'] )
PerceptualSpeedIndex.append( data[0]['visualMetrics'][i]['PerceptualSpeedIndex'] )
ContentfulSpeedIndex.append( data[0]['visualMetrics'][i]['ContentfulSpeedIndex'] )
FirstVisualChange.append( data[0]['visualMetrics'][i]['FirstVisualChange'] )
VisualComplete85.append( data[0]['visualMetrics'][i]['VisualComplete85'] )
# no switch in python
if options.speedindex:
metric = SpeedIndex
elif options.contentfulspeedindex:
metric = ContentfulSpeedIndex
elif options.perceptualspeedindex:
metric = PerceptualSpeedIndex
elif options.pageload:
metric = pageLoadTime
else:
metric = SpeedIndex
# TODO FVC/VC85
median=statistics.median(metric)
bestVideo = url + '/' + session + '/' + data[0]['files']['video'][0]
lowestDiff=abs(metric[0]-median)
lowestIndex = 0
for i in range(1,iterations):
diff=abs(metric[i]-median)
if options.verbose:
print ('Value: ' + str(metric[i]) + ' diff: ' + str(diff) + ' median: ' + str(median) )
if diff < lowestDiff:
if options.videos:
bestVideo = url + '/' + session + '/' + data[0]['files']['video'][i]
if options.verbose:
print('found new best video: ' + bestVideo)
lowestDiff = diff
lowestIndex = i;
# if not os.path.exists(bestVideo):
# print( '** best not found **')
# for i in range(1,iterations):
# bestVideo = url + '/' + session + '/' + data[0]['files']['video'][i]
# if os.path.exists(bestVideo):
# break
print("-------------------------------------------------------")
print(url + " -- " + session)
if options.videos:
print(" bestVideo " + bestVideo)
if options.verbose:
print("lowestDiff = " + str(lowestDiff))
print(SpeedIndex)
print("Median = " + str(statistics.median(SpeedIndex)) + ", median index = " + str(lowestIndex))
instance = {}
instance['pageLoadTimeMedian'] = statistics.median(pageLoadTime);
instance['pageLoadTimeStdev'] = statistics.stdev(pageLoadTime);
instance['SpeedIndexMedian'] = statistics.median(SpeedIndex);
instance['SpeedIndexStdev'] = statistics.stdev(SpeedIndex);
instance['PerceptualSpeedIndexMedian'] = statistics.median(PerceptualSpeedIndex);
instance['PerceptualSpeedIndexStdev'] = statistics.stdev(PerceptualSpeedIndex);
instance['ContentfulSpeedIndexMedian'] = statistics.median(ContentfulSpeedIndex);
instance['ContentfulSpeedIndexStdev'] = statistics.stdev(ContentfulSpeedIndex);
instance['FirstVisualChangeMedian'] = statistics.median(FirstVisualChange);
instance['FirstVisualChangeStdev'] = statistics.stdev(FirstVisualChange);
instance['VisualComplete85Median'] = statistics.median(VisualComplete85);
instance['VisualComplete85Stdev'] = statistics.stdev(VisualComplete85);
instance['value'] = session
instance['timestamp'] = data[0]['info']['timestamp']
instance['url'] = url
instance['video'] = bestVideo
instance['dir'] = session.upper()
report.append(instance)
sortedResults.append( sorted(report, key=itemgetter('timestamp')) )
os.chdir("..")
if options.videos:
for j,l in enumerate(sortedResults):
url=sortedResults[j][0]["url"]
video1=sortedResults[j][0]['video']
video2=sortedResults[j][1]['video']
#print(R"ffmpeg -i " + video1 + R" -vf drawtext='text=" + sortedResults[j][0]['dir'] + R":fontcolor=white:fontsize=24:box=1:[email protected]:boxborderw=5:x=(w-text_w)/2:y=10:scale=trunc(iw/2)*2:trunc(ih/2)*2' -vf 'scale=trunc(iw/2)*2:trunc(ih/2)*2' -codec:a copy input1.mp4")
#exit()
# command = R"ffmpeg -i " + video1 + R" -vf drawtext='text=" + sortedResults[j][0]['dir'] + R":fontfile=/users/acreskey/fonts/Raleway/Raleway-Bold.ttf:fontcolor=white:fontsize=24:box=1:[email protected]:boxborderw=5:x=(w-text_w)/2:y=10',scale='trunc(iw/2)*2:trunc(ih/2)*2' -codec:a copy input1.mp4"
# print(command)
# os.system(command)
# command = R"ffmpeg -i " + video2 + R" -vf drawtext='text=" + sortedResults[j][1]['dir'] + R":fontfile=/users/acreskey/fonts/Raleway/Raleway-Bold.ttf:fontcolor=white:fontsize=24:box=1:[email protected]:boxborderw=5:x=(w-text_w)/2:y=10',scale='trunc(iw/2)*2:trunc(ih/2)*2' -codec:a copy input2.mp4"
# print(command)
# os.system(command)
# command = R"ffmpeg -i input1.mp4" + R" -i input2.mp4" + R" -filter_complex '[0:v]pad=iw*2:ih[int];[int][1:v]overlay=W/2:0[vid]' -map [vid] -c:v libx264 -crf 23 -preset veryfast " + R"../videos/" + str(j).zfill(3) + "-" + url + ".mp4"
# print(command)
# os.system(command)
# command = R"ffmpeg -i " + video1 + R" -vf drawtext='text=" + sortedResults[j][0]['dir'] + R":fontfile=/users/acreskey/fonts/Raleway/Raleway-Bold.ttf:fontcolor=white:fontsize=24:box=1:[email protected]:boxborderw=5:x=(w-text_w)/2:y=10',scale='trunc(iw/2)*2:trunc(ih/2)*2' -codec:a copy input1.mp4"
# print(command)
# os.system(command)
# command = R"ffmpeg -i " + video2 + R" -vf drawtext='text=" + sortedResults[j][1]['dir'] + R":fontfile=/users/acreskey/fonts/Raleway/Raleway-Bold.ttf:fontcolor=white:fontsize=24:box=1:[email protected]:boxborderw=5:x=(w-text_w)/2:y=10',scale='trunc(iw/2)*2:trunc(ih/2)*2' -codec:a copy input2.mp4"
# print(command)
# os.system(command)
#command = R"ffmpeg -i " + video1 + R" -i " + video2 + R" -filter_complex '[0:v]pad=iw*2:ih[int];[int][1:v]overlay=W/2:0[vid]' -map [vid] -c:v libx264 -crf 23 -preset veryfast " + R"../videos/" + str(j).zfill(3) + "-" + url + ".mp4"
command = R"ffmpeg -i " + video1 + R" -i " + video2 + R" -filter_complex '[0:v]pad=iw*2:ih[int];[int][1:v]overlay=W/2:0[vid]' -map [vid] -c:v libx264 -crf 23 -preset veryfast " + R"../videos/" + str(j).zfill(3) + "-" + url + ".mp4"
print(command)
os.system(command)
# os.remove('input1.mp4')
# os.remove('input2.mp4')
exit
print("")