File size: 7,369 Bytes
f36a283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8e57c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f36a283
 
 
 
 
 
 
 
 
 
 
 
 
 
d8e57c8
f36a283
 
d8e57c8
 
 
 
 
f36a283
 
 
 
 
 
 
d8e57c8
 
f36a283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8e57c8
f36a283
 
 
d8e57c8
 
 
f36a283
 
 
 
 
d8e57c8
 
f36a283
 
d8e57c8
f36a283
 
d8e57c8
 
f36a283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8e57c8
f36a283
 
 
 
 
 
 
 
 
 
 
d8e57c8
f36a283
 
d8e57c8
f36a283
 
 
 
 
 
d8e57c8
f36a283
 
 
 
 
 
 
 
 
 
d8e57c8
 
 
 
 
 
 
 
 
 
 
f36a283
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CTM to SRT Converter</title>
    <style>
        body {
            font-family: sans-serif;
            margin: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        textarea, input[type="file"] {
            width: 100%;
            margin-bottom: 10px;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            padding: 10px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }


        #downloadBtn {
            margin-top: 10px;
            background-color: #008CBA;
        }

        #downloadBtn:hover {
            background-color: #0077a3;
        }
        #ctmFileName{
             margin-bottom: 10px;
        }
         #srtOutput {
            margin-top: 10px;
            min-height: 200px; /* Added for better visibility */
        }
        #copyBtn {
           background-color: #2196F3; /* Different color for distinction */
           margin-left: 10px;      /* Space between buttons */
        }
        #copyBtn:hover{
           background-color: #0d8bf2;
        }

        /* Styling for the container holding the textarea and copy button */
        .output-container {
            display: flex;
            align-items: flex-start; /* Align items to the top */
            gap: 10px;             /* Space between textarea and button */
        }
        .output-container textarea {
            flex-grow: 1; /* Allow textarea to take up remaining space */
        }

    </style>
</head>
<body>

<h1>CTM to SRT Converter</h1>

<label for="ctmInput">Paste CTM Data:</label>
<textarea id="ctmInput" rows="10" placeholder="Paste your CTM data here..."></textarea>

<label for="ctmFile">Or Upload CTM File:</label>
<input type="file" id="ctmFile" accept=".ctm">
<div id="ctmFileName"></div>


<button id="convertBtn">Convert</button>
<button id="downloadBtn" class="hidden">Download SRT</button>

<h2>SRT Output</h2>
<div class="output-container">
    <textarea id="srtOutput" readonly></textarea>
    <button id="copyBtn">Copy</button>
</div>

<script>
    const ctmInput = document.getElementById('ctmInput');
    const ctmFile = document.getElementById('ctmFile');
    const ctmFileName = document.getElementById('ctmFileName');
    const convertBtn = document.getElementById('convertBtn');
    const downloadBtn = document.getElementById('downloadBtn');
    const srtOutput = document.getElementById('srtOutput'); // SRT output textarea
    const copyBtn = document.getElementById('copyBtn');   // Copy button
    let segments = [];
    let fileData = null; // Store file data

    function parseCTM(ctmData) {
        const lines = ctmData.trim().split('\n');
        const segments = [];

        for (const line of lines) {
            const parts = line.split(/\s+/);
            if (parts.length >= 5) {
                const segment = {
                    filename: parts[0],
                    channel: parts[1],
                    startTime: parseFloat(parts[2]),
                    duration: parseFloat(parts[3]),
                    text: parts.slice(4).join(' ').replace(/<space>/g, ' '),
                };
                segments.push(segment);
            } else {
                console.warn("Skipping invalid CTM line:", line);
            }
        }
        return segments;
    }

    function generateSRT(segments) {
        let srtContent = '';
        segments.forEach((segment, index) => {
            const startTime = formatTime(segment.startTime);
            const endTime = formatTime(segment.startTime + segment.duration);
            srtContent += `${index + 1}\n${startTime} --> ${endTime}\n${segment.text}\n\n`;
        });
        return srtContent;
    }

    function formatTime(seconds) {
        const date = new Date(seconds * 1000);
        const hh = date.getUTCHours().toString().padStart(2, '0');
        const mm = date.getUTCMinutes().toString().padStart(2, '0');
        const ss = date.getUTCSeconds().toString().padStart(2, '0');
        const ms = date.getUTCMilliseconds().toString().padStart(3, '0');
        return `${hh}:${mm}:${ss},${ms}`;
    }

    function processData() {
        let ctmData = null;

        if (fileData) {
            ctmData = fileData; // Prioritize file data
        } else {
            ctmData = ctmInput.value;
        }

        if (ctmData) {
            segments = parseCTM(ctmData);
            if (segments.length > 0) {
                const srtData = generateSRT(segments); // Generate SRT content
                srtOutput.value = srtData;          // Display in textarea
                downloadBtn.classList.remove('hidden');
            } else {
                srtOutput.value = 'No valid CTM data found.';
                downloadBtn.classList.add('hidden');
            }
        } else {
            srtOutput.value = 'No CTM data provided.';
            downloadBtn.classList.add('hidden');
        }
    }


     ctmFile.addEventListener('change', (event) => {
        const file = event.target.files[0];
        if (file) {
            ctmFileName.textContent = "Current CTM file: " + file.name;
            const reader = new FileReader();
            reader.onload = (e) => {
                fileData = e.target.result; // Store the file data
                // Don't process here, wait for the Convert button
            };
            reader.onerror = () => {
              console.error("Error reading CTM file.");
              srtOutput.value = 'Error reading CTM file.';
              downloadBtn.classList.add('hidden');
              fileData = null; // Reset on error
            };
            reader.readAsText(file);
        } else {
            // No file selected, clear stored data
            fileData = null;
            ctmFileName.textContent = "";
        }
    });

    convertBtn.addEventListener('click', processData);

    downloadBtn.addEventListener('click', () => {
        const srtData = srtOutput.value; // Get SRT data from textarea
        const blob = new Blob([srtData], { type: 'text/srt' });
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;

        let srtFilename = "transcript.srt";
        if (segments.length > 0 && segments[0].filename) {
            srtFilename = segments[0].filename + ".srt";
        }

        a.download = srtFilename;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
    });

    // Copy button functionality
    copyBtn.addEventListener('click', () => {
        srtOutput.select(); // Select the text in the textarea
        document.execCommand('copy'); // Execute the copy command
        // Optional: Provide user feedback (e.g., change button text)
        copyBtn.textContent = 'Copied!';
        setTimeout(() => {
           copyBtn.textContent = 'Copy';
        }, 2000);  // Reset after 2 seconds
    });

</script>

</body>
</html>