iOS在iPhone上逐帧修改视频(添加CC)

时间:2013-03-18 15:04:03

标签: iphone ios video frame

我有兴趣在iPhone上的现有视频中添加文字(采用隐藏式字幕样式)。 stackoverflow上的一些解决方案建议添加透明的UIView来覆盖视频。这一切都很好,但是,我无法将其保存为“新的和修改过的视频”。

我看到的唯一解决方案是获取文本,从视频中取出一个帧,将文本添加到帧中,然后将带有文本的修改后的帧推回到视频中,替换视频中的原始帧。

有没有人知道如何从视频中取出一帧(我想我可以找出添加文字),然后如何将帧推回到视频中?如果你有一些想法,或者知道一个教程,我会很感激..

4 个答案:

答案 0 :(得分:5)

您不需要逐帧进行此操作。 AVFoundation支持自iOS 4.0以来的字幕。

例如,您可以创建AVMutableComposition,然后在视频顶部添加字幕轨道。 AVMediaTypeSubtitle是字幕的类型(或隐藏字幕的AVMediaTypeClosedCaption)。然后,您可以将作文提供给播放器或AVAssetWriter。为您省去所有麻烦。

答案 1 :(得分:1)

根据你所说的,这是你可以学习的一个例子。

enter image description here

从上图中可以看到,您可以添加边框,叠加,字幕,视频中的任何内容。

http://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos

答案 2 :(得分:1)

您可以通过AvFoundation Framework编辑视频...

这是一个例子..

