示例#1
0
    def create_post_media_video(cls, file, post_id, order):
        hash = sha256sum(file=file.file)
        video_backend = get_backend()

        if isinstance(file, InMemoryUploadedFile):
            # If its in memory, doing read shouldn't be an issue as the file should be small.
            in_disk_file = write_in_memory_file_to_disk(file)
            thumbnail_path = video_backend.get_thumbnail(
                video_path=in_disk_file.name, at_time=0.0)
        else:
            thumbnail_path = video_backend.get_thumbnail(
                video_path=file.file.name, at_time=0.0)

        with open(thumbnail_path, 'rb+') as thumbnail_file:
            post_video = cls.objects.create(
                file=file,
                post_id=post_id,
                hash=hash,
                thumbnail=File(thumbnail_file),
            )
        PostMedia.create_post_media(type=PostMedia.MEDIA_TYPE_VIDEO,
                                    content_object=post_video,
                                    post_id=post_id,
                                    order=order)
        return post_video
示例#2
0
    def create_thumbnail(self):

        if not self.videoFile:
            # no video file attached
            return

        new_video = self.videoFile

        encoding_backend = get_backend()

        at_time = new_video.duration / 2

        thumbnail_path = encoding_backend.get_thumbnail(new_video.path,
                                                        at_time=at_time)
        filename = Path(new_video.name).stem

        try:

            with open(thumbnail_path, 'rb') as file_handler:
                thumbnail_name = self.thumbnail.storage.get_alternative_name(
                    filename, '.jpg')
                self.thumbnail.save(thumbnail_name, File(file_handler))

        finally:
            os.unlink(thumbnail_path)
示例#3
0
    def create_thumbnail(self):

        if not self.videoFile:
            # no video file attached
            return

        new_video = self.videoFile

        encoding_backend = get_backend()

        duration = new_video.duration

        if duration < 1:
            at_time = duration / 2
        elif duration < 5:
            at_time = 2.5
        elif duration < 10:
            at_time = 5
        elif duration < 30:
            at_time = 15
        else:
            at_time = 30

        thumbnail_path = encoding_backend.get_thumbnail(new_video.path,
                                                        at_time=at_time)
        filename = Path(new_video.name).stem
        # + '.jpg'

        old_path = self.thumbnail.path
        base_url = settings.MEDIA_ROOT + '/images/default-thumbnail.jpg'

        try:
            if old_path != base_url:
                os.unlink(old_path)
        except FileNotFoundError:
            pass

        try:

            with open(thumbnail_path, 'rb') as file_handler:
                thumbnail_name = self.thumbnail.storage.get_alternative_name(
                    filename, '.jpg')
                self.thumbnail.save(thumbnail_name, File(file_handler))

        finally:
            os.unlink(thumbnail_path)
示例#4
0
def create_thumbnail(video_pk):
    video = Video.objects.get(pk=video_pk)
    if not video.file:
        # no video file attached
        return

    if video.thumbnail:
        # thumbnail has already been generated
        return

    encoding_backend = get_backend()
    thumbnail_path = encoding_backend.get_thumbnail(video.file.path)
    filename = os.path.basename(self.url),

    try:
        with open(thumbnail_path, 'rb') as file_handler:
            django_file = File(file_handler)
            video.thumbnail.save(filename, django_file)
        video.save()
    finally:
        os.unlink(thumbnail_path)
示例#5
0
def create_preview(video_pk: int) -> None:
    """
    Create preview image for the given video.

    :param video_pk: video primary key
    :return: None
    """
    from project.apps.video_archive.models import Video

    video = Video.objects.get(pk=video_pk)
    if not video.file:
        # no video file attached
        return
    if video.preview:
        # preview has already been generated
        return

    preview_path = get_backend().get_thumbnail(video.file.path)
    filename = os.path.basename(preview_path)

    with open(preview_path, 'rb') as file_handler:
        django_file = File(file_handler)
        video.preview.save(filename, django_file)
    video.save()