##// END OF EJS Templates
require explicit embed=True before embedding b64-encoded video
Min RK -
Show More
@@ -824,7 +824,7 b' class Image(DisplayObject):'
824
824
825 class Video(DisplayObject):
825 class Video(DisplayObject):
826
826
827 def __init__(self, data=None, url=None, filename=None, embed=None, mimetype=None):
827 def __init__(self, data=None, url=None, filename=None, embed=False, mimetype=None):
828 """Create a video object given raw data or an URL.
828 """Create a video object given raw data or an URL.
829
829
830 When this object is returned by an input cell or passed to the
830 When this object is returned by an input cell or passed to the
@@ -834,45 +834,54 b' class Video(DisplayObject):'
834 Parameters
834 Parameters
835 ----------
835 ----------
836 data : unicode, str or bytes
836 data : unicode, str or bytes
837 The raw image data or a URL or filename to load the data from.
837 The raw video data or a URL or filename to load the data from.
838 This always results in embedded image data.
838 Raw data will require passing `embed=True`.
839 url : unicode
839 url : unicode
840 A URL to download the data from. If you specify `url=`,
840 A URL for the video. If you specify `url=`,
841 the image data will not be embedded unless you also specify `embed=True`.
841 the image data will not be embedded.
842 filename : unicode
842 filename : unicode
843 Path to a local file to load the data from.
843 Path to a local file containing the video.
844 Videos from a file are always embedded.
844 Will be interpreted as a local URL unless `embed=True`.
845 embed : bool
845 embed : bool
846 Should the image data be embedded using a data URI (True) or be
846 Should the video be embedded using a data URI (True) or be
847 loaded using an <img> tag. Set this to True if you want the image
847 loaded using a <video> tag (False).
848 to be viewable later with no internet connection in the notebook.
849
848
850 Default is `True`, unless the keyword argument `url` is set, then
849 Since videos are large, embedding them should be avoided, if possible.
851 default value is `False`.
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 mimetype: unicode
856 mimetype: unicode
855 Specify the mimetype in case you load in a encoded video.
857 Specify the mimetype for embedded videos.
858 Default will be guessed from file extension, if available.
856
859
857 Examples
860 Examples
858 --------
861 --------
859
862
860 Video('https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4')
863 Video('https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4')
861 Video('path/to/video.mp4')
864 Video('path/to/video.mp4')
862 Video('path/to/video.mp4', embed=False)
865 Video('path/to/video.mp4', embed=True)
863 Video(b'videodata')
866 Video(b'raw-videodata', embed=True)
864 Video(u'b64-encoded-videodata')
865 """
867 """
866 if url is None and isinstance(data, string_types) and data.startswith(('http:', 'https:')):
868 if url is None and isinstance(data, string_types) and data.startswith(('http:', 'https:')):
867 url = data
869 url = data
868 data = None
870 data = None
869 embed = False
870 elif os.path.exists(data):
871 elif os.path.exists(data):
871 filename = data
872 filename = data
872 data = None
873 data = None
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)
873
882
874 self.mimetype = mimetype
883 self.mimetype = mimetype
875 self.embed = embed if embed is not None else (filename is not None)
884 self.embed = embed
876 super(Video, self).__init__(data=data, url=url, filename=filename)
885 super(Video, self).__init__(data=data, url=url, filename=filename)
877
886
878 def _repr_html_(self):
887 def _repr_html_(self):
@@ -884,7 +893,8 b' class Video(DisplayObject):'
884 Your browser does not support the <code>video</code> element.
893 Your browser does not support the <code>video</code> element.
885 </video>""".format(url)
894 </video>""".format(url)
886 return output
895 return output
887 # Embedded videos uses base64 encoded videos.
896
897 # Embedded videos are base64-encoded.
888 mimetype = self.mimetype
898 mimetype = self.mimetype
889 if self.filename is not None:
899 if self.filename is not None:
890 if not mimetype:
900 if not mimetype:
@@ -155,10 +155,24 b' def test_json():'
155
155
156 def test_video_embedding():
156 def test_video_embedding():
157 """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash"""
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 with tempfile.NamedTemporaryFile(suffix='.mp4') as f:
167 with tempfile.NamedTemporaryFile(suffix='.mp4') as f:
159 with open(f.name,'wb') as f:
168 with open(f.name,'wb') as f:
160 f.write(b'abc')
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 v = display.Video(f.name, embed=True)
176 v = display.Video(f.name, embed=True)
163 html = v._repr_html_()
177 html = v._repr_html_()
164 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
178 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
@@ -174,3 +188,4 b' def test_video_embedding():'
174 v = display.Video(u'YWJj', embed=True, mimetype='video/xyz')
188 v = display.Video(u'YWJj', embed=True, mimetype='video/xyz')
175 html = v._repr_html_()
189 html = v._repr_html_()
176 nt.assert_in('src="data:video/xyz;base64,YWJj"',html)
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