From 839315f68c8899cea80cb33a2a649462b022fe9f 2014-06-15 07:35:56 From: Daniel Wehner Date: 2014-06-15 07:35:56 Subject: [PATCH] Add support to embed videos. --- diff --git a/IPython/core/display.py b/IPython/core/display.py index e99a4fa..67a4ec9 100644 --- a/IPython/core/display.py +++ b/IPython/core/display.py @@ -21,6 +21,7 @@ from __future__ import print_function import os import struct +import mimetypes from IPython.core.formatters import _safe_get_formatter_method from IPython.utils.py3compat import (string_types, cast_bytes_py2, cast_unicode, @@ -734,6 +735,90 @@ class Image(DisplayObject): def _find_ext(self, s): return unicode_type(s.split('.')[-1].lower()) +class Video(DisplayObject): + + def __init__(self, data=None, url=None, filename=None, embed=None, mimetype=None): + """Create a video object given raw data or an URL. + + When this object is returned by an input cell or passed to the + display function, it will result in the video being displayed + in the frontend. + + Parameters + ---------- + data : unicode, str or bytes + The raw image data or a URL or filename to load the data from. + This always results in embedded image data. + url : unicode + A URL to download the data from. If you specify `url=`, + the image data will not be embedded unless you also specify `embed=True`. + filename : unicode + Path to a local file to load the data from. + Videos from a file are always embedded. + embed : bool + Should the image data be embedded using a data URI (True) or be + loaded using an tag. Set this to True if you want the image + to be viewable later with no internet connection in the notebook. + + Default is `True`, unless the keyword argument `url` is set, then + default value is `False`. + + Note that QtConsole is not able to display images if `embed` is set to `False` + mimetype: unicode + Specify the mimetype in case you load in a encoded video. + Examples + -------- + Video('https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4') + Video('path/to/video.mp4') + Video('path/to/video.mp4', embed=False) + """ + if url is None and (data.startswith('http') or data.startswith('https')): + url = data + data = None + embed = False + elif os.path.exists(data): + filename = data + data = None + + self.mimetype = mimetype + self.embed = embed if embed is not None else (filename is not None) + super(Video, self).__init__(data=data, url=url, filename=filename) + + def _repr_html_(self): + # External URLs and potentially local files are not embedded into the + # notebook output. + if not self.embed: + url = self.url if self.url is not None else self.filename + output = """""".format(url) + return output + # Embedded videos uses base64 encoded videos. + if self.filename is not None: + mimetypes.init() + mimetype, encoding = mimetypes.guess_type(self.filename) + + video = open(self.filename, 'rb').read() + video_encoded = video.encode('base64') + else: + video_encoded = self.data + mimetype = self.mimetype + output = """""".format(mimetype, video_encoded) + return output + + def reload(self): + # TODO + pass + + def _repr_png_(self): + # TODO + pass + def _repr_jpeg_(self): + # TODO + pass def clear_output(wait=False): """Clear the output of the current cell receiving output.