Transcode video using celery and ffmpeg in django

738 views
Skip to first unread message

Robin Lery

unread,
Feb 20, 2015, 8:05:20 AM2/20/15
to django...@googlegroups.com
I would like to transcode user uploaded videos using celery. I think first I should upload the video, and spawn a celery task for transcoding.

Maybe something like this in the tasks.py:

    subprocess.call('ffmpeg -i path/.../original path/.../output')

Just completed First steps with celery, so confused how to do so in the `views.py` and `tasks.py`. Also is it a good solution? I would really appreciate your help and advice. Thank you.

models.py:

    class Video(models.Model):
        user = models.ForeignKey(User)
        title = models.CharField(max_length=100)
        original = models.FileField(upload_to=get_upload_file_name)
        mp4_480 = models.FileField(upload_to=get_upload_file_name)
        mp4_720 = models.FileField(upload_to=get_upload_file_name)
        privacy = models.CharField(max_length=1,choices=PRIVACY, default='F')
        pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)


my incomplete views.py:


    @login_required
    def upload_video(request):
        if request.method == 'POST':
            form = VideoForm(request.POST, request.FILES)
            if form.is_valid():
                if form.cleaned_data:
                    user = request.user
                    #
                    #
                    # No IDEA WHAT TO DO NEXT
                    #
                    #
                    return HttpResponseRedirect('/')

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

Vijay Khemlani

unread,
Feb 20, 2015, 8:19:31 AM2/20/15
to django...@googlegroups.com
I would do something like this

video = Video()
video.original = form.cleaned_data['video']  # Assuming the form field is "video"
video.user = user
video.title = form.cleaned_data['title']  # Assuming the form field is "title"
video.save()              # You might need to make the mp4_480 and mp4_720 fields "nullable" on the model
yourapp.tasks.transcode_video.delay(video)   # Assuming the task is the "transcode_video" function.

return HttpResponseRedirect('/')

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-nGoz%3D_qCx%2BbSnOfNFLYiufqUUES3S3T9y4GXoZiaBV9tyw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Vijay Khemlani

unread,
Feb 20, 2015, 8:19:58 AM2/20/15
to django...@googlegroups.com
Then in your task function you can get the file from the video model instance, trascode it, and store it in the corresponding fields of the object (mp4_480, mp4_720)

Robin Lery

unread,
Feb 20, 2015, 9:20:17 AM2/20/15
to django...@googlegroups.com
So mp4_480 and mp4_720 should be blank=True and null=True. And then pass the video instance object to the delay() ???

And also, could you please show me how to write the tasks.py, so that I can use ffmpeg code (subprocess.call('ffmpeg -i path/.../original path/.../mp4_720') to transcode.

Thank you.

Vijay Khemlani

unread,
Feb 20, 2015, 10:06:04 AM2/20/15
to django...@googlegroups.com
The task is just a function, you finished the tutorial, it's the same thing

@app.task
def trascode(video):
    subprocess.call('ffmpeg -i path/.../original path/.../mp4_720') # Change the path to the one given by video.original
    video.mp4_480 = # Something
    video.mp4_720 = # Something else
    video.save()

Reply all
Reply to author
Forward
0 new messages