Permalink
Browse files

Added remaining files (still learning how to use git)

  • Loading branch information...
1 parent 2178d8c commit d4fc60a3c8a05043915f3e73a53f4977a22635e5 @nmfm committed Oct 25, 2011
Showing with 152 additions and 0 deletions.
  1. +53 −0 filer/admin/videoadmin.py
  2. +23 −0 filer/management/commands/convert_video.py
  3. +76 −0 filer/models/video.py
@@ -0,0 +1,53 @@
+#-*- coding: utf-8 -*-
+from django import forms
+from django.utils.translation import ugettext as _
+from filer import settings as filer_settings
+from filer.admin.fileadmin import FileAdmin
+from filer.models import Video
+
+
+class VideoAdminFrom(forms.ModelForm):
+ #subject_location = forms.CharField(
+ #max_length=64, required=False,
+ #label=_('Subject location'),
+ #help_text=_('Location of the main subject of the scene.'))
+
+ def sidebar_image_ratio(self):
+ if self.instance:
+ # this is very important. It forces the value to be returned as a
+ # string and always with a "." as seperator. If the conversion
+ # from float to string is done in the template, the locale will
+ # be used and in some cases there would be a "," instead of ".".
+ # javascript would parse that to an integer.
+ return "%.6F" % self.instance.sidebar_image_ratio()
+ else:
+ return ''
+
+ class Meta:
+ model = Video
+
+ class Media:
+ css = {
+ #'all': (settings.MEDIA_URL + 'filer/css/focal_point.css',)
+ }
+ js = (
+ filer_settings.FILER_STATICMEDIA_PREFIX + 'js/raphael.js',
+ filer_settings.FILER_STATICMEDIA_PREFIX + 'js/focal_point.js',
+ )
+
+
+class VideoAdmin(FileAdmin):
+ form = VideoAdminFrom
+ fieldsets = (
+ FileAdmin.fieldsets[0],
+ (_('Advanced'), {
+ 'fields': ('status', 'file', 'sha1', 'output'),
+ #'fields': ('default_alt_text', 'default_caption',
+ #'author', 'file', 'sha1',),
+ 'classes': ('collapse',),
+ }),
+ #('Subject Location', {
+ #'fields': ('subject_location',),
+ #'classes': ('collapse',),
+ #}),
+ )
@@ -0,0 +1,23 @@
+from django.core.management.base import BaseCommand, CommandError
+from filer.models.videomodels import Video
+
+class Command(BaseCommand):
+ args = ''
+ help = 'process next video conversion task'
+
+ def handle(self, *args, **options):
+ try:
+ vid = Video.objects.filter(status='new')[0:1].get()
+ except Video.DoesNotExist:
+ self.stdout.write("No more tasks\n")
+ return
+ vid.status = 'process'
+ vid.save()
+ result, output = vid.convert()
+ if result:
+ vid.status = 'error'
+ else:
+ vid.status = 'ok'
+ vid.output = output
+ vid.save()
+ self.stdout.write("Processed %s\n" % vid.file.name)
View
@@ -0,0 +1,76 @@
+# encoding: utf-8
+"""handles all conversion tasks - originally only video but now also for PDF"""
+
+import os, sys, commands, os.path
+from django.conf import settings
+#from filebrowser.views import filebrowser_post_upload
+from imagemodels import Video
+
+def is_widescreen(path, full=False):
+ path = os.path.dirname(path) if full else path
+ return 'widescreen' in os.path.split(path) #lol
+
+
+#@receiver(post_save, sender=Video)
+#def post_upload_callback(sender, **kwargs):
+ #"""
+ #Receiver function called each time an upload has finished.
+
+ #kwargs['path'] is the URL path, not the FS path
+ #"""
+ #if kwargs['file'].filetype == "Video" and kwargs['file'].extension != '.flv':
+ #path = os.path.dirname(kwargs['file'].path_full)
+ #task = VideoTask(path=path, filename=kwargs['file'].filename, extension=kwargs['file'].extension, status='new')
+ #task.save()
+ ##elif kwargs['file'].filetype == "Document" and kwargs['file'].extension == '.pdf':
+ ##path = os.path.dirname(kwargs['file'].path_full)
+ ##task = PdfTask(path=path, filename=kwargs['file'].filename, extension=kwargs['file'].extension, status='new')
+ ##task.save()
+# FIXME HOOK
+#filebrowser_post_upload.connect(post_upload_callback)
+
+# Podia estar no método save do modelo :-P
+#from django.db.models.signals import post_save
+#from django.dispatch import receiver
+#@receiver(post_save, sender=Video)
+#def pre_save_callback(sender, instance, **kwargs):
+ #path = instance.original.path
+ #path, filename = os.path.split(path) #os.path.dirname(kwargs['file'].path_full)
+ #extension = os.path.splitext(filename)[1]
+ #task = VideoTask(path=path, filename=filename, extension=extension, status='new')
+ #task.save()
+
+def convert_video(original_path, path, filename, extension):
+ """returns True, msg if error or False, msg if ok"""
+ sourcefile = os.path.join(original_path, filename)
+ if sourcefile is None:
+ return True, "Sourcefile does not exist"
+ filebase = filename[:-len(extension)]
+ flvfilename = "%s.flv" % filebase
+ thumbnailfilename = "%s.png" % filebase
+ targetfile = os.path.join(path, flvfilename)
+ thumbnailfile = os.path.join(path, thumbnailfilename)
+
+ if not os.path.exists(path):
+ os.makedirs(path)
+ command_str = settings.FFMPEG_WIDE_CMD if is_widescreen(path) else settings.FFMPEG_CMD
+ ffmpeg = command_str % (commands.mkarg(sourcefile), commands.mkarg(targetfile))
+ grabimage = settings.GRABIMG_CMD % (commands.mkarg(sourcefile), commands.mkarg(thumbnailfile))
+ #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)
+ grab = commands.getoutput(grabimage)
+ except:
+ return True, sys.exc_info[1]
+ return False, "\n".join([ffmpegresult, grab])
+

0 comments on commit d4fc60a

Please sign in to comment.