From 15477e3fa52a75759818935ce53b96dbb8b11fe8 Mon Sep 17 00:00:00 2001 From: Jason Lyu Date: Tue, 12 Dec 2023 12:13:43 +0800 Subject: [PATCH] Feature: add chapters support (#11) --- Contents/Code/__init__.py | 21 +++++++++++++++++++++ Contents/Code/constants.py | 1 + Contents/DefaultPrefs.json | 6 ++++++ 3 files changed, 28 insertions(+) diff --git a/Contents/Code/__init__.py b/Contents/Code/__init__.py index 0fb44ac..5c70ee6 100644 --- a/Contents/Code/__init__.py +++ b/Contents/Code/__init__.py @@ -307,6 +307,7 @@ def get_media_files(obj): # Reviews metadata.reviews.clear() + # if None: # r = metadata.reviews.new() # r.author = review.get('critic') @@ -315,6 +316,26 @@ def get_media_files(obj): # r.link = review.get('link') # r.text = review.text + def get_media_durations(obj): + if hasattr(obj, 'all_parts'): + return [int(part.duration) for part in obj.all_parts() if hasattr(part, 'duration')] + + # Chapters + metadata.chapters.clear() + # only generate chapters if is a single video file + durations = get_media_durations(media) + if Prefs[KEY_ENABLE_CHAPTERS] and len(durations) == 1 \ + and durations[0] > 10 * 60 * 1000: + duration = durations[0] + interval = 5 * 60 * 1000 # every 5 minutes + for i, offset in enumerate(range(0, duration, interval)): + start, end = offset, offset + interval + chapter = metadata.chapters.new() + chapter.title = 'Chapter {i}'.format(i=(i + 1)) + chapter.start_time_offset = start + chapter.end_time_offset = (end if end < duration + else duration) + # Director metadata.directors.clear() if Prefs[KEY_ENABLE_DIRECTORS] and m.director: diff --git a/Contents/Code/constants.py b/Contents/Code/constants.py index 8d6f196..218d172 100644 --- a/Contents/Code/constants.py +++ b/Contents/Code/constants.py @@ -49,6 +49,7 @@ KEY_API_SERVER = 'api_server' KEY_API_TOKEN = 'api_token' KEY_ENABLE_COLLECTIONS = 'enable_collections' +KEY_ENABLE_CHAPTERS = 'enable_chapters' KEY_ENABLE_DIRECTORS = 'enable_directors' KEY_ENABLE_RATINGS = 'enable_ratings' KEY_ENABLE_TRAILERS = 'enable_trailers' diff --git a/Contents/DefaultPrefs.json b/Contents/DefaultPrefs.json index 86f031b..23946e3 100644 --- a/Contents/DefaultPrefs.json +++ b/Contents/DefaultPrefs.json @@ -17,6 +17,12 @@ "type": "bool", "default": "false" }, + { + "id": "enable_chapters", + "label": "Enable chapters", + "type": "bool", + "default": "true" + }, { "id": "enable_directors", "label": "Enable directors",