ffmpeg crop detect python 使用子进程

ffmpeg crop detect python using subprocess

我想使用 python 子进程调用 ffmpeg 并使用裁剪检测来查找视频中的全黑。作物检测 return 我想放入一个字符串变量并放入数据库中。目前我可以在终端中获取进程 运行,但我不确定如何获取终端(stdout)输出的特定部分:

脚本:

def cropDetect():
    p = subprocess.Popen(["ffmpeg", "-i", "/Desktop/ffprobe_instance/Crop_detect/video_file.mpg", "-vf", "cropdetect=24:16:0", "-vframes", "10", "dummy.mp4"], stdout=subprocess.PIPE)
    result = p.communicate()[0]
    print result


# SCRIPT
cropDetect()

终端结果: [Parsed_cropdetect_0 @ 0x7fa1d840cb80] x1:719 x2:0 y1:575 y2:0 w:-704 h:-560 x:714 y:570 pos:432142 pts:44102 t:0.490022 crop=-704:-560:714:570

如何获取 "crop=-704:-560:714:570" 并将其放入可以存储在数据库中的变量中?

根据更新:

def cropDetect1():
    p = subprocess.check_output(["ffmpeg", "-i", "/Desktop/ffprobe_instance/Crop_detect/video_file.mpg", "-vf", "cropdetect=24:16:0", "-vframes", "10", "dummy.mp4"])
    match = re.search("crop\S+", p)
    crop_result = None
    if match is not None:
        crop_result = match.group()
        print "hello %s" % crop_result

我似乎无法打印出 "crop_result" - 我假设这意味着变量为空?

更新:找到它:

def detectCropFile(localPath):
    fpath = "/xxx/xx/Desktop/Crop_detect/videos/USUV.mp4"
    print "File to detect crop: %s " % fpath
    p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", "cropdetect=24:16:0", "-vframes", "500", "-f", "rawvideo", "-y", "/dev/null"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    infos = p.stderr.read()
    print infos
    allCrops = re.findall(CROP_DETECT_LINE + ".*", infos)
    print allCrops 
    mostCommonCrop = Counter(allCrops).most_common(1)
    print "most common crop: %s" % mostCommonCrop
    print mostCommonCrop[0][0]
    global crop
    crop = mostCommonCrop[0][0]
    video_rename()

使用:p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", "cropdetect=24:16:0", "-vframes", "500", "-f", "rawvideo", "-y", "/dev/null"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 输出

看来您不需要使用较低级别的 subprocess.Popen 界面;我只是调用 subprocess.check_output,它将 return os 调用的值作为字符串。从那里,只需进行字符串处理即可获得您的价值。

result = subprocess.check_output(["ffmpeg", "-i" ... ])
# this regex matches the string crop followed by one or more non-whitespace characters 
match = re.search("crop\S+", result) 
crop_result = None
if match is not None:
     crop_result = match.group()

如果 ffmpeg 输出改为 stderr:

result = subprocess.check_output(["ffmpeg", ...], stderr=subprocess.STDOUT)

好的,找到了

def detectCropFile(localPath):
    fpath = "/xxx/xx/Desktop/Crop_detect/videos/USUV.mp4"
    print "File to detect crop: %s " % fpath
    p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", "cropdetect=24:16:0", "-vframes", "500", "-f", "rawvideo", "-y", "/dev/null"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    infos = p.stderr.read()
    print infos
    allCrops = re.findall(CROP_DETECT_LINE + ".*", infos)
    print allCrops 
    mostCommonCrop = Counter(allCrops).most_common(1)
    print "most common crop: %s" % mostCommonCrop
    print mostCommonCrop[0][0]
    global crop
    crop = mostCommonCrop[0][0]
    video_rename()

所以在子进程调用中使用它来获取裁剪检测的值:

p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", "cropdetect=24:16:0", "-vframes", "500", "-f", "rawvideo", "-y", "/dev/null"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)