gpt4 book ai didi

ios - 从 MOV 转换为 MP4 文件后上传为 AWS S3 中的 0 字节文件

转载 作者:行者123 更新时间:2023-11-28 11:27:33 26 4
gpt4 key购买 nike

当我以原始格式 (.MOV) 上传捕获的视频文件时,TransferUtility 运行良好,但当上传转换后的文件时,它变成 0 字节文件。

将文件从 .MOV 转换为 .MP4 后,我检查文件大小不为 0。此外,我还必须将内容类型从电影/quicktime 更改为视频/mp4。

这是正确的过程吗?

调用上传函数

  uploadToAWS(path: filePath, contentType: "video/mp4", key: videoname)

文件转换函数

 func exportVideo(inputurl: URL,
presetName: String = AVAssetExportPresetHighestQuality,
outputFileType: AVFileType = .mp4,
fileExtension: String = "mp4",
then completion: @escaping (URL?) -> Void)
{
let asset = AVAsset(url: inputurl)


let filename = filePath.deletingPathExtension().appendingPathExtension(fileExtension).lastPathComponent
outputURL = FileManager.default.temporaryDirectory.appendingPathComponent(filename)

if let session = AVAssetExportSession(asset: asset, presetName: presetName) {
session.outputURL = outputURL
session.outputFileType = outputFileType

session.shouldOptimizeForNetworkUse = true
session.exportAsynchronously {
switch session.status {
case .completed:
completion(self.outputURL)
case .cancelled:
debugPrint("Video export cancelled.")
completion(nil)
case .failed:
let errorMessage = session.error?.localizedDescription ?? "n/a"
debugPrint("Video export failed with error: \(errorMessage)")
completion(nil)
default:
break
}
}
} else {
completion(nil)
}
}

上传功能

func uploadToAWS(path: URL, contentType: String, key: String) {

exportVideo(inputurl: path, presetName: AVAssetExportPresetHighestQuality, outputFileType: .mp4, fileExtension: "mp4") { (outputURL) in

//here i checked that the file has not 0 bytes
do {
let resources = try outputURL?.resourceValues(forKeys:[.fileSizeKey])
let fileSize = resources?.fileSize!
print ("size of this video is \(fileSize)")
} catch {
print("Error: \(error)")
}

}

let expression = AWSS3TransferUtilityUploadExpression()
expression.progressBlock = progressBlock

transferUtility.uploadFile(outputURL, bucket: bucket, key: key, contentType: contentType, expression: expression, completionHandler: completionHandler).continueWith { (task) -> AnyObject? in
if let error = task.error {
print("Error: \(error.localizedDescription)")
DispatchQueue.main.async {
print("failed")
}
}
if let _ = task.result {
DispatchQueue.main.async {
print("Upload Starting!")
}
// Do something with uploadTask.
}
return nil;
}
}

最佳答案

您的上传 (transferUtility.uploadFile(...)) 当前在 exportVideo 返回后立即开始,这仅保证 AVAssetExportSession已经创建,而不是等到它完成。将上传逻辑移到导出完成中,您应该会发现上传操作是在完成的导出上进行的:

func uploadToAWS(path: URL, contentType: String, key: String) {

exportVideo(inputurl: path, presetName: AVAssetExportPresetHighestQuality, outputFileType: .mp4, fileExtension: "mp4") { (outputURL) in

//here i checked that the file has not 0 bytes
do {
let resources = try outputURL?.resourceValues(forKeys:[.fileSizeKey])
let fileSize = resources?.fileSize!
print ("size of this video is \(fileSize)")

let expression = AWSS3TransferUtilityUploadExpression()
expression.progressBlock = progressBlock

transferUtility.uploadFile(outputURL, bucket: bucket, key: key, contentType: contentType, expression: expression, completionHandler: completionHandler).continueWith { (task) -> AnyObject? in
if let error = task.error {
print("Error: \(error.localizedDescription)")
DispatchQueue.main.async {
print("failed")
}
}
if let _ = task.result {
DispatchQueue.main.async {
print("Upload Starting!")
}
// Do something with uploadTask.
}
return nil
}
} catch {
print("Error: \(error)")
}
}
}

关于ios - 从 MOV 转换为 MP4 文件后上传为 AWS S3 中的 0 字节文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57744376/

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