gpt4 book ai didi

python - 使用cv2.VideoWriter将PIL图像列表放入视频幻灯片

转载 作者:行者123 更新时间:2023-12-02 17:16:35 25 4
gpt4 key购买 nike

我创建了关注功能。

import cv2
import numpy as np

FPS = 10

def write_video(file_name, images, slide_time=5):
fourcc = cv2.VideoWriter.fourcc(*'X264')
out = cv2.VideoWriter(file_name, fourcc, FPS, (870, 580))

for image in images:
cv_img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
for _ in range(slide_time * FPS):
out.write(cv_img)

out.release()
它曾经工作过一次,但我把它弄坏了,不知道如何。
当我尝试使用MPV打开时,我得到:
[ffmpeg/demuxer] avi: Could not find codec parameters for stream 0 (Video: h264 (X264 / 0x34363258), none, 870x580): unspecified pixel format
[ffmpeg/demuxer] Consider increasing the value for the 'analyzeduration' and 'probesize' options
(+) Video --vid=1 (h264 870x580 10.000fps)


Exiting... (Errors when loading file)
这是一个最小的可重现示例:
import requests
from PIL import Image
from io import BytesIO
import cv2
import numpy as np



FPS = 10

res = requests.get('https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.bNvIoWPYwYWXe-gpRKsalQHaE9%26pid%3DApi&f=1')

image = Image.open(BytesIO(res.content))
image = np.array(image)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

fourcc = cv2.VideoWriter.fourcc(*'MJPG')
video = cv2.VideoWriter('output.avi', fourcc, FPS, (870, 580))


for _ in range(13 * FPS):
video.write(image)

video.release()

最佳答案

我想解决您的代码问题。

  • 问题#1:
  • 错误提示:

    Could not find codec parameters for stream 0 (Video: h264"


  • 表示x264编解码器在当前环境中不可用。
  • 您可以安装opencv-python
  • 将编解码器初始化为MJPG(Motion-JPEG)。原因是MJPG适合.avi文件。

  • fourcc = cv2.VideoWriter.fourcc(*'MJPG')

  • 问题#2:
  • 您正在从RGB转换为BGR
  • 我建议您阅读图像,因此您不必转换为BGR
  • cv2.imreadBGR格式读取图像
  • 还要确保每个图像的大小与VideoCapture定义的大小相同。

  • for image in images:
    cv_img = cv2.imread(image)
    cv_img = cv2.resize(cv_img, (870, 580))


  • 代码会根据最小可复制错误进行更新:

    import cv2
    from glob import glob

    FPS = 10


    def write_video(file_name, images, slide_time=5):
    fourcc = cv2.VideoWriter.fourcc(*'MJPG')
    out = cv2.VideoWriter(file_name, fourcc, FPS, (870, 580))

    for image in images:
    cv_img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
    for _ in range(slide_time * FPS):
    cv_img = cv2.resize(image, (870, 580))
    out.write(cv_img)

    out.release()


    if __name__ == '__main__':
    write_video(file_name='result.avi', images=glob("b/*.jpg"))

    关于python - 使用cv2.VideoWriter将PIL图像列表放入视频幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64506236/

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