Created
August 18, 2023 15:55
-
-
Save ivanopcode/4687d2d4ca81d5bb56c983e20e4da143 to your computer and use it in GitHub Desktop.
primitive m3u8 encoder-decoder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// License: MIT | |
// 2023 Ivan Oparin | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
// THE SOFTWARE. | |
// | |
import Foundation | |
enum m3u8 { | |
static func encode(_ playlist: HLSPlaylist, to url: URL) throws { | |
let lines = playlist.entries.map { entry -> String in | |
switch entry { | |
case .directive(let directive): | |
return directive | |
case .segment(let segment): | |
return segment | |
} | |
} | |
let text = lines.joined(separator: "\n") | |
try text.write(to: url, atomically: true, encoding: .utf8) | |
} | |
static func decode(_ playlist: URL) throws -> HLSPlaylist { | |
let text = try String(contentsOf: playlist, encoding: .utf8) | |
let lines = text.split(separator: "\n") | |
var playlist = HLSPlaylist() | |
for line in lines { | |
if line.hasPrefix("#") { | |
// This is a directive | |
playlist.entries.append(.directive(String(line))) | |
} else { | |
// This is a URI for a media segment or sub-playlist | |
playlist.entries.append(.segment(String(line))) | |
} | |
} | |
return playlist | |
} | |
static func decode(_ playlist: URL, prependingUrl components: URL) throws -> HLSPlaylist { | |
let text = try String(contentsOf: playlist, encoding: .utf8) | |
let lines = text.split(separator: "\n") | |
var playlist = HLSPlaylist() | |
for line in lines { | |
if line.hasPrefix("#") { | |
// This is a directive | |
playlist.entries.append(.directive(String(line))) | |
} else { | |
// This is a URI for a media segment or sub-playlist | |
playlist.entries.append(.segment("\(components.path)\(String(line))")) | |
} | |
} | |
return playlist | |
} | |
enum PlaylistEntry: Equatable { | |
case directive(String) | |
case segment(String) | |
var isSegment: Bool { | |
switch self { | |
case .directive: | |
return false | |
case .segment: | |
return true | |
} | |
} | |
var value: String { | |
switch self { | |
case let .directive(value): | |
return value | |
case let .segment(value): | |
return value | |
} | |
} | |
} | |
struct HLSPlaylist { | |
var entries: [PlaylistEntry] = [] | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment