2021-06-10 22:50:10 +00:00
|
|
|
import Alamofire
|
2021-06-14 18:05:02 +00:00
|
|
|
import AVKit
|
2021-06-10 22:50:10 +00:00
|
|
|
import Foundation
|
|
|
|
import SwiftyJSON
|
|
|
|
|
2021-06-11 00:05:59 +00:00
|
|
|
final class Video: Identifiable, ObservableObject {
|
2021-06-10 22:50:10 +00:00
|
|
|
let id: String
|
|
|
|
var title: String
|
2021-06-11 21:11:59 +00:00
|
|
|
var thumbnailURL: URL?
|
2021-06-10 22:50:10 +00:00
|
|
|
var author: String
|
2021-06-11 00:05:59 +00:00
|
|
|
var length: TimeInterval
|
|
|
|
var published: String
|
|
|
|
var views: Int
|
2021-06-11 21:11:59 +00:00
|
|
|
var channelID: String
|
|
|
|
|
2021-06-14 18:05:02 +00:00
|
|
|
var streams = [Stream]()
|
2021-06-10 22:50:10 +00:00
|
|
|
|
2021-06-11 21:54:00 +00:00
|
|
|
init(
|
|
|
|
id: String,
|
|
|
|
title: String,
|
|
|
|
thumbnailURL: URL?,
|
|
|
|
author: String,
|
|
|
|
length: TimeInterval,
|
|
|
|
published: String,
|
2021-06-14 18:05:02 +00:00
|
|
|
views: Int,
|
|
|
|
channelID: String
|
2021-06-11 21:54:00 +00:00
|
|
|
) {
|
2021-06-10 22:50:10 +00:00
|
|
|
self.id = id
|
|
|
|
self.title = title
|
|
|
|
self.thumbnailURL = thumbnailURL
|
|
|
|
self.author = author
|
2021-06-11 00:05:59 +00:00
|
|
|
self.length = length
|
|
|
|
self.published = published
|
|
|
|
self.views = views
|
2021-06-14 18:05:02 +00:00
|
|
|
self.channelID = channelID
|
2021-06-10 22:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init(_ json: JSON) {
|
|
|
|
id = json["videoId"].stringValue
|
|
|
|
title = json["title"].stringValue
|
|
|
|
author = json["author"].stringValue
|
2021-06-11 00:05:59 +00:00
|
|
|
length = json["lengthSeconds"].doubleValue
|
|
|
|
published = json["publishedText"].stringValue
|
|
|
|
views = json["viewCount"].intValue
|
2021-06-11 21:11:59 +00:00
|
|
|
channelID = json["authorId"].stringValue
|
2021-06-14 18:05:02 +00:00
|
|
|
thumbnailURL = extractThumbnailURL(from: json)
|
2021-06-11 00:05:59 +00:00
|
|
|
|
2021-06-14 18:05:02 +00:00
|
|
|
streams = extractFormatStreams(from: json["formatStreams"].arrayValue)
|
|
|
|
streams.append(contentsOf: extractAdaptiveFormats(from: json["adaptiveFormats"].arrayValue))
|
2021-06-10 22:50:10 +00:00
|
|
|
}
|
2021-06-11 00:05:59 +00:00
|
|
|
|
|
|
|
var playTime: String? {
|
2021-06-16 19:30:50 +00:00
|
|
|
guard !length.isZero else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-11 00:05:59 +00:00
|
|
|
let formatter = DateComponentsFormatter()
|
|
|
|
|
|
|
|
formatter.unitsStyle = .positional
|
|
|
|
formatter.allowedUnits = length >= (60 * 60) ? [.hour, .minute, .second] : [.minute, .second]
|
|
|
|
formatter.zeroFormattingBehavior = [.pad]
|
|
|
|
|
|
|
|
return formatter.string(from: length)
|
|
|
|
}
|
|
|
|
|
|
|
|
var viewsCount: String {
|
|
|
|
let formatter = NumberFormatter()
|
|
|
|
formatter.numberStyle = .decimal
|
|
|
|
formatter.maximumFractionDigits = 1
|
|
|
|
|
|
|
|
var number: NSNumber
|
|
|
|
var unit: String
|
|
|
|
|
|
|
|
if views < 1_000_000 {
|
|
|
|
number = NSNumber(value: Double(views) / 1000.0)
|
|
|
|
unit = "K"
|
|
|
|
} else {
|
|
|
|
number = NSNumber(value: Double(views) / 1_000_000.0)
|
|
|
|
unit = "M"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "\(formatter.string(from: number)!)\(unit)"
|
|
|
|
}
|
2021-06-14 18:05:02 +00:00
|
|
|
|
|
|
|
var selectableStreams: [Stream] {
|
|
|
|
let streams = streams.sorted { $0.resolution > $1.resolution }
|
|
|
|
var selectable = [Stream]()
|
|
|
|
|
|
|
|
StreamResolution.allCases.forEach { resolution in
|
|
|
|
if let stream = streams.filter({ $0.resolution == resolution }).min(by: { $0.type < $1.type }) {
|
|
|
|
selectable.append(stream)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return selectable
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultStream: Stream? {
|
|
|
|
selectableStreams.first { $0.type == .stream }
|
|
|
|
}
|
|
|
|
|
2021-06-15 16:35:21 +00:00
|
|
|
var bestStream: Stream? {
|
|
|
|
selectableStreams.min { $0.resolution > $1.resolution }
|
|
|
|
}
|
|
|
|
|
2021-06-14 18:05:02 +00:00
|
|
|
private func extractThumbnailURL(from details: JSON) -> URL? {
|
|
|
|
if details["videoThumbnails"].arrayValue.isEmpty {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let thumbnail = details["videoThumbnails"].arrayValue.first { $0["quality"].stringValue == "medium" }!
|
|
|
|
return thumbnail["url"].url!
|
|
|
|
}
|
|
|
|
|
|
|
|
private func extractFormatStreams(from streams: [JSON]) -> [Stream] {
|
|
|
|
streams.map {
|
2021-06-15 16:35:21 +00:00
|
|
|
AudioVideoStream(
|
|
|
|
avAsset: AVURLAsset(url: DataProvider.proxyURLForAsset($0["url"].stringValue)!),
|
2021-06-14 18:05:02 +00:00
|
|
|
resolution: StreamResolution.from(resolution: $0["resolution"].stringValue)!,
|
|
|
|
type: .stream,
|
|
|
|
encoding: $0["encoding"].stringValue
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func extractAdaptiveFormats(from streams: [JSON]) -> [Stream] {
|
|
|
|
let audioAssetURL = streams.first { $0["type"].stringValue.starts(with: "audio/mp4") }
|
|
|
|
guard audioAssetURL != nil else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
let videoAssetsURLs = streams.filter { $0["type"].stringValue.starts(with: "video/mp4") && $0["encoding"].stringValue == "h264" }
|
|
|
|
|
|
|
|
return videoAssetsURLs.map {
|
|
|
|
Stream(
|
|
|
|
audioAsset: AVURLAsset(url: DataProvider.proxyURLForAsset(audioAssetURL!["url"].stringValue)!),
|
|
|
|
videoAsset: AVURLAsset(url: DataProvider.proxyURLForAsset($0["url"].stringValue)!),
|
|
|
|
resolution: StreamResolution.from(resolution: $0["resolution"].stringValue)!,
|
|
|
|
type: .adaptive,
|
|
|
|
encoding: $0["encoding"].stringValue
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 22:50:10 +00:00
|
|
|
}
|