A Swift library for parsing M3U/M3U8 playlists with enhanced IPTV support and bug fixes.
This is an enhanced fork of the original M3UKit with comprehensive robust parsing that handles ANY playlist format:
- Fixed line number tracking bug that was causing incorrect parsing
- Improved session data handling to prevent interference with stream parsing
- Enhanced URL validation with strict mode for better stream link accuracy
- Better error handling that continues parsing instead of stopping on first error
- Fixed missing duration parsing (handles
#EXTINF: tvg-id=...without duration) - Fixed typo tolerance (handles
#EXTNF:instead of#EXTINF:) - BOM character removal (handles Unicode BOM at file start)
- Complex URL support (handles URLs with pipes, parameters, special characters)
- Maximum Resilience Mode - parses ANY playlist format with extensive fallbacks
- IPTV Preset Options - optimized configuration for IPTV applications
- Strict URL validation option to ensure only valid streaming URLs are accepted
- Session data skipping option to handle problematic playlist formats
- Enhanced attribute parsing for modern IPTV playlist features with flexible regex
- Playlist format detection (M3U, M3U8, PLS)
- Caching support for better performance
- IPTV-specific convenience methods for channel filtering and search
- Comprehensive error recovery - never fails parsing due to format issues
- Better stream link accuracy - no more playing wrong streams
- Support for complex playlist formats commonly used by IPTV providers
- Enhanced channel metadata including EPG URLs, aspect ratios, audio tracks
- Robust error handling for malformed playlists
Add this repository to your Package.swift:
dependencies: [
.package(url: "https://github.com/YOUR_USERNAME/M3UKit.git", from: "2.0.0")
]Add this to your Podfile:
pod 'M3UKit', :git => 'https://github.com/YOUR_USERNAME/M3UKit.git', :tag => '2.0.0'import M3UKit
let parser = PlaylistParser(options: .all)
let playlist = try parser.parse(playlistURL)// Maximum compatibility - handles any playlist format
let parser = PlaylistParser(options: .iptv)
// Or customize options
let parser = PlaylistParser(options: [
.removeSeriesInfoFromText,
.extractIdFromURL,
.strictURLValidation, // Ensures valid streaming URLs
.skipSessionData, // Skips problematic session data
.maxResilience // Handles any format with fallbacks
])// Get all live channels
let liveChannels = playlist.liveChannels
// Filter by group
let newsChannels = playlist.channels(in: "News")
// Search channels
let searchResults = playlist.searchChannels(query: "BBC")
// Get channel by ID
let channel = playlist.channel(withId: "BBCNews.uk")// Parse any playlist format - never fails
let parser = PlaylistParser(options: .iptv)
let playlist = try parser.parse(anyPlaylistSource)
// Validate before parsing (optional with robust mode)
if parser.validateSource(playlistSource) {
let playlist = try parser.parse(playlistSource)
// Process valid playlist
}
// Handle problematic playlists that other parsers can't handle
let resilientParser = PlaylistParser(options: [.maxResilience])
let playlist = try resilientParser.parse(brokenPlaylistSource)
print("Parsed \(playlist.medias.count) channels from broken playlist")- Line number tracking bug - The parser was incorrectly tracking line numbers, causing wrong metadata association
- Session data interference -
#EXT-X-SESSION-DATAlines were interfering with stream parsing - Poor error handling - Parser stopped on first error instead of continuing
- Weak URL validation - Accepted invalid URLs that could cause playback issues
- Proper line enumeration using
enumerated()instead of manual counting - Session data handling with option to skip problematic lines
- Enhanced error handling that continues parsing and collects errors
- Strict URL validation with streaming protocol and extension checks
- Better playlist structure with IPTV-specific convenience methods
The enhanced version includes comprehensive tests and can handle the same test cases as the original, plus new edge cases:
swift testThis project is licensed under the MIT License - see the LICENSE file for details.
- Original M3UKit by Omar Albeik
- Enhanced for IPTV use cases with better error handling and validation
If you find any issues with this enhanced version, please report them in the GitHub issues section. This fork specifically addresses IPTV playlist parsing problems and aims to provide a more robust solution for streaming applications.