2022-05-19 07:29:25 -06:00
|
|
|
import AVFoundation
|
2022-07-27 07:13:47 -06:00
|
|
|
import Photos
|
2022-05-19 07:29:25 -06:00
|
|
|
|
2024-01-06 12:06:53 -07:00
|
|
|
// MARK: - RCTVideoAssetsUtils
|
|
|
|
|
|
|
|
enum RCTVideoAssetsUtils {
|
|
|
|
static func getMediaSelectionGroup(
|
|
|
|
asset: AVAsset?,
|
|
|
|
for mediaCharacteristic: AVMediaCharacteristic
|
2024-04-04 05:23:44 -06:00
|
|
|
) async -> AVMediaSelectionGroup? {
|
2024-01-06 12:06:53 -07:00
|
|
|
if #available(iOS 15, tvOS 15, visionOS 1.0, *) {
|
2024-08-21 02:26:32 -06:00
|
|
|
do {
|
|
|
|
guard let asset else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return try await asset.loadMediaSelectionGroup(for: mediaCharacteristic)
|
|
|
|
} catch {
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-06 12:06:53 -07:00
|
|
|
} else {
|
|
|
|
#if !os(visionOS)
|
2024-04-04 05:23:44 -06:00
|
|
|
return asset?.mediaSelectionGroup(forMediaCharacteristic: mediaCharacteristic)
|
2024-01-06 12:06:53 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
static func getTracks(asset: AVAsset, withMediaType: AVMediaType) async -> [AVAssetTrack]? {
|
2024-01-06 12:06:53 -07:00
|
|
|
if #available(iOS 15, tvOS 15, visionOS 1.0, *) {
|
2024-04-04 05:23:44 -06:00
|
|
|
return try? await asset.loadTracks(withMediaType: withMediaType)
|
2024-01-06 12:06:53 -07:00
|
|
|
} else {
|
2024-01-15 00:04:29 -07:00
|
|
|
#if !os(visionOS)
|
2024-04-04 05:23:44 -06:00
|
|
|
return asset.tracks(withMediaType: withMediaType)
|
2024-01-15 00:04:29 -07:00
|
|
|
#endif
|
2024-01-06 12:06:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - RCTVideoUtils
|
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
/*!
|
|
|
|
* Collection of pure functions
|
|
|
|
*/
|
|
|
|
enum RCTVideoUtils {
|
|
|
|
/*!
|
|
|
|
* Calculates and returns the playable duration of the current player item using its loaded time ranges.
|
|
|
|
*
|
|
|
|
* \returns The playable duration of the current player item in seconds.
|
|
|
|
*/
|
2023-12-07 00:47:40 -07:00
|
|
|
static func calculatePlayableDuration(_ player: AVPlayer?, withSource source: VideoSource?) -> NSNumber {
|
2024-01-04 12:16:23 -07:00
|
|
|
guard let player,
|
2023-12-07 00:47:40 -07:00
|
|
|
let video: AVPlayerItem = player.currentItem,
|
2022-05-19 07:29:25 -06:00
|
|
|
video.status == AVPlayerItem.Status.readyToPlay else {
|
|
|
|
return 0
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
if source?.cropStart != nil && source?.cropEnd != nil {
|
2023-11-24 04:52:46 -07:00
|
|
|
return NSNumber(value: (Float64(source?.cropEnd ?? 0) - Float64(source?.cropStart ?? 0)) / 1000)
|
2023-02-07 13:50:54 -07:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
var effectiveTimeRange: CMTimeRange?
|
|
|
|
for value in video.loadedTimeRanges {
|
|
|
|
let timeRange: CMTimeRange = value.timeRangeValue
|
2022-05-19 07:29:25 -06:00
|
|
|
if CMTimeRangeContainsTime(timeRange, time: video.currentTime()) {
|
|
|
|
effectiveTimeRange = timeRange
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-01-04 12:16:23 -07:00
|
|
|
if let effectiveTimeRange {
|
2023-12-07 00:47:40 -07:00
|
|
|
let playableDuration: Float64 = CMTimeGetSeconds(CMTimeRangeGetEnd(effectiveTimeRange))
|
2022-05-19 07:29:25 -06:00
|
|
|
if playableDuration > 0 {
|
2023-12-07 00:47:40 -07:00
|
|
|
if source?.cropStart != nil {
|
|
|
|
return NSNumber(value: playableDuration - Float64(source?.cropStart ?? 0) / 1000)
|
2023-02-07 13:50:54 -07:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
return playableDuration as NSNumber
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-09-22 10:41:25 -06:00
|
|
|
static func urlFilePath(filepath: NSString?, searchPath: FileManager.SearchPathDirectory) -> NSURL! {
|
|
|
|
guard let _filepath = filepath else { return nil }
|
|
|
|
|
|
|
|
if _filepath.contains("file://") {
|
|
|
|
return NSURL(string: _filepath as String)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
// if no file found, check if the file exists in the Document directory
|
2024-09-22 10:41:25 -06:00
|
|
|
let paths: [String] = NSSearchPathForDirectoriesInDomains(searchPath, .userDomainMask, true)
|
|
|
|
var relativeFilePath: String = _filepath.lastPathComponent
|
2022-05-19 07:29:25 -06:00
|
|
|
// the file may be multiple levels below the documents directory
|
2024-09-22 10:41:25 -06:00
|
|
|
let directoryString: String = searchPath == .cachesDirectory ? "Library/Caches/" : "Documents"
|
|
|
|
let fileComponents: [String] = _filepath.components(separatedBy: directoryString)
|
2022-05-19 07:29:25 -06:00
|
|
|
if fileComponents.count > 1 {
|
|
|
|
relativeFilePath = fileComponents[1]
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-09-22 10:41:25 -06:00
|
|
|
guard let _pathFirst = paths.first else { return nil }
|
|
|
|
let path: String = (_pathFirst as NSString).appendingPathComponent(relativeFilePath)
|
2022-05-19 07:29:25 -06:00
|
|
|
if FileManager.default.fileExists(atPath: path) {
|
|
|
|
return NSURL.fileURL(withPath: path) as NSURL
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
static func playerItemSeekableTimeRange(_ player: AVPlayer?) -> CMTimeRange {
|
2022-05-19 07:29:25 -06:00
|
|
|
if let playerItem = player?.currentItem,
|
|
|
|
playerItem.status == .readyToPlay,
|
|
|
|
let firstItem = playerItem.seekableTimeRanges.first {
|
|
|
|
return firstItem.timeRangeValue
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
return CMTimeRange.zero
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
static func playerItemDuration(_ player: AVPlayer?) -> CMTime {
|
2022-05-19 07:29:25 -06:00
|
|
|
if let playerItem = player?.currentItem,
|
|
|
|
playerItem.status == .readyToPlay {
|
2023-12-07 00:47:40 -07:00
|
|
|
return playerItem.duration
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
return CMTime.invalid
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
static func calculateSeekableDuration(_ player: AVPlayer?) -> NSNumber {
|
|
|
|
let timeRange: CMTimeRange = RCTVideoUtils.playerItemSeekableTimeRange(player)
|
|
|
|
if CMTIME_IS_NUMERIC(timeRange.duration) {
|
2022-05-19 07:29:25 -06:00
|
|
|
return NSNumber(value: CMTimeGetSeconds(timeRange.duration))
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
static func getAudioTrackInfo(_ player: AVPlayer?) async -> [AnyObject] {
|
|
|
|
guard let player, let asset = player.currentItem?.asset else {
|
|
|
|
return []
|
|
|
|
}
|
2023-01-28 06:54:01 -07:00
|
|
|
|
2024-09-22 10:41:25 -06:00
|
|
|
let audioTracks = NSMutableArray()
|
2024-01-06 12:06:53 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
let group = await RCTVideoAssetsUtils.getMediaSelectionGroup(asset: asset, for: .audible)
|
2024-01-06 12:06:53 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
for i in 0 ..< (group?.options.count ?? 0) {
|
|
|
|
let currentOption = group?.options[i]
|
|
|
|
var title = ""
|
|
|
|
let values = currentOption?.commonMetadata.map(\.value)
|
|
|
|
if (values?.count ?? 0) > 0, let value = values?[0] {
|
|
|
|
title = value as! String
|
|
|
|
}
|
2024-09-22 10:41:25 -06:00
|
|
|
let language: String = currentOption?.extendedLanguageTag ?? ""
|
2024-01-06 12:06:53 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
let selectedOption: AVMediaSelectionOption? = player.currentItem?.currentMediaSelection.selectedMediaOption(in: group!)
|
2023-01-28 06:54:01 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
let audioTrack = [
|
|
|
|
"index": NSNumber(value: i),
|
|
|
|
"title": title,
|
2024-09-22 10:41:25 -06:00
|
|
|
"language": language,
|
2024-04-04 05:23:44 -06:00
|
|
|
"selected": currentOption?.displayName == selectedOption?.displayName,
|
|
|
|
] as [String: Any]
|
|
|
|
audioTracks.add(audioTrack)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
|
|
|
|
return audioTracks as [AnyObject]
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
static func getTextTrackInfo(_ player: AVPlayer?) async -> [TextTrack] {
|
|
|
|
guard let player, let asset = player.currentItem?.asset else {
|
|
|
|
return []
|
|
|
|
}
|
2022-05-19 07:29:25 -06:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
// if streaming video, we extract the text tracks
|
|
|
|
var textTracks: [TextTrack] = []
|
|
|
|
let group = await RCTVideoAssetsUtils.getMediaSelectionGroup(asset: asset, for: .legible)
|
2024-01-06 12:06:53 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
for i in 0 ..< (group?.options.count ?? 0) {
|
|
|
|
let currentOption = group?.options[i]
|
|
|
|
var title = ""
|
|
|
|
let values = currentOption?.commonMetadata.map(\.value)
|
|
|
|
if (values?.count ?? 0) > 0, let value = values?[0] {
|
|
|
|
title = value as! String
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2024-09-22 10:41:25 -06:00
|
|
|
let language: String = currentOption?.extendedLanguageTag ?? ""
|
2024-04-04 05:23:44 -06:00
|
|
|
let selectedOption: AVMediaSelectionOption? = player.currentItem?.currentMediaSelection.selectedMediaOption(in: group!)
|
|
|
|
let textTrack = TextTrack([
|
|
|
|
"index": NSNumber(value: i),
|
|
|
|
"title": title,
|
2024-09-13 02:49:43 -06:00
|
|
|
"language": language as Any,
|
2024-04-04 05:23:44 -06:00
|
|
|
"selected": currentOption?.displayName == selectedOption?.displayName,
|
|
|
|
])
|
|
|
|
textTracks.append(textTrack)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
|
|
|
|
return textTracks
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
// UNUSED
|
2023-12-07 00:47:40 -07:00
|
|
|
static func getCurrentTime(playerItem: AVPlayerItem?) -> Float {
|
2022-05-19 07:29:25 -06:00
|
|
|
return Float(CMTimeGetSeconds(playerItem?.currentTime() ?? .zero))
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
static func base64DataFromBase64String(base64String: String?) -> Data? {
|
2024-01-04 12:16:23 -07:00
|
|
|
if let base64String {
|
2023-12-07 00:47:40 -07:00
|
|
|
return Data(base64Encoded: base64String)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
static func replaceURLScheme(url: URL, scheme: String?) -> URL? {
|
|
|
|
var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
|
|
|
|
urlComponents?.scheme = scheme
|
|
|
|
|
|
|
|
return urlComponents?.url
|
|
|
|
}
|
|
|
|
|
|
|
|
static func extractDataFromCustomSchemeUrl(from url: URL, scheme: String) -> Data? {
|
|
|
|
guard url.scheme == scheme,
|
2023-12-07 00:47:40 -07:00
|
|
|
let adoptURL = RCTVideoUtils.replaceURLScheme(url: url, scheme: nil) else { return nil }
|
2022-05-19 07:29:25 -06:00
|
|
|
|
|
|
|
return Data(base64Encoded: adoptURL.absoluteString)
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
static func generateMixComposition(_ asset: AVAsset) async -> AVMutableComposition {
|
|
|
|
let videoTracks = await RCTVideoAssetsUtils.getTracks(asset: asset, withMediaType: .video)
|
|
|
|
let audioTracks = await RCTVideoAssetsUtils.getTracks(asset: asset, withMediaType: .audio)
|
|
|
|
|
|
|
|
let mixComposition = AVMutableComposition()
|
|
|
|
|
|
|
|
if let videoAsset = videoTracks?.first, let audioAsset = audioTracks?.first {
|
|
|
|
let videoCompTrack: AVMutableCompositionTrack! = mixComposition.addMutableTrack(
|
|
|
|
withMediaType: AVMediaType.video,
|
|
|
|
preferredTrackID: kCMPersistentTrackID_Invalid
|
|
|
|
)
|
|
|
|
try? videoCompTrack.insertTimeRange(
|
|
|
|
CMTimeRangeMake(start: .zero, duration: videoAsset.timeRange.duration),
|
|
|
|
of: videoAsset,
|
|
|
|
at: .zero
|
|
|
|
)
|
|
|
|
|
|
|
|
let audioCompTrack: AVMutableCompositionTrack! = mixComposition.addMutableTrack(
|
|
|
|
withMediaType: AVMediaType.audio,
|
|
|
|
preferredTrackID: kCMPersistentTrackID_Invalid
|
|
|
|
)
|
|
|
|
|
|
|
|
try? audioCompTrack.insertTimeRange(
|
|
|
|
CMTimeRangeMake(start: .zero, duration: audioAsset.timeRange.duration),
|
|
|
|
of: audioAsset,
|
|
|
|
at: .zero
|
|
|
|
)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
|
|
|
|
return mixComposition
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-01-06 12:06:53 -07:00
|
|
|
static func getValidTextTracks(asset: AVAsset, assetOptions: NSDictionary?, mixComposition: AVMutableComposition,
|
2024-04-04 05:23:44 -06:00
|
|
|
textTracks: [TextTrack]?) async -> [TextTrack] {
|
2023-12-07 00:47:40 -07:00
|
|
|
var validTextTracks: [TextTrack] = []
|
2024-04-19 14:38:52 -06:00
|
|
|
var tracks: [([AVAssetTrack], AVURLAsset)] = []
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
let videoTracks = await RCTVideoAssetsUtils.getTracks(asset: asset, withMediaType: .video)
|
|
|
|
guard let videoAsset = videoTracks?.first else { return validTextTracks }
|
2024-01-06 12:06:53 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
if let textTracks, !textTracks.isEmpty {
|
|
|
|
for textTrack in textTracks {
|
|
|
|
var textURLAsset: AVURLAsset!
|
|
|
|
let textUri: String = textTrack.uri
|
2024-01-06 12:06:53 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
if textUri.lowercased().hasPrefix("http") {
|
|
|
|
textURLAsset = AVURLAsset(url: NSURL(string: textUri)! as URL, options: (assetOptions as! [String: Any]))
|
|
|
|
} else {
|
|
|
|
let isDisabledTrack: Bool! = textTrack.type == "disabled"
|
|
|
|
let searchPath: FileManager.SearchPathDirectory = isDisabledTrack ? .cachesDirectory : .documentDirectory
|
|
|
|
textURLAsset = AVURLAsset(
|
|
|
|
url: RCTVideoUtils.urlFilePath(filepath: textUri as NSString?, searchPath: searchPath) as URL,
|
|
|
|
options: nil
|
|
|
|
)
|
|
|
|
}
|
2024-01-06 12:06:53 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
if let track = await RCTVideoAssetsUtils.getTracks(asset: textURLAsset, withMediaType: .text) {
|
2024-04-19 14:38:52 -06:00
|
|
|
tracks.append((track, textURLAsset))
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-04-19 14:38:52 -06:00
|
|
|
for (index, tracksPair) in tracks.enumerated() {
|
|
|
|
let (tracks, trackAsset) = tracksPair
|
|
|
|
guard let track = tracks.first else { continue } // fix when there's no textTrackAsset
|
2024-01-06 12:06:53 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
let textCompTrack: AVMutableCompositionTrack! = mixComposition.addMutableTrack(withMediaType: AVMediaType.text,
|
|
|
|
preferredTrackID: kCMPersistentTrackID_Invalid)
|
2024-01-06 12:06:53 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
do {
|
2024-04-19 14:38:52 -06:00
|
|
|
try textCompTrack.insertTimeRange(CMTimeRangeMake(start: .zero, duration: trackAsset.duration), of: track, at: .zero)
|
|
|
|
validTextTracks.append(textTracks[index])
|
2024-04-04 05:23:44 -06:00
|
|
|
} catch {
|
|
|
|
// TODO: upgrade error by call some props callback to better inform user
|
|
|
|
print("Error occurred on textTrack insert attempt: \(error.localizedDescription)")
|
|
|
|
continue
|
2024-01-06 12:06:53 -07:00
|
|
|
}
|
|
|
|
}
|
2022-06-13 03:40:43 -06:00
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
|
|
|
|
if !validTextTracks.isEmpty {
|
|
|
|
let emptyVttFile: TextTrack? = self.createEmptyVttFile()
|
|
|
|
if emptyVttFile != nil {
|
|
|
|
validTextTracks.append(emptyVttFile!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return validTextTracks
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
|
2022-06-13 03:40:43 -06:00
|
|
|
/*
|
2023-12-07 00:47:40 -07:00
|
|
|
* Create an useless/almost empty VTT file in the list with available tracks.
|
|
|
|
* This track gets selected when you give type: "disabled" as the selectedTextTrack
|
2022-06-13 03:40:43 -06:00
|
|
|
* This is needed because there is a bug where sideloaded texttracks cannot be disabled in the AVPlayer. Loading this VTT file instead solves that problem.
|
|
|
|
* For more info see: https://github.com/react-native-community/react-native-video/issues/1144
|
|
|
|
*/
|
|
|
|
static func createEmptyVttFile() -> TextTrack? {
|
|
|
|
let fileManager = FileManager.default
|
|
|
|
let cachesDirectoryUrl = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
|
2022-06-13 03:42:12 -06:00
|
|
|
let filePath = cachesDirectoryUrl.appendingPathComponent("empty.vtt").path
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-06-13 03:40:43 -06:00
|
|
|
if !fileManager.fileExists(atPath: filePath) {
|
|
|
|
let stringToWrite = "WEBVTT\n\n1\n99:59:59.000 --> 99:59:59.001\n."
|
|
|
|
|
|
|
|
do {
|
|
|
|
try stringToWrite.write(to: URL(fileURLWithPath: filePath), atomically: true, encoding: String.Encoding.utf8)
|
|
|
|
} catch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2022-06-13 03:40:43 -06:00
|
|
|
return TextTrack([
|
|
|
|
"language": "disabled",
|
|
|
|
"title": "EmptyVttFile",
|
|
|
|
"type": "text/vtt",
|
|
|
|
"uri": filePath,
|
|
|
|
])
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
static func delay(seconds: Int = 0, completion: @escaping () async throws -> Void) {
|
|
|
|
return DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(seconds)) {
|
|
|
|
Task.detached(priority: .userInitiated) {
|
|
|
|
try await completion()
|
2023-12-07 00:47:40 -07:00
|
|
|
}
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
static func preparePHAsset(uri: String) async -> AVAsset? {
|
|
|
|
let assetId = String(uri[uri.index(uri.startIndex, offsetBy: "ph://".count)...])
|
|
|
|
guard let phAsset = PHAsset.fetchAssets(withLocalIdentifiers: [assetId], options: nil).firstObject else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let options = PHVideoRequestOptions()
|
|
|
|
options.isNetworkAccessAllowed = true
|
|
|
|
|
|
|
|
return await withCheckedContinuation { continuation in
|
2022-07-27 07:34:08 -06:00
|
|
|
PHCachingImageManager().requestAVAsset(forVideo: phAsset, options: options) { data, _, _ in
|
2024-04-04 05:23:44 -06:00
|
|
|
continuation.resume(returning: data)
|
2022-07-27 07:13:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
|
|
|
static func prepareAsset(source: VideoSource) -> (asset: AVURLAsset?, assetOptions: NSMutableDictionary?)? {
|
2022-07-27 07:13:47 -06:00
|
|
|
guard let sourceUri = source.uri, sourceUri != "" else { return nil }
|
2023-12-07 00:47:40 -07:00
|
|
|
var asset: AVURLAsset!
|
2024-09-22 10:41:25 -06:00
|
|
|
let bundlePath = Bundle.main.path(forResource: sourceUri, ofType: source.type) ?? ""
|
|
|
|
guard let url = source.isNetwork || source.isAsset
|
|
|
|
? URL(string: sourceUri)
|
|
|
|
: URL(fileURLWithPath: bundlePath) else { return nil }
|
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
let assetOptions: NSMutableDictionary! = NSMutableDictionary()
|
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
if source.isNetwork {
|
2023-12-07 00:47:40 -07:00
|
|
|
if let headers = source.requestHeaders, !headers.isEmpty {
|
|
|
|
assetOptions.setObject(headers, forKey: "AVURLAssetHTTPHeaderFieldsKey" as NSCopying)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
let cookies: [AnyObject]! = HTTPCookieStorage.shared.cookies
|
2024-09-13 02:49:43 -06:00
|
|
|
assetOptions.setObject(cookies as Any, forKey: AVURLAssetHTTPCookiesKey as NSCopying)
|
2024-09-22 10:41:25 -06:00
|
|
|
asset = AVURLAsset(url: url, options: assetOptions as? [String: Any])
|
2022-05-19 07:29:25 -06:00
|
|
|
} else {
|
2024-09-22 10:41:25 -06:00
|
|
|
asset = AVURLAsset(url: url)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
return (asset, assetOptions)
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2023-09-09 08:15:51 -06:00
|
|
|
static func createMetadataItems(for mapping: [AVMetadataIdentifier: Any]) -> [AVMetadataItem] {
|
2023-12-07 00:47:40 -07:00
|
|
|
return mapping.compactMap { createMetadataItem(for: $0, value: $1) }
|
2023-09-09 08:15:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static func createMetadataItem(for identifier: AVMetadataIdentifier,
|
2023-11-17 00:19:39 -07:00
|
|
|
value: Any) -> AVMetadataItem {
|
2023-09-09 08:15:51 -06:00
|
|
|
let item = AVMutableMetadataItem()
|
|
|
|
item.identifier = identifier
|
|
|
|
item.value = value as? NSCopying & NSObjectProtocol
|
|
|
|
// Specify "und" to indicate an undefined language.
|
|
|
|
item.extendedLanguageTag = "und"
|
|
|
|
return item.copy() as! AVMetadataItem
|
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-05-07 04:30:57 -06:00
|
|
|
static func createImageMetadataItem(imageUri: String) async -> Data? {
|
|
|
|
guard let url = URL(string: imageUri) else {
|
|
|
|
return nil
|
2023-10-07 07:14:10 -06:00
|
|
|
}
|
2023-12-07 00:47:40 -07:00
|
|
|
|
2024-05-07 04:30:57 -06:00
|
|
|
do {
|
|
|
|
let (data, _) = try await URLSession.shared.data(from: url)
|
|
|
|
guard let image = UIImage(data: data), let pngData = image.pngData() else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return pngData
|
|
|
|
} catch {
|
|
|
|
print("Error fetching image data: \(error.localizedDescription)")
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-07 07:14:10 -06:00
|
|
|
}
|
2023-12-28 04:58:25 -07:00
|
|
|
|
|
|
|
static func getCurrentWindow() -> UIWindow? {
|
|
|
|
if #available(iOS 13.0, tvOS 13, *) {
|
|
|
|
return UIApplication.shared.connectedScenes
|
|
|
|
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
|
|
|
|
.last { $0.isKeyWindow }
|
|
|
|
} else {
|
|
|
|
#if !os(visionOS)
|
|
|
|
return UIApplication.shared.keyWindow
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2024-01-06 12:06:53 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
static func generateVideoComposition(asset: AVAsset, filter: CIFilter) async -> AVVideoComposition? {
|
2024-01-06 12:06:53 -07:00
|
|
|
if #available(iOS 16, tvOS 16, visionOS 1.0, *) {
|
2024-04-04 05:23:44 -06:00
|
|
|
return try? await AVVideoComposition.videoComposition(
|
|
|
|
with: asset,
|
|
|
|
applyingCIFiltersWithHandler: { (request: AVAsynchronousCIImageFilteringRequest) in
|
2024-09-13 02:49:43 -06:00
|
|
|
let image: CIImage! = request.sourceImage.clampedToExtent()
|
|
|
|
filter.setValue(image, forKey: kCIInputImageKey)
|
|
|
|
let output: CIImage! = filter.outputImage?.cropped(to: request.sourceImage.extent)
|
|
|
|
request.finish(with: output, context: nil)
|
2024-04-04 05:23:44 -06:00
|
|
|
}
|
|
|
|
)
|
2024-01-06 12:06:53 -07:00
|
|
|
} else {
|
|
|
|
#if !os(visionOS)
|
2024-04-04 05:23:44 -06:00
|
|
|
return AVVideoComposition(
|
|
|
|
asset: asset,
|
|
|
|
applyingCIFiltersWithHandler: { (request: AVAsynchronousCIImageFilteringRequest) in
|
2024-09-13 02:49:43 -06:00
|
|
|
let image: CIImage! = request.sourceImage.clampedToExtent()
|
|
|
|
filter.setValue(image, forKey: kCIInputImageKey)
|
|
|
|
let output: CIImage! = filter.outputImage?.cropped(to: request.sourceImage.extent)
|
|
|
|
request.finish(with: output, context: nil)
|
2024-04-04 05:23:44 -06:00
|
|
|
}
|
|
|
|
)
|
2024-01-06 12:06:53 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|