video.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """
  2. Video Tag
  3. ---------
  4. This implements a Liquid-style video tag for Pelican,
  5. based on the octopress video tag [1]_
  6. Syntax
  7. ------
  8. {% video url/to/video [width height] [url/to/poster] %}
  9. Example
  10. -------
  11. {% video http://site.com/video.mp4 720 480 http://site.com/poster-frame.jpg %}
  12. Output
  13. ------
  14. <video width='720' height='480' preload='none' controls poster='http://site.com/poster-frame.jpg'>
  15. <source src='http://site.com/video.mp4' type='video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\"'/>
  16. </video>
  17. [1] https://github.com/imathis/octopress/blob/master/plugins/video_tag.rb
  18. """
  19. import os
  20. import re
  21. from .mdx_liquid_tags import LiquidTags
  22. SYNTAX = "{% video url/to/video [url/to/video] [url/to/video] [width height] [url/to/poster] %}"
  23. VIDEO = re.compile(r'(/\S+|https?:\S+)(\s+(/\S+|https?:\S+))?(\s+(/\S+|https?:\S+))?(\s+(\d+)\s(\d+))?(\s+(/\S+|https?:\S+))?')
  24. VID_TYPEDICT = {'.mp4':"type='video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\"'",
  25. '.ogv':"type='video/ogg; codecs=theora, vorbis'",
  26. '.webm':"type='video/webm; codecs=vp8, vorbis'"}
  27. @LiquidTags.register('video')
  28. def video(preprocessor, tag, markup):
  29. videos = []
  30. width = None
  31. height = None
  32. poster = None
  33. match = VIDEO.search(markup)
  34. if match:
  35. groups = match.groups()
  36. videos = [g for g in groups[0:6:2] if g]
  37. width = groups[6]
  38. height = groups[7]
  39. poster = groups[9]
  40. if any(videos):
  41. video_out = """
  42. <div class="videobox">
  43. <video width="{width}" height="{height}" preload="none" controls poster="{poster}">
  44. """.format(width=width, height=height, poster=poster).strip()
  45. for vid in videos:
  46. base, ext = os.path.splitext(vid)
  47. if ext not in VID_TYPEDICT:
  48. raise ValueError("Unrecognized video extension: "
  49. "{0}".format(ext))
  50. video_out += ("<source src='{0}' "
  51. "{1}>".format(vid, VID_TYPEDICT[ext]))
  52. video_out += "</video></div>"
  53. else:
  54. raise ValueError("Error processing input, "
  55. "expected syntax: {0}".format(SYNTAX))
  56. return video_out
  57. #----------------------------------------------------------------------
  58. # This import allows image tag to be a Pelican plugin
  59. from liquid_tags import register