forked from django-cms/django-filer
/
videomodels.py
131 lines (109 loc) · 4.77 KB
/
videomodels.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
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
#-*- coding: utf-8 -*-
import os
import mimetypes
from django.db import models
from django.utils.translation import ugettext_lazy as _
from filer import settings as filer_settings
from filer.models.filemodels import File
from filer.utils.video import convert_video, grab_poster, get_dimensions
VIDEO_STATUS_TYPE = (
('new', _('New')),
('process', _('Being precessed')),
('ok', _('Converted successfully')),
('error', _('Conversion failed')),
)
class Video(File):
file_type = 'Video'
_icon = "video"
original_height = models.IntegerField(null=True, blank=True, default=0)
original_width = models.IntegerField(null=True, blank=True, default=0)
height = models.IntegerField(null=True, blank=True, default=0)
width = models.IntegerField(null=True, blank=True, default=0)
date_taken = models.DateTimeField(_('date taken'), null=True, blank=True,
editable=False)
default_alt_text = models.CharField(_('default alt text'), max_length=255, blank=True, null=True)
default_caption = models.CharField(_('default caption'), max_length=255, blank=True, null=True)
author = models.CharField(_('author'), max_length=255, null=True, blank=True)
must_always_publish_author_credit = models.BooleanField(_('must always publish author credit'), default=False)
must_always_publish_copyright = models.BooleanField(_('must always publish copyright'), default=False)
conversion_status = models.CharField(max_length=50, choices=VIDEO_STATUS_TYPE, default='new')
conversion_output = models.TextField(blank=True)
class Meta:
app_label = 'filer'
verbose_name = _('video')
verbose_name_plural = _('videos')
@classmethod
def matches_file_type(cls, iname, ifile, request):
iext = os.path.splitext(iname)[1].lower().lstrip('.')
return iext in filer_settings.FILER_SOURCE_VIDEO_FORMATS
def set_initial_dimensions(self):
sourcefile = self.file.storage.path(self.file.name)
x, y = get_dimensions(sourcefile)
self.original_width = x
self.original_height = y
self.width = x
self.height = y
def save(self, *args, **kwargs):
self.has_all_mandatory_data = self._check_validity()
if not ( self.original_width or self.original_height or \
self.width or self.height ):
self.set_initial_dimensions()
super(Video, self).save(*args, **kwargs)
def _check_validity(self):
if not self.name:
return False
return True
@property
def formats(self):
_formats = {}
for ext in filer_settings.FILER_VIDEO_FORMATS:
try:
_formats[ext] = self.file.get_format_url(ext)
except Exception, e:
pass
return _formats
def original_format(self):
url = self.file.storage.url(self.file.name)
name, ext = os.path.splitext(self.file.name)
mimetype = mimetypes.guess_type(self.file.name)[0]
fmt = ext.replace('.', '')
return {'url': url, 'format': fmt, 'mimetype': mimetype}
def formats_html5(self):
""" Video formats supported by HTML5 browsers """
HTML5_FORMATS = {'mp4':'video/mp4', 'ogv':'video/ogg','webm':'video/webm'}
_formats = []
for fmt, url in self.formats.items():
if fmt in HTML5_FORMATS:
_formats.append({'format': fmt, 'url': url, 'mimetype': HTML5_FORMATS[format]})
return _formats
def format_flash(self):
""" Returns flash video file if available """
return self.formats.get('flv', None)
@property
def poster(self):
try:
return self.file.get_poster_url()
except Exception, e:
return ""
def convert(self):
original_path = self.file.storage.path(self.file.name)
path = os.path.split(self.file.format_storage.path(self.file.name))[0]
# loop in all
output = []
error = False
#only set new dimensions if diferent from the original and not zero
if self.width and self.height and (
(self.width, self.height) != (self.original_width, self.original_height)):
new_dimensions = "%sx%s" % (self.width, self.height)
else:
new_dimensions = filer_settings.FFMPEG_TARGET_DIMENSIONS
if new_dimensions:
self.width, self.height = new_dimensions.split('x')
for extension in filer_settings.FILER_VIDEO_FORMATS:
res, out = convert_video(original_path, path, extension, new_dimensions)
error = error or res
output.append(out)
res, out = grab_poster(original_path, path, new_dimensions)
error = error or res
output.append(out)
return error, "\n".join(output)