Write me an html that displays srt file without the timestamps and the numbers
- User
Write me an html that displays srt file without the timestamps and the numbers
- Assistant
Write me an html that displays srt file without the timestamps and the numbers
Sure! Below is a complete HTML example that allows you to upload an SRT (SubRip Subtitle) file and displays its content without the numbers and timestamps. This is achieved using JavaScript to parse the SRT file and extract only the subtitle text.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SRT Subtitle Viewer</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
#subtitleDisplay {
white-space: pre-wrap; /* Preserves line breaks */
background-color: #f4f4f4;
padding: 15px;
border: 1px solid #ccc;
max-height: 500px;
overflow-y: auto;
}
.container {
max-width: 800px;
margin: auto;
}
input[type="file"] {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>SRT Subtitle Viewer</h1>
<input type="file" id="srtFileInput" accept=".srt">
<div id="subtitleDisplay">Upload an SRT file to display subtitles without numbers and timestamps.</div>
</div>
<script>
document.getElementById('srtFileInput').addEventListener('change', function(event) {
const file = event.target.files[0];
if (!file) {
return;
}
const reader = new FileReader();
reader.onload = function(e) {
const content = e.target.result;
const subtitles = parseSRT(content);
document.getElementById('subtitleDisplay').textContent = subtitles;
};
reader.readAsText(file);
});
/**
* Parses SRT content and extracts subtitle texts without numbers and timestamps.
* @param {string} srt - The raw SRT file content.
* @returns {string} - The cleaned subtitle text.
*/
function parseSRT(srt) {
// Split the content by double newlines to separate each subtitle block
const blocks = srt.split(/\r?\n\r?\n/);
const texts = [];
blocks.forEach(block => {
// Split each block by newlines
const lines = block.split(/\r?\n/);
// Skip if the block doesn't have at least 3 lines (number, timestamp, text)
if (lines.length >= 3) {
// The actual subtitle text starts from the third line
const textLines = lines.slice(2);
const text = textLines.join(' ');
texts.push(text);
}
});
// Join all subtitle texts with line breaks
return texts.join('\n');
}
</script>
</body>
</html>
File Selection:
<input type="file"> element allows users to select an SRT file from their device. It accepts files with the .srt extension.Reading the File:
FileReader object reads its content as text.Parsing SRT Content:
parseSRT function processes the raw SRT content.Displaying Subtitles:
<div id="subtitleDisplay"> element.Original SRT Content:
1
00:00:01,000 --> 00:00:04,000
Hello!
2
00:00:05,000 --> 00:00:07,000
How are you?
3
00:00:08,000 --> 00:00:10,000
I'm fine, thank you!
Displayed Output:
Hello!
How are you?
I'm fine, thank you!
Multiple Lines: If a single subtitle block contains multiple lines of text, they will be concatenated with spaces.
Error Handling: This basic example does minimal error handling. For a more robust solution, consider adding checks for malformed SRT files or displaying error messages to the user.
Styling: Feel free to customize the CSS to match your design preferences.
srt_viewer.html..srt file.This simple tool can be handy for quickly viewing the textual content of SRT files without the additional metadata.
Not yet included in a published release.