##// END OF EJS Templates
handle raw video data as input to Video
Min RK -
Show More
@@ -6,7 +6,11 b''
6
6
7 from __future__ import print_function
7 from __future__ import print_function
8
8
9 import base64
9 try:
10 from base64 import encodebytes as base64_encode
11 except ImportError:
12 from base64 import encodestring as base64_encode
13
10 import json
14 import json
11 import mimetypes
15 import mimetypes
12 import os
16 import os
@@ -849,13 +853,17 b' class Video(DisplayObject):'
849 Note that QtConsole is not able to display images if `embed` is set to `False`
853 Note that QtConsole is not able to display images if `embed` is set to `False`
850 mimetype: unicode
854 mimetype: unicode
851 Specify the mimetype in case you load in a encoded video.
855 Specify the mimetype in case you load in a encoded video.
856
852 Examples
857 Examples
853 --------
858 --------
859
854 Video('https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4')
860 Video('https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4')
855 Video('path/to/video.mp4')
861 Video('path/to/video.mp4')
856 Video('path/to/video.mp4', embed=False)
862 Video('path/to/video.mp4', embed=False)
863 Video(b'videodata')
864 Video(u'b64-encoded-videodata')
857 """
865 """
858 if url is None and (data.startswith('http') or data.startswith('https')):
866 if url is None and isinstance(data, string_types) and data.startswith(('http:', 'https:')):
859 url = data
867 url = data
860 data = None
868 data = None
861 embed = False
869 embed = False
@@ -877,19 +885,25 b' class Video(DisplayObject):'
877 </video>""".format(url)
885 </video>""".format(url)
878 return output
886 return output
879 # Embedded videos uses base64 encoded videos.
887 # Embedded videos uses base64 encoded videos.
888 mimetype = self.mimetype
880 if self.filename is not None:
889 if self.filename is not None:
881 mimetypes.init()
890 if not mimetype:
882 mimetype, encoding = mimetypes.guess_type(self.filename)
891 mimetype, _ = mimetypes.guess_type(self.filename)
883
892
884 video = open(self.filename, 'rb').read()
893 with open(self.filename, 'rb') as f:
885 video_encoded = base64.b64encode(video)
894 video = f.read()
895 else:
896 video = self.data
897 if isinstance(video, unicode_type):
898 # unicode input is already b64-encoded
899 b64_video = video
886 else:
900 else:
887 video_encoded = self.data
901 b64_video = base64_encode(video).decode('ascii').rstrip()
888 mimetype = self.mimetype
902
889 output = """<video controls>
903 output = """<video controls>
890 <source src="data:{0};base64,{1}" type="{0}">
904 <source src="data:{0};base64,{1}" type="{0}">
891 Your browser does not support the video tag.
905 Your browser does not support the video tag.
892 </video>""".format(mimetype, video_encoded.decode('ASCII'))
906 </video>""".format(mimetype, b64_video)
893 return output
907 return output
894
908
895 def reload(self):
909 def reload(self):
@@ -162,3 +162,15 b' def test_video_embedding():'
162 v = display.Video(f.name, embed=True)
162 v = display.Video(f.name, embed=True)
163 html = v._repr_html_()
163 html = v._repr_html_()
164 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
164 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
165
166 v = display.Video(f.name, embed=True, mimetype='video/other')
167 html = v._repr_html_()
168 nt.assert_in('src="data:video/other;base64,YWJj"',html)
169
170 v = display.Video(b'abc', embed=True, mimetype='video/mp4')
171 html = v._repr_html_()
172 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
173
174 v = display.Video(u'YWJj', embed=True, mimetype='video/xyz')
175 html = v._repr_html_()
176 nt.assert_in('src="data:video/xyz;base64,YWJj"',html)
General Comments 0
You need to be logged in to leave comments. Login now