def get_dims_from_ffmpeg_probe( self, probe_result: typing.Dict[str, typing.Any]) -> utils.ImgDims: """ Extract the width and height of the video stream from probe dict and return it """ video_stream_data = {} # type:typing.Dict[str,typing.Any] for stream_data in probe_result["streams"]: if stream_data["codec_type"] == "video": video_stream_data = stream_data if not video_stream_data: raise NoVideoStream return utils.ImgDims(width=video_stream_data["width"], height=video_stream_data["height"])
def _get_extraction_size(self, video_dims: utils.ImgDims, preview_dims: utils.ImgDims) -> utils.ImgDims: """ Compute extraction dimensions. The extract size in order to directly get the right height or width according to what is expected for preview :param video_dims: ImgDims object representing width and height of the video stream :param preview_dims: ImgDims object representing width and height of the preview to generate :return: ImgDims to use for ffmpeg video frame extraction """ extract_size = utils.ImgDims(-1, -1) if video_dims.ratio() > preview_dims.ratio(): extract_size.width = preview_dims.width else: extract_size.height = preview_dims.height return extract_size
def build_jpeg_preview(self, file_path: str, preview_name: str, cache_path: str, page_id: int, extension: str = '.jpg', size: utils.ImgDims = None, mimetype: str = '') -> None: """ generate the pdf small preview """ if not size: size = utils.ImgDims(256, 256) with open(file_path, 'rb') as pdf: # HACK - D.A. - 2017-08-11 Deactivate strict mode # This avoid crashes when PDF are not standard # See https://github.com/mstamy2/PyPDF2/issues/244 input_pdf = utils.get_decrypted_pdf(pdf, strict=False) output_pdf = PdfFileWriter() output_pdf.addPage(input_pdf.getPage(int(page_id))) output_stream = BytesIO() output_pdf.write(output_stream) output_stream.seek(0, 0) result = convert_pdf_to_jpeg(output_stream, size) if page_id == -1: preview_path = '{path}{file_name}{extension}'.format( file_name=preview_name, path=cache_path, extension=extension) else: preview_path = '{path}{file_name}{extension}'.format( file_name=preview_name, path=cache_path, page_id=page_id, extension=extension) with open(preview_path, 'wb') as jpeg: buffer = result.read(1024) while buffer: jpeg.write(buffer) buffer = result.read(1024)