PageRenderTime 140ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/apps/video_convertor/models.py

https://github.com/HiPiH/life
Python | 142 lines | 102 code | 26 blank | 14 comment | 10 complexity | 16da30f6bff5a7ee07d724f0516ec020 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # coding: UTF-8
  2. from django.db import models
  3. from django.contrib.contenttypes.models import ContentType
  4. from django.contrib.contenttypes import generic
  5. from django.utils.translation import ugettext_lazy as _
  6. import datetime
  7. import os
  8. from django.conf import settings
  9. ERROR_TARGET_NOT_FOUND = 1
  10. ERROR_SOURCE_NOT_FOUND = 2
  11. class VideoManager(models.Manager):
  12. def related(self, obj):
  13. ct = ContentType.objects.get_for_model(obj)
  14. return self.filter(content_type = ct, object_id = obj.id)
  15. def related_and_processed(self, obj):
  16. return self.related(obj).filter(error_code=0, processed=True).order_by('date')
  17. def queue_for_converting(self):
  18. return self.filter(processed=False, error_code=0).order_by('date')
  19. class Video(models.Model):
  20. objects = VideoManager()
  21. video = models.FileField(_(u'video'), upload_to='video/')
  22. flv = models.FileField(_(u'flv'), max_length=255, upload_to='video/flv/', blank=True, null=True)
  23. processed = models.BooleanField(_(u'processed'), default=False)
  24. convert_in_process = models.BooleanField(_(u'processed'), default=False)
  25. thumb = models.ImageField(_('thumb'), upload_to='video/flv/thumb/')
  26. date = models.DateTimeField(_('date'), default=datetime.datetime.now)
  27. content_type = models.ForeignKey(ContentType, blank=True, null=True)
  28. object_id = models.PositiveIntegerField(blank=True, null=True)
  29. owner = generic.GenericForeignKey('content_type', 'object_id', )
  30. #size in Kb
  31. source_size = models.PositiveIntegerField(blank=True, default=0)
  32. #size in Kb
  33. target_size = models.PositiveIntegerField(blank=True, default=0)
  34. #secconds
  35. convert_time = models.PositiveIntegerField(blank=True, default=0)
  36. error_code = models.PositiveSmallIntegerField(default=0)
  37. class Meta:
  38. verbose_name = _(u'video')
  39. verbose_name_plural = _(u'videos')
  40. @models.permalink
  41. def get_absolute_url(self):
  42. return ('video_player', (), {'id': self.id})
  43. #
  44. # def delete(self):
  45. # #os.unlink(os.path.join(settings.MEDIA_ROOT, self.flv.name))
  46. # return super(Video, self).delete()
  47. #
  48. # def get_object(self):
  49. # return self.content_type.get_object_for_this_type(pk=self.object_id)
  50. def save(self):
  51. super(Video, self).save()
  52. def update_source_size(self):
  53. try:
  54. self.source_size = os.path.getsize(os.path.normpath(os.path.join(settings.MEDIA_ROOT, self.video.name)))/1024 #convert to Kb
  55. except:
  56. self.error_code = ERROR_TARGET_NOT_FOUND
  57. self.save()
  58. #
  59. def convert_to_flv(self, verbosity):
  60. if self.error_code!=0:
  61. return False
  62. dt_start = datetime.datetime.now()
  63. self.convert_in_process=True
  64. self.error_code=0
  65. self.save()
  66. try:
  67. if self.error_code==0:
  68. if verbosity==1:
  69. print "Start convert (%s)" % self.video.name
  70. #check dir exists
  71. dirs=[]
  72. dirs.append(os.path.join(settings.MEDIA_ROOT, 'video'))
  73. dirs.append(os.path.join(settings.MEDIA_ROOT, 'video','flv'))
  74. dirs.append(os.path.join(settings.MEDIA_ROOT, 'video','flv','thumb'))
  75. dirs.append(os.path.join(settings.MEDIA_ROOT, 'video','flv','thumb','tmp'))
  76. for dir in dirs:
  77. if not os.path.isdir(dir):
  78. os.mkdir(dir)
  79. source = os.path.normpath(os.path.join(settings.MEDIA_ROOT, self.video.name))
  80. video_name = os.path.splitext(os.path.split(self.video.name)[1])[0]
  81. target = "%s.flv" % os.path.normpath(os.path.join(settings.MEDIA_ROOT, 'video','flv', video_name))
  82. thumb = '%s.jpg' % os.path.normpath(os.path.join(settings.MEDIA_ROOT, 'video','flv','thumb', video_name))
  83. command = "mencoder '%s' -o '%s' -of lavf -oac mp3lame -lameopts abr:br=56 -srate 22050 -ovc lavc -lavcopts vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3" % (source, target,)
  84. convert_ret_code = os.system(command)
  85. os.chdir(os.path.normpath(os.path.join(settings.MEDIA_ROOT, 'video','flv','thumb','tmp')))
  86. command = "mplayer -ss 2 -frames 1 -vo jpeg: -nosound %s" % source
  87. thumb_ret_code = os.system(command)
  88. try:
  89. self.target_size = os.path.getsize(target)/1024 #convert to Kb
  90. except:
  91. self.error_code = ERROR_TARGET_NOT_FOUND
  92. if self.error_code==0:
  93. temp_thumb = os.path.normpath(os.path.join(settings.MEDIA_ROOT, 'video','flv','thumb','tmp', '00000001.jpg'))
  94. if os.path.isfile(temp_thumb):
  95. os.rename(temp_thumb, thumb)
  96. self.flv = os.path.join('video','flv','%s.%s') % (video_name, 'flv')
  97. self.thumb = os.path.join('video','flv','thumb','%s.%s') % (video_name, 'jpg')
  98. self.processed = True
  99. #Delete original
  100. self.video=""
  101. os.unlink(source)
  102. except:
  103. try:
  104. self.target_size = os.path.getsize(target)/1024 #convert to Kb
  105. except:
  106. self.error_code = ERROR_TARGET_NOT_FOUND
  107. finally:
  108. self.convert_in_process=False
  109. delta = datetime.datetime.now() - dt_start
  110. self.convert_time = delta.days*86400 + delta.seconds
  111. # dt_start =
  112. self.save()