gpt4 book ai didi

python - 为什么ffmpeg只能录制动画的前100帧?

转载 作者:行者123 更新时间:2023-12-04 10:37:03 28 4
gpt4 key购买 nike

我正在使用以下 Python 代码制作动画并希望通过 FFmpeg 将其保存为视频(在 PyCharm 中):

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

def func():
for j in range(1, len(t)):
time = j * 0.01
print('time:{:2}'.format(time))
yield time

def animate(data):
time = data
ax2.plot(time, time, **{'marker':'o'})
ax2.set_title('t = {:.2}'.format(time))
return ax2

def init():
ax2.plot(0, 0)
return ax2

dt = 0.01
t = np.arange(0, 50, dt)

fig2 = plt.figure()
ax2 = fig2.add_subplot(111, autoscale_on=True)
ax2.grid()

ani = animation.FuncAnimation(fig2, animate, func, interval=dt*1000, blit=False, init_func=init, repeat=False)

plt.rcParams['animation.ffmpeg_path'] = 'C:\Program Files\\ffmpeg\\bin\\ffmpeg.exe'
writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
ani.save("movie.mp4", writer=writer)

#plt.show()

但是当 time达到 1.0 ,进程停止但它应该在 time 时停止达到 50.0 .下图显示了进程停止时的情况。 The PyCharm Run console
然后我检查 movie.mp4发现视频在 time 时结束达到 1.0。
也就是说只有动画的前100帧被转换成 .mp4文件,所以我很困惑其余的帧去哪儿了?

我试图通过 windows cmd 运行代码,但得到了相同的结果。
然后我取消注释行 #plt.show()发现进程在 time时停止达到 50.0,动画可以正常显示,但仍然只转换了前 100 帧。

我现在对这个问题很困惑,不知道如何解决。感谢您的帮助。:)

最佳答案

我看起来像 FuncAnimation目前没有记录:

animation.py , 在类(class) FuncAnimation(TimedAnimation): ...

有一行代码:

    if self.save_count is None:
# If we're passed in and using the default, set save_count to 100.
self.save_count = 100

这就是 100帧来自。

根据文档, save_count是要缓存的帧数:

save_count : int, optional The number of values from frames to cache.



它要么是文档错误,要么是 animation.py 中的实现错误。 .

设置 save_count5000记录 5000 帧:
ani = animation.FuncAnimation(fig2, animate, func, save_count=5000, interval=dt*1000, blit=False, init_func=init, repeat=False)

  • 请注意,还缺少一个 \在您的代码中:'C:\\Program Files\\ffmpeg\\bin\\ffmpeg.exe'
  • 关于python - 为什么ffmpeg只能录制动画的前100帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60127671/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com