Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

# -*- encoding: utf-8 -*- 

r""" 

Video Output Types 

 

This module defines the rich output types for video formats. 

""" 

 

#***************************************************************************** 

# Copyright (C) 2015 Martin von Gagern <Martin.vGagern@gmx.net> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

 

import os 

 

from sage.repl.rich_output.output_basic import OutputBase 

from sage.repl.rich_output.buffer import OutputBuffer 

 

 

class OutputVideoBase(OutputBase): 

 

def __init__(self, video, loop=True): 

""" 

Abstract base class for rich video output 

 

INPUT: 

 

- ``video`` -- 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. 

The video data. 

- ``loop`` -- boolean. Whether to repeat the video in an endless loop. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputVideoOgg 

sage: OutputVideoOgg.example() # indirect doctest 

OutputVideoOgg container 

""" 

assert isinstance(video, OutputBuffer) 

self.video = video 

self.loop = loop 

 

@classmethod 

def example(cls): 

r""" 

Construct a sample video output container 

 

This static method is meant for doctests, so they can easily 

construct an example. The method is implemented in the abstract 

:class:`OutputVideoBase` class, but should get invoked on a 

concrete subclass for which an actual example can exist. 

 

OUTPUT: 

 

An instance of the class on which this method is called. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputVideoOgg 

sage: OutputVideoOgg.example() 

OutputVideoOgg container 

sage: OutputVideoOgg.example().video 

buffer containing 5612 bytes 

sage: OutputVideoOgg.example().ext 

'.ogv' 

sage: OutputVideoOgg.example().mimetype 

'video/ogg' 

""" 

from sage.env import SAGE_EXTCODE 

filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 

'example' + cls.ext) 

return cls(OutputBuffer.from_file(filename), 

{'controls': True, 'loop': False}) 

 

def html_fragment(self, url, link_attrs=''): 

r""" 

Construct a HTML fragment for embedding this video 

 

INPUT: 

 

- ``url`` -- string. The URL where the data of this video can be found. 

 

- ``link_attrs`` -- string. Can be used to style the fallback link 

which is presented to the user if the video is not supported. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputVideoOgg 

sage: print(OutputVideoOgg.example().html_fragment 

....: ('foo', 'class="bar"').replace('><','>\n<')) 

<video autoplay="autoplay" controls="controls" loop="loop"> 

<source src="foo" type="video/ogg" /> 

<p> 

<a target="_new" href="foo" class="bar">Download video/ogg video</a> 

</p> 

</video> 

""" 

attrs = { 

'autoplay': 'autoplay', 

'controls': 'controls', 

} 

if self.loop: attrs['loop'] = 'loop' 

attrs = ''.join(' {}="{}"'.format(k, v) 

for k, v in sorted(attrs.items())) 

return ('<video{attrs}>' 

'<source src="{url}" type="{mimetype}" /><p>' 

'<a target="_new" href="{url}" {link_attrs}>' 

'Download {mimetype} video</a></p></video>' 

).format(url=url, 

mimetype=self.mimetype, 

attrs=attrs, 

link_attrs=link_attrs, 

) 

 

class OutputVideoOgg(OutputVideoBase): 

""" 

Ogg video, Ogg Theora in particular 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputVideoOgg 

sage: OutputVideoOgg.example() 

OutputVideoOgg container 

""" 

 

ext = ".ogv" 

mimetype = "video/ogg" 

 

class OutputVideoWebM(OutputVideoBase): 

""" 

WebM video 

 

The video can be encoded using VP8, VP9 or an even more recent codec. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputVideoWebM 

sage: OutputVideoWebM.example() 

OutputVideoWebM container 

""" 

 

ext = ".webm" 

mimetype = "video/webm" 

 

class OutputVideoMp4(OutputVideoBase): 

""" 

MPEG 4 video 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputVideoMp4 

sage: OutputVideoMp4.example() 

OutputVideoMp4 container 

""" 

 

ext = ".mp4" 

mimetype = "video/mp4" 

 

class OutputVideoFlash(OutputVideoBase): 

""" 

Flash video 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputVideoFlash 

sage: OutputVideoFlash.example() 

OutputVideoFlash container 

""" 

 

ext = ".flv" 

mimetype = "video/x-flv" 

 

class OutputVideoMatroska(OutputVideoBase): 

""" 

Matroska Video 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputVideoMatroska 

sage: OutputVideoMatroska.example() 

OutputVideoMatroska container 

""" 

 

ext = ".mkv" 

mimetype = "video/x-matroska" 

 

class OutputVideoAvi(OutputVideoBase): 

""" 

AVI video 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputVideoAvi 

sage: OutputVideoAvi.example() 

OutputVideoAvi container 

""" 

 

ext = ".avi" 

mimetype = "video/x-msvideo" 

 

class OutputVideoWmv(OutputVideoBase): 

""" 

Windows Media Video 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputVideoWmv 

sage: OutputVideoWmv.example() 

OutputVideoWmv container 

""" 

 

ext = ".wmv" 

mimetype = "video/x-ms-wmv" 

 

class OutputVideoQuicktime(OutputVideoBase): 

""" 

Quicktime video 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputVideoQuicktime 

sage: OutputVideoQuicktime.example() 

OutputVideoQuicktime container 

""" 

 

ext = ".mov" 

mimetype = "video/quicktime"