【问题标题】:Matplotlib animation error : Requested MovieWriter (ffmpeg) not available, while ffmpeg is installedMatplotlib 动画错误:请求的 MovieWriter (ffmpeg) 不可用,而 ffmpeg 已安装
【发布时间】:2020-10-23 06:26:07
【问题描述】:

我正在尝试使用 Python 对参数复杂函数的颜色映射表示进行动画处理。 我逐渐将一些东西放在一起,并检查它们是否正常工作。但我无法保存动画。

我遇到了这个错误:

Requested MovieWriter (ffmpeg) not available

但是,我的系统上确实安装了ffmpeg, 在 Windows 控制台上ffmpeg -version 返回有关 ffmpeg 的各种信息。另外,我还使用 pip pip install ffmpeg 在 Python 脚本目录中安装了 ffmpeg,这是成功的。我还在我的代码中设置了 ffmepg 路径:plt.rcParams['animation.ffmpeg_path'] = "C:\FFmpeg\bin\ffmpeg.exe"

我的想法已经用完了。

这是我的代码。 感谢您的阅读。

import numpy as np
import math
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
pi=math.pi
plt.rcParams['animation.ffmpeg_path'] = "C:\FFmpeg\bin\ffmpeg.exe"
fig = plt.figure()

def complex_array_to_rgb(X, theme='dark', rmax=None):
  absmax = rmax or np.abs(X).max()
  Y = np.zeros(X.shape + (3,), dtype='float')
  Y[..., 0] = np.angle(X) / (2 * pi) % 1
  if theme == 'light':
    Y[..., 1] = np.clip(np.abs(X) / absmax, 0, 1)
    Y[..., 2] = 1
  elif theme == 'dark':
    Y[..., 1] = 1
    Y[..., 2] = np.clip(np.abs(X) / absmax, 0, 1)
  Y = matplotlib.colors.hsv_to_rgb(Y)
  return Y

# Set up formatting for the movie files
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)

fps = 10
nSeconds = 1
snapshots = [ complex_array_to_rgb(np.array([[3*(x + 1j*y)**(2.9+p/300) + 1/(x + 1j*y)**2 for x in np.arange(-1,1,0.05)] for y in np.arange(-1,1,0.05)])) for p in range( nSeconds * fps ) ]

fig = plt.figure( figsize=(3,3) )

Z2=snapshots[0]
im=plt.imshow(Z2, extent=(-1,1,-1,1))

def animate_func(i):
    if i % fps == 0:
        print( '.')

    im.set_array(snapshots[i])
    return [im]
    
anim = animation.FuncAnimation(
                               fig, 
                               animate_func, 
                               frames = nSeconds * fps,
                               interval = 1000 / fps, # in ms
                               )

anim.save('test_anim.mp4', writer=writer)

【问题讨论】:

    标签: python matplotlib animation ffmpeg


    【解决方案1】:

    Python 在字符串中使用反斜杠来表示转义字符,因此这些会影响您的文件路径。尝试使用任一

    plt.rcParams['animation.ffmpeg_path'] = "C:/FFmpeg/bin/ffmpeg"
    

    或者,有点混乱

    plt.rcParams['animation.ffmpeg_path'] = "C:\\FFmpeg\\bin\\ffmpeg"
    

    如果这也不起作用,您可以尝试直接使用 ffmpeg writer 类:

    FFwriter = animation.FFMpegWriter()
    

    【讨论】:

      【解决方案2】:

      使用 Windows10 和 JupyterLab

      我之前做过一些动画,效果很好......但几天前我使用了相同的笔记本/电脑,它失败了,给出了错误

      RuntimeError: Requested MovieWriter (ffmpeg) not available
      

      而且肯定有 FFmpeg!搜索了一下,发现不止一个,关联其他软件(OBS、Shotcut等)。

      在尝试了很多东西之后,唯一有效的方法是下载一个构建(https://www.gyan.dev/ffmpeg/builds/)并在笔记本中添加这样的地址(包括“ffmpeg.exe”)

      plt.rcParams['animation.ffmpeg_path'] = r'c:\FFMPEG\ffmpeg-2021-10-14-git-c336c7a9d7-full_build\bin\ffmpeg.exe'
      

      【讨论】:

        猜你喜欢
        • 2020-10-01
        • 1970-01-01
        • 2019-08-14
        • 2021-01-29
        • 2020-05-18
        • 2015-08-28
        • 2020-11-22
        • 2015-09-20
        • 2019-11-22
        相关资源
        最近更新 更多