From fe185974d31c15351131b6ee2bf991a1e3cec2f2 Mon Sep 17 00:00:00 2001 From: Blondel MONDESIR <16546989+deldesir@users.noreply.github.com> Date: Fri, 15 Sep 2023 09:58:55 -0400 Subject: [PATCH] Generate covers Generate a cropped cover when uploading a video or a picture. --- cps/uploader.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/cps/uploader.py b/cps/uploader.py index 23dfc4a6..9b489913 100644 --- a/cps/uploader.py +++ b/cps/uploader.py @@ -18,6 +18,8 @@ import os import hashlib +import shutil +from subprocess import run from tempfile import gettempdir from flask_babel import gettext as _ @@ -84,6 +86,11 @@ def process(tmp_file_path, original_file_name, original_file_extension, rar_exec original_file_name, original_file_extension, rar_executable) + elif extension_upper in ['.MP4', '.WEBM', '.AVI', '.MKV', '.M4V', '.MPG', '.MPEG','.OGV']: + meta = video_metadata(tmp_file_path, original_file_name, original_file_extension) + elif extension_upper in ['.JPG', '.JPEG', '.PNG', '.GIF', '.SVG', '.WEBP']: + meta = image_metadata(tmp_file_path, original_file_name, original_file_extension) + except Exception as ex: log.warning('cannot parse metadata, using default: %s', ex) @@ -238,6 +245,63 @@ def pdf_preview(tmp_file_path, tmp_dir): log.warning('On Windows this error could be caused by missing ghostscript') return None +def video_metadata(tmp_file_path, original_file_name, original_file_extension): + video_cover(tmp_file_path) + meta = BookMeta( + file_path=tmp_file_path, + extension=original_file_extension, + title=original_file_name, + author='Unknown', + cover=os.path.splitext(tmp_file_path)[0] + '.cover.jpg', + description='', + tags='', + series="", + series_id="", + languages="", + publisher="", + pubdate="", + identifiers=[]) + return meta + + +def video_cover(tmp_file_path): + """generate cover image from video using ffmpeg""" + ffmpeg_executable = os.getenv('FFMPEG_PATH', 'ffmpeg') + ffmpeg_output_file = os.path.splitext(tmp_file_path)[0] + '.cover.jpg' + ffmpeg_args = [ + ffmpeg_executable, + '-i', tmp_file_path, + '-vframes', '1', + '-y', ffmpeg_output_file + ] + + try: + ffmpeg_result = run(ffmpeg_args, capture_output=True, check=True) + log.debug(f"ffmpeg output: {ffmpeg_result.stdout}") + + except Exception as e: + log.warning(f"ffmpeg failed: {e}") + return None + + +def image_metadata(tmp_file_path, original_file_name, original_file_extension): + shutil.copyfile(tmp_file_path, os.path.splitext(tmp_file_path)[0] + '.cover.jpg') + meta = BookMeta( + file_path=tmp_file_path, + extension=original_file_extension, + title=original_file_name, + author='Unknown', + cover=os.path.splitext(tmp_file_path)[0] + '.cover.jpg', + description='', + tags='', + series="", + series_id="", + languages="", + publisher="", + pubdate="", + identifiers=[]) + return meta + def get_magick_version(): ret = dict()