1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 06:10:32 +05:30
yattee/Model/Video.swift

196 lines
5.9 KiB
Swift
Raw Normal View History

2021-06-11 04:20:10 +05:30
import Alamofire
2021-06-14 23:35:02 +05:30
import AVKit
2021-06-11 04:20:10 +05:30
import Foundation
import SwiftyJSON
struct Video: Identifiable {
2021-06-11 04:20:10 +05:30
let id: String
var title: String
2021-07-08 04:09:18 +05:30
var thumbnails: [Thumbnail]
2021-06-11 04:20:10 +05:30
var author: String
2021-06-11 05:35:59 +05:30
var length: TimeInterval
var published: String
var views: Int
2021-06-12 02:41:59 +05:30
var channelID: String
2021-06-18 04:13:29 +05:30
var description: String
var genre: String
2021-06-12 02:41:59 +05:30
2021-07-22 18:13:13 +05:30
// index used when in the Playlist
let indexID: String?
2021-07-22 18:13:13 +05:30
var live: Bool
var upcoming: Bool
2021-06-14 23:35:02 +05:30
var streams = [Stream]()
2021-07-22 18:13:13 +05:30
var hlsUrl: URL?
init(
id: String,
title: String,
author: String,
length: TimeInterval,
published: String,
views: Int,
channelID: String,
description: String,
genre: String,
thumbnails: [Thumbnail] = [],
indexID: String? = nil,
live: Bool = false,
upcoming: Bool = false
) {
self.id = id
self.title = title
self.author = author
self.length = length
self.published = published
self.views = views
self.channelID = channelID
self.description = description
self.genre = genre
self.thumbnails = thumbnails
self.indexID = indexID
self.live = live
self.upcoming = upcoming
}
2021-06-11 04:20:10 +05:30
init(_ json: JSON) {
let videoID = json["videoId"].stringValue
if let id = json["indexId"].string {
indexID = id
self.id = videoID + id
} else {
indexID = nil
id = videoID
}
2021-06-11 04:20:10 +05:30
title = json["title"].stringValue
author = json["author"].stringValue
2021-06-11 05:35:59 +05:30
length = json["lengthSeconds"].doubleValue
published = json["publishedText"].stringValue
views = json["viewCount"].intValue
2021-06-12 02:41:59 +05:30
channelID = json["authorId"].stringValue
2021-06-18 04:13:29 +05:30
description = json["description"].stringValue
genre = json["genre"].stringValue
2021-07-08 04:09:18 +05:30
thumbnails = Video.extractThumbnails(from: json)
2021-06-11 05:35:59 +05:30
2021-07-22 18:13:13 +05:30
live = json["liveNow"].boolValue
upcoming = json["isUpcoming"].boolValue
2021-07-08 04:09:18 +05:30
streams = Video.extractFormatStreams(from: json["formatStreams"].arrayValue)
streams.append(contentsOf: Video.extractAdaptiveFormats(from: json["adaptiveFormats"].arrayValue))
2021-07-22 18:13:13 +05:30
hlsUrl = json["hlsUrl"].url
2021-06-11 04:20:10 +05:30
}
2021-06-11 05:35:59 +05:30
var playTime: String? {
guard !length.isZero else {
return nil
}
2021-06-11 05:35:59 +05:30
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.allowedUnits = length >= (60 * 60) ? [.hour, .minute, .second] : [.minute, .second]
formatter.zeroFormattingBehavior = [.pad]
return formatter.string(from: length)
}
2021-07-22 18:13:13 +05:30
var publishedDate: String? {
(published.isEmpty || published == "0 seconds ago") ? nil : published
}
2021-06-11 05:35:59 +05:30
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 23:35:02 +05:30
var selectableStreams: [Stream] {
let streams = streams.sorted { $0.resolution > $1.resolution }
var selectable = [Stream]()
2021-07-22 18:13:13 +05:30
Stream.Resolution.allCases.forEach { resolution in
if let stream = streams.filter({ $0.resolution == resolution }).min(by: { $0.kind < $1.kind }) {
2021-06-14 23:35:02 +05:30
selectable.append(stream)
}
}
return selectable
}
var defaultStream: Stream? {
2021-07-22 18:13:13 +05:30
selectableStreams.first { $0.kind == .stream }
2021-06-14 23:35:02 +05:30
}
2021-06-15 22:05:21 +05:30
var bestStream: Stream? {
selectableStreams.min { $0.resolution > $1.resolution }
}
2021-07-22 18:13:13 +05:30
func streamWithResolution(_ resolution: Stream.Resolution) -> Stream? {
2021-06-20 01:40:14 +05:30
selectableStreams.first { $0.resolution == resolution }
}
func defaultStreamForProfile(_ profile: Profile) -> Stream? {
2021-07-08 04:09:18 +05:30
streamWithResolution(profile.defaultStreamResolution.value) ?? streams.first
2021-06-20 01:40:14 +05:30
}
2021-07-22 18:13:13 +05:30
func thumbnailURL(quality: Thumbnail.Quality) -> URL? {
2021-07-08 04:09:18 +05:30
thumbnails.first { $0.quality == quality }?.url
}
2021-06-14 23:35:02 +05:30
2021-07-08 04:09:18 +05:30
private static func extractThumbnails(from details: JSON) -> [Thumbnail] {
details["videoThumbnails"].arrayValue.map { json in
Thumbnail(json)
}
2021-06-14 23:35:02 +05:30
}
2021-07-22 18:13:13 +05:30
static let options = [AVURLAssetPreferPreciseDurationAndTimingKey: false]
2021-07-08 04:09:18 +05:30
private static func extractFormatStreams(from streams: [JSON]) -> [Stream] {
2021-06-14 23:35:02 +05:30
streams.map {
2021-07-22 18:13:13 +05:30
SingleAssetStream(
avAsset: AVURLAsset(url: InvidiousAPI.proxyURLForAsset($0["url"].stringValue)!, options: options),
resolution: Stream.Resolution.from(resolution: $0["resolution"].stringValue)!,
kind: .stream,
2021-06-14 23:35:02 +05:30
encoding: $0["encoding"].stringValue
)
}
}
2021-07-08 04:09:18 +05:30
private static func extractAdaptiveFormats(from streams: [JSON]) -> [Stream] {
2021-06-14 23:35:02 +05:30
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(
2021-07-22 18:13:13 +05:30
audioAsset: AVURLAsset(url: InvidiousAPI.proxyURLForAsset(audioAssetURL!["url"].stringValue)!, options: options),
videoAsset: AVURLAsset(url: InvidiousAPI.proxyURLForAsset($0["url"].stringValue)!, options: options),
resolution: Stream.Resolution.from(resolution: $0["resolution"].stringValue)!,
kind: .adaptive,
2021-06-14 23:35:02 +05:30
encoding: $0["encoding"].stringValue
)
}
}
2021-06-11 04:20:10 +05:30
}