python - 尝试将 ffprobe 的输出放入变量中

标签 python ffprobe

我正在尝试将视频文件中的 ffprobe 值获取到一个变量中,我可以将其与其他变量进行比较或将值移动到数据库中。我的问题是;有比下面更好的方法吗?

我不喜欢多个 if/elif/line.startswith 语句,并且我不确定 split 是获取 ffprobe 值的最佳方式吗?

#!/usr/bin/python
import os, sys, subprocess, shlex, re, fnmatch
from subprocess import call 

videoDrop_dir="/mnt/VoigtKampff/Temp/_Jonatha/test_drop"

for r,d,f in os.walk(videoDrop_dir):
    for files in f:
        print "Files: %s" % files
        if files.startswith(('._', '.')):
            print "This file: %s is not valid" % files
         elif files.endswith(('.mov', '.mpg', '.mp4', '.wmv', '.mxf')):
            fpath = os.path.join(r, files)
            def probe_file(fpath):
                cmnd = ['ffprobe', '-show_format', '-show_streams', '-pretty', '-loglevel', 'quiet', fpath]
                p = subprocess.Popen(cmnd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                print files
                out, err = p.communicate()
                print "===============================OUTPUT START: %s ===============================" % files
                print out
                for line in out.split('\n'):
                line = line.strip()
                if line.startswith('codec_name='):
                    s = line 
                    codec_name = s.split('codec_name=', 1)
                    print "Codec is: %s" % codec_name[1]
                    codec_1 = codec_name[1]
                elif line.startswith('codec_type='): 
                    s = line 
                    codec_type = s.split('codec_type=', 1)
                    print "Codec type is: %s" % codec_type[1]
                    codec_type1 = codec_type[1]   
                elif line.startswith('codec_long_name=', 1):
                    s = line 
                    codec_long_name = s.split('codec_long_name=', 1)
                    print "Codec long name: %s" % codec_long_name[1]
                    codec_long_name = codec_long_name[1]
                elif line.startswith('format_long_name=', 1):
                    s = line 
                    format_long_name = s.split('format_long_name=', 1)
                    print "Format long name: %s" % format_long_name[1]
                    format_long_name = format_long_name[1]
                elif line.startswith('width='): 
                    s = line 
                    width = s.split('width=', 1)
                    print "Video pixel width is: %s" % width[1]
                    p_width = width[1]
                elif line.startswith('height='): 
                    s = line 
                    height = s.split('height=', 1)
                    print "Video pixel height is: %s" % height[1]
                    p_height = height[1]    
                elif line.startswith('bit_rate='):
                    s = line 
                    bit_rate = s.split('bit_rate=', 1)
                    print "Bit rate is: %s" % bit_rate[1]
                    bit_rate1 = bit_rate[1]
                elif line.startswith('display_aspect_ratio=', 1):
                    s = line 
                    display_aspect_ratio = s.split('display_aspect_ratio=', 1)
                    print "Display aspect ratio: %s" % display_aspect_ratio[1]
                    display_aspect_ratio1 = display_aspect_ratio[1]
                elif line.startswith('avg_frame_rate=', 1):
                    s = line 
                    avg_frame_rate = s.split('avg_frame_rate=', 1)
                    print "Average Frame Rate: %s" % avg_frame_rate[1]
                    avg_frame_rate1 = avg_frame_rate[1]

            print "===============================OUTPUT FINISH: %s ===============================" % files
            if err: 
                print "===============================ERROR: %s ===============================" % files
                print err
        probe_file(fpath)
    else:
        if not files.startswith(('.mov', '.mpg', '.mp4', '.wmv', '.mxf')):
            print "This file: %s is not a valid video file" % files 

最佳答案

这有点晚了,但希望它可以帮助其他人寻找类似的答案。

import json, subprocess

# grab info about video_file
ffprobe_cmd = '/home/ubuntu/bin/ffprobe -v quiet -print_format json -show_format -show_streams -  i ' + v + ' 2>&1'
# print ffprobe_cmd        
s = subprocess.Popen(ffprobe_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

ffprobe_out, err = s.communicate()

ffprobe_dict = json.loads(ffprobe_out)

从这里开始,我重新使用了一个常用方法 search_dict,它的用法如下:

search_dict(ffprobe_dict, 'height')

def search_dict(my_dict, field):
"""Takes a dict with nested lists and dicts,
and searches all dicts for a key of the field
provided.
"""
fields_found = []

for key, value in my_dict.iteritems():

    if key == field:
        fields_found.append(value)

    elif isinstance(value, dict):
        results = search_dict(value, field)
        for result in results:
            fields_found.append(result)

    elif isinstance(value, list):
        for item in value:
            if isinstance(item, dict):
                more_results = search_dict(item, field)
                for another_result in more_results:
                    fields_found.append(another_result)

return fields_found

关于python - 尝试将 ffprobe 的输出放入变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15122942/

相关文章:

ffmpeg - ffprobe 的数据包大小单位是多少(类似于 ffmpeg)?

python - bignums 在内部是如何表示的?

python - PySpark 在 Dataframe 列中插入常量 SparseVector

python - 属性错误: module 'pip._internal.download' has no attribute 'HTTPAdapter'

ffmpeg - 使用 ffmpeg 为 h.26x 视频的单个帧提取量化参数

python - 使用用户在 Python 中插入的文件运行 Linux shell 脚本

FFmpeg 无法识别 hstack 命令中的正确输入高度

python - matplotlib 嵌入 wxPython 中,带有导航工具栏坐标

python - 在Python中,如何拟合最小分位数b样条回归线?

json - FFprobe,如何将动画 gif 的帧速率作为 JSON 格式的小数