IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

Enregistrer une animation matplotlib en format .mp4


Sujet :

Python

  1. #1
    Nouveau candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2020
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2020
    Messages : 2
    Par défaut Enregistrer une animation matplotlib en format .mp4
    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 :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
     
    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 :

    Nom : message_erreur.png
Affichages : 1653
Taille : 17,9 Ko

    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 !

  2. #2
    Membre à l'essai

    Femme Profil pro
    Ingénieur validation
    Inscrit en
    Décembre 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 50
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur validation

    Informations forums :
    Inscription : Décembre 2015
    Messages : 5
    Billets dans le blog
    1
    Par défaut sauvegarde animation
    bonjour

    as tu resolu ton probleme ? ca m'intéresse je veux faire la meme chose !!

    merci

    sg

  3. #3
    Nouveau candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2020
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2020
    Messages : 2
    Par défaut
    Bonjour,
    Il me semble que j'avais réussi à faire fonctionner le code en ajoutant l'extension dans le nom du fichier de sortie : "test1.mp4"

  4. #4
    Membre habitué
    Homme Profil pro
    CPGE - PC
    Inscrit en
    Avril 2021
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : CPGE - PC

    Informations forums :
    Inscription : Avril 2021
    Messages : 10
    Par défaut
    Je rencontre le même problème...
    As-tu résolu ton problème ?

  5. #5
    Expert confirmé
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 198
    Par défaut
    hello,
    Citation Envoyé par Houthakker Voir le message
    Je rencontre le même problème...
    As-tu résolu ton problème ?
    avec le code à Maxime.f et sa remarque sur l'extension mp4 à mettre, son code fonctionne chez moi. Quel est ton problème, ton message d'erreur, ton code ?
    Ami calmant, J.P

  6. #6
    Membre habitué
    Homme Profil pro
    CPGE - PC
    Inscrit en
    Avril 2021
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : CPGE - PC

    Informations forums :
    Inscription : Avril 2021
    Messages : 10
    Par défaut Mon problème
    Citation Envoyé par jurassic pork Voir le message
    hello,

    avec le code à Maxime.f et sa remarque sur l'extension mp4 à mettre, son code fonctionne chez moi. Quel est ton problème, ton message d'erreur, ton code ?
    Ami calmant, J.P
    Voici mon programme :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
     
    import math
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    from mpl_toolkits.mplot3d import Axes3D
     
    fig = plt.figure(figsize=(8,8))
    ax = fig.add_subplot(111, projection="3d")
    def init():
        k=300
        Z = [i for i in range(k)]
        X = [math.cos(i/5)*(k-i) for i in range(k)]
        Y = [math.sin(i/5)*(k-i) for i in range(k)]
        ax.scatter(X,Y,Z, c="green", marker="^")
        step = 3
        c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
        Z = [i for i in range(1,k,step)]
        X = [math.cos(i/5+2)*(k-i+10) for i in range(1,k,step)]
        Y = [math.sin(i/5+2)*(k-i+10) for i in range(1,k,step)]
        ax.scatter(X,Y,Z, c=c, marker="o",s=40)
        plt.xlim(-500,500)
        plt.ylim(-500,500)
        return fig,
    def animate(f):
        fig.clear()
        ax = fig.add_subplot(111, projection="3d")
        k=300
        Z = [i for i in range(k)]
        X = [math.cos(i/5+f/10)*(k-i) for i in range(k)]
        Y = [math.sin(i/5+f/10)*(k-i) for i in range(k)]
        ax.scatter(X,Y,Z, c="green", marker="^")
        step = 3
        c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
        Z = [i for i in range(1,k,step)]
        X = [math.cos(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
        Y = [math.sin(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
        ax.scatter(X,Y,Z, c="red", marker="o",s=40)
        plt.xlim(-500,500)
        plt.ylim(-500,500)
        return fig,
    ani=animation.FuncAnimation(fig, animate, init_func=init, frames=90,
                                       interval=50, blit=False)
     
    DPI=90
    writer = animation.FFMpegWriter(fps=30, bitrate=5000)
    ani.save("test.mp4", writer = writer, dpi=DPI)
    et voici mon message d'erreur :

    Nom : Capture d’écran 2022-01-05 221548.png
Affichages : 1158
Taille : 63,3 Ko

  7. #7
    Expert confirmé
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 198
    Par défaut
    hello,
    pour pouvoir utiliser le FFMpegWriter de animation il faut que FFMpeg soit installé dans le système d'exploitation et il faut indiquer à pyplot le chemin de l'exécutable (ffmpeg.exe).
    Exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    import matplotlib.pyplot as plt
     
     
    plt.rcParams['animation.ffmpeg_path'] = r'C:\ffmpeg\bin\ffmpeg.exe'
    Ami calmant, J.P

  8. #8
    Membre habitué
    Homme Profil pro
    CPGE - PC
    Inscrit en
    Avril 2021
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : CPGE - PC

    Informations forums :
    Inscription : Avril 2021
    Messages : 10
    Par défaut
    Citation Envoyé par jurassic pork Voir le message
    hello,
    pour pouvoir utiliser le FFMpegWriter de animation il faut que FFMpeg soit installé dans le système d'exploitation et il faut indiquer à pyplot le chemin de l'exécutable (ffmpeg.exe).
    Exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    import matplotlib.pyplot as plt
     
     
    plt.rcParams['animation.ffmpeg_path'] = r'C:\ffmpeg\bin\ffmpeg.exe'
    Ami calmant, J.P
    J'ai toujours un problème... Pourquoi y a-t-il un r dans l'exemple ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    import math
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    from mpl_toolkits.mplot3d import Axes3D
     
    plt.rcParams['animation.ffmpeg_path'] = 'c:\users\victor\appdata\local\programs\python\python39\lib\site-packages\ffmpeg.exe'
     
    fig = plt.figure(figsize=(8,8))
    ax = fig.add_subplot(111, projection="3d")
    def init():
        k=300
        Z = [i for i in range(k)]
        X = [math.cos(i/5)*(k-i) for i in range(k)]
        Y = [math.sin(i/5)*(k-i) for i in range(k)]
        ax.scatter(X,Y,Z, c="green", marker="^")
        step = 3
        c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
        Z = [i for i in range(1,k,step)]
        X = [math.cos(i/5+2)*(k-i+10) for i in range(1,k,step)]
        Y = [math.sin(i/5+2)*(k-i+10) for i in range(1,k,step)]
        ax.scatter(X,Y,Z, c=c, marker="o",s=40)
        plt.xlim(-500,500)
        plt.ylim(-500,500)
        return fig,
    def animate(f):
        fig.clear()
        ax = fig.add_subplot(111, projection="3d")
        k=300
        Z = [i for i in range(k)]
        X = [math.cos(i/5+f/10)*(k-i) for i in range(k)]
        Y = [math.sin(i/5+f/10)*(k-i) for i in range(k)]
        ax.scatter(X,Y,Z, c="green", marker="^")
        step = 3
        c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
        Z = [i for i in range(1,k,step)]
        X = [math.cos(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
        Y = [math.sin(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
        ax.scatter(X,Y,Z, c="red", marker="o",s=40)
        plt.xlim(-500,500)
        plt.ylim(-500,500)
        return fig,
    ani=animation.FuncAnimation(fig, animate, init_func=init, frames=90,
                                       interval=50, blit=False)
     
    DPI=90
    writer = animation.FFMpegWriter(fps=30, bitrate=5000)
    ani.save("test.mp4", writer = writer, dpi=DPI)
    Et voici le nouveau message d'erreur :
    Nom : Capture d’écran 2022-01-08 165149.png
Affichages : 1084
Taille : 26,4 Ko

    Amic allemand

  9. #9
    Expert confirmé
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 198
    Par défaut
    hello,
    En Python, les chaînes brutes sont utilisées pour renvoyer une chaîne lorsqu’elle n’est pas du tout traitée. Cela signifie que si une chaîne est préfixée par un r ou une raw string et que cette chaîne se compose de tout caractère d’échappement invalide comme \u, alors une erreur ne se produira pas.
    Fait comme essai de code :
    et
    tu comprendras l'utilité du r

    Ami calmant, J.P

  10. #10
    Membre habitué
    Homme Profil pro
    CPGE - PC
    Inscrit en
    Avril 2021
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : CPGE - PC

    Informations forums :
    Inscription : Avril 2021
    Messages : 10
    Par défaut
    Ah oui je vois, merci :-)
    Par contre je n'arrive pas à trouver l'emplacement du fichier ffmpeg dans mon arborescence... Où devrait-il se trouver ?

  11. #11
    Expert confirmé
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 198
    Par défaut
    hello,
    tu as fait un pip install ffmpeg mais cela n'installe pas les binaires de ffmpeg.
    Il faut aller sur le site de ffmpeg et télécharger les binaires pour son O.S et les installer sur son ordinateur :

    2) Installing ffmpeg (if needed)
    This is a common issue for many people, sometimes they don’t have the ffmpeg packages on their computer and this causes problem when trying to generate a video file with Python or its libraries.


    Installing ffmpeg is simple: Just navigate to the official download page here . Make sure you chose the appropriate OS for your computer (Linux-Windows-Mac available). Once you extract the contents or installed the package you will have an executable ffmpeg.exe file in the following path: ffmpeg\bin\ffmpeg.exe


    In most situations pointing to this path with following command will solve the problem.


    Pointing ffmpeg_path to the correct ffmpeg executable file (if needed)


    Include the code below somewhere right after importing the matplotlib library as such:


    make sure you type the right path on your computer.
    also make sure the path is pointing to the executable file and not just to the folder.
    Example:
    import matplotlib as mpl
    mpl.rcParams['animation.ffmpeg_path'] = r'C:\\Users\\xx\\Desktop\\ffmpeg\\bin\\ffmpeg.exe'

    Ami calmant, J.P

  12. #12
    Membre habitué
    Homme Profil pro
    CPGE - PC
    Inscrit en
    Avril 2021
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 22
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : CPGE - PC

    Informations forums :
    Inscription : Avril 2021
    Messages : 10
    Par défaut
    Merci beaucoup, problème résolu !!

    à mi-calmant, Houthak

Discussions similaires

  1. Enregistrer une animation image par image
    Par aouache dans le forum GLC_lib
    Réponses: 1
    Dernier message: 07/02/2014, 22h39
  2. Réponses: 3
    Dernier message: 25/05/2007, 22h17
  3. Enregistrer une animation pour le web
    Par paradeofphp dans le forum Flash
    Réponses: 1
    Dernier message: 01/08/2006, 14h30
  4. [VB.NET] Enregistrer une BDD sous un autre format
    Par botanique dans le forum Windows Forms
    Réponses: 9
    Dernier message: 16/01/2006, 14h42
  5. [GLScene] Comment enregistrer une image au format TGA
    Par Invité dans le forum API, COM et SDKs
    Réponses: 1
    Dernier message: 27/05/2005, 15h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo