Skip to content

Commit

Permalink
YouTube+PlayerItem: prefer using already combined stream urls for gen…
Browse files Browse the repository at this point in the history
…erating player items
  • Loading branch information
alexeichhorn committed Oct 17, 2024
1 parent c82335e commit fe9372e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Sources/YouTubeKit/YouTube+PlayerItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Foundation
import AVFoundation
import os.log

@available(iOS 13.0, watchOS 6.0, tvOS 13.0, macOS 10.15, *)
extension YouTube {
Expand All @@ -25,6 +26,14 @@ extension YouTube {
throw YouTubeKitError.extractError
}

// prefer already combined streams if available
if let bestCombinedStream = streams.filter({ $0.isNativelyPlayable }).filterVideoAndAudio().filter(byResolution: { ($0 ?? .max) <= (maxResolution ?? .max) }).highestResolutionStream() {
if (bestCombinedStream.videoResolution ?? 0) >= (videoStream.videoResolution ?? 0) {
os_log("Using already combined stream for %{public}@", log: OSLog(category: "YouTube+PlayerItem"), type: .info, videoID)
return AVPlayerItem(asset: AVURLAsset(url: bestCombinedStream.url))
}
}

// Add video track
let videoAsset = AVURLAsset(url: videoStream.url)
let videoTrack = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)
Expand Down

0 comments on commit fe9372e

Please sign in to comment.