Feature: Improve formatting for dialogues (-) and 3-line subtitles

Open
#13,161 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
csharp, javascript
Domain
content, desktop

Research direction

No project files or tests are named; the issue includes an HTML/JavaScript prototype for subtitle processing. Start by locating the subtitle formatting entry point and its existing tests, then compare the prototype's dialogue and multi-line cases with current behavior. Done means both requested formatting modes work without changing one- or two-line subtitles.

Written by the indexing model from the issue text.

Description

This issue proposes adding two new features to improve subtitle formatting:

  1. Dialogue Support (Lines starting with -)
    Currently, two-person dialogues are not handled properly. I have added a feature that detects lines starting with a dash (-) and splits each person's dialogue into a separate line.

before

Image

after

Image

3-Line to 2-Line Conversion**
I also added a feature that intelligently converts subtitles with 3 or more lines down to 2 lines, using punctuation rules and word count balancing.

Image

after

Image

I have already written and tested the code for these features. I would be happy to submit a Pull Request (PR) to add this to the project if you are interested!

<title>Advanced Subtitle Fixer</title> <style> body { font-family: Tahoma, sans-serif; padding: 20px; background: #f4f4f9; } textarea { width: 100%; height: 250px; margin-bottom: 10px; direction: ltr; padding: 10px; font-family: monospace; font-size: 14px; } button { padding: 12px 24px; font-size: 16px; cursor: pointer; background: #28a745; color: white; border: none; border-radius: 5px; } button:hover { background: #218838; } </style>

3-Line to 2-Line Subtitle Converter (Dialogue Support -)

Paste your subtitle file in the box below. This version detects two-person conversations starting with a dash (-) and places each person on a separate line:

<textarea id="inputSrt" placeholder="Paste the original subtitle here..."></textarea>
<button onclick="fixSrt()">Smart Process Subtitle</button>
<textarea id="outputSrt" placeholder="The final subtitle will be displayed here..." readonly></textarea>

<script>
    function fixSrt() {
        const text = document.getElementById('inputSrt').value;
        // Separate subtitle blocks completely independently
        const blocks = text.trim().split(/\r?\n\s*\r?\n/);
        
        const processed = blocks.map(block => {
            const lines = block.split(/\r?\n/);
            
            // If the subtitle has 3 or more lines of text
            if (lines.length > 4) {
                const index = lines[0];
                const time = lines[1];
                const textLines = lines.slice(2);
                
                let line1 = "";
                let line2 = "";
                let isDialogSplit = false;

                // Golden rule: Check for a two-person conversation (find the line starting with -)
                for (let i = 1; i < textLines.length; i++) {
                    // If the line starts with a dash (might have leading spaces)
                    if (textLines[i].trim().match(/^[-–—]/)) {
                        line1 = textLines.slice(0, i).join(" ").trim();
                        line2 = textLines.slice(i).join(" ").trim();
                        isDialogSplit = true;
                        break; // Found it, so we break here
                    }
                }

                // If not a two-person conversation, move to punctuation rules (period, comma)
                if (!isDialogSplit) {
                    let splitIndex = -1;
                    let maxScore = -1;
                    let minDiff = Infinity;
                    
                    const totalWords = textLines.join(" ").split(/\s+/).length;

                    for (let i = 0; i < textLines.length - 1; i++) {
                        const line = textLines[i].trim();
                        const wordsSoFar = textLines.slice(0, i + 1).join(" ").split(/\s+/).length;
                        const diff = Math.abs(wordsSoFar - (totalWords / 2));
                        
                        let score = 0;
                        if (line.match(/[.?!]["']?$/)) score = 2; 
                        else if (line.match(/[,;:]["']?$/)) score = 1; 
                        
                        if (score > maxScore || (score === maxScore && diff < minDiff)) {
                            maxScore = score;
                            minDiff = diff;
                            splitIndex = i;
                        }
                    }

                    if (maxScore > 0) {
                        line1 = textLines.slice(0, splitIndex + 1).join(" ").trim();
                        line2 = textLines.slice(splitIndex + 1).join(" ").trim();
                    } else {
                        const fullText = textLines.join(" ").trim();
                        const words = fullText.split(/\s+/);
                        const mid = Math.ceil(words.length / 2);
                        line1 = words.slice(0, mid).join(" ");
                        line2 = words.slice(mid).join(" ");
                    }
                }
                
                return `${index}\n${time}\n${line1}\n${line2}`;
            }
            // 1 and 2-line subtitles remain completely untouched
            return block;
        });
        
        document.getElementById('outputSrt').value = processed.join("\n\n");
    }
</script>
Dominant language
C#
Stars
14.3k
Forks
1.3k
Avg merge
1h 19m
Merged PRs (30d)
782

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from SubtitleEdit/subtitleedit

All issues in SubtitleEdit/subtitleedit

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.