gpt4 book ai didi

python - ffmpeg 裁剪使用子进程检测 python

转载 作者:行者123 更新时间:2023-12-04 23:16:41 24 4
gpt4 key购买 nike

我想使用 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("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 ,它将以字符串形式返回 os 调用的值。从那里,只需进行字符串处理即可获得您的值(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()

如果 ffmpeg 输出改为 stderr:
result = subprocess.check_output(["ffmpeg", ...], stderr=subprocess.STDOUT)

关于python - ffmpeg 裁剪使用子进程检测 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29519340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com