3

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 ?

Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
  • http://stackoverflow.com/questions/4778607/converting-video-files-uploaded-by-user-serving-it-using-django-python – catherine Mar 07 '13 at 06:36
  • @catherine:That helped some what,and it is taking a long time to convert the video to mp4, is there any ultra fast way of converting the videos to mp4 in python – Shiva Krishna Bavandla Mar 07 '13 at 06:45
  • http://stackoverflow.com/questions/1120707/using-python-to-execute-a-command-on-every-file-in-a-folder – catherine Mar 07 '13 at 07:18

1 Answers1

4
import subprocess
subprocess.call('ffmpeg -i video.flv video.mp4') 
varan
  • 795
  • 2
  • 10
  • 29
  • k that converts .flv video file in to mp4 right, but what about other formats like 3gp,MXF etc., because the user can able to upload any kind of videos(If he was not restricted to upload only MP4 format files :) ) during uploading right? so finally we need to convert the uploaded video of any format in to MP4 format, so that we can display that file directly by using video tag as above !!!! – Shiva Krishna Bavandla Mar 07 '13 at 06:32
  • Also it is taking a long time to convert the video to mp4, is there any ultra fast way of converting the videos to mp4 in python ? – Shiva Krishna Bavandla Mar 07 '13 at 06:45