parseM3U8Content function

List<Map<String, String>> parseM3U8Content(
  1. String m3u8Content
)

Implementation

List<Map<String, String>> parseM3U8Content(String m3u8Content) {
  List<Map<String, String>> qualities = [];
  List<String> lines = m3u8Content.split('\n');

  String? resolution;
  String? bandwidth;
  String? url;

  for (String line in lines) {
    if (line.startsWith('#EXT-X-STREAM-INF')) {
      // Extract bandwidth and resolution
      RegExp bandwidthExp = RegExp(r'BANDWIDTH=(\d+)');
      RegExp resolutionExp = RegExp(r'RESOLUTION=(\d+x\d+)');

      bandwidth = bandwidthExp.firstMatch(line)?.group(1);
      resolution = resolutionExp.firstMatch(line)?.group(1);
    } else if (line.endsWith('.m3u8')) {
      // This line should be the URL to the specific stream
      url = line;

      if (bandwidth != null && resolution != null) {
        qualities.add({
          'resolution': resolution,
          'bandwidth': bandwidth,
          'url': url,
        });

        // Reset variables
        resolution = null;
        bandwidth = null;
        url = null;
      }
    }
  }
  return qualities;
}