ffmpeg裁剪使用子进程检测python



我想使用python子进程来调用ffmpeg,并使用裁剪检测来查找视频中的所有黑色。我想将作物检测返回放入字符串变量并放入数据库中。目前,我可以让进程在终端中运行,但我不确定如何获取终端(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("cropS+", 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,它将以字符串的形式返回os调用的值。从那里,只需进行字符串处理即可获得您的值。

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

如果ffmpeg输出改为stderr:

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

Ok找到

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)

最新更新