修复完成转换视频后会同时导出多个结果的bug

This commit is contained in:
bluesea 2024-03-11 17:02:48 +08:00
parent 2856b13cbe
commit f9cd80af4b
3 changed files with 12 additions and 28 deletions

View File

@ -588,34 +588,18 @@ extension VRVideoTransformController {
let outputVideoURL = URL.documentsDirectory.appending(path:"convertor_one.mov") let outputVideoURL = URL.documentsDirectory.appending(path:"convertor_one.mov")
let videoOriginalAsset:AVAsset? = AVAsset(url: sourceVideoURL!) let videoOriginalAsset:AVAsset? = AVAsset(url: sourceVideoURL!)
Task { Task {
try await spatialVideoConver.convertVideo(asset: videoOriginalAsset!, outputFile: outputVideoURL ,type: self.selected3DFormat) { [self] progress in try await spatialVideoConver.convertVideo(asset: videoOriginalAsset!, outputFile: outputVideoURL ,type: self.selected3DFormat) { [self] (progress,state) in
print(progress) print(progress)
// DispatchQueue.main.async { [weak self] in if(state){
if(progress > 0.99){ print("zzh the progress \(progress)")
DispatchQueue.main.asyncAfter(deadline: .now() + 20) {
// //
self.exportVideo(url: outputVideoURL, outputURL: outputURL, width: width, height: height,codecType:codecType, dataRate: dataRate, horizontalDisparity: horizontalDisparity, horizontalFieldOfView: horizontalFieldOfView) { exportedAsset in self.exportVideo(url: outputVideoURL, outputURL: outputURL, width: width, height: height,codecType:codecType, dataRate: dataRate, horizontalDisparity: horizontalDisparity, horizontalFieldOfView: horizontalFieldOfView) { exportedAsset in
DispatchQueue.main.async {
PHPhotoLibrary.shared().performChanges {
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: outputURL)
} completionHandler: { isSuccess, error in
if isSuccess {
SVProgressHUD.showSuccess(withStatus: "成功导出视频")
} else {
SVProgressHUD.showSuccess(withStatus: "导出视频失败")
} }
} }
} }
} }
} }
}
// }
}
}
}
private func exportVideo(url: URL, outputURL: URL, width: Int, height: Int,codecType:AVVideoCodecType, dataRate: Int, horizontalDisparity: Float, horizontalFieldOfView: Float, completion: @escaping (AVAsset?) -> Void) { private func exportVideo(url: URL, outputURL: URL, width: Int, height: Int,codecType:AVVideoCodecType, dataRate: Int, horizontalDisparity: Float, horizontalFieldOfView: Float, completion: @escaping (AVAsset?) -> Void) {
let asset = AVAsset(url: url) let asset = AVAsset(url: url)
@ -709,7 +693,7 @@ extension VRVideoTransformController {
} }
assetExportSession.videoComposition = AVMutableVideoComposition(propertiesOf: composition) assetExportSession.videoComposition = AVMutableVideoComposition(propertiesOf: composition)
assetExportSession.outputFileType = AVFileType.mp4 assetExportSession.outputFileType = AVFileType.mov
assetExportSession.outputURL = outputURL assetExportSession.outputURL = outputURL
assetExportSession.exportAsynchronously { [self] in assetExportSession.exportAsynchronously { [self] in
@ -737,7 +721,7 @@ extension VRVideoTransformController {
case .cancelled: case .cancelled:
print("Export cancelled") print("Export cancelled")
DispatchQueue.main.async { DispatchQueue.main.async {
SVProgressHUD.showInfo(withStatus: "视频导出失败") SVProgressHUD.showInfo(withStatus: "视频导出失败 cancelled")
} }
completion(nil) completion(nil)
default: default:

View File

@ -22,7 +22,7 @@ class SpatialVideoConvertor {
func convertVideo( asset : AVAsset, outputFile: URL,type:Video3DFormat, progress: ((Float)->())? = nil ) async throws { func convertVideo( asset : AVAsset, outputFile: URL,type:Video3DFormat, progress: ((Float,Bool)->())? = nil ) async throws {
do { do {
try FileManager.default.removeItem(atPath: outputFile.path) try FileManager.default.removeItem(atPath: outputFile.path)
print("视频文件删除成功") print("视频文件删除成功")
@ -168,7 +168,7 @@ class SpatialVideoConvertor {
print( "Added frame at \(time)") print( "Added frame at \(time)")
// callback with progress // callback with progress
progress?( Float(time.value)/Float(duration.value)) progress?( Float(time.value)/Float(duration.value),false)
// This sleep is needed to stop memory blooming - keeps around 280Mb rather than spiraling up to 8+Gig! // This sleep is needed to stop memory blooming - keeps around 280Mb rather than spiraling up to 8+Gig!
try await Task.sleep(nanoseconds: 3_000_000) try await Task.sleep(nanoseconds: 3_000_000)
@ -184,7 +184,7 @@ class SpatialVideoConvertor {
print( "Finished") print( "Finished")
_ = try await vw!.finish() _ = try await vw!.finish()
progress?( Float(1.0),true)
} }

View File

@ -102,7 +102,7 @@ class SpatialVideoWriter {
print("可以保存") print("可以保存")
completion(true, nil) completion(true, nil)
self.saveVideoToLibrary(videoURL: outputVideoURL, completion: completion) // self.saveVideoToLibrary(videoURL: outputVideoURL, completion: completion)
} }
} catch { } catch {
print("生成失败") print("生成失败")