Show More
@@ -824,7 +824,7 b' class Image(DisplayObject):' | |||
|
824 | 824 | |
|
825 | 825 | class Video(DisplayObject): |
|
826 | 826 | |
|
827 |
def __init__(self, data=None, url=None, filename=None, embed= |
|
|
827 | def __init__(self, data=None, url=None, filename=None, embed=False, mimetype=None): | |
|
828 | 828 | """Create a video object given raw data or an URL. |
|
829 | 829 | |
|
830 | 830 | When this object is returned by an input cell or passed to the |
@@ -834,45 +834,54 b' class Video(DisplayObject):' | |||
|
834 | 834 | Parameters |
|
835 | 835 | ---------- |
|
836 | 836 | data : unicode, str or bytes |
|
837 |
The raw |
|
|
838 | This always results in embedded image data. | |
|
837 | The raw video data or a URL or filename to load the data from. | |
|
838 | Raw data will require passing `embed=True`. | |
|
839 | 839 | url : unicode |
|
840 |
A URL |
|
|
841 |
the image data will not be embedded |
|
|
840 | A URL for the video. If you specify `url=`, | |
|
841 | the image data will not be embedded. | |
|
842 | 842 | filename : unicode |
|
843 |
Path to a local file |
|
|
844 | Videos from a file are always embedded. | |
|
843 | Path to a local file containing the video. | |
|
844 | Will be interpreted as a local URL unless `embed=True`. | |
|
845 | 845 | embed : bool |
|
846 |
Should the |
|
|
847 | loaded using an <img> tag. Set this to True if you want the image | |
|
848 | to be viewable later with no internet connection in the notebook. | |
|
846 | Should the video be embedded using a data URI (True) or be | |
|
847 | loaded using a <video> tag (False). | |
|
849 | 848 | |
|
850 | Default is `True`, unless the keyword argument `url` is set, then | |
|
851 | default value is `False`. | |
|
849 | Since videos are large, embedding them should be avoided, if possible. | |
|
850 | You must confirm embedding as your intention by passing `embed=True`. | |
|
851 | ||
|
852 | Local files can be displayed with URLs without embedding the content, via:: | |
|
853 | ||
|
854 | Video('./video.mp4') | |
|
852 | 855 | |
|
853 | Note that QtConsole is not able to display images if `embed` is set to `False` | |
|
854 | 856 | mimetype: unicode |
|
855 |
Specify the mimetype |
|
|
857 | Specify the mimetype for embedded videos. | |
|
858 | Default will be guessed from file extension, if available. | |
|
856 | 859 | |
|
857 | 860 | Examples |
|
858 | 861 | -------- |
|
859 | 862 | |
|
860 | 863 | Video('https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4') |
|
861 | 864 | Video('path/to/video.mp4') |
|
862 |
Video('path/to/video.mp4', embed= |
|
|
863 | Video(b'videodata') | |
|
864 | Video(u'b64-encoded-videodata') | |
|
865 | Video('path/to/video.mp4', embed=True) | |
|
866 | Video(b'raw-videodata', embed=True) | |
|
865 | 867 | """ |
|
866 | 868 | if url is None and isinstance(data, string_types) and data.startswith(('http:', 'https:')): |
|
867 | 869 | url = data |
|
868 | 870 | data = None |
|
869 | embed = False | |
|
870 | 871 | elif os.path.exists(data): |
|
871 | 872 | filename = data |
|
872 | 873 | data = None |
|
873 | 874 | |
|
875 | if data and not embed: | |
|
876 | msg = ''.join([ | |
|
877 | "To embed videos, you must pass embed=True ", | |
|
878 | "(this may make your notebook files huge)\n", | |
|
879 | "Consider passing Video(url='...')", | |
|
880 | ]) | |
|
881 | raise ValueError(msg) | |
|
882 | ||
|
874 | 883 | self.mimetype = mimetype |
|
875 | self.embed = embed if embed is not None else (filename is not None) | |
|
884 | self.embed = embed | |
|
876 | 885 | super(Video, self).__init__(data=data, url=url, filename=filename) |
|
877 | 886 | |
|
878 | 887 | def _repr_html_(self): |
@@ -884,7 +893,8 b' class Video(DisplayObject):' | |||
|
884 | 893 | Your browser does not support the <code>video</code> element. |
|
885 | 894 | </video>""".format(url) |
|
886 | 895 | return output |
|
887 | # Embedded videos uses base64 encoded videos. | |
|
896 | ||
|
897 | # Embedded videos are base64-encoded. | |
|
888 | 898 | mimetype = self.mimetype |
|
889 | 899 | if self.filename is not None: |
|
890 | 900 | if not mimetype: |
@@ -155,10 +155,24 b' def test_json():' | |||
|
155 | 155 | |
|
156 | 156 | def test_video_embedding(): |
|
157 | 157 | """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash""" |
|
158 | v = display.Video("http://ignored") | |
|
159 | assert not v.embed | |
|
160 | html = v._repr_html_() | |
|
161 | nt.assert_not_in('src="data:', html) | |
|
162 | nt.assert_in('src="http://ignored"', html) | |
|
163 | ||
|
164 | with nt.assert_raises(ValueError): | |
|
165 | v = display.Video(b'abc') | |
|
166 | ||
|
158 | 167 | with tempfile.NamedTemporaryFile(suffix='.mp4') as f: |
|
159 | 168 | with open(f.name,'wb') as f: |
|
160 | 169 | f.write(b'abc') |
|
161 | 170 | |
|
171 | v = display.Video(f.name) | |
|
172 | assert not v.embed | |
|
173 | html = v._repr_html_() | |
|
174 | nt.assert_not_in('src="data:', html) | |
|
175 | ||
|
162 | 176 | v = display.Video(f.name, embed=True) |
|
163 | 177 | html = v._repr_html_() |
|
164 | 178 | nt.assert_in('src="data:video/mp4;base64,YWJj"',html) |
@@ -174,3 +188,4 b' def test_video_embedding():' | |||
|
174 | 188 | v = display.Video(u'YWJj', embed=True, mimetype='video/xyz') |
|
175 | 189 | html = v._repr_html_() |
|
176 | 190 | nt.assert_in('src="data:video/xyz;base64,YWJj"',html) |
|
191 |
General Comments 0
You need to be logged in to leave comments.
Login now