Skip to content

Conversation

@Hupanbdfx
Copy link

@convert_path_to_string("filename")
def file_to_subtitles(filename, encoding=None):
"""Converts a srt file into subtitles.

The returned list is of the form ``[((start_time,end_time),'some text'),...]``
and can be fed to SubtitlesClip.

Only works for '.srt' format for the moment.
"""
times_texts = []
current_times = None
current_text = ""
with open(filename, "r", encoding=encoding) as file:
    for line in file:
        times = re.findall("([0-9]*:[0-9]*:[0-9]*,[0-9]*)", line)
        if times:
            current_times = [convert_to_seconds(t) for t in times]
        elif line.strip() == "":
            times_texts.append((current_times, current_text.strip("\n")))
            current_times, current_text = None, ""
        elif current_times:
            current_text += line
    # Fix the issue where the subtitle is not added to times_texts when it is the last line in an SRT file and the loop ends.
    if current_times is not None:
        times_texts.append((current_times, current_text.strip("\n")))
return times_texts

…is the last line in an SRT file and the loop ends.
@JPLeBreton
Copy link

Another bug I noticed can happen during this function is that the elif line.strip() == "": block can add a (time, text) tuple to the list where time is None, which will always cause a ValueError. Simply checking if current_times is None before appending will avoid that.

Cases I found where this could happen include multiple subtitles in a row that are a blank line.

@keikoro
Copy link
Collaborator

keikoro commented Sep 16, 2025

@JPLeBreton Are you asking @Hupanbdfx to adapt their PR? If there's something else that needs fixing, it shouldn't get lost in the depths of a PR comment chain.

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.

3 participants