Show More
@@ -21,6 +21,7 b' from __future__ import print_function' | |||||
21 |
|
21 | |||
22 | import os |
|
22 | import os | |
23 | import struct |
|
23 | import struct | |
|
24 | import mimetypes | |||
24 |
|
25 | |||
25 | from IPython.core.formatters import _safe_get_formatter_method |
|
26 | from IPython.core.formatters import _safe_get_formatter_method | |
26 | from IPython.utils.py3compat import (string_types, cast_bytes_py2, cast_unicode, |
|
27 | from IPython.utils.py3compat import (string_types, cast_bytes_py2, cast_unicode, | |
@@ -734,6 +735,90 b' class Image(DisplayObject):' | |||||
734 | def _find_ext(self, s): |
|
735 | def _find_ext(self, s): | |
735 | return unicode_type(s.split('.')[-1].lower()) |
|
736 | return unicode_type(s.split('.')[-1].lower()) | |
736 |
|
737 | |||
|
738 | class Video(DisplayObject): | |||
|
739 | ||||
|
740 | def __init__(self, data=None, url=None, filename=None, embed=None, mimetype=None): | |||
|
741 | """Create a video object given raw data or an URL. | |||
|
742 | ||||
|
743 | When this object is returned by an input cell or passed to the | |||
|
744 | display function, it will result in the video being displayed | |||
|
745 | in the frontend. | |||
|
746 | ||||
|
747 | Parameters | |||
|
748 | ---------- | |||
|
749 | data : unicode, str or bytes | |||
|
750 | The raw image data or a URL or filename to load the data from. | |||
|
751 | This always results in embedded image data. | |||
|
752 | url : unicode | |||
|
753 | A URL to download the data from. If you specify `url=`, | |||
|
754 | the image data will not be embedded unless you also specify `embed=True`. | |||
|
755 | filename : unicode | |||
|
756 | Path to a local file to load the data from. | |||
|
757 | Videos from a file are always embedded. | |||
|
758 | embed : bool | |||
|
759 | Should the image data be embedded using a data URI (True) or be | |||
|
760 | loaded using an <img> tag. Set this to True if you want the image | |||
|
761 | to be viewable later with no internet connection in the notebook. | |||
|
762 | ||||
|
763 | Default is `True`, unless the keyword argument `url` is set, then | |||
|
764 | default value is `False`. | |||
|
765 | ||||
|
766 | Note that QtConsole is not able to display images if `embed` is set to `False` | |||
|
767 | mimetype: unicode | |||
|
768 | Specify the mimetype in case you load in a encoded video. | |||
|
769 | Examples | |||
|
770 | -------- | |||
|
771 | Video('https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4') | |||
|
772 | Video('path/to/video.mp4') | |||
|
773 | Video('path/to/video.mp4', embed=False) | |||
|
774 | """ | |||
|
775 | if url is None and (data.startswith('http') or data.startswith('https')): | |||
|
776 | url = data | |||
|
777 | data = None | |||
|
778 | embed = False | |||
|
779 | elif os.path.exists(data): | |||
|
780 | filename = data | |||
|
781 | data = None | |||
|
782 | ||||
|
783 | self.mimetype = mimetype | |||
|
784 | self.embed = embed if embed is not None else (filename is not None) | |||
|
785 | super(Video, self).__init__(data=data, url=url, filename=filename) | |||
|
786 | ||||
|
787 | def _repr_html_(self): | |||
|
788 | # External URLs and potentially local files are not embedded into the | |||
|
789 | # notebook output. | |||
|
790 | if not self.embed: | |||
|
791 | url = self.url if self.url is not None else self.filename | |||
|
792 | output = """<video src="{0}" controls> | |||
|
793 | Your browser does not support the <code>video</code> element. | |||
|
794 | </video>""".format(url) | |||
|
795 | return output | |||
|
796 | # Embedded videos uses base64 encoded videos. | |||
|
797 | if self.filename is not None: | |||
|
798 | mimetypes.init() | |||
|
799 | mimetype, encoding = mimetypes.guess_type(self.filename) | |||
|
800 | ||||
|
801 | video = open(self.filename, 'rb').read() | |||
|
802 | video_encoded = video.encode('base64') | |||
|
803 | else: | |||
|
804 | video_encoded = self.data | |||
|
805 | mimetype = self.mimetype | |||
|
806 | output = """<video controls> | |||
|
807 | <source src="data:{0};base64,{1}" type="{0}"> | |||
|
808 | Your browser does not support the video tag. | |||
|
809 | </video>""".format(mimetype, video_encoded) | |||
|
810 | return output | |||
|
811 | ||||
|
812 | def reload(self): | |||
|
813 | # TODO | |||
|
814 | pass | |||
|
815 | ||||
|
816 | def _repr_png_(self): | |||
|
817 | # TODO | |||
|
818 | pass | |||
|
819 | def _repr_jpeg_(self): | |||
|
820 | # TODO | |||
|
821 | pass | |||
737 |
|
822 | |||
738 | def clear_output(wait=False): |
|
823 | def clear_output(wait=False): | |
739 | """Clear the output of the current cell receiving output. |
|
824 | """Clear the output of the current cell receiving output. |
General Comments 0
You need to be logged in to leave comments.
Login now