我正在尝试根据时间从视频中提取帧,在每个帧上绘制一些x和y坐标值数据,然后使用moviepy
将帧组合成视频。我使用此question作为绘制提取帧数据的参考。我正在使用此video进行测试。以下是我正在运行的代码:
from moviepy.editor import *
from moviepy.Clip import *
from moviepy.video.io.bindings import PIL_to_npimage
from moviepy.video.VideoClip import DataVideoClip
import matplotlib.pyplot as plt
import numpy as np
import moviepy.editor as mpy
video = VideoFileClip("N:\SampleVideo_1280x720_1mb.mp4")
audio1 = video.audio
t = np.arange(0, video.duration, (1/video.fps))
images_list = []
for i, n in enumerate(t):
a = video.get_frame(n)
clip = ImageClip(a)
path = "C:\\Users\\Naveen\\Desktop\\frame\\frame" + str(i) + ".png"
clip.save_frame(path)
images_list.append(path)
x = np.random.randn(10, 1)
y = np.random.randn(10, 1)
p = plt.plot(x, y, 'kx')
i = 0
def data_to_frame(time):
global i
xn = x + np.sin(2 * np.pi * time / 10.0)
yn = y + np.cos(2 * np.pi * time / 8.0)
p[0].set_data(xn, yn)
image = ImageClip(images_list[i])
f = PIL_to_npimage(image)
i += 1
return f
animate = DataVideoClip(t, data_to_frame, fps=1)
animate.write_videofile("test2.mp4", fps=15)
这是我运行代码时遇到的错误:
Traceback (most recent call last):
File "h.py", line 37, in <module>
animate = DataVideoClip(t, data_to_frame, fps=1)
File "C:\Python27\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\video\VideoClip.py", line 814, in __init__
duration=1.0*len(data)/fps, has_constant_size=has_constant_size)
File "C:\Python27\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\video\VideoClip.py", line 106, in __init__
self.size = self.get_frame(0).shape[:2][::-1]
File "<decorator-gen-14>", line 2, in get_frame
File "C:\Python27\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\decorators.py", line 89, in wrapper
return f(*new_a, **new_kw)
File "C:\Python27\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\Clip.py", line 95, in get_frame
return self.make_frame(t)
File "C:\Python27\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\video\VideoClip.py", line 812, in <lambda>
make_frame = lambda t: self.data_to_frame( self.data[int(self.fps*t)])
File "h.py", line 33, in data_to_frame
f = PIL_to_npimage(image)
File "C:\Python27\lib\site-packages\moviepy-0.2.2.11-py2.7.egg\moviepy\video\io\bindings.py", line 11, in PIL_to_npimage
d = (4 if im.mode=="RGBA" else 3)
AttributeError: ImageClip instance has no attribute 'mode'
这里的问题是我不知道将帧导入函数PIL_to_npimage
的正确方法。或者更确切地说,这里的问题应该是,如何将图像作为图像导入moviepy
。此外,最终视频中不应有轴线。我希望能够只绘制数据。