python - 可视化 ffmpeg 基准

标签 python matplotlib ffmpeg

我已经生成了基准,用于比较使用 ffmpeg 工具缩小视频文件 (mp4) 的两种方法。

基准以这种格式记录:

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



我正在为多个视频文件和多个分辨率执行此操作。假设我需要使用下面的条形图对给定分辨率的方法 A 和方法 B 的基准(实时)比较进行可视化:

Sample Bar Chart

如何有效地从日志中获取必要的值并在 python 中使用 matplotlib 绘制它们?

(我对你解决这个问题的方法更感兴趣)

最佳答案

我的方法是这样的

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()

基本思想:通过非常典型的解析从日志中提取时间,将它们存储到每个文件的字典中,然后填充一个数组并绘制它的切片。有更简单的方法可以做到这一点,记账要少得多,但我发现它往往更容易跟踪事情。

问题中日志文件的结果是

image

关于python - 可视化 ffmpeg 基准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60861191/

相关文章:

python-3.x - 更快地绘制实时音频信号

android - ffmpeg 配置时的空过滤器(没有这样的过滤器 'eq' )

ffmpeg - 如何使用 FFmpeg 获取视频前 x 分钟的大小?

python - 我如何从线程中更新 Kivy 元素?

python - 如何在 pylab/pyplot 中裁剪极坐标图

python - OpenCV & Python -- 无法检测到蓝色物体

python - 使用 matplotlib 并排绘制图像

batch-file - 如何连接所有 ffmpeg 输入而不使用 Windows 批处理脚本将它们映射到过滤器中?

Python urllib.request.urlopen() 返回错误 10061?

python - 如何通过 SWIG 在 Python 和 C 之间编码指向 cstring 的指针