var assetTrack = AVAsset.assetWithURL(filePath1)as! AVURLAsset         var mutableComposition = AVMutableComposition()

    // Step - 1  pass url to  avasset

    // video

    var compositionVideoTrack = AVMutableCompositionTrack()
    compositionVideoTrack = mutableComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())


    var assetVideoTrack = assetTrack.tracksWithMediaType(AVMediaTypeVideo)[0] as! AVAssetTrack
    compositionVideoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero , assetTrack.duration), ofTrack: assetVideoTrack, atTime: kCMTimeZero, error: nil)

    // audio

    var compositionAudioTrack = AVMutableCompositionTrack()
    compositionAudioTrack = mutableComposition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())

    var assetAudioTrack = assetTrack.tracksWithMediaType(AVMediaTypeAudio)[0] as! AVAssetTrack
    compositionAudioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero , assetTrack.duration), ofTrack: assetAudioTrack, atTime: kCMTimeZero, error: nil)

    var videoAssetOrientation_: UIImageOrientation = .Up
    var isVideoAssetPortrait_: Bool = false

    var videoTransform: CGAffineTransform = assetTrack.preferredTransform



    var videosize = CGSize()
    videosize = assetVideoTrack.naturalSize

    var parentLayer = CALayer()
    var videoLayer = CALayer()
    var textLayer = CALayer()

    parentLayer.frame = CGRectMake(0, 0, videosize.width, videosize.height)
    videoLayer.frame = CGRectMake(0, 0, videosize.width, videosize.height)
    textLayer.frame = CGRectMake(0, 0, videosize.width, videosize.height)

    parentLayer.addSublayer(videoLayer)

    if drawingView.image != nil
    {
        var drawingLayer = CALayer()
        drawingLayer.frame = CGRectMake(0, 0, videosize.width, videosize.height)
        drawingLayer.contents = drawingView.image.CGImage

        var image = UIImage()
        parentLayer.addSublayer(drawingLayer)

    }


    var textV = UIView()
    textV.backgroundColor = UIColor.clearColor()
    textV.layer.backgroundColor = UIColor.clearColor().CGColor
    textV.frame = CGRectMake(self.captureView.frame.size.width, 0, self.captureView.frame.size.width, self.captureView.frame.size.height)

    var textL = UILabel()
    textL = textShowOnPreview
    textV.addSubview(textL)

    if textL != ""
    {
        UIGraphicsBeginImageContext(textV.bounds.size)
        textV.layer.renderInContext(UIGraphicsGetCurrentContext())
        var image1: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        if ( TextAnimation == "")
        {
            textLayer.contents = image1.CGImage
            parentLayer.addSublayer(textLayer)
        }

        else if  ( TextAnimation == "flip")
        {

            var overlayer1 = CALayer()
            overlayer1.backgroundColor = UIColor.clearColor().CGColor
            let screenSize: CGRect = UIScreen.mainScreen().bounds
            overlayer1.contents = image1.CGImage
            overlayer1.masksToBounds = true
            overlayer1.frame = CGRectMake(videosize.width/2-300, videosize.height/2 - 400, videosize.width,videosize.width);
            var   animation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
            animation.duration=5.0;
            animation.repeatCount=5;
            animation.autoreverses = true;
            // rotate from 0 to 360
            animation.fromValue = 0
            animation.toValue = (2.0 * M_PI);
            animation.beginTime = AVCoreAnimationBeginTimeAtZero;
            overlayer1.addAnimation(animation, forKey:"rotation")
            [parentLayer .addSublayer(overlayer1)]

        }
        else if ( TextAnimation == "fade")
        {

                       //opacity
            var overlayer1 = CALayer()
            overlayer1.backgroundColor = UIColor.clearColor().CGColor
            overlayer1.contents = image1.CGImage
            overlayer1.masksToBounds = true

            overlayer1.frame = CGRectMake(videosize.width/2 - 300,  videosize.height/2 - 100 , videosize.width+20, videosize.width);
            var   animation : CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
            animation.duration = 2.0;
            animation.repeatCount = 3;
            animation.autoreverses = true;
            // rotate from 0 to 360

            animation.fromValue = 0.5;
            animation.toValue = 1.0;
            animation.beginTime = AVCoreAnimationBeginTimeAtZero;

            overlayer1.addAnimation(animation, forKey:"scale")
            [parentLayer .addSublayer(overlayer1)]

        }
        else if ( TextAnimation == "bounce")
        {
            var overlayer1 = CALayer()

            var bounce : CABasicAnimation = CABasicAnimation (keyPath:"position.y");
            overlayer1.backgroundColor = UIColor.clearColor().CGColor
            overlayer1.contents = image1.CGImage
            overlayer1.masksToBounds = true
            overlayer1.frame = CGRectMake(videosize.width/2 - 300,  videosize.height/2 - 100 , videosize.width, videosize.width);
            bounce.duration = 1.0;
            bounce.fromValue =  overlayer1.frame.origin.y

            bounce.toValue =  overlayer1.frame.origin.y - 100
            bounce.repeatCount = 10
            bounce.autoreverses = true;
            overlayer1.addAnimation(bounce, forKey: "y")

            var animation = CABasicAnimation(keyPath: "transform.scale")
            animation.toValue = NSNumber(float: 0.9)
            animation.duration = 1.0
            animation.repeatCount = 10;
            animation.autoreverses = true
            overlayer1.addAnimation(animation, forKey: nil)
             [parentLayer .addSublayer(overlayer1)]

        }

    }

    var mutableVideoComposition = AVMutableVideoComposition()
    mutableVideoComposition.frameDuration = CMTimeMake(1, 30)
    mutableVideoComposition.renderSize = videosize

    mutableVideoComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, inLayer: parentLayer)


    var passThroughInstruction = AVMutableVideoCompositionInstruction()
    passThroughInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, mutableComposition.duration)


    var passThroughLayerInstruction = AVMutableVideoCompositionLayerInstruction()


    // video

    var assestVideoMutableCompositionVideo = mutableComposition.tracksWithMediaType(AVMediaTypeVideo)[0] as! AVAssetTrack
    passThroughLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: assestVideoMutableCompositionVideo)



    if isVideoAssetPortrait_ == false
    {

        var FirstAssetScaleFactor: CGAffineTransform = CGAffineTransformMakeScale(1, 1)

        passThroughLayerInstruction.setTransform(CGAffineTransformConcat(assetVideoTrack.preferredTransform, FirstAssetScaleFactor), atTime: kCMTimeZero)

    }


    passThroughInstruction.layerInstructions = [passThroughLayerInstruction]
    mutableVideoComposition.instructions = [passThroughInstruction]

    let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as! NSURL
    let filePath = documentsURL.URLByAppendingPathComponent("NewWatermarkedVideo.mov") as NSURL


    var fileManager:NSFileManager = NSFileManager.defaultManager()
    fileManager.removeItemAtURL(filePath, error: nil)

   var exporter: AVAssetExportSession = AVAssetExportSession(asset: mutableComposition, presetName: AVAssetExportPresetMediumQuality)
    exporter.videoComposition = mutableVideoComposition
    exporter.outputFileType = AVFileTypeQuickTimeMovie
    exporter.outputURL = filePath
    exporter.shouldOptimizeForNetworkUse = true
    self.captureView.addSubview(textShowOnPreview)

    exporter.exportAsynchronouslyWithCompletionHandler({() -> Void in

        println(exporter.status)

        if  exporter.status == AVAssetExportSessionStatus.Completed
        {


            dispatch_async(dispatch_get_main_queue(), { () -> Void in




                MBProgressHUD.hideAllHUDsForView(self.view, animated: true)

                self.topicSelectedImage.highlighted = false
                self.timelineSelectedImage.highlighted = false
                self.selectCat = ""
                self.postView.hidden = false

            })

            println("Completed")
            self.mediaData =  NSData(contentsOfURL:filePath, options: nil, error: nil)!


            var err: NSError? = nil
            var asset = AVURLAsset(URL: filePath, options: nil)
            var imgGenerator = AVAssetImageGenerator(asset: asset)
            var cgImage = imgGenerator.copyCGImageAtTime(CMTimeMake(0, 30), actualTime: nil, error: &err)
            var uiImage = UIImage(CGImage: cgImage)!
            self.videoThumbData = UIImageJPEGRepresentation(uiImage, 0.1)

            var assetTrack = AVAsset.assetWithURL(filePath) as! AVURLAsset
            self.videoTime = Int(CMTimeGetSeconds(assetTrack.duration)) + 3

            println(self.videoTime)


        }
        else if exporter.status == AVAssetExportSessionStatus.Cancelled
        {


        }
        else if exporter.status == AVAssetExportSessionStatus.Failed
        {


        }
    })

答案 3 :(得分:0)

对于那些想要逐帧编辑电影的人,请使用AVReaderWriter。虽然它是OS X Apple示例代码,但AVFoundation可在两个平台上使用,几乎没有变化。