作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我以原始格式 (.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/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!