Shiva Krishna Bavandla
Shiva Krishna Bavandla

Reputation: 26668

convert the videos of any format(flv,3gp,MXF etc.,) to MP4 in django using python

I am using django and very newbie in using it.Presently building a basic uploads page, that uploads the videos to hard disk(basically in to media folder inside the django project)

So my code is working fine and videos are uploaded successfully in to media folder.

And I am displaying all those uploaded videos on a separate html page, using html video tag, so actually the problem is the videos which are in the format of MP4 are playing and remaining are not.

Below are some of my partial html code and view functions

uploads_list.html

<div style="padding-top:90px;">
  <video id="vid" width="350" height="250" controls="controls">
           <source src="{{MEDIA_URL}}videos/django_cms-_frontend_editing_1280x720.mp4" type="video/mp4" />       
   </video>
  <video id="vid_2" width="350" height="223" controls="controls">
           <source src="{{MEDIA_URL}}videos/1100000.flv" type="video/ogg" />       
   </video>
</div>

So finally what my intention is, to convert all the uploaded videos to MP4 format and store in the media folder during uploading, so that we can display all those videos using html video tag.

views.py

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid() and form.is_multipart():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponseRedirect('/uploads_list')  # which renders `uploads_list.html` page
    else:
        form = UploadFileForm()
    return render_to_response('uploads_form.html', {'form': form,'total_files':os.listdir(settings.MEDIA_ROOT),'path':settings.MEDIA_ROOT},context_instance=RequestContext(request))

def handle_uploaded_file(file,path=''):
    filename = file._get_name()
    destination_file = open('%s/%s' % (settings.MEDIA_ROOT, str(path) + str(filename)), 'wb+')
    for chunk in file.chunks():
        destination_file.write(chunk)
    destination_file.close()

So can anyone please let me know, how to convert the uploaded video which is of any kind(format like flv,3gp,MXF etc.,) to MP4 format before storing in to media folder in django ?

Upvotes: 3

Views: 6755

Answers (1)

varan
varan

Reputation: 354

import subprocess
subprocess.call('ffmpeg -i video.flv video.mp4') 

Upvotes: 4

Related Questions