Так что я использую AVAssetExportSession для размещения видео друг на друге. Я использую несколько различных последовательных экспортов для достижения эффекта, но один из них последовательно добавляет примерно 0,3 секунды к продолжительности файла, и я не могу понять, почему.
Мой код выглядит следующим образом (он был урезан)
fileprivate func setupVideo2(_ asset: AVURLAsset, inArea area: CGSize, completion: @escaping BlurredBackgroundManagerCompletion) {
let mixComposition = AVMutableComposition()
var instructionLayers : Array<AVMutableVideoCompositionLayerInstruction> = []
let track = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
if let videoTrack = asset.tracks(withMediaType: AVMediaType.video).first {
let timeRange = CMTimeRangeMake(start: .zero, duration: asset.duration)
try? track?.insertTimeRange(timeRange, of: videoTrack, at: .zero)
let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: track!)
instructionLayers.append(layerInstruction)
}
let mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = CMTimeRangeMake(start: .zero, duration: mixComposition.duration)
mainInstruction.layerInstructions = instructionLayers
let mainCompositionInst = AVMutableVideoComposition()
mainCompositionInst.instructions = [mainInstruction]
mainCompositionInst.frameDuration = CMTimeMake(value: 1, timescale: 30)
mainCompositionInst.renderSize = area
let url = self.videoOutputUrl()
try? FileManager.default.removeItem(at: url)
let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
exporter?.outputURL = url
exporter?.outputFileType = .mp4
exporter?.videoComposition = mainCompositionInst
exporter?.shouldOptimizeForNetworkUse = true
exporter?.exportAsynchronously(completionHandler: {
if let anError = exporter?.error {
completion(nil, anError)
} else if exporter?.status == AVAssetExportSession.Status.completed {
completion(url, nil)
}
})
}
Насколько я вижу, мои значения правильны, поэтому я не могу определить, где находятся дополнительные 0,3 секунды. ,
Мое использование таково:
let asset = AVURLAsset(url: url)
let size = CGSize(width: 1000, height: 1000)
print("DEBUG: STARTING URL DURATION \(url.getMediaDuration())")
setupVideo(asset, inArea: size) { (outputUrl, error) in
if let outputUrl = outputUrl {
print("DEBUG: Second Step URL Duration \(outputUrl.getMediaDuration())")
}
}
, и операторы print дают мне пример вывода:
DEBUG: STARTING URL DURATION 3.5033333333333334
DEBUG: Second Step URL Duration 3.533333333333333
При необходимости некоторые из вещей, упомянутых вкод, который может нуждаться в ссылках, это мой BlurnBackgroundManagerCompletion, который определен как:
typealias BlurredBackgroundManagerCompletion = ((URL?, Error?) -> ())
и моя функция videoOutputUrl ():
fileprivate func videoOutputUrl() -> URL {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! + "/output.mp4"
return URL(fileURLWithPath: path)
}
и моя функция getMediaDuration (), которая находится вРасширение URL
func getMediaDuration() -> Double {
let asset = AVURLAsset(url: self)
let duration = asset.duration
return CMTimeGetSeconds(duration)
}
Любая помощь будет принята с благодарностью.