在 Python 中使用 FFprobe

Using FFprobe in Python

如何在 python 脚本中使用 FFprobe,它也将输出导出为 csv 文件?

我要执行的命令是:

ffprobe -i file_name -show_frames -select_streams v:1 -print_format csv > filename.csv 

看了其他类似问题的帖子,稍微修改一下:

def probe_file(filename):
    cmnd = ['ffprobe', '-i',filename, '-show_frames', '-select_streams', 'a', '-print_format', 'csv']
    p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    print filename
    out, err =  p.communicate()
    print "==========output=========="
    print out
    if err:
        print "========= error =r======="
        print err

但是,我似乎无法使用“> filename.csv”。

分析完视频后,我希望所有输出为csv文件,命名与文件名相同

有人知道我该如何处理吗?

提前致谢

如果您想要 python 可用的数据(作为列表的列表):

data = [l.split(',') for l in out.decode('ascii').splitlines()]

如果您只想将数据转储到文件中:

with open("{}.csv".format(filename), "w") as f:
    f.write(out.decode('ascii'))

请注意,我使用的是 Python 3,因此 decode()s(outbytes,必须解码)。您显然正在使用 Python 2,因此可能不需要 decode()