MP4不起作用用于制作框架的视频,但AVI起作用

发布于 2025-01-25 10:29:55 字数 681 浏览 1 评论 0原文

我正在使用以下代码从我拥有的图片中创建电影,并使用以下代码段:

import cv2
import os

image_folder = 'shots'
video_name = 'video.avi'
fps = 25

images = [img for img in os.listdir(image_folder) if img.endswith(".png")]

images = sorted(images)[:][:steps]

frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape

video = cv2.VideoWriter(video_name, 0, fps, (width, height))

for image in images:
  video.write(cv2.imread(os.path.join(image_folder, image)))

cv2.destroyAllWindows()
video.release()

问题是,当我将扩展名更改为mp4时,它不起作用。如何更改代码以使其能够工作? mp4的原因是该过程的速度非常慢,我认为这是因为avimp4具有更高的质量。

I am using the following code to create a movies out of pictures that I have and use the following code snippet:

import cv2
import os

image_folder = 'shots'
video_name = 'video.avi'
fps = 25

images = [img for img in os.listdir(image_folder) if img.endswith(".png")]

images = sorted(images)[:][:steps]

frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape

video = cv2.VideoWriter(video_name, 0, fps, (width, height))

for image in images:
  video.write(cv2.imread(os.path.join(image_folder, image)))

cv2.destroyAllWindows()
video.release()

The problem is that when I change the extension to mp4 it does not work. How can I alter my code so that I can get it to work? The reason for mp4 is the speed of the process which is very slow and I think it is because avi has more quality than mp4.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当梦初醒 2025-02-01 10:29:55

cv2.VideWriter语法中具有(文件名,fourcc,fps,frameSize)这些参数您缺少一个称为fourcce> fourcc的参数(fourcc:4--编解码器的字符代码用于压缩帧)

import cv2
import os

image_folder = 'shots'
video_name = 'video.mp4'
fps = 25

images = [img for img in os.listdir(image_folder) if img.endswith(".png")]

images = sorted(images)[:][:steps]

frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape

video = cv2.VideoWriter(video_name,cv2.VideoWriter_fourcc(*'MP4V'), fps, (width, height))

for image in images:
  video.write(cv2.imread(os.path.join(image_folder, image)))

cv2.destroyAllWindows()
video.release()

In cv2.VideoWriter syntax have (filename, fourcc, fps, frameSize) these parametrs you are missing one parameter called fourcc (fourcc: 4-character code of codec used to compress the frames)

import cv2
import os

image_folder = 'shots'
video_name = 'video.mp4'
fps = 25

images = [img for img in os.listdir(image_folder) if img.endswith(".png")]

images = sorted(images)[:][:steps]

frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape

video = cv2.VideoWriter(video_name,cv2.VideoWriter_fourcc(*'MP4V'), fps, (width, height))

for image in images:
  video.write(cv2.imread(os.path.join(image_folder, image)))

cv2.destroyAllWindows()
video.release()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文