有关animation,ffmpeg问题解决:RuntimeError: Passing in values for arguments fps, codec ...

版权声明:如需转载或引用,请注明出处。 https://blog.csdn.net/weixin_39278265/article/details/84147458

前言

本文旨在解决Python下使用ffmpeg将FuncAnimation画出来的动图保存为视频时出现的相关错误。

1 环境

windows 10
anaconda
spyder
已经安装好的ffmpeg(安装教程参考: https://blog.csdn.net/weixin_39278265/article/details/84102556#6_anaconda_ffmpeg_177)

2 问题代码(运行实例)

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
plt.rcParams['animation.ffmpeg_path'] = '/opt/local/bin'

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init,
                           frames=200, interval=20, blit=True)

FFwriter = animation.FFMpegWriter()
anim.save('basic_animation.mp4', writer = FFwriter,fps=20, extra_args=['-vcodec', 'libx264'])

输出结果:
在这里插入图片描述

动画确实能够输出,但是!!!保存视频失败,报错信息为:


File "D:\software\Anaconda\lib\site-packages\matplotlib\animation.py", line 1114, in save
    raise RuntimeError('Passing in values for arguments '
    
RuntimeError: Passing in values for arguments fps, codec, bitrate, extra_args, or metadata is not supported when writer is an existing MovieWriter instance. These should instead be passed as arguments when creating the MovieWriter instance.

3 解决方案

3.1

主要参考 [1]
其实,根据报错信息也可以了解到,fps, codec, bitrate, extra_args, or metadataFFwriter = animation.FFMpegWriter() 这个FFMpegWriter初始化的时候就应该赋值(不用赋全部,赋一部分就行,不能全为None)

所以修改最后两行代码为:

FFwriter = animation.FFMpegWriter(fps=20, extra_args=['-vcodec', 'libx264'])
anim.save('basic_animation.mp4', writer = FFwriter)

3.2

然而,真正尴尬的事情来了:
之前那个错误确实没了,但是spyder再次报错:


  File "D:\software\Anaconda\lib\subprocess.py", line 756, in __init__
    restore_signals, start_new_session)

  File "D:\software\Anaconda\lib\subprocess.py", line 1155, in _execute_child
    startupinfo)

FileNotFoundError: [WinError 2] 系统找不到指定的文件。

心里非常之不解。然后中间经过漫长的调试............................................................................................................................................

确实学会了一些调试技巧,但是真的浪费时间。

最后,无奈之下只能用搜索引擎搜索(其实之前也搜过,但是真的找不到合适的解决方法),
这次,我换了一个关键字,因为之前搜FileNotFoundError: [WinError 2] 系统找不到指定的文件。
根本找不到好结果。

这次,我直接搜:animation.FFMpegWriter
非常nice,先后受 [2][3] 的启发,发现原来是**自己设置的ffmpeg目录不对!!!**

我通过电脑左下角搜索栏搜索ffmpeg,右键打开文件所在位置,发现其在:
D:\software\Anaconda\Library\bin\ffmpeg.exe

在这里插入图片描述

所以,我将代码
plt.rcParams['animation.ffmpeg_path'] = '/opt/local/bin'
改成:
plt.rcParams['animation.ffmpeg_path'] = 'D:\\software\\Anaconda\\Library\\bin\\ffmpeg.exe'

这样就没有问题了!

成功输出视频文件:
在这里插入图片描述

感谢

前后花了1个小时吧。写文章又用了半个小时。
问题主要在于:
1)我对ffmpeg不太了解,不知道要提前设置这个路径。
2)对animation也不太了解

感悟:
1)感觉调试技术有所进步(主要是之前用的真的很少,没怎么debug过)
2)相信搜索引擎。。。原来我犯过的错误,真的世界上确实有人犯过。。。
所以只要会用恰当的关键词搜索,基本上都能够搜到答案。

参考文献

[1] FFMPEG file writer in python 2.7. https://stackoverflow.com/questions/43251965/ffmpeg-file-writer-in-python-2-7

[2] Cannot save matplotlib animation with ffmpeg. https://stackoverflow.com/questions/23074484/cannot-save-matplotlib-animation-with-ffmpeg

[3] Can’t save matplotlib animation. https://stackoverflow.com/questions/23856990/cant-save-matplotlib-animation

猜你喜欢

转载自blog.csdn.net/weixin_39278265/article/details/84147458
FPS
今日推荐