【jupyter 交互式播放视频片段】 linux下通过IPython 播放视频

jupyter HTML 播放视频片段

环境依赖:

  • matplotlib
  • opencv
  • numpy
  • IPython

可能需要安装ffmpeg

pip install ffmpeg
apt-get install ffmpeg

步骤

  1. 导入依赖库
import matplotlib.pyplot as plt
from matplotlib import animation
import cv2
import os
import numpy as np
from IPython.display import HTML
from IPython import display
  1. 制作视频,或者图片队列
video_path="zhz1.mp4"

assert os.path.exists(video_path)

video_part=[]
video_cap=cv2.VideoCapture(video_path)

index=0
vis_index=list(np.arange(100,1100)) # 抽取视频片段帧

while True:
    ret,frame=video_cap.read()
    if index in vis_index:
        video_part.append(frame)
    index+=1
    if len(video_part)==len(vis_index):
        break
video_cap.release()
video_part=np.array(video_part)
  1. 播放函数

def plot_video(video):

    fig=plt.figure(figsize=(8,8))

    im=plt.imshow(video[0,:,:,::-1])
    plt.close()

    def init():
        im.set_data(video[0,:,:,::-1])

    def animate(i):
        im.set_data(video[i,:,:,::-1])

    anim = animation.FuncAnimation(fig, animate, init_func=init, frames=video.shape[0],
                               interval=50)

    video_show=anim.to_html5_video()
    html=display.HTML(video_show)
    display.display(html)
    plt.close()
  1. 播放视频
plot_video(video_part)

播放效果

【jupyter 交互式播放视频片段】 linux下通过IPython 播放视频_第1张图片
【jupyter 交互式播放视频片段】 linux下通过IPython 播放视频_第2张图片

你可能感兴趣的:(jupyter,音视频,linux)