如何在我的IPython笔记本中播放本地视频? [英] How can I play a local video in my IPython notebook?

查看:310
本文介绍了如何在我的IPython笔记本中播放本地视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地视频文件(.avi,但可以转换),我想展示一个客户端(即它是私有的,不能发布到网上),但我不能弄清楚如何在IPython笔记本中播放它。

I've got a local video file (an .avi, but could be converted) that I would like to show a client (ie it is private and can't be published to the web), but I can't figure out how to play it in IPython notebook.

经过一番谷歌搜索后,似乎HTML5视频标签可能就行了,但我不知道任何HTML,也无法让它发挥作用。

After a little Googling it seems that maybe the HTML5 video tag is the way to go, but I don't know any html and can't get it to play.

关于如何嵌入此内容的任何想法?

Any thoughts on how I can embed this?

推荐答案

你由于IPython Notebook不是静态文件服务器,因此必须将视频读入内存并对其进行base64编码:

You have to read the video into memory and base64 encode it, since the IPython Notebook is not a static file server:

import io
import base64
from IPython.display import HTML

video = io.open('test.mp4', 'r+b').read()
encoded = base64.b64encode(video)
HTML(data='''<video alt="test" controls>
                <source src="data:video/mp4;base64,{0}" type="video/mp4" />
             </video>'''.format(encoded.decode('ascii')))

这篇关于如何在我的IPython笔记本中播放本地视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