ACT icon indicating copy to clipboard operation
ACT copied to clipboard

Function to create movies

Open kenkehoe opened this issue 2 years ago • 1 comments

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.

kenkehoe avatar May 27 '22 19:05 kenkehoe

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

zssherman avatar Jun 23 '22 19:06 zssherman

@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 avatar Feb 09 '23 19:02 zssherman

@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 avatar Feb 10 '23 19:02 AdamTheisen

@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.

zssherman avatar Feb 15 '23 15:02 zssherman

@AdamTheisen @zssherman an additional use case would be plotting particle imagery from the AAF if we wanted to go that route.

jrobrien91 avatar Feb 15 '23 16:02 jrobrien91

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 avatar Feb 15 '23 16:02 kenkehoe

@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')

AdamTheisen avatar Dec 15 '23 19:12 AdamTheisen

Well that is very promising. Worth investigating. I will try to find some time to test this out.

kenkehoe avatar Dec 15 '23 21:12 kenkehoe

@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

AdamTheisen avatar Dec 18 '23 20:12 AdamTheisen

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.

kenkehoe avatar Dec 18 '23 20:12 kenkehoe

Yeah, same dimensions

AdamTheisen avatar Dec 18 '23 21:12 AdamTheisen