MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / to_html5_video

Method to_html5_video

lib/matplotlib/animation.py:1275–1351  ·  view source on GitHub ↗

Convert the animation to an HTML5 `` `` tag. This saves the animation as an h264 video, encoded in base64 directly into the HTML5 video tag. This respects :rc:`animation.writer` and :rc:`animation.bitrate`. This also makes use of the *interval* to cont

(self, embed_limit=None)

Source from the content-addressed store, hash-verified

1273 self._on_resize)
1274
1275 def to_html5_video(self, embed_limit=None):
1276 """
1277 Convert the animation to an HTML5 ``<video>`` tag.
1278
1279 This saves the animation as an h264 video, encoded in base64
1280 directly into the HTML5 video tag. This respects :rc:`animation.writer`
1281 and :rc:`animation.bitrate`. This also makes use of the
1282 *interval* to control the speed, and uses the *repeat*
1283 parameter to decide whether to loop.
1284
1285 Parameters
1286 ----------
1287 embed_limit : float, optional
1288 Limit, in MB, of the returned animation. No animation is created
1289 if the limit is exceeded.
1290 Defaults to :rc:`animation.embed_limit` = 20.0.
1291
1292 Returns
1293 -------
1294 str
1295 An HTML5 video tag with the animation embedded as base64 encoded
1296 h264 video.
1297 If the *embed_limit* is exceeded, this returns the string
1298 "Video too large to embed."
1299 """
1300 VIDEO_TAG = r''&#x27;<video {size} {options}>
1301 <source type="video/mp4" src="data:video/mp4;base64,{video}">
1302 Your browser does not support the video tag.
1303</video>''&#x27;
1304 # Cache the rendering of the video as HTML
1305 if not hasattr(self, '_base64_video'):
1306 # Save embed limit, which is given in MB
1307 embed_limit = mpl._val_or_rc(embed_limit, 'animation.embed_limit')
1308
1309 # Convert from MB to bytes
1310 embed_limit *= 1024 * 1024
1311
1312 # Can't open a NamedTemporaryFile twice on Windows, so use a
1313 # TemporaryDirectory instead.
1314 with TemporaryDirectory() as tmpdir:
1315 path = Path(tmpdir, "temp.m4v")
1316 # We create a writer manually so that we can get the
1317 # appropriate size for the tag
1318 Writer = writers[mpl.rcParams['animation.writer']]
1319 writer = Writer(codec='h264',
1320 bitrate=mpl.rcParams['animation.bitrate'],
1321 fps=1000. / self._interval)
1322 self.save(str(path), writer=writer)
1323 # Now open and base64 encode.
1324 vid64 = base64.encodebytes(path.read_bytes())
1325
1326 vid_len = len(vid64)
1327 if vid_len >= embed_limit:
1328 _log.warning(
1329 "Animation movie is %s bytes, exceeding the limit of %s. "
1330 "If you're sure you want a large animation embedded, set "
1331 "the animation.embed_limit rc parameter to a larger value "
1332 "(in MB).", vid_len, embed_limit)

Callers 1

_repr_html_Method · 0.95

Calls 3

saveMethod · 0.95
PathClass · 0.85
joinMethod · 0.80

Tested by

no test coverage detected