I am trying to extract frames from a video based on time, plot some x and y coordinate value data on each frame and combine the frames into a video again using moviepy
. I used this question as a reference for plotting data on the extracted frames. I am using this video for testing. Below is the code that I am running:
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)
This is the error I get when I run the code:
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'
The problem here is that I don't know the right way to import the frames into the function PIL_to_npimage
. Or rather, the question here should be, how to import an image as an image in moviepy
. Also, there should be no axis lines in the final video. I want to be able to plot only the data.