I’m looking for some tips on how to best work with
.mov files in python (though I imagine what I’m looking for would also apply to other compressed movie files too).
One thing I’m trying to do is interactively visualize them in napari, but I might also be interested in doing some processing on them too, possibly with dask in a lazy fashion.
Right now I currently have
.mov files that are ~5000 time points, ~1200x700 and rgb. The total size as a
.mov file is ~60 MB which is very reasonable due to it’s compression - thanks @lambdaloop for this example movie
I can load these files into python and get a numpy array using
ffmpeg as follows
import imageio import numpy as np vid = imageio.get_reader(path, 'ffmpeg') movie = np.array([im for im in vid.iter_data()], dtype=np.uint8)
For the work I am doing though I find this a little slow (curious if there are better approaches), and the numpy array that is generated is quite large numpy with
12.6e9 elements. If I have longer movies this will become in-practical.
I don’t really want to work with the generator or the object called
vid above, I’d really prefer something array-like, which makes trying to use
dask seem like a good choice.
I think ideally I’d probably like to have a lazy loading scheme of the sort used in
dask-image, see these blog posts https://blog.dask.org/2019/06/20/load-image-data and https://napari.org/tutorials/applications/dask for example, but
imread doesn’t natively support reading the
.mov file (and maybe I should be posting some of this question on the dask-image GitHub, but I’m trying here first), so I tried writing my own lazy style reader
import imageio from dask import delayed import dask.array as da vid = imageio.get_reader(path, 'ffmpeg') shape = vid.get_meta_data()['size'][::-1] + (3,) lazy_imread = delayed(vid.get_data) movie = da.stack([da.from_delayed(lazy_imread(i), shape=shape, dtype=np.uint8) for i in range(vid.count_frames())])
But I find this very slow too. It could be because I’ve done the dask delayed poorly, it could be for other reasons related to how ffmpeg works. Has anyone else tried this / succeeded here? Maybe @talley has ideas