Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix generating pdf with empty subtitles #10

Merged
merged 3 commits into from Jan 15, 2022

Conversation

EKarton
Copy link
Owner

@EKarton EKarton commented Jan 15, 2022

Description:

Problem:

When we have a video like https://www.youtube.com/watch?v=KN6OSdUfgyA with subtitles like
subtitles_8-old.txt, we get an exception thrown:

Number of frames: 103
Getting subtitles for each frame
Traceback (most recent call last):
  File "src/main.py", line 87, in <module>
    runner.run(sys.argv[1:])
  File "src/main.py", line 51, in run
    self.__run__(
  File "src/main.py", line 70, in __run__
    segments = segment_finder.get_subtitle_segments(subtitle_breaks)
  File "D:\Study\Sixth-Sem\Lecture-Video-to-PDF\src\subtitle_segment_finder.py", line 42, in get_subtitle_segments
    pos = self.__get_part_position_of_time_break__(time_break)
  File "D:\Study\Sixth-Sem\Lecture-Video-to-PDF\src\subtitle_segment_finder.py", line 122, in __get_part_position_of_time_break__
    if self.parts[right_part_index].text[right_part_char_index] == ".":
IndexError: string index out of range```

Cause:

This is because the subtitle file contains subtitle parts that has no content in it. For instance,

image

This causes the check of self.parts[right_part_index].text[right_part_char_index] to fail since right_part_index is a valid index in self.parts but since the text is empty and we first set right_part_char_index to 0, it will cause self.parts[right_part_index].text[right_part_char_index] to fail.

Solution:

Each subtitle part needs to have some text. A subtitle part with no text is redundant. Hence, when we parse the subtitles, we need to remove the subtitle parts that have no text.

Manual Test:

  1. Ran python3 src/main.py tests/videos/input_8.mp4 -s tests/subtitles/subtitles_8.1.vrt -o output.pdf. It did not crash, and it returned this PDF:

output.pdf

  1. Unfortunately, the subtitle file is not the best, and the subtitle at tests/subtitles/subtitles_8.srt is a better replacement for that video's subtitle. Running python3 src/main.py tests/videos/input_8.mp4 -s tests/subtitles/subtitles_8.srt -o output.pdf` produced this subtitle:

output.pdf

@EKarton EKarton marked this pull request as ready for review January 15, 2022 20:30
@EKarton EKarton merged commit 98e7525 into main Jan 15, 2022
@EKarton EKarton deleted the fix/fix-with-empty-subtitles branch January 15, 2022 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant