How to set ffmpeg for matplotlib in mac os x
Asked Answered
R

4

12

I want to animate some plots with matplotlib. The version I have is the Enthough Canopy distribution (Version: 1.1.0.1371), running in a mac os x 10.8 (Mountain Lion). I have used the FuncAnimation routine from the animation package of matplotlib. My problem comes saving the animation. I want to save in mp4 format:

anim.save('test.mp4',fps=30)

The error I get is:

UserWarning: MovieWriter ffmpeg unavailable 
warnings.warn("MovieWriter %s unavailable" % writer)

So I installed ffmpeg via Macports. But I am still having the same error. Do you know how to setup matplotlib in order to recognise ffmpeg? Do I have to change the matplotlibrc file? Thanks.

EDIT: I have realized that I can manually put '/opt/local/bin' in the PYTHONPATH, but it does not change the PATH in Enthough Canopy. Do anyone know how to change the PATH in canopy?

Rahmann answered 16/9, 2013 at 17:17 Comment(4)
Is ffmpeg in path? If you type ffmpeg in console, does it runs?Subtractive
Yes, it runs. I double-checked that the PATH is OK and the program runs.Rahmann
Regarding changing PATH and other env vars on OSX: support.enthought.com/entries/…Embed
Thank you Jonathan. I saw your post, and I have tried that. The strange thing is that the python from my terminal load the full path, whereas the python at the editor in canopy only loads a shorter path. Why? I would like to know where the canopy editor loads the variable os.environ['PATH']. What is the difference between run ipython from the terminal and run it from the canopy editor?Rahmann
P
7

I had the same problem. My solution was very simple.

Download the binary from here.

Then do

sudo mv ~/Downloads/ffmpeg /usr/bin/
Psychotechnics answered 4/2, 2014 at 11:36 Comment(1)
If you're going to go this route, I would strongly suggest adding ffmeg to your usr/local/bin. It's general good unix/linux practice to not modify your usr/bin directory.Sanorasans
F
4

I had success when installing with homebrew: brew install ffmpeg

After that set up the FFMpegWriter yourself by:

mywriter = animation.FFMpegWriter()
anim.save('mymovie.mp4',writer=mywriter)
Florella answered 29/9, 2018 at 5:42 Comment(0)
I
2

I think the solution can be found in the workaround in this and this post.

It seems that the path of the shell is not loaded by matplotlib, and since macports are installed in /opt/local/bin, ffmpeg can't be found.

Either go for the hack described above, try making a symlink in /bin for ffmpeg, or try adding the path to ffmpeg to the python path as suggested in the comments of the second link

Illuminism answered 16/9, 2013 at 23:4 Comment(2)
Thank you for the answer. I have tried the solution of the second post, and include a launch.conf file. It changed the PYTHONPATH variable, but when I type os.environ['PATH'],the path /opt7local/bin is not there. So I get the same error.Rahmann
which launch.conf are you talking about? try something like sudo ln -s /opt/local/bin/ffmpeg /usr/bin/ffmpegIlluminism
D
0

If you are agnostic to the movie file type, you could use the Python cv2 library to write .avi movie files from a folder of the images you wish to combine into a movie. Here's an example:

import cv2 

# Create avi movie from static plots created of each time slice
image_folder = 'path_to_png_files'
video_name = '/mov-{}.avi'.format('optional label')

# Store individual frames into list
images = [img for img in os.listdir(image_folder) if img.endswith(".png")]

# Create frame dimension and store its shape dimensions
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape

# cv2's VideoWriter object will create a frame 
video = cv2.VideoWriter(avi_path + video_name, 0, 1, (width,height))

# Create the video from individual images using for loop
for image in images:
    video.write(cv2.imread(os.path.join(image_folder, image)))

# Close all the frames
cv2.destroyAllWindows()

# Release the video write object
video.release()
Decapod answered 1/7, 2023 at 19:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.