1. Core Class: M3U8Downloader
- parseM3U8: Parses the M3U8 text, filters out lines ending with .ts, and resolves relative URLs using the base URL of the M3U8 file.
- downloadTS: Downloads a single TS segment and returns an object containing the segment index and Blob to ensure correct ordering.
- downloadAllTS: Downloads all segments with concurrency control (default: 5) to avoid overwhelming the network or server.
- mergeAndDownload: Merges all TS Blobs into a single Blob and creates a download link for the user to save the file locally.
- download: Orchestrates the entire process: fetching the M3U8, parsing, downloading segments, merging, and exporting.
2. Usage Notes
1. CORS Issues: Browsers restrict cross-origin requests by default. To resolve this:
- Ensure the target server has CORS enabled for your domain.
- Use a proxy server to forward requests.
- For local testing, you can start Chrome with disabled web security (not recommended for general use):
chrome --disable-web-security --user-data-dir=/tmp/chrome-test
2. Encrypted M3U8: If the M3U8 contains an #EXT-X-KEY tag (indicating AES-128 encryption), additional decryption logic is required.
3. Large File Handling: For very large videos, browser memory may be insufficient. Consider downloading and merging in chunks.
4. Compatibility: This solution relies on the Fetch API and Blob, which are supported in modern browsers (Chrome, Firefox, Edge, etc.). Internet Explorer is not supported.