### Install srt-parser-2 using npm or yarn Source: https://github.com/1c7/srt-parser-2/blob/master/README.md Instructions for installing the srt-parser-2 library using either npm or yarn package managers. This is the first step to using the library in your project. ```bash npm install srt-parser-2 ``` ```bash yarn add srt-parser-2 ``` -------------------------------- ### Login to NPM Source: https://github.com/1c7/srt-parser-2/blob/master/doc/2. How to Publish New Version.md Logs the user into the NPM registry. This command prompts for username, password, and a one-time password if two-factor authentication is enabled. Successful login is indicated by a confirmation message. ```bash npm adduser ``` -------------------------------- ### Build Project for NPM Publication Source: https://github.com/1c7/srt-parser-2/blob/master/doc/2. How to Publish New Version.md Executes the build script defined in `package.json` to prepare the project for publishing. This is typically the first step before any NPM operations. ```bash npm run build ``` -------------------------------- ### Check NPM Registry Configuration Source: https://github.com/1c7/srt-parser-2/blob/master/doc/2. How to Publish New Version.md Lists the current NPM configuration, including the registry URL. This helps verify if a mirror is being used, which might affect publishing. ```bash npm config list ``` -------------------------------- ### Publish Package to NPM Source: https://github.com/1c7/srt-parser-2/blob/master/doc/2. How to Publish New Version.md Publishes the current version of the package to the NPM registry. This command should be run after building the project, logging in, and updating the version in `package.json`. ```bash npm publish ``` -------------------------------- ### Convert SRT file to JSON using CLI Source: https://github.com/1c7/srt-parser-2/blob/master/README.md Shows how to use the command-line interface (CLI) provided by srt-parser-2 to convert an input SRT file to a JSON output file. It includes options for specifying input and output files, and for minifying the output. ```bash npx srt-parser-2 -i input.srt -o output.json --minify ``` -------------------------------- ### Reset NPM Registry to Default Source: https://github.com/1c7/srt-parser-2/blob/master/doc/2. How to Publish New Version.md Sets the NPM registry URL to the official NPM registry. This is a crucial step if a private or mirrored registry is causing issues with publishing. ```bash npm set registry https://registry.npmjs.org ``` -------------------------------- ### Verify NPM Login Status Source: https://github.com/1c7/srt-parser-2/blob/master/doc/2. How to Publish New Version.md Checks the currently logged-in NPM user. If not logged in, the user will need to log in before publishing. ```bash npm whoami ``` -------------------------------- ### Handle Non-Standard SRT Formats (JavaScript) Source: https://context7.com/1c7/srt-parser-2/llms.txt Illustrates the robust parsing capabilities of SRT Parser 2 in handling various non-standard or incorrect SRT time formats that might cause other parsers to fail. It demonstrates how the library automatically normalizes formats like using a dot instead of a comma for milliseconds, single-digit time components, and four-digit milliseconds. ```javascript import srtParser2 from "srt-parser-2"; const parser = new srtParser2(); // Dot separator instead of comma (common issue) const dotFormat = `1 00:00:11.544 --> 00:00:12.682 Hello with dot separator`; console.log(parser.fromSrt(dotFormat)); // Correctly parses and normalizes to comma format // Single-digit time components const singleDigit = `1 1:2:3,400 --> 1:2:5,600 Single digit hours, minutes, seconds`; console.log(parser.fromSrt(singleDigit)); // Normalizes to 01:02:03,400 --> 01:02:05,600 // 4-digit milliseconds (gets truncated to 3) const fourDigitMs = `1 00:00:00.3333 --> 00:00:01.4444 Four digit milliseconds`; console.log(parser.fromSrt(fourDigitMs)); // Normalizes milliseconds to 3 digits: 333 and 444 // Mixed issues const mixedIssues = `1 0:0:10.5 --> 0:0:15.75 Mixed format problems`; const result = parser.fromSrt(mixedIssues); console.log(result[0].startTime); // "00:00:10,500" console.log(result[0].endTime); // "00:00:15,750" ``` -------------------------------- ### Parse SRT string to Javascript array and vice versa Source: https://github.com/1c7/srt-parser-2/blob/master/README.md Demonstrates how to use the srt-parser-2 library to convert an SRT formatted string into a Javascript array of subtitle objects, and how to convert the array back into an SRT string. This showcases the core parsing and serialization capabilities of the library. ```javascript let srt = ` 1 00:00:11,544 --> 00:00:12,682 Hello `; import srtParser2 from "srt-parser-2"; var parser = new srtParser2(); var srt_array = parser.fromSrt(srt); console.log(srt_array); // turn array back to SRT string. var srt_string = parser.toSrt(srt_array); console.log(srt_string); ``` -------------------------------- ### Read and Process SRT File (JavaScript/TypeScript) Source: https://context7.com/1c7/srt-parser-2/llms.txt Reads an SRT file from the file system using Node.js and parses its content into an array of subtitle Line objects. It demonstrates accessing subtitle properties, calculating durations, filtering by time, and searching for specific text within subtitles. ```javascript import srtParser2 from "srt-parser-2"; import fs from "fs"; const parser = new srtParser2(); // Read SRT file and parse const srtContent = fs.readFileSync("./subtitles.srt", { encoding: "utf-8" }); const subtitles = parser.fromSrt(srtContent); // Access individual subtitle entries subtitles.forEach((subtitle) => { console.log(`[${subtitle.startTime} --> ${subtitle.endTime}]`); console.log(`Duration: ${subtitle.endSeconds - subtitle.startSeconds}s`); console.log(`Text: ${subtitle.text}`); console.log("---"); }); // Filter subtitles by time range const firstMinute = subtitles.filter((s) => s.startSeconds < 60); console.log(`Subtitles in first minute: ${firstMinute.length}`); // Search for text in subtitles const matches = subtitles.filter((s) => s.text.toLowerCase().includes("hello") ); ``` -------------------------------- ### SRT Subtitle Line Interface and Creation (TypeScript) Source: https://context7.com/1c7/srt-parser-2/llms.txt Defines the TypeScript interface for subtitle line objects, which are returned by `fromSrt()` and accepted by `toSrt()`. This interface includes properties for ID, timestamps (string and seconds), and text content. It also demonstrates how to programmatically create subtitle data structures and convert them into SRT format. ```typescript import srtParser2, { Line } from "srt-parser-2"; // Line interface structure interface Line { id: string; // Subtitle sequence number startTime: string; // Start timestamp "HH:MM:SS,mmm" startSeconds: number; // Start time in seconds endTime: string; // End timestamp "HH:MM:SS,mmm" endSeconds: number; // End time in seconds text: string; // Subtitle text content } // Create subtitles programmatically const customSubtitles: Line[] = [ { id: "1", startTime: "00:00:01,000", startSeconds: 1, endTime: "00:00:03,000", endSeconds: 3, text: "Welcome to our video" }, { id: "2", startTime: "00:00:04,000", startSeconds: 4, endTime: "00:00:07,500", endSeconds: 7.5, text: "Let's get started!" } ]; const parser = new srtParser2(); const srtOutput = parser.toSrt(customSubtitles); console.log(srtOutput); ``` -------------------------------- ### Update Package Version in package.json Source: https://github.com/1c7/srt-parser-2/blob/master/doc/2. How to Publish New Version.md Modifies the `version` field in the `package.json` file to reflect the new version being published. This is a manual step required before publishing. ```json { "version": "1.1.8" } ``` -------------------------------- ### Convert Subtitle Array to SRT String (JavaScript/TypeScript) Source: https://context7.com/1c7/srt-parser-2/llms.txt Converts an array of Line objects back into a properly formatted SRT string. This is useful for programmatically modifying subtitles and saving them to a file. It includes a helper function to format seconds into the SRT timestamp format. ```javascript import srtParser2 from "srt-parser-2"; import fs from "fs"; const parser = new srtParser2(); // Parse existing SRT const original = `1 00:00:11,544 --> 00:00:12,682 Hello`; const subtitles = parser.fromSrt(original); // Modify subtitles - shift timing by 5 seconds const shifted = subtitles.map((s, index) => ({ id: String(index + 1), startTime: formatTime(s.startSeconds + 5), startSeconds: s.startSeconds + 5, endTime: formatTime(s.endSeconds + 5), endSeconds: s.endSeconds + 5, text: s.text })); // Convert back to SRT string const srtString = parser.toSrt(shifted); fs.writeFileSync("./output.srt", srtString); // Helper function to format seconds as SRT timestamp function formatTime(seconds) { const hours = Math.floor(seconds / 3600); const minutes = Math.floor((seconds % 3600) / 60); const secs = Math.floor(seconds % 60); const ms = Math.round((seconds % 1) * 1000); return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(secs).padStart(2, '0')},${String(ms).padStart(3, '0')}`; } ``` -------------------------------- ### Parse SRT String to Array (JavaScript/TypeScript) Source: https://context7.com/1c7/srt-parser-2/llms.txt Parses an SRT-formatted string into an array of subtitle Line objects. Each Line object contains id, startTime, startSeconds, endTime, endSeconds, and text properties. This method automatically handles variations in timestamp separators (comma or dot). ```javascript import srtParser2 from "srt-parser-2"; const parser = new srtParser2(); // Parse SRT string to array const srt = `1 00:00:11,544 --> 00:00:12,682 Hello 2 00:00:13,000 --> 00:00:15,500 World`; const result = parser.fromSrt(srt); console.log(result); // Output: // [ // { // id: '1', // startTime: '00:00:11,544', // startSeconds: 11.544, // endTime: '00:00:12,682', // endSeconds: 12.682, // text: 'Hello' // }, // { // id: '2', // startTime: '00:00:13,000', // startSeconds: 13, // endTime: '00:00:15,500', // endSeconds: 15.5, // text: 'World' // } // ] ``` -------------------------------- ### Correct SRT Timestamp Format (JavaScript/TypeScript) Source: https://context7.com/1c7/srt-parser-2/llms.txt Normalizes various non-standard timestamp formats to the correct SRT format (HH:MM:SS,mmm). This method automatically handles issues such as dot separators instead of commas, incorrect millisecond digit counts, and single-digit hour/minute/second values. ```javascript import srtParser2 from "srt-parser-2"; const parser = new srtParser2(); // Fix dot separator (should be comma) console.log(parser.correctFormat("00:00:28.967")); // Output: "00:00:28,967" // Fix 4-digit milliseconds console.log(parser.correctFormat("00:00:28.9670")); // Output: "00:00:28,967" // Fix 2-digit milliseconds (pads with zeros) console.log(parser.correctFormat("00:00:28.96")); // Output: "00:00:28,960" // Fix 1-digit milliseconds console.log(parser.correctFormat("00:00:28,9")); // Output: "00:00:28,900" // Fix single-digit hour/minute/second console.log(parser.correctFormat("1:2:3,400")); // Output: "01:02:03,400" // Fix all issues at once console.log(parser.correctFormat("9:5:8.5")); // Output: "09:05:08,500" ``` -------------------------------- ### Convert SRT Timestamp to Seconds (JavaScript) Source: https://context7.com/1c7/srt-parser-2/llms.txt Converts a formatted SRT timestamp string (HH:MM:SS,mmm) into the total number of seconds as a floating-point number. This function accounts for JavaScript's floating-point precision issues by implementing proper rounding. It's useful for calculating durations or synchronizing subtitles programmatically. ```javascript import srtParser2 from "srt-parser-2"; const parser = new srtParser2(); // Convert timestamps to seconds console.log(parser.timestampToSeconds("00:00:11,544")); // Output: 11.544 console.log(parser.timestampToSeconds("00:01:20,460")); // Output: 80.46 console.log(parser.timestampToSeconds("01:30:45,500")); // Output: 5445.5 // Calculate duration between two timestamps const start = parser.timestampToSeconds("00:05:30,000"); const end = parser.timestampToSeconds("00:05:35,500"); console.log(`Duration: ${end - start} seconds`); // Output: Duration: 5.5 seconds ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.