我想使用python子进程来调用ffmpeg并使用crop detect来查找视频中的所有黑色。裁剪检测返回我想放入一个字符串变量并放入数据库。目前我可以在终端中运行进程,但我不确定如何获取终端(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)
将其删除
答案 0 :(得分:1)
看起来您不需要使用较低级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("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)
答案 1 :(得分:0)
好的找到了
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()
因此在子进程调用中使用它来从crop crop中获取值:
p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", "cropdetect=24:16:0", "-vframes", "500", "-f", "rawvideo", "-y", "/dev/null"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)