mirror of
https://github.com/santinic/audiblez.git
synced 2025-08-31 22:09:26 +00:00
Merge pull request #42 from agleyzer/main
Improve cover image retrieval logic
This commit is contained in:
commit
02be53ff20
1 changed files with 24 additions and 3 deletions
27
audiblez.py
27
audiblez.py
|
@ -45,10 +45,10 @@ def main(kokoro, file_path, lang, voice, pick_manually, speed, providers):
|
|||
title = book.get_metadata('DC', 'title')[0][0]
|
||||
creator = book.get_metadata('DC', 'creator')[0][0]
|
||||
|
||||
cover_maybe = [c for c in book.get_items() if c.get_type() == ebooklib.ITEM_COVER]
|
||||
cover_image = cover_maybe[0].get_content() if cover_maybe else b""
|
||||
cover_maybe = find_cover(book)
|
||||
cover_image = cover_maybe.get_content() if cover_maybe else b""
|
||||
if cover_maybe:
|
||||
print(f'Found cover image {cover_maybe[0].file_name} in {cover_maybe[0].media_type} format')
|
||||
print(f'Found cover image {cover_maybe.file_name} in {cover_maybe.media_type} format')
|
||||
|
||||
intro = f'{title} by {creator}'
|
||||
print(intro)
|
||||
|
@ -105,6 +105,27 @@ def main(kokoro, file_path, lang, voice, pick_manually, speed, providers):
|
|||
create_index_file(title, creator, chapter_mp3_files, durations)
|
||||
create_m4b(chapter_mp3_files, filename, title, creator, cover_image)
|
||||
|
||||
def find_cover(book):
|
||||
def is_image(item):
|
||||
return item is not None and item.media_type.startswith('image/')
|
||||
|
||||
for item in book.get_items_of_type(ebooklib.ITEM_COVER):
|
||||
if is_image(item):
|
||||
return item
|
||||
|
||||
# https://idpf.org/forum/topic-715
|
||||
for meta in book.get_metadata('OPF', 'cover'):
|
||||
if is_image(item := book.get_item_with_id(meta[1]['content'])):
|
||||
return item
|
||||
|
||||
if is_image(item := book.get_item_with_id('cover')):
|
||||
return item
|
||||
|
||||
for item in book.get_items_of_type(ebooklib.ITEM_IMAGE):
|
||||
if 'cover' in item.get_name().lower() and is_image(item):
|
||||
return item
|
||||
|
||||
return None
|
||||
|
||||
def extract_texts(chapters):
|
||||
texts = []
|
||||
|
|
Loading…
Add table
Reference in a new issue