Skip to content

Improve SRT Subtitle Generation for Less Granular Timing #8

@thisguymartin

Description

@thisguymartin

Description:

Issue:
Currently, our SRT subtitle generation function is precise but can result in subtitles with very granular timing. This can make the subtitles appear too rapidly and can be challenging for viewers to read. To enhance the user experience, we should modify the SRT generation function to provide subtitles with a more evenly spread timing.

Proposed Solution:
We propose modifying the SRT generation function to introduce a time interval parameter. This parameter will allow users to specify the desired time interval between subtitles. By default, we can set this interval to 3 seconds, ensuring that subtitles are spaced out and easier to read.

Benefits of the Proposed Solution:

  • Subtitles will be less granular and will have a more relaxed timing, improving readability.
  • Users will have the flexibility to adjust the timing interval as needed for their specific content.

Implementation Steps:

  1. Add a time_interval parameter to the SRT generation function, allowing users to specify the time interval between subtitles.
  2. Calculate the end time for each subtitle based on the specified time interval.
  3. Generate subtitles with the updated timing.
  4. Ensure backward compatibility by using a default time interval of 3 seconds if the parameter is not provided.

Additional Considerations:

  • Update documentation to inform users about the new time_interval parameter and its usage.
  • Test the modified function with various time intervals to ensure proper functionality.

Possible Code:

def convert_to_srt(transcript_data: dict, time_interval=3):
    """Convert AWS Transcribe JSON to SRT format with a specified time interval."""
    srt_string = ''
    counter = 1

    start_time = 0

    for item in transcript_data['results']['items']:
        if item['type'] == 'pronunciation':
            end_time = float(item['end_time'])
            content = item['alternatives'][0]['content']

            # Calculate the end time based on the specified time_interval
            end_time = start_time + time_interval

            # Format the timestamps
            start_timestamp = '{:02}:{:02}:{:02},{:03}'.format(
                int(start_time // 3600), int(start_time % 3600 // 60),
                int(start_time % 60), int(start_time % 1 * 1000))
            end_timestamp = '{:02}:{:02}:{:02},{:03}'.format(
                int(end_time // 3600), int(end_time % 3600 // 60),
                int(end_time % 60), int(end_time % 1 * 1000))

            # Append to the SRT string
            srt_string += f'{counter}\n{start_timestamp} --> {end_timestamp}\n{content}\n\n'
            counter += 1

            # Update the start time for the next interval
            start_time = end_time

    return srt_string

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions