Animation demo

This notebook creates a movie file.

NOTE the animation is not (directly) visible in the browser! This would be possible, however it is not intended here. Instead, a video is rendered. This video, however, is then included in the notebook :)

(btw.: this is a "Markdown" cell)

First step: create each image for the animation as separate file


In [1]:
# This notebook requires pylab
# e.g., start the notebook server with --oylab=inline, or run 
# from pylab import *


# create the folder to store the images
!mkdir -p anim
!rm anim/*.png


fig = figure(figsize=(5,5))
ax = fig.add_subplot(1, 1, 1)
t = linspace(0, 2.*pi, 100)
x = cos(t)
y = sin(t)
ax.plot(x,y)
phi = 0
r = plot(cos(phi), sin(phi), 'rd', markersize=12 )

for imnum, phi in enumerate(linspace(0,2.*pi, 100)):
    r[0].set_data(cos(phi), sin(phi)) # update the line properties
    # note that only the final figure will be transferred to the browser!
    draw() # necessary to update the figure!
    fig.savefig('anim/fig%03i.png' % imnum, res=80) # resolution: 80dpi


rm: cannot remove `anim/*.png': No such file or directory

Next step: convert into a video file


In [2]:
# convert into mp4:
# old version (raises deprecation warning
#!ffmpeg -f image2 -i anim/fig%03d.png anim.mp4
# new version
# -y: overwrite existing file, -r 20: 20 fps, -f image2 : filetype image,
# -i anim/fig%03d.png: input files, all that start with anim/fig, have 3 numbers, and end with .png
# anim/anim.ogg - the output file name
# *NOTE* 'ogg' is web-compatible. If you want another format, just change the suffix (e.g. '.mp4')
!avconv -y -r 20 -f image2 -i anim/fig%03d.png anim/anim.ogg
#!rm anim/*.png


avconv version 0.8.9-6:0.8.9-0ubuntu0.12.10.1, Copyright (c) 2000-2013 the Libav developers
  built on Nov  9 2013 19:12:35 with gcc 4.7.2
Input #0, image2, from 'anim/fig%03d.png':
  Duration: 00:00:05.00, start: 0.000000, bitrate: N/A
    Stream #0.0: Video: png, bgra, 360x360, 20 fps, 20 tbr, 20 tbn, 20 tbc
Incompatible pixel format 'bgra' for codec 'libtheora', auto-selecting format 'yuv420p'
[buffer @ 0x151b1a0] w:360 h:360 pixfmt:bgra
[avsink @ 0x14f47c0] auto-inserting filter 'auto-inserted scaler 0' between the filter 'src' and the filter 'out'
[scale @ 0x14f4ea0] w:360 h:360 fmt:bgra -> w:360 h:360 fmt:yuv420p flags:0x4
Output #0, ogg, to 'anim/anim.ogg':
  Metadata:
    encoder         : Lavf53.21.1
    Stream #0.0: Video: libtheora, yuv420p, 360x360, q=2-31, 200 kb/s, 20 tbn, 20 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (png -> libtheora)
Press ctrl-c to stop encoding

video:128kB audio:0kB global headers:3kB muxing overhead 0.517060%

Finally: display the video in the browser


In [3]:
# display the video inline!
# note: this needs a recent browser... with HTML5 and the option of OGG-video playback
from IPython.core.display import HTML
video = open('anim/anim.ogg', 'rb').read()
video_encoded = video.encode('base64') # encode the video in HTML-conform style
video_tag = ('<video width="400" height="400"  controls type="video/ogg" '
             + 'alt="our simple animation" src="data:video/ogg;base64,{0}">').format(video_encoded)
HTML(data = video_tag)


Out[3]:

In [33]: