def addvideo(request): """View to present a video upload form and process the upload""" if request.method == 'POST': form = VideoUploadForGlossForm(request.POST, request.FILES) if form.is_valid(): gloss_id = form.cleaned_data['gloss_id'] gloss = get_object_or_404(Gloss, pk=gloss_id) # deal with any existing video for this sign goal_folder = WRITABLE_FOLDER + GLOSS_VIDEO_DIRECTORY + '/' + gloss.idgloss[: 2] + '/' goal_filename = gloss.idgloss + '-' + str(gloss.pk) + '.mp4' goal_location = goal_folder + goal_filename vfile = form.cleaned_data['videofile'] vfile.name = gloss.idgloss + "-" + str(gloss.pk) + ".mp4" redirect_url = form.cleaned_data['redirect'] old_vid = GlossVideo.objects.filter(gloss_id=gloss_id) old_vid.first().delete() # make a new GlossVideo object for the new file video = GlossVideo(videofile=vfile, gloss=gloss) video.save() # Issue #162: log the upload history log_entry = GlossVideoHistory(action="upload", gloss=gloss, actor=request.user, uploadfile=vfile, goal_location=goal_location) log_entry.save() # TODO: provide some feedback that it worked (if # immediate display of video isn't working) return redirect(redirect_url) # if we can't process the form, just redirect back to the # referring page, should just be the case of hitting # Upload without choosing a file but could be # a malicious request, if no referrer, go back to root if 'HTTP_REFERER' in request.META: url = request.META['HTTP_REFERER'] else: url = '/' return redirect(url)
def handle(self, *args, **options): firsterror = True for gloss in Gloss.objects.all(): # Rename all the videos of a gloss to the correct form """ Using this function instead of GlossVideo.objects.all() because it holds the filenaming convention. Also this hopefully makes sure that we don't rename GlossVideos based on possibly outdated foreignkey data, because we want to name GlossVideos according to Gloss coupled with GlossVideo.pk. """ try: GlossVideo.rename_glosses_videos(gloss) except OSError as err: if firsterror: firsterror = False eprint("OSError, the following files did not exist:\n-------------------------------------------") eprint(err)
def handle(self, *args, **options): firsterror = True for gloss in Gloss.objects.all(): # Rename all the videos of a gloss to the correct form """ Using this function instead of GlossVideo.objects.all() because it holds the filenaming convention. Also this hopefully makes sure that we don't rename GlossVideos based on possibly outdated foreignkey data, because we want to name GlossVideos according to Gloss coupled with GlossVideo.pk. """ try: GlossVideo.rename_glosses_videos(gloss) except OSError as err: if firsterror: firsterror = False eprint( "OSError, the following files did not exist:\n-------------------------------------------" ) eprint(err)
def import_existing_gloss_videos(path): # delete all existing videos GlossVideo.objects.all().delete() # scan the directory and make an entry for each video file found for videofile in os.listdir(path): (idgloss, ext) = os.path.splitext(videofile) if ext in ['.mov', '.MOV', '.mp4']: fullpath = os.path.join(path, videofile) glosses = Gloss.objects.filter(idgloss=idgloss) if len(glosses) == 0 and '-' in idgloss: # try replacing the first - with : idgloss = idgloss.replace('-', ':', 1) glosses = Gloss.objects.filter(idgloss=idgloss) if len(glosses) == 1: gloss = glosses[0] print fullpath, gloss # replace apostrophe to make nicer URLs cleanname = videofile.replace("'", '1') h = open(fullpath) uf = UploadedFile(h, name=cleanname) gv = GlossVideo(gloss=gloss, videofile=uf) gv.save() else: print "gloss matches for", videofile, glosses else: print 'skipping ', videofile
def create_new_glossvideo(gloss, video_path, dry_run): # Backup the existing video objects stored in the database if not dry_run: existing_videos = GlossVideo.objects.filter(gloss=gloss) for video_object in existing_videos: video_object.reversion(revert=False) print("Gloss {} (id: {}): creating a new glossvideo.".format( gloss, gloss.id)) new_glossvideo = GlossVideo(gloss=gloss, videofile=video_path) if not dry_run: new_glossvideo.save() new_glossvideo.move_video()
def addvideo(request): """View to present a video upload form and process the upload""" if request.method == 'POST': form = VideoUploadForGlossForm(request.POST, request.FILES) if form.is_valid(): gloss_id = form.cleaned_data['gloss_id'] gloss = get_object_or_404(Gloss, pk=gloss_id) vfile = form.cleaned_data['videofile'] # construct a filename for the video, use sn # if present, otherwise use idgloss+gloss id if gloss.sn != None: vfile.name = str(gloss.sn) + ".mp4" else: vfile.name = gloss.idgloss + "-" + str(gloss.pk) + ".mp4" redirect_url = form.cleaned_data['redirect'] # deal with any existing video for this sign goal_folder = WRITABLE_FOLDER+GLOSS_VIDEO_DIRECTORY + '/' + gloss.idgloss[:2] + '/' goal_filename = gloss.idgloss + '-' + str(gloss.pk) + '.mp4' goal_location = goal_folder + goal_filename os.remove(goal_location) video = GlossVideo(videofile=vfile, gloss=gloss) video.save() # Issue #162: log the upload history log_entry = GlossVideoHistory(action="upload", gloss=gloss, actor=request.user, uploadfile=vfile, goal_location=goal_location) log_entry.save() # Issue #197: convert to thumbnail try: from CNGT_scripts.python.resizeVideos import VideoResizer resizer = VideoResizer([goal_location], FFMPEG_PROGRAM, 180, 0, 0) resizer.run() except ImportError as i: print("Error resizing video: ",i) # Issue #214: generate still image from signbank.tools import generate_still_image generate_still_image(gloss.idgloss[:2], goal_folder, goal_filename) # TODO: provide some feedback that it worked (if # immediate display of video isn't working) return redirect(redirect_url) # if we can't process the form, just redirect back to the # referring page, should just be the case of hitting # Upload without choosing a file but could be # a malicious request, if no referrer, go back to root if 'HTTP_REFERER' in request.META: url = request.META['HTTP_REFERER'] else: url = '/' return redirect(url)
def addvideo(request): """View to present a video upload form and process the upload""" if request.method == 'POST': form = VideoUploadForGlossForm(request.POST, request.FILES) if form.is_valid(): gloss_id = form.cleaned_data['gloss_id'] gloss = get_object_or_404(Gloss, pk=gloss_id) vfile = form.cleaned_data['videofile'] # construct a filename for the video, use sn # if present, otherwise use idgloss+gloss id if gloss.sn != None: vfile.name = str(gloss.sn) + ".mp4" else: vfile.name = gloss.idgloss + "-" + str(gloss.pk) + ".mp4" redirect_url = form.cleaned_data['redirect'] # deal with any existing video for this sign goal_folder = WRITABLE_FOLDER + GLOSS_VIDEO_DIRECTORY + '/' + gloss.idgloss[: 2] + '/' goal_filename = gloss.idgloss + '-' + str(gloss.pk) + '.mp4' goal_location = goal_folder + goal_filename goal_filename_small = gloss.idgloss + '-' + str( gloss.pk) + '_small' + '.mp4' goal_location_small = goal_folder + goal_filename_small if os.path.isfile(goal_location): os.remove(goal_location) if os.path.isfile(goal_location_small): os.remove(goal_location_small) # test for other video files for this gloss.pk with a different filename, such as version or old idgloss if os.path.isfile(goal_folder): file_listing = os.listdir(goal_folder) for fname in file_listing: if re.match('.*\-' + str(gloss.pk) + '\..*', fname): if os.path.isfile(goal_folder + fname): os.remove(goal_folder + fname) # clean up the database entry for an old file, if necessary video_links_count = GlossVideo.objects.filter(gloss=gloss).count() video_links_objects = GlossVideo.objects.filter(gloss=gloss) if video_links_count > 0: # delete the old video object links stored in the database for video_object in video_links_objects: video_object.delete() # make a new GlossVideo object for the new file video = GlossVideo(videofile=vfile, gloss=gloss) video.save() #Make sure the rights of the new file are okay if os.path.isfile(goal_location): os.chmod(goal_location, 0o660) # Issue #162: log the upload history log_entry = GlossVideoHistory(action="upload", gloss=gloss, actor=request.user, uploadfile=vfile, goal_location=goal_location) log_entry.save() # Issue #197: convert to thumbnail try: from CNGT_scripts.python.resizeVideos import VideoResizer resizer = VideoResizer([goal_location], FFMPEG_PROGRAM, 180, 0, 0) resizer.run() except ImportError as i: print("Error resizing video: ", i) # Issue #214: generate still image try: from signbank.tools import generate_still_image generate_still_image(gloss.idgloss[:2], goal_folder, goal_filename) except: print('Error generating still image') if os.path.isfile(goal_location_small): os.chmod(goal_location_small, 0o660) # TODO: provide some feedback that it worked (if # immediate display of video isn't working) return redirect(redirect_url) # if we can't process the form, just redirect back to the # referring page, should just be the case of hitting # Upload without choosing a file but could be # a malicious request, if no referrer, go back to root if 'HTTP_REFERER' in request.META: url = request.META['HTTP_REFERER'] else: url = '/' return redirect(url)
def get_gloss_videos(self): """Return a list of videos for a Gloss""" return GlossVideo.get_glosses_videos(self)
def has_videos(self): """Returns True if the Gloss has any videos (video.GlossVideo)""" return GlossVideo.gloss_has_videos(self)
def handle(self, *args, **options): for gloss in Gloss.objects.all(): print gloss.id, GlossVideo.get_glosses_videos(gloss)