Using Custom Video Compositor Class rotates the video

I’m building a subtitle generator. I have a subtitle layer which has all the implementations for the subtitles such as animations, styling, etc.

When I’m exporting the video, the video is rotated to 90 degrees to left. I’ve tried applying some transforms. It does work when I swap width and height on the render size and apply preferredTransform.

layerInstruction.setTransform(videoTrack.preferredTransform, at: .zero)

The composition code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> let subtitleLayer = VideoSubtitleLayer()
subtitleLayer.frame = CGRect(x: 0, y: 0, width: videoTrack.naturalSize.width, height: videoTrack.naturalSize.height)
let videoComposition = AVMutableVideoComposition()
videoComposition.frameDuration = CMTime(value: 1, timescale: 30)
videoComposition.renderSize = CGSize(width: videoTrack.naturalSize.height, height: videoTrack.naturalSize.width) // swap width and height
let defaultPosition = CGPoint(x: videoTrack.naturalSize.width * 0.5, y: videoTrack.naturalSize.height * 0.9)
let instruction = VideoSubtitleInstruction(
timeRange: CMTimeRange(start: .zero, duration: videoAsset.duration),
subtitleLayer: subtitleLayer,
savedSubtitlePosition: savedSubtitlePosition ?? defaultPosition
)
let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: compositionVideoTrack)
layerInstruction.setTransform(videoTrack.preferredTransform, at: .zero)
instruction.layerInstructions = [layerInstruction]
videoComposition.instructions = [instruction]
videoComposition.customVideoCompositorClass = SubtitleCompositor.self
</code>
<code> let subtitleLayer = VideoSubtitleLayer() subtitleLayer.frame = CGRect(x: 0, y: 0, width: videoTrack.naturalSize.width, height: videoTrack.naturalSize.height) let videoComposition = AVMutableVideoComposition() videoComposition.frameDuration = CMTime(value: 1, timescale: 30) videoComposition.renderSize = CGSize(width: videoTrack.naturalSize.height, height: videoTrack.naturalSize.width) // swap width and height let defaultPosition = CGPoint(x: videoTrack.naturalSize.width * 0.5, y: videoTrack.naturalSize.height * 0.9) let instruction = VideoSubtitleInstruction( timeRange: CMTimeRange(start: .zero, duration: videoAsset.duration), subtitleLayer: subtitleLayer, savedSubtitlePosition: savedSubtitlePosition ?? defaultPosition ) let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: compositionVideoTrack) layerInstruction.setTransform(videoTrack.preferredTransform, at: .zero) instruction.layerInstructions = [layerInstruction] videoComposition.instructions = [instruction] videoComposition.customVideoCompositorClass = SubtitleCompositor.self </code>
     let subtitleLayer = VideoSubtitleLayer()
        subtitleLayer.frame = CGRect(x: 0, y: 0, width: videoTrack.naturalSize.width, height: videoTrack.naturalSize.height)

        let videoComposition = AVMutableVideoComposition()
        videoComposition.frameDuration = CMTime(value: 1, timescale: 30)
        videoComposition.renderSize = CGSize(width: videoTrack.naturalSize.height, height: videoTrack.naturalSize.width) // swap width and height

        let defaultPosition = CGPoint(x: videoTrack.naturalSize.width * 0.5, y: videoTrack.naturalSize.height * 0.9)

        let instruction = VideoSubtitleInstruction(
            timeRange: CMTimeRange(start: .zero, duration: videoAsset.duration),
            subtitleLayer: subtitleLayer,
            savedSubtitlePosition: savedSubtitlePosition ?? defaultPosition
        )

        let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: compositionVideoTrack)

        layerInstruction.setTransform(videoTrack.preferredTransform, at: .zero)

        instruction.layerInstructions = [layerInstruction]
        videoComposition.instructions = [instruction]
        videoComposition.customVideoCompositorClass = SubtitleCompositor.self

