Skip to content

Commit

Permalink
Checking #EXTM3U
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhvorost committed Feb 29, 2024
1 parent 533be7e commit 0589ea2
Show file tree
Hide file tree
Showing 3 changed files with 367 additions and 364 deletions.
7 changes: 3 additions & 4 deletions Sources/M3U8Decoder/M3U8Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,11 @@ public class M3U8Decoder {
public func decode<T>(_ type: T.Type, from text: String) throws -> T where T : Decodable {
let parser = M3U8Parser()

guard text.isEmpty == false, let dict = parser.parse(text: text) else {
throw "Bad data."
guard text.isEmpty == false else {
throw "Empty data."
}

// Debug
//print(dict)
let dict = try parser.parse(text: text)

let decoder = JSONDecoder()

Expand Down
29 changes: 19 additions & 10 deletions Sources/M3U8Decoder/M3U8Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,23 @@ class M3U8Parser {
]
private static let charSetQuotes = CharacterSet(charactersIn: "\"")

func parse(text: String) -> [String : Any]? {
func parse(text: String) throws -> [String : Any] {
var dict = [String : Any]()

let items = text.components(separatedBy: .newlines)
for i in 0..<items.count {
let line = items[i]
let line = items[i].trimmingCharacters(in: .whitespaces)

// Empty line
guard line.isEmpty == false else {
// Skip empty lines
guard !line.isEmpty else {
continue
}

// Check for the first line with #EXTM3U
guard !dict.isEmpty || line == "#EXTM3U" else {
throw "Not the Playlist."
}

// #EXT
if line.hasPrefix("#EXT") {
let range = NSRange(location: 0, length: line.utf16.count)
Expand Down Expand Up @@ -95,8 +100,12 @@ class M3U8Parser {
}
}
}
// Comments
else if line.hasPrefix("#") {
//print(line)
}
// URI
else if let _ = URL(string: line) {
else {
if var items = dict[Self.uriKey] as? [Any] {
items.append(line)
dict[Self.uriKey] = items
Expand All @@ -106,7 +115,7 @@ class M3U8Parser {
}
}
}
return dict.count > 0 ? dict : nil
return dict
}

private func convertType(text: String) -> Any {
Expand All @@ -130,7 +139,7 @@ class M3U8Parser {
let range = NSRange(location: 0, length: value.utf16.count)

switch name {
// #EXTINF:<duration>,[<title>]
// #EXTINF:<duration>,[<title>]
case "EXTINF":
if let match = Self.regexExtInf.matches(in: value, options: [], range: range).first,
match.numberOfRanges == 3,
Expand All @@ -148,7 +157,7 @@ class M3U8Parser {
return .object(keyValues)
}

// #EXT-X-BYTERANGE:<n>[@<o>]
// #EXT-X-BYTERANGE:<n>[@<o>]
case "EXT-X-BYTERANGE":
fallthrough
case "BYTERANGE":
Expand All @@ -168,7 +177,7 @@ class M3U8Parser {
return .object(keyValues)
}

// #RESOLUTION=<width>x<height>
// #RESOLUTION=<width>x<height>
case "RESOLUTION":
let matches = Self.regexResolution.matches(in: value, options: [], range: range)
if let match = matches.first, match.numberOfRanges == 3,
Expand All @@ -184,7 +193,7 @@ class M3U8Parser {
return .object(keyValues)
}

// CODECS="codec1,codec2,..."
// CODECS="codec1,codec2,..."
case "CODECS":
let array = value
.trimmingCharacters(in: Self.charSetQuotes)
Expand Down
Loading

0 comments on commit 0589ea2

Please sign in to comment.