Enregistrer une animation matplolib en format .mp4

Résolu/Fermé
maxime.f - 3 mars 2020 à 21:05
 maxime.f - 3 mars 2020 à 23:18
Bonjour à tous !

J'ai pour projet de réaliser diverses animations et je souhaiterais les enregistrer en format .mp4

Voici donc un code test pour m’entraîner à enregistrer

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation


plt.rcParams['animation.ffmpeg_path'] = r'C:\ffmpeg\bin\ffmpeg.exe'

TWOPI = 2*np.pi

fig, ax = plt.subplots()

t = np.arange(0.0, TWOPI, 0.001)
s = np.sin(t)
l = plt.plot(t, s)

ax = plt.axis([0,TWOPI,-1,1])

redDot, = plt.plot([0], [np.sin(0)], 'ro')

def animate(i):
    redDot.set_data(i, np.sin(i))
    return redDot,


myAnimation = animation.FuncAnimation(fig, animate, frames=np.arange(0.0, TWOPI, 0.1), \
                                      interval=10, blit=True, repeat=False)

#plt.show()


DPI=90
writer = animation.FFMpegWriter(fps=30, bitrate=5000)
myAnimation.save("test1", writer = writer, dpi=DPI) 


L'animation marche très bien sans la partie #enregistrement, mais quand je l’exécute avec il y a le message d'erreur suivant que je n'arrive vraiment pas à comprendre :
"
Traceback (most recent call last):
File "C:\Users\Victor SECHAUD\Desktop\test_enregistrement.py", line 33, in <module>
myAnimation.save("test1", writer = writer, dpi=DPI)
File "c:\users\victor sechaud\appdata\local\programs\python\python37-32\lib\site-packages\matplotlib\animation.py", line 1174, in save
writer.grab_frame(**savefig_kwargs)
File "c:\users\victor sechaud\appdata\local\programs\python\python37-32\lib\site-packages\matplotlib\animation.py", line 383, in grab_frame
'DEBUG.'.format(e, out, err))
OSError: Error saving animation to file (cause: [Errno 22] Invalid argument) Stdout: b'' StdError: b''. It may help to re-run with logging level set to DEBUG."

Donc si quelqu'un arrivait à m'expliquer la cause de ce message et comment corriger cela je vous en serais très reconnaissant !

PS : le format .mp4 n'est pas obligatoire, juste un format que je puisse lire avec mon ordinateur.

Merci d'avance !


Configuration: Windows / Chrome 80.0.3987.122
A voir également:

1 réponse

quent217 Messages postés 421 Date d'inscription vendredi 25 septembre 2015 Statut Membre Dernière intervention 1 mars 2024 344
3 mars 2020 à 22:40
Bonjour,
J'ai réussi à faire fonctionner votre code en ajoutant l'extention dans le nom du fichier de sortie : "test1.mp4"

Sinon je pense que vous pourrez obtenir plus de détails sur l'erreur en ajoutant cette ligne au début :
plt.set_loglevel("debug")
1
Ah oui en effet ça marche mieux ! Merci beaucoup de m'avoir débloqué !
Je vais aller tester la ligne que vous m'avez donner pour essayer de mieux comprendre ce qu'il s'est passé.
0