为了以MP3格式从Matplotlib中保存动画视频,我需要什么?

发布于 2025-02-10 23:57:13 字数 3691 浏览 4 评论 0原文

我正在Linux Mint 19.3上使用Python3.8,并且正在尝试保存Matplotlib中蜂窝自动机模型创建的动画。我对该模型的实际代码是私有的,但是它使用相同的代码来保存动画与下面所示的代码,这是对官方Matplotlib文档中显示的一个示例之一的稍作修改:

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

fig, ax = plt.subplots()


def f(x, y):
    return np.sin(x) + np.cos(y)

x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)

fig, ax = plt.subplots()


    
ims = []
for i in range(60):
    x += np.pi / 15.
    y += np.pi / 20.
    im = ax.imshow(f(x, y), animated=True)
    if i == 0:
        ax.imshow(f(x, y))  # show an initial one first
    ims.append([im])

ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
                                    repeat_delay=1000)

    # To save the animation, use e.g.
    #
    # ani.save("movie.mp4")
    #
    # or
    #
writer = animation.FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
ani.save("movie.mp3", writer=writer)

执行后,代码会产生此错误:

    MovieWriter stderr:
    Output file #0 does not contain any stream

    Traceback (most recent call last):
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 234, in saving
        yield self
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 1093, in save
        writer.grab_frame(**savefig_kwargs)
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 351, in grab_frame
        self.fig.savefig(self._proc.stdin, format=self.frame_format,
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/figure.py", line 3046, in savefig
        self.canvas.print_figure(fname, **kwargs)
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/backend_bases.py", line 2319, in print_figure
        result = print_method(
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/backend_bases.py", line 1648, in wrapper
        return func(*args, **kwargs)
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/_api/deprecation.py", line 415, in wrapper
        return func(*inner_args, **inner_kwargs)
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/backends/backend_agg.py", line 486, in print_raw
        fh.write(renderer.buffer_rgba())
    BrokenPipeError: [Errno 32] Broken pipe

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/home/justin/animation_test.py", line 36, in <module>
        ani.save("movie.mp3", writer=writer)
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 1093, in save
        writer.grab_frame(**savefig_kwargs)
      File "/usr/lib/python3.8/contextlib.py", line 131, in __exit__
        self.gen.throw(type, value, traceback)
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 236, in saving
        self.finish()
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 342, in finish
        self._cleanup()  # Inline _cleanup() once cleanup() is removed.
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 373, in _cleanup
        raise subprocess.CalledProcessError(
    subprocess.CalledProcessError: Command '['ffmpeg', '-f', 'rawvideo', '-vcodec', 'rawvideo', '-s', '640x480', '-pix_fmt', 'rgba', '-r', '15', '-loglevel', 'error', '-i', 'pipe:', '-vcodec', 'h264', '-pix_fmt', 'yuv420p', '-b', '1800k', '-metadata', 'artist=Me', '-y', 'movie.mp3']' returned non-zero exit status 1.

我已经查看了有关Matplotlib动画的类似查询的帖子,但是没有一个专门包括错误输出文件#0不包含任何流。我在FFMPEG方面几乎没有经验,所以我想知道会缺少什么。

I am using python3.8 on Linux Mint 19.3, and I am trying to save an animation created by a cellular automata model in matplotlib. My actual code for the model is private, but it uses the same code for saving the animation as the code shown below, which is a slight modification of one of the examples shown in the official matplotlib documentation:

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

fig, ax = plt.subplots()


def f(x, y):
    return np.sin(x) + np.cos(y)

x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)

fig, ax = plt.subplots()


    
ims = []
for i in range(60):
    x += np.pi / 15.
    y += np.pi / 20.
    im = ax.imshow(f(x, y), animated=True)
    if i == 0:
        ax.imshow(f(x, y))  # show an initial one first
    ims.append([im])

ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
                                    repeat_delay=1000)

    # To save the animation, use e.g.
    #
    # ani.save("movie.mp4")
    #
    # or
    #
writer = animation.FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
ani.save("movie.mp3", writer=writer)

When executed, the code produces this error:

    MovieWriter stderr:
    Output file #0 does not contain any stream

    Traceback (most recent call last):
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 234, in saving
        yield self
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 1093, in save
        writer.grab_frame(**savefig_kwargs)
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 351, in grab_frame
        self.fig.savefig(self._proc.stdin, format=self.frame_format,
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/figure.py", line 3046, in savefig
        self.canvas.print_figure(fname, **kwargs)
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/backend_bases.py", line 2319, in print_figure
        result = print_method(
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/backend_bases.py", line 1648, in wrapper
        return func(*args, **kwargs)
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/_api/deprecation.py", line 415, in wrapper
        return func(*inner_args, **inner_kwargs)
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/backends/backend_agg.py", line 486, in print_raw
        fh.write(renderer.buffer_rgba())
    BrokenPipeError: [Errno 32] Broken pipe

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/home/justin/animation_test.py", line 36, in <module>
        ani.save("movie.mp3", writer=writer)
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 1093, in save
        writer.grab_frame(**savefig_kwargs)
      File "/usr/lib/python3.8/contextlib.py", line 131, in __exit__
        self.gen.throw(type, value, traceback)
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 236, in saving
        self.finish()
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 342, in finish
        self._cleanup()  # Inline _cleanup() once cleanup() is removed.
      File "/usr/local/lib/python3.8/dist-packages/matplotlib/animation.py", line 373, in _cleanup
        raise subprocess.CalledProcessError(
    subprocess.CalledProcessError: Command '['ffmpeg', '-f', 'rawvideo', '-vcodec', 'rawvideo', '-s', '640x480', '-pix_fmt', 'rgba', '-r', '15', '-loglevel', 'error', '-i', 'pipe:', '-vcodec', 'h264', '-pix_fmt', 'yuv420p', '-b', '1800k', '-metadata', 'artist=Me', '-y', 'movie.mp3']' returned non-zero exit status 1.

I have looked at posts on similar queries concerning matplotlib animations, but none have specifically included the error Output file #0 does not contain any stream. I have little experience with ffmpeg, so I am wondering what might be missing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文