Convert image sequence to video using Moviepy

梦想的初衷 提交于 2019-12-07 05:40:51

问题


I tried to convert PNG images to video by list images in directory

clips[]
for filename in os.listdir('.'):
  if filename.endswith(".png"):
    clips.append(ImageClip(filename))

Then convert it

video = concatenate(clips, method='compose')
video.write_videofile('test.mp4')

The error is: Full code

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')

回答1:


I found another way to do it:

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)

And from current folder:

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)



回答2:


This is how I did it using your initial code. The error you were seeing was due to not specifying set_duration for the clips. I also sorted the files in the directory so that the resulting mp4 is sequential (was not the case by default).

    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)


来源:https://stackoverflow.com/questions/44732602/convert-image-sequence-to-video-using-moviepy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!