Upload new file to the uploaded file path

42 views
Skip to first unread message

Robin Lery

unread,
Aug 4, 2015, 4:46:15 PM8/4/15
to django...@googlegroups.com
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 view:

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 directed to upload_to=get_upload_file_name, its just taking the value of convert_video_name file path (which is in the base directory of the project) but not to the path applied.

How do I upload the new converted file it to the uploaded path?

Thank you

Tom Evans

unread,
Aug 5, 2015, 7:08:17 AM8/5/15
to django...@googlegroups.com
You asked this about 3 weeks ago as well (and replied to that email
today), did this approach not work?:

This is explained in the docs for FileField, you need to create a
django.core.files.File subclass, and save() it on the FieldFile object
that is returned when you access the FileField field on your model
instance.

Eg:

from django.core.files import File
fp = open('/path/to/file')
myfile = File(fp)
my_obj.mp4_720.save(name="....", content=myfile)

https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.fields.files.FieldFile.save

Cheers

Tom

Robin Lery

unread,
Aug 8, 2015, 8:13:55 AM8/8/15
to django...@googlegroups.com
It works perfectly! I am using it to upload in s3, and even for s3 it works as it is supposed to. I am really grateful to you.

Thank you.


Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1%2BxUVC0vnV2wctro46ZOfWrF53BjOj1odQdzg92SDvL0Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages