Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storing Processed Video clip takes a long time #726

Closed
waterruns opened this issue Feb 15, 2018 · 14 comments
Closed

Storing Processed Video clip takes a long time #726

waterruns opened this issue Feb 15, 2018 · 14 comments

Comments

@waterruns
Copy link

I have used moviepy to load and process a video. during processing the fps and total number of frames dont change, but each frame is rotated with 10 degrees, the outer area of frame are set as black.
when I want to store the videos, it takes a very long time (4 times the video time) using the the following command,
clip.write_videofile(outpath, fps=clip.fps, codec = 'mpeg4', audio=True, threads=8)

@tburrows13
Copy link
Collaborator

How long is the video? What is the difference in writing times between before you’ve edited it, and after?

Also, you shouldn’t need to pass fps=clip.fps into write_videofile().

@waterruns
Copy link
Author

the video is 30 seconds with 900 frames, it takes almost 1.5 minutes to write, before processing it takes around 40 seconds to write

@Zulko
Copy link
Owner

Zulko commented Feb 15, 2018

What is the image resolution ? For HD video, rotating 1 frame can take some time, so if you have 30 frames per second it will be slow. What do you use to rotate the video ? Maybe there exist faster options.

@waterruns
Copy link
Author

the whole processing is done separately, and takes 3 seconds per video, the above numbers are for just saving the result of the video with or without rotation process (3 seconds)

@Zulko
Copy link
Owner

Zulko commented Feb 15, 2018

Can you paste the exact code you are using ?

@waterruns
Copy link
Author

totaltime=time.time()
clip, ID = video_capture_api(dirpath)
fps  = clip.fps

t = time.time()
clip_realiz = generate_realization(clip.copy(), varins=(somearguments))
print('Realiz Gen Time: {}'.format(time.time() - t)); # this one takes 3 seconds
t = time.time();     
outpath = output_dir + '/' + ID + '_' + str(n_real) + '.mp4'      
clip_realiz.write_videofile(outpath, codec = 'mpeg4', audio=True, threads=100) 
print('Storage Time: {}'.format(time.time() - t));
print('Total Time: {}'.format(time.time()-totaltime)) # this one is about 2 minutes

def generate_realization(video, varins):
    video = video.fl_image(lambda frame: frame_manipulate(frame, varins),apply_to='mask')
return video

@waterruns
Copy link
Author

if I save clip.write_video , it is super fast, but clip_realiz.write_video is slow

@tburrows13
Copy link
Collaborator

I think the results you are getting are pretty typical. I may be wrong, but I don’t think moviepy does much processing in your generate_realization line. It ‘saves’ it all until you actually write it to a file, hence why it takes so much longer.

@Zulko
Copy link
Owner

Zulko commented Feb 15, 2018

It's clearer now, and I think your problem is wrong assumptions on what is going on in the script. When you apply generate_realization, it does NOT process all the frames of your clip, only the first frame (hence the 3 seconds that you observe). The other frames of your clip are only processed, one by one, when you call write_video_file.

@waterruns
Copy link
Author

oh, i see, thank you for that,
is there a way to make sure the processing is done inside generate_realization ?
also is an in herit function inside moviepy for shifting and rotating frames?

@tburrows13
Copy link
Collaborator

tburrows13 commented Feb 15, 2018

Not sure about your first question, but you should be able to use clip = clip.rotate(10). You’ll need pillow installed for angles other than 90, 180 and 270 though.

@waterruns
Copy link
Author

is there a way to replace all the frames inside a clip with a numpy array?
I can make my processing really fast on the whole numpy array of frames, if the replacement can be done, it will be helpful.

@Zulko
Copy link
Owner

Zulko commented Feb 15, 2018

I am closing this thread as it is not a moviepy bug/feature request anymore.

To answer your first question, yes you could with the unfortunately undocumented ImagesSequenceClip

But beware that all these numpy arrays in memory are going to take a lot of RAM !

Another option is to convert your clip to a sequence of images on your disk using "clip.write_images_sequence", then load these images using ImageSequenceClip (which in this case will only load the images as they are needed).

@Zulko Zulko closed this as completed Feb 15, 2018
@waterruns
Copy link
Author

Thank you for the replies and solution provided. I really appreciate you guys efforts in building such a nice package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants