mirror of
https://github.com/janeczku/calibre-web
synced 2024-11-15 22:34:56 +00:00
Generate covers
Generate a cropped cover when uploading a video or a picture.
This commit is contained in:
parent
fe21e1154b
commit
fe185974d3
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user