Celery to process task and modify the model fields

79 views
Skip to first unread message

Robin Lery

unread,
Jul 14, 2015, 6:33:10 PM7/14/15
to django...@googlegroups.com

I would like to convert video into mp4 using ffmpeg and celery for the asynchronous task. When user uploads a video, it will be for the original_video and save it. After that I want celery to convert it into a different version for the mp4_720 field. However I am confused on how to apply that logic using celery.

app.models.py:

    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)

app.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()

                // Celery to convert the video
                convert_video.delay(video)

                return HttpResponseRedirect('/')
        else:
            form = VideoForm()
        return render(request, 'upload_video.html', {
            'form':form
        })

app.tasks.py:

    @app.task
    def convert_video(video):

        // Convert the original video into required format and save it in the mp4_720 field using the following command:
        //subprocess.call('ffmpeg -i (path of the original_video) (video for mp4_720)')

        // Change the converted boolean field to True

        // Save

Basically my question is how to save the converted video in mp4_720. Your help and guidance will be very much appreciated. Thank you.

Vijay Khemlani

unread,
Jul 14, 2015, 7:45:45 PM7/14/15
to django...@googlegroups.com
I remember this question from... february I think, what's changed since then?

--
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/CA%2B4-nGrKucaFSi7oZoOcHtOXdSsnPEac2_LXLZ2TccRCaWUhzA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Robin Lery

unread,
Jul 14, 2015, 8:16:12 PM7/14/15
to django...@googlegroups.com
Yes. That time you were the one to guide me. I was looking at other projects after that.

I am just confused on how to get the video.mp4_720 to be the converted video. Will you please help.

Thank you.

Tom Evans

unread,
Jul 15, 2015, 10:38:44 AM7/15/15
to django...@googlegroups.com
On Tue, Jul 14, 2015 at 9:15 PM, Robin Lery <robi...@gmail.com> wrote:
> Yes. That time you were the one to guide me. I was looking at other projects
> after that.
>
> I am just confused on how to get the video.mp4_720 to be the converted
> video. Will you please help.
>

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

Also, you should not pass objects as arguments to celery tasks, you
should instead pass the id of the object and fetch the object again in
the celery task.

Cheers

Tom

Robin Lery

unread,
Aug 4, 2015, 11:07:25 PM8/4/15
to django...@googlegroups.com
Hi,
Thank you so very much kind sir. This really helped!

--
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.
Reply all
Reply to author
Forward
0 new messages