繁体   English   中英

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

[英]After converting from MOV to MP4 file uploads as a 0 byte file in AWS S3

当我上传捕获的视频文件的原始格式 (.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)")
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM