gpt4 book ai didi

ios - IOS 的 Firebase 存储不从相机胶卷上传 .MOV

转载 作者:行者123 更新时间:2023-11-28 15:10:45 24 4
gpt4 key购买 nike

我正在从 PHAsset 对象中检索 URL (file:///var/mobile/Media/DCIM/100APPLE/IMG_0840.MOV),并且能够在设备上很好地重播视频。但是,当需要在 Firebase 存储上上传/放置文件时,我什至没有找回我的 completionHandler。

    //requesting url from asset
PHImageManager.default().requestAVAsset(forVideo: asset, options: options, resultHandler: { (videoAsset, audioMix, info) in

if let assetURL = videoAsset as? AVURLAsset {

let url = assetURL.url

//upload from url
let storageRef = Storage.storage().reference().child("videos").child("\(memory.ID!)")
let uploadMetaData = StorageMetadata()
uploadMetaData.contentType = "video/quicktime"
self.storageRef.putFile(from: url, metadata: uploadMetaData) { (metaData, error) in

if error != nil {
print(error!.localizedDescription)
}
}

如果我使用这样的本地 url 从 AVFoundation 捕获上传 .mp4 文件,(file:///var/mobile/Containers/Data/Application/209BB017-13C7-4432-AFE4-EC3A13469900/Documents/ff49bbc9ac794ed28d6f25944f128933.mp4),它工作正常。

我也试过在 Firebase 存储控制台上上传一个 .MOV 文件,这也有效。

谢谢

最佳答案

有时 PHAssets 不会返回 URLAsset,因此我们必须检查给定的 Assets 是否是 AVURLAsset,如果不是,我们必须将此 Assets 保存在我们的文档目录中,然后从文档目录获取 url。

func uploadVideoToFirebase(content: PHAsset) {
let options = PHVideoRequestOptions()
options.isNetworkAccessAllowed = true
PHImageManager.default().requestAVAsset(forVideo: content, options: options) { (asset, mix, args) in
if let asset = asset as? AVURLAsset {
let url = asset.url
// URL OF THE VIDEO IS GOT HERE
} else {
guard let asset = asset else {return}
self.startAnimating(message: "Processing.")
self.saveVideoInDocumentsDirectory(withAsset: asset, completion: { (url, error) in
if let error = error {
print(error.localizedDescription)
}
if let url = url {
// SAVED IN DOCUMENTS DIRECTORY AND URL IS GOT HERE
}
})
}
}
}

保存到文档目录:

func saveVideoInDocumentsDirectory(withAsset asset: AVAsset, completion: @escaping (_ url: URL?,_ error: Error?) -> Void) {
let manager = FileManager.default
guard let documentDirectory = try? manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {return}
var outputURL = documentDirectory.appendingPathComponent("output")
do {
try manager.createDirectory(at: outputURL, withIntermediateDirectories: true, attributes: nil)
let name = NSUUID().uuidString
outputURL = outputURL.appendingPathComponent("\(name).mp4")
}catch let error {
print(error.localizedDescription)
}
//Remove existing file
_ = try? manager.removeItem(at: outputURL)
guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetMediumQuality) else {return}
exportSession.outputURL = outputURL
exportSession.outputFileType = AVFileTypeMPEG4
exportSession.exportAsynchronously {
switch exportSession.status {
case .completed:
print("exported at \(outputURL)")
completion(outputURL, exportSession.error)
case .failed:
print("failed \(exportSession.error?.localizedDescription ?? "")")
completion(nil, exportSession.error)
case .cancelled:
print("cancelled \(exportSession.error?.localizedDescription ?? "")")
completion(nil, exportSession.error)
default: break
}
}
}

我们必须在成功上传后清除这个不需要的视频,或者在下次应用启动时清除它。我使用这种方法在下次应用启动时清除了它。

func clearDocumentsDirectory() {
let manager = FileManager.default
guard let documentDirectory = try? manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {return}
let outputURL = documentDirectory.appendingPathComponent("output")
//Remove existing file
_ = try? manager.removeItem(at: outputURL)
}

希望这对您有所帮助。

关于ios - IOS 的 Firebase 存储不从相机胶卷上传 .MOV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47682192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com