Git Bash for Windows AttributeError:'NoneType'对象没有属性'groups'使用 Python re 解析字符串



我正试图为一个项目使用预先训练的CNN模型,其中一些代码在我的机器上出现了问题。Windows 10,git版本2.28.0.Windows.1,Python 3.9.0

此代码来自https://github.com/zhoubolei/moments_models/tree/v2

output = subprocess.Popen(['ffmpeg', '-i', video_file], stderr=subprocess.PIPE, shell=True).communicate()
# Search and parse 'Duration: 00:05:24.13,' from ffmpeg stderr.
re_duration = re.compile(r'Duration: (.*?).')
duration = re_duration.search(str(output[1])).groups()[0]

我得到以下回溯:

Traceback (most recent call last):
File "C:UsersgradxPycharmProjectsfinalmoments_modelstest_video.py", line 62, in <module>
frames = extract_frames(args.video_file, args.num_segments)
File "C:UsersgradxPycharmProjectsfinalmoments_modelsutils.py", line 21, in extract_frames
duration = re_duration.search(str(output[1])).groups()[0]
AttributeError: 'NoneType' object has no attribute 'groups'

本质上,目标是从一些字符串Popen和re.complile((product中收集输入视频文件的运行时间。这不是我的代码,所以我不能说为什么使用这种方法,但也不能建议不同的方法。我尝试过修改传递给re.comfile((的正则表达式,因为我意识到如果找不到任何东西,它可能会返回None,但这并没有帮助。

感谢您的支持。

编辑:原来问题是ffmpeg不见了。

您的代码的主要问题

您正在搜索一个模式,并且在检查搜索是否返回内容之前,尝试获取其组甚至

# As the code is returning only the errors (stderr=subprocess.PIPE)
# it is better to create a variable called error instead of output:
_, error = subprocess.Popen(['ffmpeg', '-i', video_file], stderr=subprocess.PIPE, shell=True).communicate()
# Lets check if the execution returned some error
if error:
print(f"Omg some error occurred: {str(error)}")
# Get the duration returned by the error
# error are bytes, so we need to decode it
re_duration = re.search(r'Duration: (.*?).', error.decode())
# if the search by duration returned something
# then we are taking the group 1 (00:05:24)
if re_duration:
re_duration = re_duration.group(1)
print(re_duration)
# 00:05:24

我们假设Duration...在发生错误时被返回,但如果它是由成功输出返回的,那么您必须反转变量并向您的子流程添加一个更改:

# Changing variable to output and changing the return to stdout (successful return)
output, _ = subprocess.Popen(['ffmpeg', '-i', video_file], stdout=subprocess.PIPE, shell=True).communicate()

相关内容

最新更新