From 4474a8959080be8d113173aa2891f84aedc51610 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 1 Nov 2024 19:05:30 +0530 Subject: [PATCH] Fix media:duration only for last SMIL file Also fix serialization of clip timestamps incorrectly leading to negative durations. --- src/calibre/ebooks/oeb/polish/container.py | 5 +++-- src/calibre/ebooks/oeb/polish/tts.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/container.py b/src/calibre/ebooks/oeb/polish/container.py index a279ffb69b..9eb2d07b74 100644 --- a/src/calibre/ebooks/oeb/polish/container.py +++ b/src/calibre/ebooks/oeb/polish/container.py @@ -14,6 +14,7 @@ import uuid from collections import defaultdict from io import BytesIO from itertools import count +from math import floor from css_parser import getUrls, replaceUrls @@ -129,14 +130,14 @@ def href_to_name(href, root, base=None): def seconds_to_timestamp(duration: float) -> str: - seconds = int(duration) + seconds = int(floor(duration)) float_part = int((duration - seconds) * 1000) hours = seconds // 3600 minutes = (seconds % 3600) // 60 seconds = seconds % 60 ans = f'{hours:02d}:{minutes:02d}:{seconds:02d}' if float_part: - ans += f'.{float_part}' + ans += f'.{float_part}'.rstrip('0') return ans diff --git a/src/calibre/ebooks/oeb/polish/tts.py b/src/calibre/ebooks/oeb/polish/tts.py index ef064ee4ba..60bdef228d 100644 --- a/src/calibre/ebooks/oeb/polish/tts.py +++ b/src/calibre/ebooks/oeb/polish/tts.py @@ -502,8 +502,8 @@ def embed_tts(container, report_progress=None, callback_to_download_voices=None) snum = 0 size_of_audio_data = 0 mmap = {container.href_to_name(item.get('href'), container.opf_name):item for item in container.manifest_items} + duration_map = {} for name, pfd in name_map.items(): - duration_map = {} audio_map: dict[Sentence, tuple[bytes, float]] = {} for (lang, voice), sentences in pfd.key_map.items(): texts = tuple(s.text for s in sentences)