コード例 #1
0
def prepare_and_store_movie(preview_file_id, uploaded_movie_path):
    """
    Prepare movie preview, normalize the movie as a .mp4, build the thumbnails
    and store the files.
    """
    from zou.app import app as current_app
    with current_app.app_context():
        project = get_project_from_preview_file(preview_file_id)
        fps = get_preview_file_fps(project)
        (width, height) = get_preview_file_dimensions(project)

        # Build movie
        current_app.logger.info("start normalization")
        try:
            (normalized_movie_path, normalized_movie_low_path,
             err) = movie.normalize_movie(uploaded_movie_path,
                                          fps=fps,
                                          width=width,
                                          height=height)

            if err:
                current_app.logger.error(
                    "Fail to add silent audiotrack to: %s" %
                    uploaded_movie_path)
                current_app.logger.error(err)

            current_app.logger.info("file normalized %s" %
                                    normalized_movie_path)
            file_store.add_movie("previews", preview_file_id,
                                 normalized_movie_path)
            file_store.add_movie("lowdef", preview_file_id,
                                 normalized_movie_low_path)
            current_app.logger.info("file stored")
        except Exception as exc:
            if isinstance(exc, ffmpeg.Error):
                current_app.logger.error(exc.stderr)
            current_app.logger.error("failed", exc_info=1)
            preview_file = set_preview_file_as_broken(preview_file_id)
            return preview_file

        # Build thumbnails
        size = movie.get_movie_size(normalized_movie_path)
        original_picture_path = \
            movie.generate_thumbnail(normalized_movie_path)
        tile_picture_path = \
            movie.generate_tile(normalized_movie_path)
        thumbnail_utils.resize(original_picture_path, size)
        save_variants(preview_file_id, original_picture_path)
        file_size = os.path.getsize(normalized_movie_path)
        current_app.logger.info("thumbnail created %s" % original_picture_path)

        # Remove files and update status
        os.remove(uploaded_movie_path)
        os.remove(normalized_movie_path)
        preview_file = update_preview_file(preview_file_id, {
            "status": "ready",
            "file_size": file_size
        })
        return preview_file
コード例 #2
0
def prepare_and_store_movie(preview_file_id, uploaded_movie_path):
    """
    Prepare movie preview, normalize the movie as a .mp4, build the thumbnails
    and store the files.
    """
    from zou.app import app as current_app

    with current_app.app_context():
        try:
            project = get_project_from_preview_file(preview_file_id)
        except PreviewFileNotFoundException:
            time.sleep(2)
            try:
                project = get_project_from_preview_file(preview_file_id)
            except PreviewFileNotFoundException:
                current_app.logger.error(
                    "Data is missing from database", exc_info=1
                )
                time.sleep(2)
                preview_file = set_preview_file_as_broken(preview_file_id)
                return preview_file

        fps = get_preview_file_fps(project)
        (width, height) = get_preview_file_dimensions(project)

        # Build movie
        current_app.logger.info("start normalization")
        try:
            if (
                config.ENABLE_JOB_QUEUE_REMOTE
                and len(config.JOB_QUEUE_NOMAD_NORMALIZE_JOB) > 0
            ):
                file_store.add_movie(
                    "source", preview_file_id, uploaded_movie_path
                )
                result = _run_remote_normalize_movie(
                    current_app, preview_file_id, fps, width, height
                )
                if result is True:
                    err = None
                else:
                    err = result

                normalized_movie_path = fs.get_file_path_and_file(
                    config,
                    file_store.get_local_movie_path,
                    file_store.open_movie,
                    "previews",
                    preview_file_id,
                    ".mp4",
                )
            else:
                (
                    normalized_movie_path,
                    normalized_movie_low_path,
                    err,
                ) = movie.normalize_movie(
                    uploaded_movie_path, fps=fps, width=width, height=height
                )
                file_store.add_movie(
                    "previews", preview_file_id, normalized_movie_path
                )
                file_store.add_movie(
                    "lowdef", preview_file_id, normalized_movie_low_path
                )
            if err:
                current_app.logger.error(
                    "Fail to normalize: %s" % uploaded_movie_path
                )
                current_app.logger.error(err)

            current_app.logger.info(
                "file normalized %s" % normalized_movie_path
            )
            current_app.logger.info("file stored")
        except Exception as exc:
            if isinstance(exc, ffmpeg.Error):
                current_app.logger.error(exc.stderr)
            current_app.logger.error("failed", exc_info=1)
            preview_file = set_preview_file_as_broken(preview_file_id)
            return preview_file

        # Build thumbnails
        size = movie.get_movie_size(normalized_movie_path)
        original_picture_path = movie.generate_thumbnail(normalized_movie_path)
        thumbnail_utils.resize(original_picture_path, size)
        save_variants(preview_file_id, original_picture_path)
        file_size = os.path.getsize(normalized_movie_path)
        current_app.logger.info("thumbnail created %s" % original_picture_path)

        # Remove files and update status
        os.remove(uploaded_movie_path)
        os.remove(normalized_movie_path)
        preview_file = update_preview_file(
            preview_file_id, {"status": "ready", "file_size": file_size}
        )
        return preview_file