与IPython Display并排播放多个视频



我一直在使用这篇StackOverflow文章中建议的有用答案,在Jupyter Notebook中同时查看多个视频。从本质上讲,HTML似乎不起作用,但IPython确实起到了这样的作用(给想要的视频一个filepaths列表(:

from IPython import display
for filepath in filepaths:
display.display(display.Video(filepath, embed=True))

现在我在输出中显示所有视频。然而,这些视频是垂直堆叠的。侧面有很大的空间,最好先并排放置,而不是垂直放置,这样我就可以很容易地在屏幕上看到它们。我该怎么做?

您可以使用ipywidgets执行此操作:在ipywidgets.Output小部件中显示视频,然后使用ipywidgets.GridspecLayout来排列小部件。这里有一个例子:

from ipywidgets import Output, GridspecLayout
from IPython import display
grid = GridspecLayout(1, len(filepaths))
for i, filepath in enumerate(filepaths):
out = Output()
with out:
display.display(display.Video(filepath, embed=True))
grid[0, i] = out
grid

这在Colab中对我来说很好:

from IPython.display import HTML
from base64 import b64encode
html_str=""
for filepath in filepaths:
mp4 = open(filepath,'rb').read()
data_url = "data:video/mp4;base64," + b64encode(mp4).decode()
html_str += """
<video width=400 controls>
<source src="%s" type="video/mp4">
</video>
""" % data_url
HTML(html_str)

最新更新