简体   繁体   中英

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 :

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 :

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.

How do I upload the new converted file in to the filefield of mp4_720 with the uploaded path?

Thank you

1. upload_to save image relative to your MEDIA_ROOT ( ref ). And this work only if you're uploading file using form but here, you are manually setting 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:

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 . Now your code should work as in statement video.mp4_720 = convert_video_name is already relative to MEDIA_ROOT .

I hope this will solve your problem. I have to tested the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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