def xconvert(self, sourcefile, targetfile): """Convert video to mp4 format""" errormsg = "" # ffmpeg command options: # -y answer yes to all queries # -v -1 be less verbose # -i sourcefile input file # -f flv output format is flv # -an no audio in output # -s geometry set size of output # # new options for conversion to h264/mp4 # ffmpeg -v -1 -i $1 -an -vcodec libx264 -vpre hq -crf 22 -threads 0 $1:r.mp4 # ffmpeg = [settings.FFMPEG_PROGRAM, "-y", "-v", "-1", "-i", sourcefile, "-vcodec", "libx264", "-an", "-vpre", "hq", "-crf", "22", "-threads", "0", targetfile] debug(" ".join(ffmpeg)) process = Popen(ffmpeg, stdout=PIPE, stderr=PIPE) start = time.time() while process.poll() == None: if time.time()-start > settings.FFMPEG_TIMEOUT: # we've gone over time, kill the process os.kill(process.pid, signal.SIGKILL) debug("Killing ffmpeg process") errormsg = "Conversion of video took too long. This site is only able to host relatively short videos." status = process.poll() #out,err = process.communicate() # Check if file exists and is > 0 Bytes try: s = os.stat(targetfile) fsize = s.st_size if (fsize == 0): os.remove(targetfile) errormsg = "Conversion of video failed: please try to use a diffent format" except: errormsg = "Conversion of video failed: please try to use a different video format" if errormsg: # we have a conversion error # notify the admins, attaching the offending file msgtxt = "Error: %s\n\nCommand: %s\n\n" % (errormsg, " ".join(ffmpeg)) message = EmailMessage("Video conversion failed", msgtxt, to=[a[1] for a in settings.ADMINS]) message.attach_file(sourcefile) message.send(fail_silently=False) # and raise a validation error for the caller raise ValidationError(errormsg)
def save(self, commit=True): debug("Saving a video form") debug("VideoName: %s" % (self.cleaned_data['video'], )) debug("Cleaned data: %s" % (self.cleaned_data, )) instance = super(PageVideoForm, self).save(commit=commit) debug("Instance video: %s" % instance.video) return instance
def save(self, commit=True): debug("Saving a video form") debug("VideoName: %s" % (self.cleaned_data['video'],)) debug("Cleaned data: %s" % (self.cleaned_data,)) instance = super(PageVideoForm, self).save(commit=commit) debug("Instance video: %s" % instance.video) return instance
def clean(self, data, initial=None): """Checks that the file is valid video and converts it to FLV format""" f = super(VideoUploadToFLVField, self).clean(data, initial) if f is None: return None elif not data and initial: return initial # We need the data to be in a real file for ffmpeg. # either it's already written out to a tmp file or # we have to do it here if hasattr(data, 'temporary_file_path'): tmpname = data.temporary_file_path() else: # need to store in-memory data out to a temp file if settings.FILE_UPLOAD_TEMP_DIR: (tmp, tmpname) = mkstemp(prefix=self.prefix, dir=settings.FILE_UPLOAD_TEMP_DIR) else: (tmp, tmpname) = mkstemp(prefix=self.prefix) for chunk in f.chunks(): os.write(tmp,chunk) os.close(tmp) # construct an mp4 filename mp4file = tmpname+".mp4" # now do the conversion to mp4 # will raise an error on failure self.convert(tmpname, mp4file) # we want to return an UploadedFile obj representing # the flv file, not the original but I can't # create one of those from an existing file # so I use my own wrapper class debug("Converted to mp4: " + mp4file) #os.unlink(tmpname) return UploadedFLVFile(mp4file)