我尝试通过目录
中的列表图片将PNG图像转换为视频clips[]
for filename in os.listdir('.'):
if filename.endswith(".png"):
clips.append(ImageClip(filename))
然后转换它
video = concatenate(clips, method='compose')
video.write_videofile('test.mp4')
import os
from moviepy.editor import *
clips = []
base_dir = os.path.realpath(".")
print(base_dir)
for filename in os.listdir('.'):
if filename.endswith(".png"):
clips.append(ImageClip(filename))
video = concatenate(clips, method='compose')
video.write_videofile('test.mp4')
答案 0 :(得分:2)
我找到了另一种方法:
from moviepy.editor import *
img = ['1.png', '2.png', '3.png', '4.png', '5.png', '6.png',
'7.png', '8.png', '9.png', '10.png', '11.png', '12.png']
clips = [ImageClip(m).set_duration(2)
for m in img]
concat_clip = concatenate_videoclips(clips, method="compose")
concat_clip.write_videofile("test.mp4", fps=24)
从当前文件夹:
import os
import glob
from natsort import natsorted
from moviepy.editor import *
base_dir = os.path.realpath("./images")
print(base_dir)
gif_name = 'pic'
fps = 24
file_list = glob.glob('*.png') # Get all the pngs in the current directory
file_list_sorted = natsorted(file_list,reverse=False) # Sort the images
clips = [ImageClip(m).set_duration(2)
for m in file_list_sorted]
concat_clip = concatenate_videoclips(clips, method="compose")
concat_clip.write_videofile("test.mp4", fps=fps)
答案 1 :(得分:2)
这是我使用您的初始代码完成的方式。您看到的错误是由于未为剪辑指定set_duration
。我还对目录中的文件进行了排序,以便生成的mp4是连续的(默认情况下不是这种情况)。
import os
from moviepy.editor import *
base_dir = os.path.realpath(".")
print(base_dir)
directory=sorted(os.listdir('.'))
print(directory)
for filename in directory:
if filename.endswith(".png"):
clips.append(ImageClip(filename).set_duration(1))
print(clips)
video = concatenate(clips, method="compose")
video.write_videofile('test1.mp4', fps=24)