简体   繁体   中英

ffmpeg crop detect python using subprocess

I want to use python subprocess to call ffmpeg and use crop detect to find all black in a video. The crop detect return I want to put into a string variable and put in a database. At the moment I can get the process running in terminal and but I am unsure about how to grab the specific part of the terminal (stdout) output:

the script:

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

Result in terminal: [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

How do I take "crop=-704:-560:714:570" and put it into a variable that I can store in a database?

As per update:

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

I can't seem to print out the "crop_result" - I am presuming that means that the variable is empty?

UPDATE: Found it:

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

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

It looks like you don't need to be using the lower level subprocess.Popen interface; I'd just call subprocess.check_output , which will return the value of the os call as a string. From there, just do string processing to get your value.

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

If ffmpeg output is instead to stderr:

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

Ok found it

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

So use this in the subprocess call to grab the values from crop detect:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM