Topic: Subtitle Encoding and Conversion
modify below code and make ends 1 milisecond ealier than read from srt

import re
import subprocess

def burn_subtitles(video_path, ass_subtitle_path, output_video_path):
    command = [
        'ffmpeg',
        '-i', video_path,                       # Input video file
        '-vf', f"subtitles={ass_subtitle_path}", # Correct filter for subtitles
        '-c:a', 'copy',                          # Copy audio stream without re-encoding
        output_video_path                        # Output video file
    ]
    subprocess.run(command)

import re

import re

import re

def convert_srt_to_ass(srt_content):
    # ASS header
    ass_header = (
        "[Script Info]\n"
        "ScriptType: v4.00+\n"
        "PlayResX: 384\n"
        "PlayResY: 288\n\n"
        "[V4+ Styles]\n"
        "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n"
        "Style: Default,Arial,16,&H00FFFFFF,&H0000FF00,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,1,0,2,10,10,10,1\n\n"
        "[Events]\n"
        "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n"
    )

    ass_content = ass_header
    # Adjust regex to properly capture subtitle number, start time, end time, and text
    matches = list(re.finditer(r'(\d+)\n(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})\n(.+?)\n\n', srt_content, re.DOTALL))

    prev_end = "00:00:00.000"
    
    for i, match in enumerate(matches):
        start, end, text = match.group(2), match.group(3), match.group(4)
        start = start.replace(',', '.')
        end = end.replace(',', '.')

        # Calculate the correct start time to ensure no overlap
        if start <= prev_end:
            start = prev_end

        # Update prev_end to the end time of the current subtitle
        prev_end = end
        
        # Change color of currently spoken word (to green in this example)
        text = text.replace('<u>', '{\\c&H00FF00&}').replace('</u>', '{\\c&HFFFFFF&}')
        text = text.replace('\n', '\\N')  # Convert newlines within text for ASS format
        ass_content += f"Dialogue: 0,{start},{end},Default,,0,0,0,,{text}\n"

    return ass_content








srt_file_path = 'a.srt'  # Replace with the correct path to the SRT file

# Read the SRT file content
with open(srt_file_path, 'r', encoding='utf-8') as file:
    srt_content = file.read()

# Convert SRT to ASS
ass_content = convert_srt_to_ass(srt_content)

# Write the ASS content to a file
ass_file_path = 'a.ass'
with open(ass_file_path, 'w') as file:
    file.write(ass_content)

# Burn the subtitles onto the video
burn_subtitles('b.mp4', ass_file_path, 'c2.mp4')