python 图像帧转视频

发布于 2024-10-30 21:50:52 字数 278 浏览 3 评论 0原文

我正在编写一个 python/django 应用程序,它需要进行图像处理,然后将图像组合成视频(每个图像都是一帧)。图像处理很容易。我正在使用 PIL,但对于转换为视频部分,我陷入困境。我找到了 pyffmpeg,但这似乎只是将视频解码为帧,而不是相反。虽然我可能错过了一些东西。我还听说 pythonMagick (imagemagick 包装器)可以做到这一点,但我似乎在文档中找不到有关编码的任何内容。

它在 Linux 服务器上运行,并且必须是 python(因为应用程序就是在其中)。

我应该用什么?

I am writing a python/django application and it needs to do image manipulation and then combine the images into a video (each image is a frame). Image manipulation is easy. I'm using PIL, but for the convert to video part, I'm stuck. I've found pyffmpeg but that just seems to do decoding of videos to frames, not the other way around. Although I may have missed something. I've also heard that pythonMagick (imagemagick wrapper) can do this, but I can't seem to find anything about encoding in the docs.

This is running on a linux server and must be python (since that is what the application is in).

What should I use?

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

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

发布评论

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

评论(4

筱武穆 2024-11-06 21:50:52

首先使用以下命令安装 ffmpeg: sudo apt install ffmpeg

import os
os.system("ffmpeg -f image2 -r 1/5 -i ./images/swissGenevaLake%01d.jpg -vcodec mpeg4 -y ./videos/swissGenevaLake.mp4")

详细信息:

上述代码从存储在 images 文件夹中的三个图像文件创建视频。每个图像在视频中播放 5 秒(由于参数:-r 1/5)。 images 文件夹中的三个文件名称分别为:“swissGenevaLake1.jpg”、“swissGenevaLake2.jpg”和“swissGenevaLake3.jpg”。

希望这有帮助。

First install ffmpeg using the following command: sudo apt install ffmpeg

import os
os.system("ffmpeg -f image2 -r 1/5 -i ./images/swissGenevaLake%01d.jpg -vcodec mpeg4 -y ./videos/swissGenevaLake.mp4")

Details:

The aforementioned code creates a video from three images file, stored in the images folder. Each image plays for 5 seconds in the video (because of the argument: -r 1/5). The three files names are: "swissGenevaLake1.jpg", "swissGenevaLake2.jpg" and "swissGenevaLake3.jpg" in images folder.

Hope this helps.

赴月观长安 2024-11-06 21:50:52

使用 OpenCV 和 python 绑定。有cv.WriteFrame函数。 类似问答

Use OpenCV and python binding. There is cv.WriteFrame function. Similar question and answer

饮惑 2024-11-06 21:50:52

您可以使用 Popen 只是在子进程中运行 ffmpeg。

You could use Popen just to run the ffmpeg in a subprocess.

多情出卖 2024-11-06 21:50:52

如果您有一个要制作为视频的图像文件夹。您可以调整参数并以排序方式排列帧。

import cv2
import os
from tqdm import tqdm
import glob
#TODO
image_folder = '<Enter your target frames folder here>/*'
video_name = 'Dir to store the video'#save as .avi
#is changeable but maintain same h&w over all  frames
width=640 
height=400 
#this fourcc best compatible for avi
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
video=cv2.VideoWriter(video_name,fourcc, 2.0, (width,height))



for i in tqdm((sorted(glob.glob(image_folder),key=os.path.getmtime))):
     x=cv2.imread(i)
     video.write(x)

cv2.destroyAllWindows()
video.release()

If u have a folder of images to be framed as video. You can tune the parameters and arrange the frames in sorted manner.

import cv2
import os
from tqdm import tqdm
import glob
#TODO
image_folder = '<Enter your target frames folder here>/*'
video_name = 'Dir to store the video'#save as .avi
#is changeable but maintain same h&w over all  frames
width=640 
height=400 
#this fourcc best compatible for avi
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
video=cv2.VideoWriter(video_name,fourcc, 2.0, (width,height))



for i in tqdm((sorted(glob.glob(image_folder),key=os.path.getmtime))):
     x=cv2.imread(i)
     video.write(x)

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