[BUGFIX] Fix chapters not working correctly on Windows

This commit is contained in:
Jesse Bannon 2023-04-19 18:13:55 -07:00
parent 9a5ee716cf
commit 96eb309f26
3 changed files with 16 additions and 59 deletions

View file

@ -302,13 +302,16 @@ class ChaptersPlugin(Plugin[ChaptersOptions]):
""" """
chapters = Chapters.from_empty() 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: 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, []): for comment in entry.kwargs_get(COMMENTS, []):
chapters = Chapters.from_string(comment.get("text", "")) chapters = Chapters.from_string(comment.get("text", ""))
if not chapters.is_empty(): if chapters.contains_any_chapters():
break break
if not chapters.is_empty(): # 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()}) entry.add_kwargs({YTDL_SUB_CUSTOM_CHAPTERS: chapters.to_file_metadata_dict()})
if not self.is_dry_run: if not self.is_dry_run:

View file

@ -164,14 +164,7 @@ class SplitByChaptersPlugin(Plugin[SplitByChaptersOptions]):
Tags the entry's audio file using values defined in the metadata options Tags the entry's audio file using values defined in the metadata options
""" """
split_videos_and_metadata: List[Tuple[Entry, FileMetadata]] = [] split_videos_and_metadata: List[Tuple[Entry, FileMetadata]] = []
if self.is_dry_run:
chapters = Chapters.from_entry_chapters(entry=entry) 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(),
)
# If no chapters, do not split anything # If no chapters, do not split anything
if not chapters.contains_any_chapters(): if not chapters.contains_any_chapters():

View file

@ -7,6 +7,7 @@ from typing import Optional
from typing import Tuple from typing import Tuple
from ytdl_sub.entries.entry import Entry from ytdl_sub.entries.entry import Entry
from ytdl_sub.entries.variables.kwargs import YTDL_SUB_CUSTOM_CHAPTERS, CHAPTERS
from ytdl_sub.utils.file_handler import FileMetadata from ytdl_sub.utils.file_handler import FileMetadata
@ -220,45 +221,6 @@ class Chapters:
return Chapters(timestamps=timestamps, titles=titles) 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 @classmethod
def from_entry_chapters(cls, entry: Entry) -> "Chapters": def from_entry_chapters(cls, entry: Entry) -> "Chapters":
""" """
@ -274,9 +236,8 @@ class Chapters:
timestamps: List[Timestamp] = [] timestamps: List[Timestamp] = []
titles: List[str] = [] titles: List[str] = []
chapters = {} # Try to get actual yt-dlp chapters first, then custom chapters, then default to empty list
if entry.kwargs_contains("chapters"): chapters = entry.kwargs_get(CHAPTERS, default=entry.kwargs_get(YTDL_SUB_CUSTOM_CHAPTERS, default=[]))
chapters = entry.kwargs("chapters") or []
for chapter in chapters: for chapter in chapters:
timestamps.append(Timestamp.from_seconds(int(float(chapter["start_time"])))) timestamps.append(Timestamp.from_seconds(int(float(chapter["start_time"]))))