##// END OF EJS Templates
handle raw video data as input to Video
Min RK -
Show More
@@ -6,7 +6,11 b''
6 6
7 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 14 import json
11 15 import mimetypes
12 16 import os
@@ -849,13 +853,17 b' class Video(DisplayObject):'
849 853 Note that QtConsole is not able to display images if `embed` is set to `False`
850 854 mimetype: unicode
851 855 Specify the mimetype in case you load in a encoded video.
856
852 857 Examples
853 858 --------
859
854 860 Video('https://archive.org/download/Sita_Sings_the_Blues/Sita_Sings_the_Blues_small.mp4')
855 861 Video('path/to/video.mp4')
856 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 867 url = data
860 868 data = None
861 869 embed = False
@@ -877,19 +885,25 b' class Video(DisplayObject):'
877 885 </video>""".format(url)
878 886 return output
879 887 # Embedded videos uses base64 encoded videos.
888 mimetype = self.mimetype
880 889 if self.filename is not None:
881 mimetypes.init()
882 mimetype, encoding = mimetypes.guess_type(self.filename)
883
884 video = open(self.filename, 'rb').read()
885 video_encoded = base64.b64encode(video)
890 if not mimetype:
891 mimetype, _ = mimetypes.guess_type(self.filename)
892
893 with open(self.filename, 'rb') as f:
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 900 else:
887 video_encoded = self.data
888 mimetype = self.mimetype
901 b64_video = base64_encode(video).decode('ascii').rstrip()
902
889 903 output = """<video controls>
890 904 <source src="data:{0};base64,{1}" type="{0}">
891 905 Your browser does not support the video tag.
892 </video>""".format(mimetype, video_encoded.decode('ASCII'))
906 </video>""".format(mimetype, b64_video)
893 907 return output
894 908
895 909 def reload(self):
@@ -162,3 +162,15 b' def test_video_embedding():'
162 162 v = display.Video(f.name, embed=True)
163 163 html = v._repr_html_()
164 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