如何在 google colab 中播放 mp4 视频

问题描述 投票:0回答:7

我有一个视频.mp4 在

content/video.mp4

如果我想在不下载的情况下在 google colab 中播放视频,请问我应该使用什么代码在我的 jupyter 笔记本中打开一种视频播放器?

python video mp4 google-colaboratory
7个回答
33
投票

这是代码

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

您可以在 Colab 笔记本中测试它这里

更新(2020 年 6 月)

为了支持大的 vdo 文件,我编写了一个库来上传到 Google Drive 并将其设置为公开。然后使用返回的 URL 来显示视频。

!pip install -U kora
from kora.drive import upload_public
url = upload_public('video.mp4')
# then display it
from IPython.display import HTML
HTML(f"""<video src={url} width=500 controls/>""")

15
投票

目前,如果格式不支持,我们需要压缩视频文件才能在google colaboratory中播放。

from IPython.display import HTML
from base64 import b64encode
import os

# Input video path
save_path = "/content/videos/result.mp4"

# Compressed video path
compressed_path = "/content/videos/result_compressed.mp4"

os.system(f"ffmpeg -i {save_path} -vcodec libx264 {compressed_path}")

# Show video
mp4 = open(compressed_path,'rb').read()
data_url = "data:video/mp4;base64," + b64encode(mp4).decode()
HTML("""
<video width=400 controls>
      <source src="%s" type="video/mp4">
</video>
""" % data_url)

参考:https://towardsdatascience.com/yolov3-pytorch-on-google-colab-c4a79eeecdea


2
投票

只需输入该功能的 mp4 视频路径即可。

from IPython.display import HTML
from base64 import b64encode
 
def show_video(video_path, video_width = 600):
   
  video_file = open(video_path, "r+b").read()
 
  video_url = f"data:video/mp4;base64,{b64encode(video_file).decode()}"
  return HTML(f"""<video width={video_width} controls><source src="{video_url}"></video>""")
 
show_video(video_path)

0
投票

支持大vdo文件,获取大视频的链接url

!pip install httplib2==0.15.0
!pip install google-api-python-client==1.7.11
#don't forget to restart the environment

from IPython.display import HTML
import IPython
from google.colab import output
!pip install -U kora
from kora.drive import upload_public

video_path='/content/drive/MyDrive/BigFileName.mkv'
video_url = upload_public(video_path)  #for google disk to https://

if (video_url.startswith('https://drive.google.com/')):
   video_url+='&confirm=t' # to bypass the window Google Drive - Virus scan warning
 print('video_url',video_url)

0
投票

可以在 Google Colab 虚拟机上启动本地 HTTP 服务器(假设在端口 8000 上)。然后可以通过笔记本中的

http://localhost:8000
访问这些文件

例如:

import subprocess
subprocess.Popen(['python3', '-m', 'http.server', '8000']);

然后渲染 HTML:

%%html
<video><source src="http://localhost:8000/video.mp4" type="video/mp4"></source></video>

演示

此外,还可以获取虚拟机的外部 URL 并从互联网访问文件:

from google.colab.output import eval_js
print(eval_js("google.colab.kernel.proxyPort(8000)"))

0
投票

没有看到任何提及 MoviePy(截至 2023 年 7 月已安装在 Google Colab 中):

import moviepy.editor
moviepy.editor.ipython_display("video.mp4")

-1
投票

这就是您需要定义的全部

import html
from IPython.display import display, Javascript, Image
from google.colab.output import eval_js

def preProcessVideo():
  js = Javascript('''
    const video = document.createElement('video');
    const labelElement = document.createElement('span');
    const videoUrl = 'https://rr2---sn-npoldn7z.c.drive.google.com/videoplayback?expire=1641882417&ei=8ercYbCiIuCKmvUPz5WB6Ac&ip=1.55.250.186&cp=QVRJU0lfUVRPSFhPOmpHU0F4ZW1JUnNobkNZVzY0MHlMYm44NDdNek45Nm5sSVQyTWota2J4MlE&id=8955091d9a3609fd&itag=18&source=webdrive&requiressl=yes&mh=yD&mm=32&mn=sn-npoldn7z&ms=su&mv=u&mvi=2&pl=27&ttl=transient&susc=dr&driveid=1S9PGt2CHDfuJSB1nIWebi4KVNRI7jEbf&app=explorer&mime=video/mp4&vprv=1&prv=1&dur=22.825&lmt=1641801389629564&mt=1641867503&txp=0011224&sparams=expire,ei,ip,cp,id,itag,source,requiressl,ttl,susc,driveid,app,mime,vprv,prv,dur,lmt&sig=AOq0QJ8wRgIhAJ8QuQoDRVLULTONbECJ9GyCqACa9Ci7i-4yK6vqgFdxAiEAoC-AMccHI239SCSOukNJEkXmqzKBIPqmb41I25Sjljs=&lsparams=mh,mm,mn,ms,mv,mvi,pl&lsig=AG3C_xAwRgIhAI650mDvui7WOdCTc-zfXSR_jXGCX0_marfJav3vEZDvAiEAz5-kvizrRBxJxmIZpO9LxDxkPQpcMTheY5Sq7pBMPQc=&cpn=BsF1Vhd4TGv91-3f&c=WEB_EMBEDDED_PLAYER&cver=1.20220109.00.00'

    async function playVideo() {
      const div = document.createElement('div');

      video.style.width = 320;
      video.style.height = 320;

      document.body.appendChild(div);
      div.appendChild(labelElement);
      div.appendChild(video);
      
      var source = document.createElement('source');

      source.setAttribute('src', videoUrl);
      source.setAttribute('type', 'video/mp4');
      video.appendChild(source);
      video.play();

      // Resize the output to fit the video element.
      google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
    }
    ''')
  display(js)
  eval_js('playVideo()'.format())

那就称呼它吧

preProcessVideo()

© www.soinside.com 2019 - 2024. All rights reserved.