forked from escaped/django-video-encoding
/
files.py
55 lines (47 loc) · 1.55 KB
/
files.py
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
import os
from django.core.files import File
from django.core.files.storage import default_storage
from .backends import get_backend
from .exceptions import FFmpegError
class VideoFile(File):
"""
A mixin for use alongside django.core.files.base.File, which provides
additional features for dealing with videos.
"""
def _get_width(self):
"""
Returns video width in pixels.
"""
return self._get_video_info().get('width', 0)
width = property(_get_width)
def _get_height(self):
"""
Returns video height in pixels.
"""
return self._get_video_info().get('height', 0)
height = property(_get_height)
def _get_duration(self):
"""
Returns duration in seconds.
"""
return self._get_video_info().get('duration', 0)
duration = property(_get_duration)
def _get_video_info(self):
"""
Returns basic information about the video as dictionary.
"""
if not hasattr(self, '_info_cache'):
encoding_backend = get_backend()
try:
_path = getattr(self, 'path', self.name)
except NotImplementedError:
_path = self.name
try:
path = default_storage.path(_path)
except NotImplementedError:
path = default_storage.url(_path)
try:
self._info_cache = encoding_backend.get_media_info(path)
except FFmpegError:
self._info_cache = {}
return self._info_cache