Esempio n. 1
0
    def make_small_video(self):
        from CNGT_scripts.python.resizeVideos import VideoResizer

        video_file_full_path = os.path.join(WRITABLE_FOLDER,
                                            str(self.videofile))
        try:
            resizer = VideoResizer([video_file_full_path], FFMPEG_PROGRAM, 180,
                                   0, 0)
            resizer.run()
        except:
            print("Error resizing video: ", video_file_full_path)
Esempio n. 2
0
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)
Esempio n. 3
0
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)
Esempio n. 4
0
def import_media(request,video):

    out = '<p>Imported</p><ul>'
    overwritten_files = '<p>Of these files, these were overwritten</p><ul>'
    errors = []

    if video:
        import_folder = settings.VIDEOS_TO_IMPORT_FOLDER
        goal_directory = settings.GLOSS_VIDEO_DIRECTORY
    else:
        import_folder = settings.IMAGES_TO_IMPORT_FOLDER
        goal_directory = settings.GLOSS_IMAGE_DIRECTORY


    for filename in os.listdir(import_folder):

        parts = filename.split('.')
        idgloss = '.'.join(parts[:-1])
        extension = parts[-1]

        try:
            gloss = Gloss.objects.get(annotation_idgloss=idgloss)
        except ObjectDoesNotExist:
            errors.append('Failed at '+filename+'. Could not find '+idgloss+'.')
            continue

        overwritten, was_allowed = save_media(import_folder,settings.WRITABLE_FOLDER+goal_directory+'/',gloss,extension)

        if not was_allowed:
            errors.append('Failed two overwrite '+gloss.annotation_idgloss+'. Maybe this file is not owned by the webserver?')
            continue

        out += '<li>'+filename+'</li>'

        # If it is a video also extract a still image and generate a thumbnail version
        if video:
            annotation_id = gloss.annotation_idgloss
            destination_folder = settings.WRITABLE_FOLDER+goal_directory+'/'+annotation_id[:2]+'/'
            video_filename = annotation_id+'-' + str(gloss.pk) + '.' + extension

            try:
                from CNGT_scripts.python.resizeVideos import VideoResizer
                from signbank.settings.server_specific import FFMPEG_PROGRAM
                resizer = VideoResizer([destination_folder+video_filename], FFMPEG_PROGRAM, 180, 0, 0)
                resizer.run()
            except ImportError as i:
                print(i.message)

            # Issue #255: generate still image
            try:
                from signbank.tools import generate_still_image
                generate_still_image(annotation_id[:2],
                                     destination_folder,
                                     video_filename)
            except ImportError as i:
                print(i.message)

        if overwritten:
            overwritten_files += '<li>'+filename+'</li>'

    overwritten_files += '</ul>'
    out += '</ul>'+overwritten_files

    if len(errors) > 0:
        out += '<p>Errors</p><ul>'

        for error in errors:
            out += '<li>'+error+'</li>'

        out += '</ul>'

    return HttpResponse(out)