问题
I have generated benchmarks for comparing two approaches taken for scaling down video files (mp4) using ffmpeg tool.
The benchmarks are logged in this format :
x.mp4 Output_Resolution : 360p
Method : A
real 0m26.817s
user 1m38.058s
sys 0m0.504s
Method : B, some-parameter-for-B : b1
real 0m26.465s
user 1m42.824s
sys 0m1.111s
Method : B, some-parameter-for-B : b2
real 0m26.236s
user 1m42.194s
sys 0m0.862s
Method : B, some-parameter-for-B : b3
real 0m25.050s
user 1m36.492s
sys 0m0.680s
y.mp4 Output_Resolution : 144p
Method : A
real 1m9.426s
user 3m38.823s
sys 0m1.353s
Method : B, some-parameter-for-B : b1
real 1m4.956s
user 4m13.764s
sys 0m2.875s
Method : B, some-parameter-for-B : b2
real 1m5.033s
user 4m13.455s
sys 0m2.183s
Method : B, some-parameter-for-B : b3
real 0m25.050s
user 1m36.492s
sys 0m0.680s
I am doing this for multiple video files and multiple resolutions. Lets say I need to visualize the comparison of benchmarks(real time) of method A and method B for given a resolution using bar chart below :
How do I efficiently get the necessary values from the the log and plot them using matplotlib in python ?
(I am more interested in the approach you would take to solve this problem)
回答1:
My approach would be something like this
import matplotlib.pyplot as plt
import itertools
import numpy as np
def gettime(s):
e = ["".join(x) for _, x in itertools.groupby(s, key=str.isdigit)]
h = float(e[e.index("h") - 1]) if "h" in e else 0.0
m = float(e[e.index("m") - 1]) if "m" in e else 0.0
s = float(''.join(e[e.index("s") - 3:e.index("s")])) if "s" in e else 0.0
return 3600 * h + 60 * m + s
lines = []
with open("log.txt") as fp:
lines = fp.read().splitlines()
files = []
idx = 0
keys = ["A", "b1", "b2", "b3"]
methods = []
while idx < len(lines):
if ".mp4" in lines[idx]:
method = {k : 0.0 for k in keys}
files.append(lines[idx].split(' ')[0])
idx += 1
while idx < len(lines) and ".mp4" not in lines[idx]:
if "Method" in lines[idx]:
substrings = list(filter(None, lines[idx].split(' ')))
if substrings[-1] in keys:
while "real" not in lines[idx]:
idx += 1
method[substrings[-1]] = gettime(lines[idx].split(' ')[-1])
idx += 1
methods.append(method)
else:
idx += 1
data = np.zeros((len(keys), len(files)))
for idx, (d, f) in enumerate(zip(methods, files)):
data[:,idx] = np.array([d[k] for k in keys]).reshape(data[:,idx].shape)
x = np.arange(len(files))
w = (1.0 / data.shape[0]) * 0.6
names = ["A", "B, b1", "B, b2", "B, b3"]
for i in range(data.shape[0]):
plt.bar(x - w * i, data[i,:], width=w, label=names[i])
plt.tick_params(bottom = False)
plt.xticks(x - w * (i - 1.5), files)
plt.legend(title="Method")
plt.show()
Basic idea: extract the times from the log via pretty typical parsing, store them into a dictionary for each file and then populate an array and plot slices of it. There are simpler ways to do this, with much less bookkeeping, but I find that it tends to make it easier to keep track of things.
The result with the log file in the question is
来源:https://stackoverflow.com/questions/60861191/visualizing-ffmpeg-benchmarks