diff --git a/src/ytdl_sub/plugins/chapters.py b/src/ytdl_sub/plugins/chapters.py index 084bc7d5..2385b8a9 100644 --- a/src/ytdl_sub/plugins/chapters.py +++ b/src/ytdl_sub/plugins/chapters.py @@ -302,21 +302,24 @@ class ChaptersPlugin(Plugin[ChaptersOptions]): """ chapters = Chapters.from_empty() + # If there are no embedded chapters, and comment chapters are allowed... if not _contains_any_chapters(entry) and self.plugin_options.allow_chapters_from_comments: + # Try to get chapters from comments for comment in entry.kwargs_get(COMMENTS, []): chapters = Chapters.from_string(comment.get("text", "")) - if not chapters.is_empty(): + if chapters.contains_any_chapters(): break - if not chapters.is_empty(): - entry.add_kwargs({YTDL_SUB_CUSTOM_CHAPTERS: chapters.to_file_metadata_dict()}) + # If some are actually found, add a special kwarg and embed them + if chapters.contains_any_chapters(): + entry.add_kwargs({YTDL_SUB_CUSTOM_CHAPTERS: chapters.to_file_metadata_dict()}) - if not self.is_dry_run: - set_ffmpeg_metadata_chapters( - file_path=entry.get_download_file_path(), - chapters=chapters, - file_duration_sec=entry.kwargs("duration"), - ) + if not self.is_dry_run: + set_ffmpeg_metadata_chapters( + file_path=entry.get_download_file_path(), + chapters=chapters, + file_duration_sec=entry.kwargs("duration"), + ) return entry diff --git a/src/ytdl_sub/plugins/split_by_chapters.py b/src/ytdl_sub/plugins/split_by_chapters.py index 447d6f25..201d02de 100644 --- a/src/ytdl_sub/plugins/split_by_chapters.py +++ b/src/ytdl_sub/plugins/split_by_chapters.py @@ -164,14 +164,7 @@ class SplitByChaptersPlugin(Plugin[SplitByChaptersOptions]): Tags the entry's audio file using values defined in the metadata options """ split_videos_and_metadata: List[Tuple[Entry, FileMetadata]] = [] - - if self.is_dry_run: - chapters = Chapters.from_entry_chapters(entry=entry) - else: - chapters = Chapters.from_embedded_chapters( - ffprobe_path=FFMPEG.ffprobe_path(), - file_path=entry.get_download_file_path(), - ) + chapters = Chapters.from_entry_chapters(entry=entry) # If no chapters, do not split anything if not chapters.contains_any_chapters(): diff --git a/src/ytdl_sub/utils/chapters.py b/src/ytdl_sub/utils/chapters.py index b3557774..785123ee 100644 --- a/src/ytdl_sub/utils/chapters.py +++ b/src/ytdl_sub/utils/chapters.py @@ -1,12 +1,12 @@ -import json import re -import subprocess from typing import Dict from typing import List from typing import Optional from typing import Tuple from ytdl_sub.entries.entry import Entry +from ytdl_sub.entries.variables.kwargs import CHAPTERS +from ytdl_sub.entries.variables.kwargs import YTDL_SUB_CUSTOM_CHAPTERS from ytdl_sub.utils.file_handler import FileMetadata @@ -220,45 +220,6 @@ class Chapters: return Chapters(timestamps=timestamps, titles=titles) - @classmethod - def from_embedded_chapters(cls, ffprobe_path: str, file_path: str) -> "Chapters": - """ - Parameters - ---------- - ffprobe_path - Path to ffprobe executable - file_path - File to read ffmpeg chapter metadata from - - Returns - ------- - Chapters object - """ - proc = subprocess.run( - [ - ffprobe_path, - "-loglevel", - "quiet", - "-print_format", - "json", - "-show_chapters", - "--", - file_path, - ], - check=True, - stdout=subprocess.PIPE, - encoding="utf-8", - ) - - embedded_chapters = json.loads(proc.stdout) - timestamps: List[Timestamp] = [] - titles: List[str] = [] - for chapter in embedded_chapters["chapters"]: - timestamps.append(Timestamp.from_seconds(int(float(chapter["start_time"])))) - titles.append(chapter["tags"]["title"]) - - return Chapters(timestamps=timestamps, titles=titles) - @classmethod def from_entry_chapters(cls, entry: Entry) -> "Chapters": """ @@ -274,9 +235,10 @@ class Chapters: timestamps: List[Timestamp] = [] titles: List[str] = [] - chapters = {} - if entry.kwargs_contains("chapters"): - chapters = entry.kwargs("chapters") or [] + # Try to get actual yt-dlp chapters first, then custom chapters, then default to empty list + chapters = entry.kwargs_get( + CHAPTERS, default=entry.kwargs_get(YTDL_SUB_CUSTOM_CHAPTERS, default=[]) + ) for chapter in chapters: timestamps.append(Timestamp.from_seconds(int(float(chapter["start_time"]))))