This is working fine if I don’t use my custom compositor class. If I start using it, the video starts to rotate 90 degrees to left again.
videoComposition.customVideoCompositorClass = SubtitleCompositor.self

The subtitle compositor implementation looks like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import AVFoundation
import CoreImage
class SubtitleCompositor: NSObject, AVVideoCompositing {
let subtitles: [Subtitle]
override init() {
self.subtitles = SubtitleStore.shared.subtitles
super.init()
}
var requiredPixelBufferAttributesForRenderContext: [String : Any] = [
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32ARGB
]
var sourcePixelBufferAttributes: [String : Any]? = [
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32ARGB
]
private var renderContext: AVVideoCompositionRenderContext?
func renderContextChanged(_ newRenderContext: AVVideoCompositionRenderContext) {
renderContext = newRenderContext
}
func startRequest(_ request: AVAsynchronousVideoCompositionRequest) {
guard let renderContext = renderContext else {
print("Render context is nil.")
request.finish(with: NSError(domain: "SubtitleCompositor", code: -1, userInfo: nil))
return
}
guard let sourcePixelBuffer = request.sourceFrame(byTrackID: request.sourceTrackIDs[0].int32Value) else {
print("Source pixel buffer is nil.")
request.finish(with: NSError(domain: "SubtitleCompositor", code: -2, userInfo: nil))
return
}
guard let destinationPixelBuffer = renderContext.newPixelBuffer() else {
print("Failed to create destination pixel buffer.")
request.finish(with: NSError(domain: "SubtitleCompositor", code: -3, userInfo: nil))
return
}
// Copy the source pixel buffer to the destination pixel buffer
copyPixelBuffer(sourcePixelBuffer, to: destinationPixelBuffer)
guard let videoInstruction = request.videoCompositionInstruction as? VideoSubtitleInstruction,
let subtitleLayer = videoInstruction.subtitleLayer else {
print("Video instruction or subtitle layer is nil.")
request.finish(withComposedVideoFrame: destinationPixelBuffer)
return
}
DispatchQueue.main.sync {
let currentTime = request.compositionTime.seconds
if let subtitle = subtitles.first(where: { $0.startTime <= currentTime && $0.endTime > currentTime }) {
subtitleLayer.updateSubtitle(subtitle: subtitle, time: CFTimeInterval(currentTime))
} else {
subtitleLayer.updateSubtitle(subtitle: nil, time: CFTimeInterval(currentTime))
}
if let position = videoInstruction.savedSubtitlePosition {
subtitleLayer.setInitialPosition(position)
}
renderLayer(subtitleLayer, to: destinationPixelBuffer)
}
request.finish(withComposedVideoFrame: destinationPixelBuffer)
}
private func copyPixelBuffer(_ sourcePixelBuffer: CVPixelBuffer, to destinationPixelBuffer: CVPixelBuffer) {
CVPixelBufferLockBaseAddress(sourcePixelBuffer, .readOnly)
CVPixelBufferLockBaseAddress(destinationPixelBuffer, [])
guard let sourceBaseAddress = CVPixelBufferGetBaseAddress(sourcePixelBuffer),
let destinationBaseAddress = CVPixelBufferGetBaseAddress(destinationPixelBuffer) else {
CVPixelBufferUnlockBaseAddress(sourcePixelBuffer, .readOnly)
CVPixelBufferUnlockBaseAddress(destinationPixelBuffer, [])
return
}
let sourceBytesPerRow = CVPixelBufferGetBytesPerRow(sourcePixelBuffer)
let destinationBytesPerRow = CVPixelBufferGetBytesPerRow(destinationPixelBuffer)
let height = CVPixelBufferGetHeight(sourcePixelBuffer)
for row in 0..<height {
memcpy(destinationBaseAddress + row * destinationBytesPerRow, sourceBaseAddress + row * sourceBytesPerRow, min(sourceBytesPerRow, destinationBytesPerRow))
}
CVPixelBufferUnlockBaseAddress(sourcePixelBuffer, .readOnly)
CVPixelBufferUnlockBaseAddress(destinationPixelBuffer, [])
}
private func renderLayer(_ layer: CALayer, to pixelBuffer: CVPixelBuffer) {
CVPixelBufferLockBaseAddress(pixelBuffer, [])
guard let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer),
width: CVPixelBufferGetWidth(pixelBuffer),
height: CVPixelBufferGetHeight(pixelBuffer),
bitsPerComponent: 8,
bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer),
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue) else {
CVPixelBufferUnlockBaseAddress(pixelBuffer, [])
return
}
// Flip the context vertically
context.translateBy(x: 0, y: CGFloat(CVPixelBufferGetHeight(pixelBuffer)))
context.scaleBy(x: 1.0, y: -1.0)
// Render the layer to the context
if layer.bounds.isEmpty || layer.sublayers?.isEmpty == true {
print("Subtitle layer is empty or has no sublayers.")
} else {
layer.render(in: context)
}
CVPixelBufferUnlockBaseAddress(pixelBuffer, [])
}
}
</code>
<code>import AVFoundation import CoreImage class SubtitleCompositor: NSObject, AVVideoCompositing { let subtitles: [Subtitle] override init() { self.subtitles = SubtitleStore.shared.subtitles super.init() } var requiredPixelBufferAttributesForRenderContext: [String : Any] = [ kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32ARGB ] var sourcePixelBufferAttributes: [String : Any]? = [ kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32ARGB ] private var renderContext: AVVideoCompositionRenderContext? func renderContextChanged(_ newRenderContext: AVVideoCompositionRenderContext) { renderContext = newRenderContext } func startRequest(_ request: AVAsynchronousVideoCompositionRequest) { guard let renderContext = renderContext else { print("Render context is nil.") request.finish(with: NSError(domain: "SubtitleCompositor", code: -1, userInfo: nil)) return } guard let sourcePixelBuffer = request.sourceFrame(byTrackID: request.sourceTrackIDs[0].int32Value) else { print("Source pixel buffer is nil.") request.finish(with: NSError(domain: "SubtitleCompositor", code: -2, userInfo: nil)) return } guard let destinationPixelBuffer = renderContext.newPixelBuffer() else { print("Failed to create destination pixel buffer.") request.finish(with: NSError(domain: "SubtitleCompositor", code: -3, userInfo: nil)) return } // Copy the source pixel buffer to the destination pixel buffer copyPixelBuffer(sourcePixelBuffer, to: destinationPixelBuffer) guard let videoInstruction = request.videoCompositionInstruction as? VideoSubtitleInstruction, let subtitleLayer = videoInstruction.subtitleLayer else { print("Video instruction or subtitle layer is nil.") request.finish(withComposedVideoFrame: destinationPixelBuffer) return } DispatchQueue.main.sync { let currentTime = request.compositionTime.seconds if let subtitle = subtitles.first(where: { $0.startTime <= currentTime && $0.endTime > currentTime }) { subtitleLayer.updateSubtitle(subtitle: subtitle, time: CFTimeInterval(currentTime)) } else { subtitleLayer.updateSubtitle(subtitle: nil, time: CFTimeInterval(currentTime)) } if let position = videoInstruction.savedSubtitlePosition { subtitleLayer.setInitialPosition(position) } renderLayer(subtitleLayer, to: destinationPixelBuffer) } request.finish(withComposedVideoFrame: destinationPixelBuffer) } private func copyPixelBuffer(_ sourcePixelBuffer: CVPixelBuffer, to destinationPixelBuffer: CVPixelBuffer) { CVPixelBufferLockBaseAddress(sourcePixelBuffer, .readOnly) CVPixelBufferLockBaseAddress(destinationPixelBuffer, []) guard let sourceBaseAddress = CVPixelBufferGetBaseAddress(sourcePixelBuffer), let destinationBaseAddress = CVPixelBufferGetBaseAddress(destinationPixelBuffer) else { CVPixelBufferUnlockBaseAddress(sourcePixelBuffer, .readOnly) CVPixelBufferUnlockBaseAddress(destinationPixelBuffer, []) return } let sourceBytesPerRow = CVPixelBufferGetBytesPerRow(sourcePixelBuffer) let destinationBytesPerRow = CVPixelBufferGetBytesPerRow(destinationPixelBuffer) let height = CVPixelBufferGetHeight(sourcePixelBuffer) for row in 0..<height { memcpy(destinationBaseAddress + row * destinationBytesPerRow, sourceBaseAddress + row * sourceBytesPerRow, min(sourceBytesPerRow, destinationBytesPerRow)) } CVPixelBufferUnlockBaseAddress(sourcePixelBuffer, .readOnly) CVPixelBufferUnlockBaseAddress(destinationPixelBuffer, []) } private func renderLayer(_ layer: CALayer, to pixelBuffer: CVPixelBuffer) { CVPixelBufferLockBaseAddress(pixelBuffer, []) guard let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer), width: CVPixelBufferGetWidth(pixelBuffer), height: CVPixelBufferGetHeight(pixelBuffer), bitsPerComponent: 8, bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer), space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue) else { CVPixelBufferUnlockBaseAddress(pixelBuffer, []) return } // Flip the context vertically context.translateBy(x: 0, y: CGFloat(CVPixelBufferGetHeight(pixelBuffer))) context.scaleBy(x: 1.0, y: -1.0) // Render the layer to the context if layer.bounds.isEmpty || layer.sublayers?.isEmpty == true { print("Subtitle layer is empty or has no sublayers.") } else { layer.render(in: context) } CVPixelBufferUnlockBaseAddress(pixelBuffer, []) } } </code>
import AVFoundation
import CoreImage

class SubtitleCompositor: NSObject, AVVideoCompositing {
    let subtitles: [Subtitle]

    override init() {
        self.subtitles = SubtitleStore.shared.subtitles
        super.init()
    }

    var requiredPixelBufferAttributesForRenderContext: [String : Any] = [
        kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32ARGB
    ]

    var sourcePixelBufferAttributes: [String : Any]? = [
        kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32ARGB
    ]

    private var renderContext: AVVideoCompositionRenderContext?

    func renderContextChanged(_ newRenderContext: AVVideoCompositionRenderContext) {
        renderContext = newRenderContext
    }

    func startRequest(_ request: AVAsynchronousVideoCompositionRequest) {
        guard let renderContext = renderContext else {
            print("Render context is nil.")
            request.finish(with: NSError(domain: "SubtitleCompositor", code: -1, userInfo: nil))
            return
        }

        guard let sourcePixelBuffer = request.sourceFrame(byTrackID: request.sourceTrackIDs[0].int32Value) else {
            print("Source pixel buffer is nil.")
            request.finish(with: NSError(domain: "SubtitleCompositor", code: -2, userInfo: nil))
            return
        }

        guard let destinationPixelBuffer = renderContext.newPixelBuffer() else {
            print("Failed to create destination pixel buffer.")
            request.finish(with: NSError(domain: "SubtitleCompositor", code: -3, userInfo: nil))
            return
        }

        // Copy the source pixel buffer to the destination pixel buffer
        copyPixelBuffer(sourcePixelBuffer, to: destinationPixelBuffer)

        guard let videoInstruction = request.videoCompositionInstruction as? VideoSubtitleInstruction,
              let subtitleLayer = videoInstruction.subtitleLayer else {
            print("Video instruction or subtitle layer is nil.")
            request.finish(withComposedVideoFrame: destinationPixelBuffer)
            return
        }

        DispatchQueue.main.sync {
            let currentTime = request.compositionTime.seconds

            if let subtitle = subtitles.first(where: { $0.startTime <= currentTime && $0.endTime > currentTime }) {
                subtitleLayer.updateSubtitle(subtitle: subtitle, time: CFTimeInterval(currentTime))
            } else {
                subtitleLayer.updateSubtitle(subtitle: nil, time: CFTimeInterval(currentTime))
            }

            if let position = videoInstruction.savedSubtitlePosition {
                subtitleLayer.setInitialPosition(position)
            }

            renderLayer(subtitleLayer, to: destinationPixelBuffer)
        }

        request.finish(withComposedVideoFrame: destinationPixelBuffer)
    }


    private func copyPixelBuffer(_ sourcePixelBuffer: CVPixelBuffer, to destinationPixelBuffer: CVPixelBuffer) {
        CVPixelBufferLockBaseAddress(sourcePixelBuffer, .readOnly)
        CVPixelBufferLockBaseAddress(destinationPixelBuffer, [])

        guard let sourceBaseAddress = CVPixelBufferGetBaseAddress(sourcePixelBuffer),
              let destinationBaseAddress = CVPixelBufferGetBaseAddress(destinationPixelBuffer) else {
            CVPixelBufferUnlockBaseAddress(sourcePixelBuffer, .readOnly)
            CVPixelBufferUnlockBaseAddress(destinationPixelBuffer, [])
            return
        }

        let sourceBytesPerRow = CVPixelBufferGetBytesPerRow(sourcePixelBuffer)
        let destinationBytesPerRow = CVPixelBufferGetBytesPerRow(destinationPixelBuffer)
        let height = CVPixelBufferGetHeight(sourcePixelBuffer)

        for row in 0..<height {
            memcpy(destinationBaseAddress + row * destinationBytesPerRow, sourceBaseAddress + row * sourceBytesPerRow, min(sourceBytesPerRow, destinationBytesPerRow))
        }

        CVPixelBufferUnlockBaseAddress(sourcePixelBuffer, .readOnly)
        CVPixelBufferUnlockBaseAddress(destinationPixelBuffer, [])
    }

    private func renderLayer(_ layer: CALayer, to pixelBuffer: CVPixelBuffer) {
        CVPixelBufferLockBaseAddress(pixelBuffer, [])

        guard let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer),
                                      width: CVPixelBufferGetWidth(pixelBuffer),
                                      height: CVPixelBufferGetHeight(pixelBuffer),
                                      bitsPerComponent: 8,
                                      bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer),
                                      space: CGColorSpaceCreateDeviceRGB(),
                                      bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue) else {
            CVPixelBufferUnlockBaseAddress(pixelBuffer, [])
            return
        }


        // Flip the context vertically
        context.translateBy(x: 0, y: CGFloat(CVPixelBufferGetHeight(pixelBuffer)))
        context.scaleBy(x: 1.0, y: -1.0)


        // Render the layer to the context
        if layer.bounds.isEmpty || layer.sublayers?.isEmpty == true {
            print("Subtitle layer is empty or has no sublayers.")
        } else {
            layer.render(in: context)
        }

        CVPixelBufferUnlockBaseAddress(pixelBuffer, [])
    }
}

I tried to rotate the sourcePixelBuffer to 90 degrees, It works fine but I don’t think it is proper way to solve this problem. It also breaks the subtitle layer.

When using a custom compositor, you are responsible for applying the video track’s preferred transform to the source frame that you receive in the request.

The size of the output frame returned from renderContext.newPixelBuffer() will match whatever you have set in AVVideoComposition.renderSize.

I suggest looking into using a CoreImage pipeline to apply your transform (and possibly subtitles) if you’re not using a custom graphics pipeline like OpenGL or Metal.

https://developer.apple.com/documentation/coreimage/ciimage/1438203-imagebyapplyingtransform?language=objc

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật