I was playing about using ImageMagick to average videos in the style discussed in an old blog from @patdavid on photo averaging, (useful for pseudo long exposure shots).
As mentioned in the blog ImageMagick has a nasty habit of loading all off the video at once so it is necessary to split into frames first, which uses a lot of disk space, and even then it will be necessary to batch the images to make things manageable.
Being a great fan of Python I decided to see if I could batch process by iterating over the frames one at a time and accumulating and of course it is possible but you still have all of those files, and all of that disk space.
Say hello to two very useful python libraries, (currently only available for Python 2):
- MoviePy: A Python module for video editing, note this library has a few dependencies and also uses FFMPEG but will download it on first use if it is not already available
- youtube-dl: A command-line program to download videos from YouTube.com and other video sites used to demo this process.
I decided to try to replicate the results of processing Die Antwoord - “I Fink You Freeky“ so downloaded from You Tube with:
> youtube-dl https://youtu.be/8Uee_mcxvrw -f mp4
[youtube] 8Uee_mcxvrw: Downloading webpage
[youtube] 8Uee_mcxvrw: Downloading video info webpage
[youtube] 8Uee_mcxvrw: Extracting video information
[download] Destination: ‘I FINK U FREEKY’ by DIE ANTWOORD (Official)-8Uee_mcxvrw.mp4
[download] 100% of 31.72MiB in 00:05
Then I was able to process the video with the following python code:
from moviepy.editor import *
clip = VideoFileClip("'I FINK U FREEKY' by DIE ANTWOORD (Official)-8Uee_mcxvrw.mp4")
fps= 2.0 # take 2 frame per second seems like a fast enough sample rate
nframes = clip.duration*fps # total number of frames used
total_image = sum(clip.iter_frames(fps,dtype=float,progress_bar=True)) # Calculate the totals
average_image = ImageClip(total_image/ nframes) # Average
average_image.save_frame('Freeky.png') # Save it.
Less than two minutes later I had the result:
I was working on Windows 10 but the above should work on most platforms - all of the software is free, gratis & open source.