AVMutableComposition 没有正确定位视频
AVMutableComposition Not Orienting Video Properly
我一天的大部分时间都在研究 Whosebug,虽然有 很多 关于这个主题的好帖子,但我还没有找到解决方案我的问题。
我正在使用 AVAssetWriter
编写视频文件,没问题。我的视频文件,如果我保存到我的相机胶卷,可以正确播放并按预期方向播放。这是我的设置方式;
init(fileUrl:URL!, height:Int, width:Int) {
// Setup the filter writer instance
fileWriter = try? AVAssetWriter(outputURL: fileUrl, fileType: AVFileType.mov)
// Setup the video settings
let videoOutputSettings: Dictionary<String, AnyObject> = [
AVVideoCodecKey : AVVideoCodecType.hevc as AnyObject,
AVVideoWidthKey : width as AnyObject,
AVVideoHeightKey : height as AnyObject
]
// Setup the attributes dictionary
let sourcePixelBufferAttributesDictionary = [
String(kCVPixelBufferPixelFormatTypeKey) : Int(kCVPixelFormatType_32BGRA),
String(kCVPixelBufferWidthKey) : Int(width),
String(kCVPixelBufferHeightKey) : Int(height),
String(kCVPixelFormatOpenGLESCompatibility) : kCFBooleanTrue
] as [String : Any]
// Setup the video input
videoInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: videoOutputSettings)
// Data should be expected in real time
videoInput.expectsMediaDataInRealTime = true
// Perform transform
videoInput.transform = CGAffineTransform(rotationAngle: CGFloat(CGFloat.pi / 2.0))
// Setup pixel buffer intput
assetWriterPixelBufferInput = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: videoInput,
sourcePixelBufferAttributes: sourcePixelBufferAttributesDictionary)
// Add the input
fileWriter.add(videoInput)
}
然后我想使用 AVMutableComposition
保存应用了图像叠加层的视频,除了视频方向不正确外,它工作正常;
func postProcessVideo(toFPS: Double, sourceVideo: URL, destination: URL, filterImage: UIImage?, completionHandler: @escaping (_ response: Bool) -> ()) {
// Log
print("Received call to begin post-processing video at:", sourceVideo)
// Instantiate the AVMutableComposion
let composition = AVMutableComposition()
// Setup the video asset
let vidAsset = AVURLAsset(url: sourceVideo, options: [:])
// Get video track
let vtrack = vidAsset.tracks(withMediaType: AVMediaType.video)
// Setup the first video track as asset track
let videoTrack: AVAssetTrack = vtrack[0]
// Setup the video timerange
let vid_timerange = CMTimeRangeMake(kCMTimeZero, vidAsset.duration)
// Setup the composition video track
let compositionvideoTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: CMPersistentTrackID())!
// Insert expected time range
do {
try compositionvideoTrack.insertTimeRange(vid_timerange, of: videoTrack, at: kCMTimeZero)
} catch {
}
// Setup the preferred transform
compositionvideoTrack.preferredTransform = videoTrack.preferredTransform
// Update time scale
let finalTimeScale: Int64 = vidAsset.duration.value * 3
// Adjust video track duration
compositionvideoTrack.scaleTimeRange(CMTimeRangeMake(kCMTimeZero, vidAsset.duration), toDuration: CMTimeMake(finalTimeScale, vidAsset.duration.timescale))
// Setup effect size
let size = videoTrack.naturalSize
// Setup the image
let imglogo = UIImage(named: "gif1.png")
let imglayer = CALayer()
imglayer.contents = imglogo?.cgImage
imglayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
imglayer.opacity = 0.0
// Setup the video layer
let videolayer = CALayer()
// Setup the video layer frame
videolayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
// Setup the parent layer
let parentlayer = CALayer()
// Setup the parent layer frame
parentlayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
// Add video layer
parentlayer.addSublayer(videolayer)
// Add filter layer
parentlayer.addSublayer(imglayer)
// Setup the layer composition
let layercomposition = AVMutableVideoComposition()
// Setup the desired frame rate
layercomposition.frameDuration = CMTimeMake(1, Int32(toFPS))
// Setup the render size
layercomposition.renderSize = size
// Setup the animation tool
layercomposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videolayer, in: parentlayer)
// Setup instruction for filter overlay
let instruction = AVMutableVideoCompositionInstruction()
// Setup the desired time range
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration)
// Setup video track
let videotrack = composition.tracks(withMediaType: AVMediaType.video)[0]
// Setup layer instruction
let layerinstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videotrack)
// Setup layer instructions
instruction.layerInstructions = [layerinstruction]
// Setup layer composition instructions
layercomposition.instructions = [instruction]
// Instantiate the asset export
let assetExport = AVAssetExportSession(asset: composition, presetName:AVAssetExportPresetHighestQuality)
// Setup the video composition
assetExport?.videoComposition = layercomposition
// Setup the output file type
assetExport?.outputFileType = AVFileType.mov
// Setup the destination
assetExport?.outputURL = destination
// Export video
assetExport?.exportAsynchronously(completionHandler: {
switch assetExport?.status{
case .failed?:
print("failed \(assetExport!.error)")
case .cancelled?:
print("cancelled \(assetExport!.error)")
default:
print("Movie complete")
completionHandler(true)
}
})
}
很抱歉太长了,但是是否有任何突出的因素可以帮助解释导出过程中的方向变化?
谢谢!
我遇到了关于方向的问题,我就是这样解决的:
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack setPreferredTransform:CGAffineTransformRotate(CGAffineTransformMakeScale(-1, 1), M_PI)];
通过旋转和缩放它。它在 objective-C 中,但您可以轻松转换它。你只需要改变这个:
// Setup the preferred transform
compositionvideoTrack.preferredTransform = videoTrack.preferredTransform
而不是 preferredTransform 手动进行转换。
我一天的大部分时间都在研究 Whosebug,虽然有 很多 关于这个主题的好帖子,但我还没有找到解决方案我的问题。
我正在使用 AVAssetWriter
编写视频文件,没问题。我的视频文件,如果我保存到我的相机胶卷,可以正确播放并按预期方向播放。这是我的设置方式;
init(fileUrl:URL!, height:Int, width:Int) {
// Setup the filter writer instance
fileWriter = try? AVAssetWriter(outputURL: fileUrl, fileType: AVFileType.mov)
// Setup the video settings
let videoOutputSettings: Dictionary<String, AnyObject> = [
AVVideoCodecKey : AVVideoCodecType.hevc as AnyObject,
AVVideoWidthKey : width as AnyObject,
AVVideoHeightKey : height as AnyObject
]
// Setup the attributes dictionary
let sourcePixelBufferAttributesDictionary = [
String(kCVPixelBufferPixelFormatTypeKey) : Int(kCVPixelFormatType_32BGRA),
String(kCVPixelBufferWidthKey) : Int(width),
String(kCVPixelBufferHeightKey) : Int(height),
String(kCVPixelFormatOpenGLESCompatibility) : kCFBooleanTrue
] as [String : Any]
// Setup the video input
videoInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: videoOutputSettings)
// Data should be expected in real time
videoInput.expectsMediaDataInRealTime = true
// Perform transform
videoInput.transform = CGAffineTransform(rotationAngle: CGFloat(CGFloat.pi / 2.0))
// Setup pixel buffer intput
assetWriterPixelBufferInput = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: videoInput,
sourcePixelBufferAttributes: sourcePixelBufferAttributesDictionary)
// Add the input
fileWriter.add(videoInput)
}
然后我想使用 AVMutableComposition
保存应用了图像叠加层的视频,除了视频方向不正确外,它工作正常;
func postProcessVideo(toFPS: Double, sourceVideo: URL, destination: URL, filterImage: UIImage?, completionHandler: @escaping (_ response: Bool) -> ()) {
// Log
print("Received call to begin post-processing video at:", sourceVideo)
// Instantiate the AVMutableComposion
let composition = AVMutableComposition()
// Setup the video asset
let vidAsset = AVURLAsset(url: sourceVideo, options: [:])
// Get video track
let vtrack = vidAsset.tracks(withMediaType: AVMediaType.video)
// Setup the first video track as asset track
let videoTrack: AVAssetTrack = vtrack[0]
// Setup the video timerange
let vid_timerange = CMTimeRangeMake(kCMTimeZero, vidAsset.duration)
// Setup the composition video track
let compositionvideoTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: CMPersistentTrackID())!
// Insert expected time range
do {
try compositionvideoTrack.insertTimeRange(vid_timerange, of: videoTrack, at: kCMTimeZero)
} catch {
}
// Setup the preferred transform
compositionvideoTrack.preferredTransform = videoTrack.preferredTransform
// Update time scale
let finalTimeScale: Int64 = vidAsset.duration.value * 3
// Adjust video track duration
compositionvideoTrack.scaleTimeRange(CMTimeRangeMake(kCMTimeZero, vidAsset.duration), toDuration: CMTimeMake(finalTimeScale, vidAsset.duration.timescale))
// Setup effect size
let size = videoTrack.naturalSize
// Setup the image
let imglogo = UIImage(named: "gif1.png")
let imglayer = CALayer()
imglayer.contents = imglogo?.cgImage
imglayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
imglayer.opacity = 0.0
// Setup the video layer
let videolayer = CALayer()
// Setup the video layer frame
videolayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
// Setup the parent layer
let parentlayer = CALayer()
// Setup the parent layer frame
parentlayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
// Add video layer
parentlayer.addSublayer(videolayer)
// Add filter layer
parentlayer.addSublayer(imglayer)
// Setup the layer composition
let layercomposition = AVMutableVideoComposition()
// Setup the desired frame rate
layercomposition.frameDuration = CMTimeMake(1, Int32(toFPS))
// Setup the render size
layercomposition.renderSize = size
// Setup the animation tool
layercomposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videolayer, in: parentlayer)
// Setup instruction for filter overlay
let instruction = AVMutableVideoCompositionInstruction()
// Setup the desired time range
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration)
// Setup video track
let videotrack = composition.tracks(withMediaType: AVMediaType.video)[0]
// Setup layer instruction
let layerinstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videotrack)
// Setup layer instructions
instruction.layerInstructions = [layerinstruction]
// Setup layer composition instructions
layercomposition.instructions = [instruction]
// Instantiate the asset export
let assetExport = AVAssetExportSession(asset: composition, presetName:AVAssetExportPresetHighestQuality)
// Setup the video composition
assetExport?.videoComposition = layercomposition
// Setup the output file type
assetExport?.outputFileType = AVFileType.mov
// Setup the destination
assetExport?.outputURL = destination
// Export video
assetExport?.exportAsynchronously(completionHandler: {
switch assetExport?.status{
case .failed?:
print("failed \(assetExport!.error)")
case .cancelled?:
print("cancelled \(assetExport!.error)")
default:
print("Movie complete")
completionHandler(true)
}
})
}
很抱歉太长了,但是是否有任何突出的因素可以帮助解释导出过程中的方向变化?
谢谢!
我遇到了关于方向的问题,我就是这样解决的:
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack setPreferredTransform:CGAffineTransformRotate(CGAffineTransformMakeScale(-1, 1), M_PI)];
通过旋转和缩放它。它在 objective-C 中,但您可以轻松转换它。你只需要改变这个:
// Setup the preferred transform
compositionvideoTrack.preferredTransform = videoTrack.preferredTransform
而不是 preferredTransform 手动进行转换。