Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Let MovieFileWriter save temp files in a new dir #11860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

fredrik-1
Copy link
Contributor

Another try to get the animation module to work better. The reason for these changes are that they make it possible to create videos of animations with a low frame rate to work in video players like the VLC player. This can be done by changing the output frame rate to a higher value by for example using extra_args=['-r', '25'] or extra_args=['-vf', 'fps=25'].

The changes in this pr:

  • deleted -vframes in ffmpeg file writer because it is usually unnecessary and make it impossible to change the output frame rate.
  • made the MovieFileWriter save the temporary files in a new directory to avoid the possible problem that the writer reads old files.
  • made it possible to not have anything except extra_args after the -i temp_files by not using a codec if codec=''. This is not really necessary but gives more freedom and might be useful for someone.

@@ -542,6 +549,9 @@ def cleanup(self):
self._temp_names)
for fname in self._temp_names:
os.remove(fname)
folder = os.path.split(fname)[0]
if folder is not '':
shutil.rmtree(folder, ignore_errors=True)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it dangerous to simply delete a (possibly non-empty) folder from the user's disk? I guess we don't want to have people saying "Matplotlib sucks, it just deleted all my data without warning".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. That code is definitely dangerous. I have changed to os.rmdir now.

One problem is that I believe that os.rmdir sometimes don't remove empty directory's on windows for some reason. It seems to work for me now and it is possible that it doesn't happen that often and never in this application.

@fredrik-1 fredrik-1 force-pushed the frame_dir branch 3 times, most recently from 22a9d12 to 380104a Compare August 16, 2018 10:10
Copy link
Contributor

@dopplershift dopplershift left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, putting the frames in a directory is a good idea and something I should have done awhile ago.

Only a few minor changes.

'''
self.fig = fig
self.outfile = outfile
if dpi is None:
dpi = self.fig.dpi
self.dpi = dpi
self._adjust_frame_size()
root, _ = os.path.splitext(outfile)
if frame_dir is None:
for i in itertools.count():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think tempfile.mkdtemp would be more straightforward here.

folder = os.path.split(fname)[0]
if folder is not '':
try:
os.rmdir(folder)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should really only delete the directory if we created it. Right now the user could pass us an existing directory, and this code would remove it when done.

@@ -597,7 +613,10 @@ class FFMpegBase(object):

@property
def output_args(self):
args = ['-vcodec', self.codec]
if self.codec is not '':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be better as:

args = []
if self.codec:
    args.extend(['-vcodec', self.codec])


frame_dir : str or None, optional
Directory where the temporary files are saved. None means that a
new directory is created and '' means that no temporary directory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's necessary to document '' as an option; given that we're eliminating vframes below, I'm not sure we want people doing it.

@@ -665,8 +684,7 @@ def _args(self):
# Returns the command line parameters for subprocess to use
# ffmpeg to create a movie using a collection of temp images
return [self.bin_path(), '-r', str(self.fps),
'-i', self._base_temp_name(),
'-vframes', str(self._frame_counter)] + self.output_args
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eliminating vframes is only ok because we're putting frames in a temp directory by default, IMO. The original intent behind adding vframes was to handle a problem where users got stale frames in their animation if they changed the number of frames.

@jklymak jklymak added this to the v3.1 milestone Aug 16, 2018
@fredrik-1
Copy link
Contributor Author

Made the suggested changes.

@anntzer
Copy link
Contributor

anntzer commented Sep 17, 2018

See also #3536 for what was one of my first PRs to matplotlib :p
I think nowadays I'd suggest using TemporaryDirectory instead, it's more robust wrt. interpreter shutdown.

@jklymak
Copy link
Member

jklymak commented Feb 27, 2019

@fredrik-1 did you want to proceed w/ this? We are trying to tag 3.1 soonish...

@jklymak jklymak modified the milestones: v3.1.0, v3.2.0 Mar 3, 2019
@jklymak jklymak added the stale label Mar 3, 2019
@jklymak
Copy link
Member

jklymak commented Jul 16, 2019

Thanks a lot for the contribution. I'm going to close this as having had no action for quite a while. OTOH, if there is still a community that wants this, please feel free to re-open, with my apologies!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.