2021-06-14 18:05:02 +00:00
|
|
|
import AVFoundation
|
2021-09-25 08:18:22 +00:00
|
|
|
import Defaults
|
2021-06-14 18:05:02 +00:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
// swiftlint:disable:next final_class
|
2021-10-16 22:48:58 +00:00
|
|
|
class Stream: Equatable, Hashable, Identifiable {
|
2021-09-25 08:18:22 +00:00
|
|
|
enum Resolution: String, CaseIterable, Comparable, Defaults.Serializable {
|
2022-04-09 21:19:00 +00:00
|
|
|
case hd2160p60
|
2022-04-09 21:27:26 +00:00
|
|
|
case hd2160p50
|
|
|
|
case hd2160p48
|
2022-06-17 10:52:10 +00:00
|
|
|
case hd2160p30
|
2022-03-27 18:59:22 +00:00
|
|
|
case hd1440p60
|
2022-04-09 21:27:26 +00:00
|
|
|
case hd1440p50
|
|
|
|
case hd1440p48
|
2022-06-17 10:52:10 +00:00
|
|
|
case hd1440p30
|
2022-03-27 18:59:22 +00:00
|
|
|
case hd1080p60
|
2022-04-09 21:27:26 +00:00
|
|
|
case hd1080p50
|
|
|
|
case hd1080p48
|
2022-06-17 10:52:10 +00:00
|
|
|
case hd1080p30
|
2022-03-27 18:59:22 +00:00
|
|
|
case hd720p60
|
2022-04-09 21:27:26 +00:00
|
|
|
case hd720p50
|
|
|
|
case hd720p48
|
2022-06-17 10:52:10 +00:00
|
|
|
case hd720p30
|
|
|
|
case sd480p30
|
|
|
|
case sd360p30
|
|
|
|
case sd240p30
|
|
|
|
case sd144p30
|
2022-03-27 18:59:22 +00:00
|
|
|
case unknown
|
2021-10-16 22:48:58 +00:00
|
|
|
|
|
|
|
var name: String {
|
2022-06-17 10:52:10 +00:00
|
|
|
"\(height)p\(refreshRate != -1 && refreshRate != 30 ? ", \(refreshRate) fps" : "")"
|
2021-10-16 22:48:58 +00:00
|
|
|
}
|
2021-07-22 12:43:13 +00:00
|
|
|
|
|
|
|
var height: Int {
|
2021-10-16 22:48:58 +00:00
|
|
|
if self == .unknown {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
let resolutionPart = rawValue.components(separatedBy: "p").first!
|
|
|
|
return Int(resolutionPart.components(separatedBy: CharacterSet.decimalDigits.inverted).joined())!
|
|
|
|
}
|
|
|
|
|
|
|
|
var refreshRate: Int {
|
|
|
|
if self == .unknown {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
let refreshRatePart = rawValue.components(separatedBy: "p")[1]
|
2022-06-17 10:52:10 +00:00
|
|
|
|
|
|
|
if refreshRatePart.isEmpty {
|
|
|
|
return 30
|
|
|
|
}
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
return Int(refreshRatePart.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()) ?? -1
|
2021-07-22 12:43:13 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 05:54:24 +00:00
|
|
|
// These values are an approximation.
|
|
|
|
// https://support.google.com/youtube/answer/1722171?hl=en#zippy=%2Cbitrate
|
|
|
|
|
|
|
|
var bitrate: Int {
|
|
|
|
switch self {
|
|
|
|
case .hd2160p60, .hd2160p50, .hd2160p48, .hd2160p30:
|
2024-05-16 16:28:32 +00:00
|
|
|
return 56_000_000 // 56 Mbit/s
|
2024-05-13 05:54:24 +00:00
|
|
|
case .hd1440p60, .hd1440p50, .hd1440p48, .hd1440p30:
|
2024-05-16 16:28:32 +00:00
|
|
|
return 24_000_000 // 24 Mbit/s
|
2024-05-13 05:54:24 +00:00
|
|
|
case .hd1080p60, .hd1080p50, .hd1080p48, .hd1080p30:
|
2024-05-16 16:28:32 +00:00
|
|
|
return 12_000_000 // 12 Mbit/s
|
2024-05-13 05:54:24 +00:00
|
|
|
case .hd720p60, .hd720p50, .hd720p48, .hd720p30:
|
2024-05-16 16:28:32 +00:00
|
|
|
return 9_500_000 // 9.5 Mbit/s
|
2024-05-13 05:54:24 +00:00
|
|
|
case .sd480p30:
|
2024-05-16 16:28:32 +00:00
|
|
|
return 4_000_000 // 4 Mbit/s
|
2024-05-13 05:54:24 +00:00
|
|
|
case .sd360p30:
|
2024-05-16 16:28:32 +00:00
|
|
|
return 1_500_000 // 1.5 Mbit/s
|
2024-05-13 05:54:24 +00:00
|
|
|
case .sd240p30:
|
2024-05-16 16:28:32 +00:00
|
|
|
return 1_000_000 // 1 Mbit/s
|
2024-05-13 05:54:24 +00:00
|
|
|
case .sd144p30:
|
2024-05-16 16:28:32 +00:00
|
|
|
return 600_000 // 0.6 Mbit/s
|
2024-05-13 05:54:24 +00:00
|
|
|
case .unknown:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-17 12:09:51 +00:00
|
|
|
static func from(resolution: String, fps: Int? = nil) -> Self {
|
2022-06-17 10:52:10 +00:00
|
|
|
allCases.first { $0.rawValue.contains(resolution) && $0.refreshRate == (fps ?? 30) } ?? .unknown
|
2021-07-22 12:43:13 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 12:09:51 +00:00
|
|
|
static func < (lhs: Self, rhs: Self) -> Bool {
|
2022-08-22 17:03:49 +00:00
|
|
|
lhs.height == rhs.height ? (lhs.refreshRate < rhs.refreshRate) : (lhs.height < rhs.height)
|
2021-07-22 12:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Kind: String, Comparable {
|
2024-04-26 10:27:25 +00:00
|
|
|
case hls, adaptive, stream
|
2021-07-22 12:43:13 +00:00
|
|
|
|
|
|
|
private var sortOrder: Int {
|
|
|
|
switch self {
|
2021-10-16 22:48:58 +00:00
|
|
|
case .hls:
|
2021-07-22 12:43:13 +00:00
|
|
|
return 0
|
2021-10-16 22:48:58 +00:00
|
|
|
case .stream:
|
2021-07-22 12:43:13 +00:00
|
|
|
return 1
|
2021-10-16 22:48:58 +00:00
|
|
|
case .adaptive:
|
|
|
|
return 2
|
2021-07-22 12:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-17 12:09:51 +00:00
|
|
|
static func < (lhs: Self, rhs: Self) -> Bool {
|
2021-07-22 12:43:13 +00:00
|
|
|
lhs.sortOrder < rhs.sortOrder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-26 10:27:25 +00:00
|
|
|
enum Format: String {
|
2022-05-21 19:30:01 +00:00
|
|
|
case avc1
|
|
|
|
case mp4
|
2024-04-26 10:27:25 +00:00
|
|
|
case av1
|
|
|
|
case webm
|
|
|
|
case hls
|
|
|
|
case stream
|
2022-05-21 19:30:01 +00:00
|
|
|
case unknown
|
|
|
|
|
2022-08-21 14:28:30 +00:00
|
|
|
var description: String {
|
|
|
|
switch self {
|
|
|
|
case .webm:
|
|
|
|
return "WebM"
|
2024-04-26 10:27:25 +00:00
|
|
|
case .hls:
|
|
|
|
return "adaptive (HLS)"
|
|
|
|
case .stream:
|
|
|
|
return "Stream"
|
2022-08-21 14:28:30 +00:00
|
|
|
default:
|
|
|
|
return rawValue.uppercased()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 19:30:01 +00:00
|
|
|
static func from(_ string: String) -> Self {
|
|
|
|
let lowercased = string.lowercased()
|
|
|
|
|
2023-06-17 12:09:51 +00:00
|
|
|
if lowercased.contains("avc1") {
|
2022-05-21 19:30:01 +00:00
|
|
|
return .avc1
|
2023-06-17 12:09:51 +00:00
|
|
|
}
|
2024-04-26 10:27:25 +00:00
|
|
|
if lowercased.contains("mpeg_4") || lowercased.contains("mp4") {
|
|
|
|
return .mp4
|
|
|
|
}
|
2023-06-17 12:09:51 +00:00
|
|
|
if lowercased.contains("av01") {
|
2022-05-21 19:30:01 +00:00
|
|
|
return .av1
|
2023-06-17 12:09:51 +00:00
|
|
|
}
|
2024-04-26 10:27:25 +00:00
|
|
|
if lowercased.contains("webm") {
|
|
|
|
return .webm
|
|
|
|
}
|
|
|
|
if lowercased.contains("stream") {
|
|
|
|
return .stream
|
|
|
|
}
|
|
|
|
if lowercased.contains("hls") {
|
|
|
|
return .hls
|
2022-05-21 19:30:01 +00:00
|
|
|
}
|
2023-06-17 12:09:51 +00:00
|
|
|
return .unknown
|
2022-05-21 19:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
let id = UUID()
|
|
|
|
|
|
|
|
var instance: Instance!
|
|
|
|
var audioAsset: AVURLAsset!
|
|
|
|
var videoAsset: AVURLAsset!
|
|
|
|
var hlsURL: URL!
|
2022-11-10 17:11:28 +00:00
|
|
|
var localURL: URL!
|
2021-06-14 18:05:02 +00:00
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
var resolution: Resolution!
|
|
|
|
var kind: Kind!
|
2022-05-21 19:30:01 +00:00
|
|
|
var format: Format!
|
2021-06-14 18:05:02 +00:00
|
|
|
|
2022-08-20 21:05:40 +00:00
|
|
|
var encoding: String?
|
|
|
|
var videoFormat: String?
|
2024-05-13 05:54:24 +00:00
|
|
|
var bitrate: Int?
|
2021-06-14 18:05:02 +00:00
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
init(
|
|
|
|
instance: Instance? = nil,
|
|
|
|
audioAsset: AVURLAsset? = nil,
|
|
|
|
videoAsset: AVURLAsset? = nil,
|
|
|
|
hlsURL: URL? = nil,
|
2022-11-10 17:11:28 +00:00
|
|
|
localURL: URL? = nil,
|
2021-10-16 22:48:58 +00:00
|
|
|
resolution: Resolution? = nil,
|
|
|
|
kind: Kind = .hls,
|
2022-02-16 20:23:11 +00:00
|
|
|
encoding: String? = nil,
|
2024-05-13 05:54:24 +00:00
|
|
|
videoFormat: String? = nil,
|
|
|
|
bitrate: Int? = nil
|
2021-10-16 22:48:58 +00:00
|
|
|
) {
|
|
|
|
self.instance = instance
|
2021-06-14 18:05:02 +00:00
|
|
|
self.audioAsset = audioAsset
|
|
|
|
self.videoAsset = videoAsset
|
2021-10-16 22:48:58 +00:00
|
|
|
self.hlsURL = hlsURL
|
2022-11-10 17:11:28 +00:00
|
|
|
self.localURL = localURL
|
2021-06-14 18:05:02 +00:00
|
|
|
self.resolution = resolution
|
2021-07-22 12:43:13 +00:00
|
|
|
self.kind = kind
|
2021-06-14 18:05:02 +00:00
|
|
|
self.encoding = encoding
|
2022-05-21 19:30:01 +00:00
|
|
|
format = .from(videoFormat ?? "")
|
2024-05-13 05:54:24 +00:00
|
|
|
self.bitrate = bitrate
|
2021-06-14 18:05:02 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 17:11:28 +00:00
|
|
|
var isLocal: Bool {
|
|
|
|
localURL != nil
|
|
|
|
}
|
|
|
|
|
2023-05-20 20:49:10 +00:00
|
|
|
var isHLS: Bool {
|
|
|
|
hlsURL != nil
|
|
|
|
}
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
var quality: String {
|
2022-11-10 17:11:28 +00:00
|
|
|
guard localURL.isNil else { return "Opened File" }
|
2024-05-09 19:15:14 +00:00
|
|
|
|
|
|
|
if kind == .hls {
|
|
|
|
return "adaptive (HLS)"
|
|
|
|
}
|
|
|
|
|
2024-04-26 10:27:25 +00:00
|
|
|
return resolution.name
|
2022-02-16 20:23:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 22:41:49 +00:00
|
|
|
var shortQuality: String {
|
2022-11-10 17:11:28 +00:00
|
|
|
guard localURL.isNil else { return "File" }
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
if kind == .hls {
|
2024-05-09 19:15:14 +00:00
|
|
|
return "adaptive (HLS)"
|
2024-04-26 10:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if kind == .stream {
|
|
|
|
return resolution.name
|
2022-06-14 22:41:49 +00:00
|
|
|
}
|
2024-04-26 10:27:25 +00:00
|
|
|
return resolutionAndFormat
|
2022-06-14 22:41:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-14 18:05:02 +00:00
|
|
|
var description: String {
|
2022-11-10 17:11:28 +00:00
|
|
|
guard localURL.isNil else { return resolutionAndFormat }
|
2022-08-21 14:28:30 +00:00
|
|
|
let instanceString = instance.isNil ? "" : " - (\(instance!.description))"
|
2024-05-09 19:15:14 +00:00
|
|
|
return format != .hls ? "\(resolutionAndFormat)\(instanceString)" : "adaptive (HLS)\(instanceString)"
|
2022-08-21 14:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var resolutionAndFormat: String {
|
|
|
|
let formatString = format == .unknown ? "" : " (\(format.description))"
|
|
|
|
return "\(quality)\(formatString)"
|
2021-06-14 18:05:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 16:35:21 +00:00
|
|
|
var assets: [AVURLAsset] {
|
|
|
|
[audioAsset, videoAsset]
|
|
|
|
}
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
var videoAssetContainsAudio: Bool {
|
2021-10-05 20:20:09 +00:00
|
|
|
assets.dropFirst().allSatisfy { $0.url == assets.first!.url }
|
2021-07-22 12:43:13 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
var singleAssetURL: URL? {
|
2022-11-10 17:11:28 +00:00
|
|
|
guard localURL.isNil else {
|
|
|
|
return URLBookmarkModel.shared.loadBookmark(localURL) ?? localURL
|
|
|
|
}
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
if kind == .hls {
|
|
|
|
return hlsURL
|
2023-06-17 12:09:51 +00:00
|
|
|
}
|
|
|
|
if videoAssetContainsAudio {
|
2021-10-16 22:48:58 +00:00
|
|
|
return videoAsset.url
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-14 18:05:02 +00:00
|
|
|
static func == (lhs: Stream, rhs: Stream) -> Bool {
|
2021-10-16 22:48:58 +00:00
|
|
|
lhs.id == rhs.id
|
2021-07-22 12:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
2023-05-20 20:49:10 +00:00
|
|
|
if let url = videoAsset?.url {
|
|
|
|
hasher.combine(url)
|
|
|
|
}
|
|
|
|
if let url = audioAsset?.url {
|
|
|
|
hasher.combine(url)
|
|
|
|
}
|
|
|
|
if let url = hlsURL {
|
|
|
|
hasher.combine(url)
|
|
|
|
}
|
2021-10-16 22:48:58 +00:00
|
|
|
}
|
2021-06-14 18:05:02 +00:00
|
|
|
}
|