[英]Multiple videos side-by-side with IPython Display
I have been using the useful answer suggested in this StackOverflow post to view multiple videos at once in Jupyter Notebook.我一直在使用此 StackOverflow 帖子中建议的有用答案在 Jupyter Notebook 中一次查看多个视频。 Essentially, HTML doesn't seem to be working but IPython does so something like this (given a list of
filepaths
to the desired videos) works magic:本质上,HTML 似乎没有工作,但 IPython 这样做(给定所需视频的文件
filepaths
列表)很神奇:
from IPython import display
for filepath in filepaths:
display.display(display.Video(filepath, embed=True))
Now I get all the videos displayed in the output.现在我得到了 output 中显示的所有视频。 However, these videos are vertically stacked.
但是,这些视频是垂直堆叠的。 There is a lot of space to the side and it would be ideal to place them side-by-side first rather than vertically so I can easily see them on the screen together.
侧面有很多空间,最好先将它们并排放置而不是垂直放置,这样我可以很容易地在屏幕上看到它们。 How can I do this?
我怎样才能做到这一点?
You can do this with ipywidgets
: Display the videos within a ipywidgets.Output
widget, and then use ipywidgets.GridspecLayout
to arange your widgets.您可以使用
ipywidgets
执行此操作:在ipywidgets.Output
小部件中显示视频,然后使用ipywidgets.GridspecLayout
来排列小部件。 Here is an example:这是一个例子:
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
This works fine in Colab for me:这对我来说在 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.