属性错误:'numpy.ndarray' 对象没有 'save_frame' 属性

0 投票
1 回答
46 浏览
提问于 2025-04-14 17:58

下面是代码:

 from moviepy.editor import VideoFileClip
 from docx import Document
 from docx.shared import Inches
 def extract_frames_to_docx(mp4_file, output_docx, interval=1):
# Open the MP4 file
   video = VideoFileClip(mp4_file)
# Create a new Word document
    doc = Document()
# Calculate the total number of frames
   total_frames = int(video.duration * video.fps)
# Determine the frame indices to extract based on the interval
   frame_indices = range(0, total_frames, interval)
# Extract frames at specified intervals and insert them into the Word document
   for idx in frame_indices:
       frame = video.get_frame(idx / video.fps)  # Get frame at specified time
       image_path = f"frame_{idx}.png"
       frame.save_frame(image_path)  # Save the frame as an image
       doc.add_picture(image_path, width=Inches(6))  # Insert the image into the Word document
# Save the Word document
   doc.save(output_docx)
# Close the video file
   video.close()
# Example usage with an interval of 10 frames
 extract_frames_to_docx("testvideo.mp4", "video_frames1.docx", interval=10)

下面是错误信息:

Traceback (most recent call last):
File "C:/Users/azs92/PycharmProjects/pythonProject/zmp4todoc(reducedimages).py", line 24, in <module>
  extract_frames_to_docx("testvideo.mp4", "video_frames1.docx", interval=10)
File "C:/Users/azs92/PycharmProjects/pythonProject/zmp4todoc(reducedimages).py", line 17, in    extract_frames_to_docx
frame.save_frame(image_path)  # Save the frame as an image
AttributeError: 'numpy.ndarray' object has no attribute 'save_frame'`

我本来希望这个程序能读取一个视频,并从中提取相关的图片到一个文档里。

1 个回答

0

是的,你可以使用这个。

with open("test_array","wb") as f:
      numpy.save(image_path,frame)`

撰写回答