ACT
ACT copied to clipboard
Function to create movies
The DQ Office creates many movies from static plots. Another researcher is working on a project that would benefit from a simple way to create moves from plots. We should create a function to make movies from a list of images. Finding a mostly Python way to do this would be best.
This is how we did it for gifs in the past during my internship: https://github.com/zssherman/pyart_animation/blob/master/pyart_animation/pyart_animation.py However for mp4s, in TINT I wrote an mp4 maker, which could probably be enhanced: https://github.com/openradar/TINT/blob/d6cdf92317a08043b7d217f1dafbcb89e18ff84d/tint/visualization.py#L259
@kenkehoe @jrobrien91 Mentioned xmovie, which uses xarray and files to create movies, so this might be what would want to try and use if we are generating the plots using xarray: https://xmovie.readthedocs.io/en/latest/examples/quickstart.html
From a list of images, there is moviepy and cv2 we can try
@zssherman I think we need to look at the use cases here and @kenkehoe can provide more but I think the main ones are radar data and images from cameras. For the radar data use case, I think Py-ART would be more appropriate and that's where I would see xmovie come in. I think a utility to take a list of files and create a movie would be more suited for ACT at the moment. @zssherman I've also used ffmpeg in python and it was fairly straight forward.
https://pypi.org/project/ffmpeg-python/
@AdamTheisen I suggested xmovie for xarray datasets for xradar instead, and we can always utilize that. And if we wanted to use ffmpeg here, I can always write up something similar to what I did for TINT.
@AdamTheisen @zssherman an additional use case would be plotting particle imagery from the AAF if we wanted to go that route.
I agree with Adam that a utility to take images and string them together to form a movie is my original intention. We could provide an xarray option as well, but I don't have much experience with that. My concern is getting it to work consistently and understanding what is going wrong when it fails. It works more like a black box from my experience. Also, all our plotting calls are in matplotlib currently.
@kenkehoe @zssherman I just did a quick test with a library called MoviePy (https://zulko.github.io/moviepy/) and it was pretty easy to pass a list of images to it and create a mp4. Granted, in the end, this still runs off ffmpeg from the looks of it but if they can handle that and make it easy and straight forward to produce a movie could we consider this an optional dependency?
https://github.com/ARM-DOE/ACT/assets/13421074/287472cd-b71d-49a1-9617-5f0dc13725ae
import os
import moviepy.video.io.ImageSequenceClip
image_folder='./images'
fps=10
image_files = [os.path.join(image_folder,img)
for img in os.listdir(image_folder)
if img.startswith("barrow_arm_noaa_")]
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps=fps)
clip.write_videofile('./images/my_video.mp4')
Well that is very promising. Worth investigating. I will try to find some time to test this out.
@kenkehoe I've been testing this a little bit. Can you guarantee that the images you would want to pass in to the movie create be all the same size? I run into the following error when I try and do a random assortment of images. I think this is generally what we could expect but wanted to check.
File "/opt/anaconda3/lib/python3.11/site-packages/moviepy/video/io/ImageSequenceClip.py", line 91, in __init__
raise Exception("Moviepy: ImageSequenceClip requires all images to be the same size")
Exception: Moviepy: ImageSequenceClip requires all images to be the same size
Hmm, OK. Do you mean the same dimensional size? I think they will all be the same size. They will not be the same file size.
Yeah, same dimensions