我記錄的前置攝像頭的視頻並輸出鏡像翻轉視頻......如何使用AVFoundation
我使用AVMutablecomposition
和layerinstructions
翻轉視頻,但沒有運氣嘗試。
谷歌搜索堆棧溢出已經沒有結果,所以我敢打賭,如何做到這一點的簡單,直接的例子是有益於許多事情的東西。
我記錄的前置攝像頭的視頻並輸出鏡像翻轉視頻......如何使用AVFoundation
我使用AVMutablecomposition
和layerinstructions
翻轉視頻,但沒有運氣嘗試。
谷歌搜索堆棧溢出已經沒有結果,所以我敢打賭,如何做到這一點的簡單,直接的例子是有益於許多事情的東西。
即使世界上使用的是錄製視頻什麼沒有任何跡象表明,病人承擔AVCaptureSession
+ AVCaptureVideoDataOutput
lazy var videoFileOutput: AVCaptureVideoDataOutput = AVCaptureVideoDataOutput()
let v = videoFileOutput.connectionWithMediaType(AVMediaTypeVideo)
v.videoOrientation = .Portrait
v.videoMirrored = true
您可以使用-[AVMutableVideoCompositionLayerInstruction setTransform:atTime:]
CGAffineTransform transform = CGAffineTransformMakeTranslation(self.config.videoSize, 0);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
[videoCompositionLayerInstruction setTransform:transform atTime:videoTime];
// then append video tracks
// [compositionTrack insertTimeRange:timeRange ofTrack:track atTime:atTime error:&error];
// apply instructions
videoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration);
videoCompositionInstruction.layerInstructions = @[videoCompositionLayerInstruction];
videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.renderSize = CGSizeMake(self.config.videoSize, self.config.videoSize);
videoComposition.frameDuration = CMTimeMake(1, self.config.videoFrameRate);
videoComposition.instructions = @[videoCompositionInstruction];
https://github.com/ElfSundae/AVDemo/tree/ef2ca437d0d8dcb3dd41c5a272c8754a29d8a936/AVSimpleEditoriOS
出口組成:
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:presetName];
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.outputURL = outputURL;
exportSession.shouldOptimizeForNetworkUse = YES;
// videoComposition contains transform instructions for video tracks
exportSession.videoComposition = videoComposition;
// audioMix contains background music for audio tracks
exportSession.audioMix = audioMix;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
AVAssetExportSessionStatus status = exportSession.status;
if (status != AVAssetExportSessionStatusCompleted) {
// exportSession.error
} else {
// exportSession.outputURL
}
}];
感謝您的例子 - 和你如何將這個作品導出到視頻中? – PinkFloydRocks
@PinkFloydRocks我使用AVAssetExportSession,答案已經更新。 –
CGAffineTransformMakeTranslation(self.config.videoSize,0); 。它需要一個CGFloat值,而不是一個CGSize值。不知道你是如何得到這段代碼的,然後在這裏發佈。 –
後你會得到你的輸出轉換視頻
func mirrorVideo(inputURL: URL, completion: @escaping (_ outputURL : URL?) ->())
{
let videoAsset: AVAsset = AVAsset(url: inputURL)
let clipVideoTrack = videoAsset.tracks(withMediaType: AVMediaType.video).first! as AVAssetTrack
let composition = AVMutableComposition()
composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: CMPersistentTrackID())
let videoComposition = AVMutableVideoComposition()
videoComposition.renderSize = CGSize(width: clipVideoTrack.naturalSize.height, height: clipVideoTrack.naturalSize.width)
videoComposition.frameDuration = CMTimeMake(1, 30)
let transformer = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30))
var transform:CGAffineTransform = CGAffineTransform(scaleX: -1.0, y: 1.0)
transform = transform.translatedBy(x: -clipVideoTrack.naturalSize.width, y: 0.0)
transform = transform.rotated(by: CGFloat(Double.pi/2))
transform = transform.translatedBy(x: 0.0, y: -clipVideoTrack.naturalSize.width)
transformer.setTransform(transform, at: kCMTimeZero)
instruction.layerInstructions = [transformer]
videoComposition.instructions = [instruction]
// Export
let exportSession = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPreset640x480)!
let fileName = UniqueIDGenerator.generate().appending(".mp4")
let filePath = documentsURL.appendingPathComponent(fileName)
let croppedOutputFileUrl = filePath
exportSession.outputURL = croppedOutputFileUrl
exportSession.outputFileType = AVFileType.mp4
exportSession.videoComposition = videoComposition
exportSession.exportAsynchronously {
if exportSession.status == .completed {
DispatchQueue.main.async(execute: {
completion(croppedOutputFileUrl)
})
return
} else if exportSession.status == .failed {
print("Export failed - \(String(describing: exportSession.error))")
}
completion(nil)
return
}
}
如果使用AVCaptureConnection記錄,我會糾正這個問題存在,使用設定影像顯示方向'setVideoOrientation' – Lefteris