2016-09-15 3 views
1

누군가가 조언 해주십시오. 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 코드를 변환하려고 시도했지만 분명히 나는 뭔가를 놓치고있다. 아래 코드는 제가 사용하고있는 코드입니다. 는 (필자는 플레이어에서 비디오 파일에 대한 몇 가지 코드 사용 : www.raywenderlich.com/90488/calayer-in-ios-with-swift-10-examples~~V을 아무 잘못이 스위프트와도 없다iOS AVMutableComposition 텍스트 오버레이를 추가하십시오.

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) 
} 
+0

'showInBounds'의 너비와 높이는 무엇입니까? ? –

+1

시뮬레이터에서 시도하는 경우 시뮬레이터에서 수행하지 마십시오. 더 나은 결과를 얻으려면 장치를 사용하십시오. –

답변

0

해석과 오히려 시뮬레이터의 렌더링 엔진에 문제가 있습니다. 시뮬레이터에서 코드를 시도하고 실제로 왜곡되고 왜곡 된 보였지만 장치에 컴파일 할 때 그것은 아름답게 작동합니다.

관련 문제