简体   繁体   English

将单个 png 文件转换为 mp4

[英]Convert single png file to mp4

I'm trying to find a way to convert a single png image to an mp4 file with a specified length.我正在尝试找到一种将单个 png 图像转换为具有指定长度的 mp4 文件的方法。 Solutions I have found are slow and unreliable when trying to make videos with longer lengths (multiple hours).在尝试制作更长(多个小时)的视频时,我发现的解决方案缓慢且不可靠。

Here is my current solution in Python 3:这是我目前在 Python 3 中的解决方案:

import os
import cv2

video_name = 'video.avi'
frame = cv2.imread('image.png')
height, width, layers = frame.shape

time = 300
    
video = cv2.VideoWriter(video_name, 0, 1 / 10, (width,height))

for i in range(int(time / 10)):
    video.write(cv2.imread(os.path.join('.', 'image.png')))

video.write(cv2.imread('image.png'))

cv2.destroyAllWindows()
video.release()

Basically the same image is appended multiple times to make up the video, and I found that it was very taxing to merge so therefore the fps is set to 0.1 seconds.基本上相同的图像被多次附加以组成视频,我发现合并非常费力,因此将 fps 设置为 0.1 秒。

This feels like the wrong approach so any solutions are appreciated这感觉像是错误的方法,所以任何解决方案都值得赞赏

you'll like PyAV.你会喜欢 PyAV。 it's a proper wrapper around ffmpeg's libraries, not the usual subprocess kludges you find in random python packages.它是 ffmpeg 库的适当包装器,而不是您在随机 python 包中找到的常见subprocess进程 kludges。

it has a sample where frames are written with custom Presentation Timestamps instead of a fixed frame rate.它有一个示例,其中使用自定义演示时间戳而不是固定帧速率编写帧。 the math/usage is a little nebulous thanks to ffmpeg not making those things clear in their own documentation.由于 ffmpeg 没有在他们自己的文档中明确说明这些事情,数学/用法有点模糊。

basically you set av.VideoFrame.pts and that's it.基本上你设置av.VideoFrame.pts就是这样。

https://github.com/PyAV-Org/PyAV/blob/main/examples/numpy/generate_video_with_pts.py https://github.com/PyAV-Org/PyAV/blob/main/examples/numpy/generate_video_with_pts.py

do understand that video with unusually long times between frames may be a challenge for some video players, at least when trying to seek instead of playing sequentially.请理解,对于某些视频播放器来说,帧之间的时间异常长的视频可能是一个挑战,至少在尝试寻找而不是顺序播放时是这样。

OpenCV is not a media library. OpenCV 不是媒体库。 its video I/O functions are intended and designed to be convenient and not flexible.它的视频 I/O 功能旨在方便而不灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM