Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Lots of fixes in the conversion methods

More pep8 nonconformances
  • Loading branch information...
commit 7a90fab38ee712c7527de01c9b232d878c01d66a 1 parent f4141bb
@nmfm authored
View
5 filer/management/commands/convert_video.py
@@ -1,6 +1,7 @@
from django.core.management.base import BaseCommand, CommandError
from filer.models import Video
+
class Command(BaseCommand):
args = ''
help = 'Processes next video conversion task'
@@ -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)
View
23 filer/models/videomodels.py
@@ -89,24 +89,19 @@ 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 = ''
+ 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.original_width or self.height != self.original_height):
+ (self.width, self.height) != (self.original_width, self.original_height)):
new_dimensions = "%sx%s" % (self.width, self.height)
else:
new_dimensions = ""
for extension in filer_settings.FILER_VIDEO_FORMATS:
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
8 filer/settings.py
@@ -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',
(
@@ -119,6 +119,8 @@
# command line for video checking of dimensions
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 -s %(target_file)s"
+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 %(dimensions)s %(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
75 filer/utils/video.py
@@ -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,30 +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)
+ os.makedirs(path)
cmd_options = {'input_file': commands.mkarg(sourcefile),
'format': commands.mkarg(extension),
- 'dimensions': commands.mkarg(dimensions),
- '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:
@@ -73,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)
Please sign in to comment.
Something went wrong with that request. Please try again.