简体   繁体   English

将新文件上传到模型文件字段

[英]Upload new file to the model filefield

I have a model for Video: 我有一个视频模型

class Video(models.Model):
    title = models.CharField(max_length=75)
    pubdate = models.DateTimeField(default=timezone.now)
    original_video = models.FileField(upload_to=get_upload_file_name)
    mp4_720 = models.FileField(upload_to=get_upload_file_name,blank=True, null=True)
    converted = models.BooleanField(default=False)

And this is the views.py : 这是views.py

def upload_video(request):
    if request.POST:
        form = VideoForm(request.POST, request.FILES)
        if form.is_valid():
            video = form.save(commit=False)
            video.save()
            convert_video.delay(video.id)
            return HttpResponseRedirect('/')

Lastly the tasks.py : 最后, task.py

def get_upload_file_name(video):
    name = video.title
    name = name+'.mp4'
    return name

from pyvid.settings import MEDIA_ROOT
@app.task
def convert_video(video_id):
    video = Video.objects.get(id=video_id)
    video_path = str(MEDIA_ROOT)+'/'+str(video.original_video)
    convert_video_name = get_upload_file_name(video)
    cmd = 'ffmpeg -i %s -codec:v libx264 -profile:v baseline -preset slow -b:v 250k -maxrate 250k -bufsize 500k -vf scale=-1:360 -threads 0 -codec:a libfdk_aac -movflags +faststart %s.mp4' % (video_path, convert_video_name)
    subprocess.call(
        cmd,
        shell=True
    )
    video.mp4_720 = convert_video_name
    video.converted = True
    video.save()

The problem is, even though video.mp4_720 is to upload by the upload_to=get_upload_file_name() , its just taking the value of convert_video_name file path (which is the converted video and is in the base directory of the project) but not uploading the new file to the path specified. 问题是,即使video.mp4_720是通过upload_to=get_upload_file_name()上传的,它也只是采用了convert_video_name文件路径(是转换后的视频,位于项目的基本目录中)的值,但没有上传新的文件到指定的路径。

How do I upload the new converted file in to the filefield of mp4_720 with the uploaded path? 如何上传新的转换后的文件中的的FileField mp4_720与上传的路径?

Thank you 谢谢

1. upload_to save image relative to your MEDIA_ROOT ( ref ). 1. upload_to保存相对于您的MEDIA_ROOT图像( 参考 )。 And this work only if you're uploading file using form but here, you are manually setting video.mp4_720 . 而且仅当您使用表单上传文件时才有效,但是在这里,您是手动设置video.mp4_720

2.While converting video, you should save output in MEDIA_ROOT (you should provide absolute url ) but you are just providing file name, Modify your code: 2.在转换视频时,应将输出保存在MEDIA_ROOT中(应提供绝对url),但仅提供文件名,请修改代码:

video_path = str(MEDIA_ROOT)+'/'+str(video.original_video)
convert_video_name = get_upload_file_name(video)
output_path = MEDIA_ROOT + '/' + convert_video_name
cmd = 'ffmpeg -i %s -codec:v libx264 -profile:v baseline -preset slow -b:v 250k -maxrate 250k -bufsize 500k -vf scale=-1:360 -threads 0 -codec:a libfdk_aac -movflags +faststart %s.mp4' % (video_path, output_path)
  1. If you are manually setting FileField, you should provide file path relating to your MEDIA_ROOT . 如果您手动设置FileField,则应提供与MEDIA_ROOT相关的文件路径。 Now your code should work as in statement video.mp4_720 = convert_video_name is already relative to MEDIA_ROOT . 现在,您的代码应按语句video.mp4_720 = convert_video_name相对于MEDIA_ROOT

I hope this will solve your problem. 我希望这能解决您的问题。 I have to tested the code. 我必须测试代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM