Permalink
Browse files

merged changes

  • Loading branch information...
2 parents 0694ba3 + 7a90fab commit 5926ff29260862ab185000711be80b3669b6ca26 @pvl pvl committed Oct 26, 2011
@@ -21,6 +21,7 @@
'private': filer_settings.FILER_PRIVATEMEDIA_FORMATS_STORAGE,
}
+
def generate_filename_multistorage(instance, filename):
if instance.is_public:
upload_to = filer_settings.FILER_PUBLICMEDIA_UPLOAD_TO
@@ -48,10 +49,9 @@ def get_format_url(self, ext):
raise NameError
def get_poster_url(self):
- original_path = self.formats_storage.path(self.name)
- original_path, filename = os.path.split(original_path)
+ original_path, filename = os.path.split(self.name)
basename = os.path.splitext(filename)[0]
- return os.path.join(original_path, basename + '.png')
+ return self.format_storage.url(os.path.join(original_path, basename + '.png'))
class MultiStorageFieldFile(ThumbnailerNameMixin,
@@ -87,7 +87,6 @@ def thumbnail_storage(self):
else:
return self.thumbnail_storages['private']
-
@property
def format_storage(self):
if self.instance.is_public:
@@ -98,7 +97,7 @@ def format_storage(self):
def get_format(self, options, save=True):
#wtf is opaque vs transparent???
pass
-
+
class MultiStorageFileField(easy_thumbnails_fields.ThumbnailerField):
attr_class = MultiStorageFieldFile
@@ -1,9 +1,10 @@
from django.core.management.base import BaseCommand, CommandError
from filer.models import Video
+
class Command(BaseCommand):
args = ''
- help = 'process next video conversion task'
+ help = 'Processes next video conversion task'
def handle(self, *args, **options):
try:
@@ -20,10 +21,10 @@ def handle(self, *args, **options):
import traceback
vid.conversion_output = traceback.format_exc()
else:
- vid.conversion_status = 'ok' if result else 'error'
+ vid.conversion_status = 'ok' if not result else 'error'
vid.conversion_output = output
vid.save()
if vid.conversion_status == 'ok':
self.stdout.write("Processed %s\n" % vid.file.name)
else:
- self.stdout.write("Failed to process %s (check conversion_output field for more info)\n" % vid.file.name)
+ self.stdout.write("Failed to process %s (check conversion_output field for more info)\n" % vid.file.name)
@@ -6,13 +6,15 @@
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')),
+ ('new', _('New')),
+ ('process', _('Being precessed')),
('ok', _('Converted successfully')),
('error', _('Conversion failed')),
)
+
class Video(File):
file_type = 'Video'
_icon = "video"
@@ -41,15 +43,15 @@ 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
+ 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)
+ x, y = get_dimensions(sourcefile)
self.original_width = x
self.original_height = y
self.width = x
@@ -77,7 +79,7 @@ def formats(self):
return _formats
def formats_html5(self):
- #video formats supported by HTML5 browsers
+ """ Video formats supported by HTML5 browsers """
HTML5_FORMATS = ('mp4','ogv','webm')
_formats = []
for format, url in self.formats.items():
@@ -86,7 +88,7 @@ def formats_html5(self):
return _formats
def format_flash(self):
- #returns flash video file if available
+ """ Returns flash video file if available """
return self.formats.get('flv', None)
@property
@@ -96,49 +98,23 @@ def poster(self):
except Exception, e:
return ""
- # def has_format_mp4(self):
- # return True if 'mp4' in self.formats else False
- #
- # def has_format_webm(self):
- # return True if 'webm' in self.formats else False
- #
- # def has_format_ogv(self):
- # return True if 'ogv' in self.formats else False
- #
- # def get_format_url(self, format):
- # basefile, ext = os.path.splitext(self.file.name)
- # newfile = os.path.join(basefile, format)
- #
- # filepath, ext = os.path.splitext(basefile)
- # formatfile =
- #
- #
- # def get_format_mp4(self):
- # pass
-
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
- full_res = True
- full_out = ''
+ # 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 = None
for extension in filer_settings.FILER_VIDEO_FORMATS:
- #only set new dimensions if diferent from the original and not zero
- if self.width and self.height and (
- self.width != self.original_width or self.height != self.original_height):
- new_dimensions = "%sx%s" % (self.width, self.height)
- else:
- new_dimensions = ""
res, out = convert_video(original_path, path, extension, new_dimensions)
- res = res or full_res
- full_out += out
- res, out = grab_poster(original_path, path)
- res = res or full_res
- full_out += out
- return full_res, full_out
-
-
-
- #def get_video_flv_url(self):
- #return self.file.formats_storage.url(self.flv())
-
+ 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)
View
@@ -24,7 +24,7 @@
'16', '32', '48', '64',
)
-# This is an ordered iterable that describes a list of
+# This is an ordered iterable that describes a list of
# classes that I should check for when adding files
FILER_FILE_MODELS = getattr(settings, 'FILER_FILE_MODELS',
(
@@ -120,5 +120,7 @@
FFMPEG_CHECK_CMD = "ffmpeg -i %(input_file)s"
# command line for video conversion
FFMPEG_CMD = "ffmpeg -i %(input_file)s -y -b 2326k -ar 44100 -ab 224k -ac 2 -f %(format)s %(dimensions)s %(target_file)s"
+# argument for setting the size in ffmpeg
+FFMPEG_SIZE_ARGUMENT = "-s %(dimensions)s"
# command line for grabbing preview image from video
-GRABIMG_CMD = "ffmpeg -y -i %(input_file)s -vframes 1 -ss 00:00:05 -an -vcodec png -f rawvideo -s 100x76 %(target_file)s"
+GRABIMG_CMD = "ffmpeg -y -i %(input_file)s -vframes 1 -ss 00:00:05 -an -vcodec png -f rawvideo %(dimensions)s %(target_file)s"
View
@@ -4,23 +4,25 @@
include conversion of video formats, extraction of information and creation
of poster image file.
"""
-
+import commands
import os
import re
-import sys
-import commands
+import sys
+import traceback
from filer import settings as filer_settings
FFMPEG_DIMENSIONS_RE = re.compile(r'Stream.*Video.*([0-9]{3,})x([0-9]{3,})')
+
def get_dimensions(sourcefile):
- """ Returns the video dimensions for a video file """
+ """Returns the video dimensions for a video file"""
ffmpeg = filer_settings.FFMPEG_CHECK_CMD % {'input_file': commands.mkarg(sourcefile)}
try:
ffmpegresult = commands.getoutput(ffmpeg)
return get_dimensions_from_output(ffmpegresult)
except:
- pass
+ return 0, 0
+
def get_dimensions_from_output(ffmpeg_output):
""" Returns the video dimensions from the output of FFMPEG """
@@ -30,6 +32,31 @@ def get_dimensions_from_output(ffmpeg_output):
else:
return 0, 0
+
+def get_size_arg(dimensions):
+ if not dimensions:
+ return ''
+ return filer_settings.FFMPEG_SIZE_ARGUMENT % {'dimensions': commands.mkarg(dimensions)}
+
+
+def execute_ffmpeg_command(com, targetfile):
+ try:
+ ffmpegresult = commands.getoutput(com)
+ # Check if file exists and is > 0 Bytes
+ try:
+ s = os.stat(targetfile)
+ fsize = s.st_size
+ if (fsize == 0):
+ os.remove(targetfile)
+ return True, "\n".join([com, ffmpegresult])
+ except:
+ return True, traceback.format_exc()
+ #return True, "\n".join([com, ffmpegresult])
+ except:
+ return True, traceback.format_exc()
+ return False, ffmpegresult
+
+
def convert_video(sourcefile, path, extension, dimensions):
"""returns True, msg if error or False, msg if ok"""
original_path, filename = os.path.split(sourcefile)
@@ -39,34 +66,17 @@ def convert_video(sourcefile, path, extension, dimensions):
convfilename = "%s.%s" % (filebase, extension)
targetfile = os.path.join(path, convfilename)
if not os.path.exists(path):
- os.makedirs(path)
- if dimensions:
- dimensions_cmd = "-s %s" % dimensions
- else:
- dimensions_cmd = ""
+ os.makedirs(path)
cmd_options = {'input_file': commands.mkarg(sourcefile),
'format': commands.mkarg(extension),
- 'dimensions': dimensions_cmd,
- 'target_file': commands.mkarg(targetfile) }
+ 'dimensions': get_size_arg(dimensions),
+ 'target_file': commands.mkarg(targetfile)
+ }
ffmpeg = filer_settings.FFMPEG_CMD % cmd_options
- #flvtool = "flvtool2 -U %s" % targetfile
- try:
- ffmpegresult = commands.getoutput(ffmpeg)
- # Check if file exists and is > 0 Bytes
- try:
- s = os.stat(targetfile)
- fsize = s.st_size
- if (fsize == 0):
- os.remove(targetfile)
- return True, ffmpegresult
- except:
- return True, ffmpegresult
- #flvresult = commands.getoutput(flvtool)
- except:
- return True, sys.exc_info[1]
- return False, ffmpegresult
+ return execute_ffmpeg_command(ffmpeg, targetfile)
+
-def grab_poster(sourcefile, path):
+def grab_poster(sourcefile, path, dimensions):
"""returns True, msg if error or False, msg if ok"""
original_path, filename = os.path.split(sourcefile)
if sourcefile is None:
@@ -77,11 +87,8 @@ def grab_poster(sourcefile, path):
if not os.path.exists(path):
os.makedirs(path)
cmd_options = {'input_file': commands.mkarg(sourcefile),
- 'target_file': commands.mkarg(thumbnailfile)}
+ 'target_file': commands.mkarg(thumbnailfile),
+ 'dimensions': get_size_arg(dimensions)
+ }
grabimage = filer_settings.GRABIMG_CMD % cmd_options
- #flvtool = "flvtool2 -U %s" % targetfile
- try:
- grab = commands.getoutput(grabimage)
- except:
- return True, sys.exc_info[1]
- return False, grab
+ return execute_ffmpeg_command(grabimage, thumbnailfile)

0 comments on commit 5926ff2

Please sign in to comment.