Skip to content

Commit

Permalink
Adding in video image template tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
etianen committed Apr 16, 2012
1 parent 63785b7 commit 7ccf91d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/optimizations/templatetags/assets.py
Expand Up @@ -10,6 +10,7 @@
from optimizations.javascriptcache import default_javascript_cache
from optimizations.stylesheetcache import default_stylesheet_cache
from optimizations.templatetags import simple_tag, inclusion_tag, assignment_tag
from optimizations.videocache import default_video_cache, PROPORTIONAL as VIDEO_PROPORTIONAL, JPEG_FORMAT, VideoError


register = template.Library()
Expand Down Expand Up @@ -59,6 +60,25 @@ def img(src, width=None, height=None, method=PROPORTIONAL, alt="", **attrs):
return params


@inclusion_tag(register, "assets/img.html")
@assignment_tag(register, name="get_video_img")
def video_img(src, width, height, method=VIDEO_PROPORTIONAL, alt="", **attrs):
"""Renders an image tag from the given video."""
params = {
"alt": alt,
"attrs": attrs,
"width": width,
"height": height,
}
try:
url = default_video_cache.get_url(src, width, height, method, format=JPEG_FORMAT)
except VideoError:
asset = AdaptiveAsset(src)
url = asset.get_url(),
params["url"] = url
return params


def is_url(s):
"""Checks if the given string is a URL."""
if not isinstance(s, basestring):
Expand Down
12 changes: 10 additions & 2 deletions src/optimizations/videocache.py
Expand Up @@ -187,7 +187,7 @@ def __init__(self, asset_cache=default_asset_cache):
"""Initializes the video cache."""
self._asset_cache = asset_cache

def get_video(self, asset, width=None, height=None, method=PAD, format=MP4_FORMAT, offset=None):
def _get_video_asset(self, asset, width=None, height=None, method=PAD, format=MP4_FORMAT, offset=None):
"""
Returns a processed video from the given video.
"""
Expand All @@ -210,7 +210,15 @@ def get_video(self, asset, width=None, height=None, method=PAD, format=MP4_FORMA
# Adapt the asset.
asset = AdaptiveAsset(asset)
# Create the video.
return self._asset_cache.get_path(VideoAsset(asset, width, height, method, format, offset), force_save=True)
return VideoAsset(asset, width, height, method, format, offset)

def get_path(self, *args, **kwargs):
"""Returns the path of the given video asset."""
return self._asset_cache.get_path(self._get_video_asset(*args, **kwargs), force_save=True)

def get_url(self, *args, **kwargs):
"""Returns the URL of the given video asset."""
return self._asset_cache.get_url(self._get_video_asset(*args, **kwargs), force_save=True)


# The default video cache.
Expand Down

0 comments on commit 7ccf91d

Please sign in to comment.