允许从Django下载部分视频文件

时间:2019-01-18 18:01:40

标签: python django ffmpeg moviepy

我有一个Django应用,允许用户制作/保存视频文件的“片段”,也就是将视频文件开始和结束时间的时间戳记保存在播放列表中。我希望用户能够下载他们保存的剪辑(以及播放列表)。

我已经对该主题进行了大量研究,并从终端上获取ffmpeg以保存部分剪辑(尽管我一直在努力找出使用ffmpeg的subprocess.call的路径名),而且我也得到了moviepy通过终端工作以保存部分剪辑。

我正在努力弄清楚如何将其整合到我的Django应用中,以便用户可以单击链接并随后开始下载特定剪辑(或播放列表)。我相信我会希望使用celery来避免捆绑服务器,但由于无法解决如何工作,我什至无法卸载任务。

  1. “剪切”较长的原始视频文件以使其更短(我需要首先制作新文件的本地副本吗?)。
  2. 将响应作为下载内容发送给用户。

这是我的模型。py:

class Match(models.Model):
    game_id = models.IntegerField(primary_key=True,
        validators=[MinValueValidator(1)],db_column="game_id")
    wide = models.FileField(upload_to='games/2018/',blank=True,null=True)

class Playlist(models.Model):
        name = models.CharField(default='',null=True,blank=True,max_length=200)
        created_by = models.ForeignKey(User,related_name="playlists",on_delete=models.CASCADE)
        created =   models.DateTimeField(editable=False)
        modified =  models.DateTimeField()


        def save(self, *args, **kwargs):
            ''' On save, update timestamps '''
            if not self.id:
                entries = Playlist.objects.order_by('-id')
                try:
                    self.id = entries[0].id + 1
                except IndexError:
                    # we don't have any PlaylistEntries yet, so we just start @ 0
                    self.id = 0
                self.created = timezone.now()
            self.modified = timezone.now()

            return super(Playlist, self).save(*args, **kwargs)


    class Clip(models.Model):
        name =  models.CharField(default='',null=True,blank=True,max_length=200)
        game_id = models.ForeignKey(Match,related_name="clips",on_delete=models.CASCADE,
            db_column="game_id",null=True,blank=True)
        clipstart = models.IntegerField(null=True,blank=True)
        clipend = models.IntegerField(null=True,blank=True)
        playlist = models.ForeignKey(Playlist,related_name="clips",on_delete=models.CASCADE,
            db_column="playlist",null=True,blank=True)
        created_by = models.ForeignKey(User,related_name="clips",on_delete=models.CASCADE)
        created =   models.DateTimeField(editable=False)
        modified =  models.DateTimeField()
        duration = models.IntegerField(null=True,blank=True)

        def save(self, *args, **kwargs):
            ''' On save, update timestamps '''
            if not self.id:
                entries = Clip.objects.order_by('-id')
                try:
                    self.id = entries[0].id + 1
                except IndexError:
                    # we don't have any PlaylistEntries yet, so we just start @ 0
                    self.id = 0
                self.created = timezone.now()
            self.modified = timezone.now()
            self.duration = int(self.clipend) - int(self.clipstart)
            return super(Clip, self).save(*args, **kwargs)

我正在努力挣扎的views.py(不确定我是否在正确使用FileField等):

from moviepy.editor import *
def ClipDownload(request,pk,*args,**kwargs):

    this_clip = models.Clip.objects.filter(pk=pk)
    file_name = this_clip[0].game_id.wide

    p = VideoFileClip(file_name,audio=False).subclip(this_clip.clipstart,this_clip.clipend)

    response = HttpResponse(p, content_type='application/force-download')
    response['Content-Disposition'] = 'attachment; filename=%s' % this_clip.name
    response['Content-Length'] = os.path.getsize(file_name)
    return response

当前代码无法正常工作,我获得了正确的Clip,但是由于出现属性错误,所以无法对FileField进行子剪辑:

AttributeError: 'FieldFile' object has no attribute 'endswith'

如果有人可以指出正确的方向下载文件,以便继续将其整合到芹菜中,我将不胜感激?

0 个答案:

没有答案