Skip to content

Instantly share code, notes, and snippets.

@kadeng
Created January 5, 2018 10:41
Show Gist options
  • Save kadeng/acd53d72339c3c3d23f50cc17a5a4fc5 to your computer and use it in GitHub Desktop.
Save kadeng/acd53d72339c3c3d23f50cc17a5a4fc5 to your computer and use it in GitHub Desktop.
How to create a Chrome/ Jupyter Notebook compatible embedded video using ffmpeg and matplotlib
# IMPORTANT:
# Install FFMPEG Static build from https://johnvansickle.com/ffmpeg/
import numpy as np
import matplotlib
#matplotlib.use("Agg")
import matplotlib.pyplot as plt
from math import sin,cos,pi
import matplotlib.animation as manimation
from IPython.display import display_html, FileLink, HTML, display
matplotlib.verbose.set_level('helpful')
FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title='Movie Test', artist='Matplotlib',
comment='Movie support!')
writer = FFMpegWriter(fps=15, metadata=metadata, extra_args=['-acodec', 'libvorbis ', '-vcodec', 'libvpx'])
x0,y0 = 0, 0
fig = plt.figure()
with writer.saving(fig, "writer_test5.webm", 100):
for i in range(100):
x0 += 0.1 * np.random.randn() + 0.05*sin(i * 2.0 * pi / 100)
y0 += 0.1 * np.random.randn() + 0.05*cos(i * 2.0 * pi / 100)
fig.clear()
plt.xlim(-5, 5)
plt.ylim(-5, 5)
l, = plt.plot(x0, y0, 'k-o')
#l.set_data(x0, y0)
writer.grab_frame()
from IPython.display import HTML
from base64 import b64encode
#video = open("writer_test5.mp4", "rb").read()
#video_encoded = b64encode(video).decode('ascii')
video_tag = '<video controls alt="test" type="video/mp4" src="writer_test5.webm">'.format(video_encoded)
HTML(data=video_tag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment