【问题标题】:iOS AVMutableComposition Add text overlayiOS AVMutableComposition 添加文字叠加
【发布时间】:2016-09-15 15:39:57
【问题描述】:

有人可以请教。 我正在尝试将文本覆盖(标题)添加到我正在使用 AVFoundation 编写的视频中。我找到了一些在线资源(见http://stackoverflow.com/questions/21684549/add-a-text-overlay-with-avmutablevideocomposition-to-a-specific-timerange) 然而,所有这些资源都在 Objective-C 中。 我的项目在 Swift 中,我在 Swift 中找不到任何相关资源。 我无法正确覆盖文本似乎扭曲了,好像渲染的框架歪斜了...... 见图片Distorted text in AVPlayer 我试图将我找到的 Objective-C 代码转换为 Swift,但显然我遗漏了一些东西。 下面是我正在使用的代码。 (我为播放器和视频文件使用了一些代码,来自:www.raywenderlich.com/90488/calayer-in-ios-with-swift-10-examples

    func MergeUnWeldedVideoByUserPref(showInBounds: CGRect) -> (AVMutableComposition, AVMutableVideoComposition)
{
    let fps: Int32 = 30

    // 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
    let mixComposition = AVMutableComposition()
    // 2 - Create a video track for each of the video assests. Add your media data to the appropriate tracks
    //let url = NSBundle.mainBundle().URLForResource("colorfulStreak", withExtension: "m4v")!
    let url = NSBundle.mainBundle().URLForResource("colorfulStreak", withExtension: "m4v")!
    let avAsset = AVAsset(URL: url)
    let track = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
    let segmentInMovie = CMTimeRangeMake(kCMTimeZero, avAsset.duration)
    let videoTrack = avAsset.tracksWithMediaType(AVMediaTypeVideo)[0]
    do
    {
        try track.insertTimeRange(segmentInMovie, ofTrack: videoTrack, atTime: kCMTimeZero)

    } catch{
        print("Failed to load track")
    }

    let mainInstruction = AVMutableVideoCompositionInstruction()
    mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, avAsset.duration)
    let instruction = videoCompositionInstructionForTrack(showInBounds, track: track, asset: avAsset)
    mainInstruction.layerInstructions.append(instruction)

    let mainComposition = AVMutableVideoComposition()
    mainComposition.instructions = [mainInstruction]
    mainComposition.frameDuration = CMTimeMake(1, fps)
    mainComposition.renderSize = CGSize(width: showInBounds.width, height: showInBounds.height)

    let textLayer = CATextLayer()
    textLayer.backgroundColor = UIColor.clearColor().CGColor
    textLayer.foregroundColor = UIColor.whiteColor().CGColor
    textLayer.string = "T E S T"
    textLayer.font = UIFont(name: "Arial", size: 18)
    textLayer.shadowOpacity = 0.5
    textLayer.alignmentMode = kCAAlignmentCenter
    textLayer.frame = CGRectMake(5, 5, 100, 50)
    textLayer.shouldRasterize = true
    textLayer.rasterizationScale = showInBounds.width / videoTrack.naturalSize.width

    let parentLayer = CALayer()
    let videoLayer = CALayer()
    parentLayer.frame = CGRectMake(0, 0, showInBounds.width, showInBounds.height);
    videoLayer.frame = CGRectMake(0, 0, showInBounds.width, showInBounds.height);
    parentLayer.addSublayer(videoLayer)
    parentLayer.addSublayer(textLayer)

    mainComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, inLayer: parentLayer)
    return (mixComposition, mainComposition)
}

【问题讨论】:

  • showInBounds的宽高是多少?
  • 如果您在模拟器中尝试,请不要在模拟器中进行。使用设备以获得更好的结果。

标签: swift avfoundation avmutablecomposition


【解决方案1】:

您的 Swift 解释没有问题,而是模拟器的渲染引擎有问题。我在模拟器上尝试了你的代码,它确实看起来歪斜和扭曲,但是在编译到设备时它工作得很好。

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 2019-08-29
    相关资源
    最近更新 更多