diff --git a/modules/videos/captions.py b/modules/videos/captions.py index 5b60d83b..8f098c1a 100755 --- a/modules/videos/captions.py +++ b/modules/videos/captions.py @@ -1,5 +1,4 @@ #! /usr/bin/env python3 -import pathlib from pathlib import Path from typing import Generator, Union @@ -7,14 +6,12 @@ import srt import webvtt from wrolpi.common import logger -from wrolpi.media_path import MediaPath -from .models import Video + +__all__ = ['get_captions'] def get_caption_text(caption_path: Union[str, Path]) -> Generator: - """ - Return all text from each caption of a caption file. - """ + """Return all text from each caption of a caption file.""" if str(caption_path).endswith('vtt'): for caption in webvtt.read(caption_path): text = str(caption.text).strip() @@ -38,15 +35,8 @@ def get_unique_caption_lines(caption_path: Union[str, Path]) -> Generator: yield line -def get_video_captions(video: Video): - """ - Parse video captions from the video's captions file. - """ - if isinstance(video.caption_path, MediaPath): - caption_path = video.caption_path.path - else: - caption_path = pathlib.Path(video.caption_path) - +def get_captions(caption_path: Path): + """Parse video captions from the video's captions file.""" try: lines = get_unique_caption_lines(str(caption_path)) block = '\n'.join(lines) diff --git a/modules/videos/lib.py b/modules/videos/lib.py index f6610a84..f6cb4fab 100644 --- a/modules/videos/lib.py +++ b/modules/videos/lib.py @@ -14,7 +14,7 @@ from wrolpi.dates import from_timestamp, Seconds from wrolpi.db import get_db_curs, get_db_session, optional_session from wrolpi.media_path import MediaPath from wrolpi.vars import PYTEST -from .captions import get_video_captions +from .captions import get_captions from .common import generate_video_paths, remove_duplicate_video_paths, apply_info_json, import_videos_config, \ get_video_duration, is_valid_poster, convert_image, generate_video_poster from .models import Channel, Video @@ -188,7 +188,7 @@ def validate_video(video: Video, channel_generate_poster: bool): # Video duration was not in the info json, use ffprobe. video.duration = get_video_duration(video_path) if not video.caption and video.caption_path: - video.caption = get_video_captions(video) + video.caption = get_captions(video.caption_path.path) if not video.size or not video.modification_datetime: stat = video_path.stat() diff --git a/modules/videos/test/test_captions.py b/modules/videos/test/test_captions.py index 894e80d1..3dbc4cdb 100644 --- a/modules/videos/test/test_captions.py +++ b/modules/videos/test/test_captions.py @@ -1,3 +1,4 @@ +import shutil from pathlib import Path from modules.videos import captions @@ -46,11 +47,11 @@ class TestCaption(TestAPI): video1 = Video(title='scream', caption_path=str(self.vtt_path1)) session.add(video1) session.commit() - video1.caption = captions.get_video_captions(video1) + video1.caption = captions.get_captions(video1.caption_path.path) video2 = Video(title='bar', caption_path=str(self.vtt_path2)) session.add(video2) session.commit() - video2.caption = captions.get_video_captions(video2) + video2.caption = captions.get_captions(video2.caption_path.path) session.commit() session.flush() @@ -91,3 +92,13 @@ class TestCaption(TestAPI): # Matches video2.title select_textsearch('bar') self.assertEqual(curs.fetchall(), [[2, ]]) + + +def test_bad_caption(test_directory): + """A caption file that cannot be parsed is ignored.""" + bad_caption_file = PROJECT_DIR / 'test/bad_caption.en.vtt' + test_file = test_directory / 'bad_caption.en.vtt' + shutil.copy(bad_caption_file, test_file) + + result = captions.get_captions(test_file) + assert result is None diff --git a/requirements.txt b/requirements.txt index 9e681a9d..3127e2c4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,7 +15,7 @@ sanic-testing==0.8.2 sanic==21.12.1 selenium==4.1.0 SQLAlchemy==1.3.22 -srt==3.5.0 +srt==3.5.1 websockets==10.1 webvtt-py==0.4.6 yt-dlp==2022.1.21 diff --git a/test/bad_caption.en.vtt b/test/bad_caption.en.vtt new file mode 100644 index 00000000..463b74ef --- /dev/null +++ b/test/bad_caption.en.vtt @@ -0,0 +1,8 @@ +WEBVTT +Kind: captions +Language: en + +00:00:00.070 --> 00:00:06.380 +this caption is bad + +00:05:08.650 --> 00:05:11.26