swiftvideoavfoundationavassetavassetexportsession

Trim video always fail when use AVAssetExportPresetPassthrough


My trimming video code:

func trim(createNew: Bool, with name: String?, file: File, startTime: Double, endTime: Double, completion: @escaping ResultCompletion<File>) {
        guard checkFreeSpace(with: file) else {
            return handleFailure(error: .fullStorage, completion: completion)
        }
        
        guard let originUrl = file.localUrl(),
              let originName = file.name.components(separatedBy: ".").first,
              let originExtension = file.name.components(separatedBy: ".").last else {
            return handleFailure(error: .mediaSavingError, completion: completion)
        }
        
        let asset = AVAsset(url: originUrl)
        let timeZoneOffset = TimeInterval(TimeZone.current.secondsFromGMT())
        let newDate = Date().addingTimeInterval(timeZoneOffset)
        let temporaryFileId = "\(file.device ?? "Unknown")-\(newDate.milliseconds)-0-\(originName)_trim.\(originExtension)"
        let outputUrl = file.buildLocalUrl(id: temporaryFileId)

        AVAssetExportSession.determineCompatibility(
            ofExportPreset: AVAssetExportPresetPassthrough,
            with: asset,
            outputFileType: .mp4) { isCompatible in
            print("Can Trim MP4: ", isCompatible)
        }
        
        guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough) else {
            return handleFailure(error: .mediaSavingError, completion: completion)
        }
        
        exportSession.outputURL = outputUrl
        exportSession.outputFileType = .mp4
        
        let timeRange = CMTimeRange(start: CMTime(seconds: startTime, preferredTimescale: 900),
                                    end: CMTime(seconds: endTime, preferredTimescale: 900))
        
        exportSession.timeRange = timeRange
        
        exportSession.exportAsynchronously { [weak self] in
            guard let self = self else {
                return
            }
            exportProgressTimer.invalidate()
            switch exportSession.status {
            case .completed:
                completion(.success(file))
            case .failed:
                self.handleFailure(error: .mediaSavingError, completion: completion)
            case .cancelled:
                completion(.failure(FileError.mediaExportCanceled))
            default:
                break
            }
        }
    }

determineCompatibility return true

When use another preset like that contained in AVAssetExportSession.exportPresets(compatibleWith: item.asset), all do makes awesome

Maybe someone can explain what goes wrong and how I can fixed it? i need resolution the same like at original video

Or how I can convert with AVAssetWriter and AVAssetReader ?


Solution

  • I wrote anwer in another question, they have the same problem, but it different way. That's how I solved the problem https://stackoverflow.com/a/70294032/7217629