Python 未知文件扩展名 .mp4

mar*_*e23 7 python ffmpeg matplotlib

我正在开展一个模拟项目,但是我无法创建视觉效果。我首先运行 matplotlib 文档中的代码(即该代码不属于我)。\n当我运行代码时,收到错误“未知文件扩展名:.mp4”。\n我已经安装了 ffmpeg 并检查它是否是更新版本。

\n\n

我使用的是 Windows 计算机和 Python 3。

\n\n
import numpy as np\nfrom matplotlib import pyplot as plt\nfrom matplotlib import animation\nplt.rcParams[\'animation.ffmpeg_path\']=\'\xe2\x80\xaaC:\\\\FFmpeg\\bin\\ffmpeg.exe\'\n\n# First set up the figure, the axis, and the plot element we want to animate\nfig = plt.figure()\nax = plt.axes(xlim=(0, 2), ylim=(-2, 2))\nline, = ax.plot([], [], lw=2)\n\n# initialization function: plot the background of each frame\ndef init():\n    line.set_data([], [])\n    return line,\n\n# animation function.  This is called sequentially\ndef animate(i):\n    x = np.linspace(0, 2, 1000)\n    y = np.sin(2 * np.pi * (x - 0.01 * i))\n    line.set_data(x, y)\n    return line,\n\n# call the animator.  blit=True means only re-draw the parts that have changed.\nanim = animation.FuncAnimation(fig, animate, init_func=init,\n                               frames=200, interval=20, blit=True)\n\n# save the animation as an mp4.  This requires ffmpeg or mencoder to be\n# installed.  The extra_args ensure that the x264 codec is used, so that\n# the video can be embedded in html5. \nanim.save(\'basic_animation.mp4\', fps=30, extra_args=[\'-vcodec\', \'libx264\'])\n\nplt.show()\n
Run Code Online (Sandbox Code Playgroud)\n

小智 9

writergif = animation.PillowWriter(fps=30)
anim.save('filename.gif',writer=writergif)
Run Code Online (Sandbox Code Playgroud)

这些代码行对我有用,从https://holypython.com/how-to-save-matplotlib-animations-the-ultimate-guide/获取它们