【问题标题】:Why is the last frame always excluded, when converting images into a video?为什么在将图像转换为视频时总是排除最后一帧?
【发布时间】:2021-07-20 19:37:30
【问题描述】:

我有一个包含 225 张地图图片的文件夹。因此,我使用imageio 将其编译为 mp4 文件。无论是编译10张地图,150张,还是全部225张,最后一张图片总是不包含在视频中。

import os
from natsort import humansorted
import imageio

os.chdir(r'folder/path/')
filenames = humansorted((fn for fn in os.listdir('.') if fn.endswith('.png')))
with imageio.get_writer('earthquake_video.mp4', mode='I', fps=2) as writer:
    for filename in filenames:
        image = imageio.imread(filename)
        writer.append_data(image)
    writer.close()

【问题讨论】:

    标签: python python-3.x mp4 imread python-imageio


    【解决方案1】:

    对我来说,您的代码适用于 10、150 甚至 225 张图像——只要我在 Windows Media Player 中打开生成的视频即可。如果我在 VLC 媒体播放器中打开视频,播放会失真,不仅跳过最后一帧。 (我有从 0 到 224 的数字,所以每一个错误的帧都会被注意到。)所以,如果你使用 VLC 媒体播放器,你的问题很可能是 this StackOverflow Q&A 中讨论的问题。

    imageio GitHub 问题跟踪器上,还有this issue,链接到this other StackOverflow question,这似乎与您的问题相同。但是,我仍然认为这是 VLC 媒体播放器的上述问题。

    不幸的是,我无法从使用imageiooutput_params 的第一个链接问答中获得解决方法,即设置-framerate-r。所以,我的解决方法是设置一个所需的fps(这里:2)和一个fps用于实际播放(在VLC媒体播放器中),例如30,然后根据需要添加尽可能多的相同帧来伪造所需的fps,即此处的30 // 2 = 15

    这里有一些代码:

    import os
    import imageio
    
    os.chdir(r'folder/path')
    filenames = [fn for fn in os.listdir('.') if fn.endswith('.png')]
    fps = 2             # Desired, "real" frames per second
    fps_vlc = 30        # Frames per second needed for proper playback in VLC
    with imageio.get_writer('earthquake_video.mp4', fps=fps_vlc) as writer:
        for filename in filenames:
            image = imageio.imread(filename)
            for i in range(fps_vlc // fps):
                writer.append_data(image)
        writer.close()
    

    生成的视频“看起来”和以前一样(在 Windows 媒体播放器中),但现在,它也可以在 VLC 媒体播放器中正常播放了。

    即使,如果这不是您的实际问题,我想这些信息会帮助其他人遇到您的问题,但实际上会遇到 VLC 媒体播放器的上述问题。

    ----------------------------------------
    System information
    ----------------------------------------
    Platform:      Windows-10-10.0.16299-SP0
    Python:        3.9.1
    PyCharm:       2021.1.1
    imageio:       2.9.0
    ----------------------------------------
    

    【讨论】:

    • 谢谢!我会尝试您稍后编写的解决方案,但视频在使用 Windows Media Player 播放时可以正常播放。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    相关资源
    最近更新 更多