栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用AVFoundation录制方形视频并添加水印

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用AVFoundation录制方形视频并添加水印

一些东西:

就音频而言,您要添加视频(摄像机)输入,但不添加音频输入。这样做以获得声音。

    let audioInputDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)    do {        let input = try AVCaptureDeviceInput(device: audioInputDevice)        if sourceAVFoundation.captureSession.canAddInput(input) { sourceAVFoundation.captureSession.addInput(input)        } else { NSLog("ERROR: Can't add audio input")        }    } catch let error {        NSLog("ERROR: Getting input device: (error)")    }

为了使视频更加方形,您将不得不使用AVAssetWriter而不是AVCaptureFileOutput。这更加复杂,但是您获得了更多的“力量”。您已经创建了一个AVCaptureSession,它非常棒,要连接AssetWriter,您需要执行以下操作:

    let fileManager = NSFileManager.defaultManager()    let urls = fileManager.URLsForDirectory(.documentDirectory, inDomains: .UserDomainMask)    guard let documentDirectory: NSURL = urls.first else {        print("Video Controller: getAssetWriter: documentDir Error")        return nil    }    let local_video_name = NSUUID().UUIDString + ".mp4"    self.videoOutputURL = documentDirectory.URLByAppendingPathComponent(local_video_name)    guard let url = self.videoOutputURL else {        return nil    }    self.assetWriter = try? AVAssetWriter(URL: url, fileType: AVFileTypeMPEG4)    guard let writer = self.assetWriter else {        return nil    }    //TODO: Set your desired video size here!     let videoSettings: [String : AnyObject] = [        AVVideoCodecKey  : AVVideoCodecH264,        AVVideoWidthKey  : captureSize.width,        AVVideoHeightKey : captureSize.height,        AVVideoCompressionPropertiesKey : [ AVVideoAverageBitRateKey : 200000, AVVideoProfileLevelKey : AVVideoProfileLevelH264baseline41, AVVideoMaxKeyframeIntervalKey : 90,        ],    ]    assetWriterInputCamera = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoSettings)    assetWriterInputCamera?.expectsMediaDataInRealTime = true    writer.addInput(assetWriterInputCamera!)    let audioSettings : [String : AnyObject] = [        AVFormatIDKey : NSInteger(kAudioFormatMPEG4AAC),        AVNumberOfChannelsKey : 2,        AVSampleRateKey : NSNumber(double: 44100.0)    ]    assetWriterInputAudio = AVAssetWriterInput(mediaType: AVMediaTypeAudio, outputSettings: audioSettings)    assetWriterInputAudio?.expectsMediaDataInRealTime = true    writer.addInput(assetWriterInputAudio!)

设置好AssetWriter之后,然后为视频和音频连接一些输出

    let bufferAudioQueue = dispatch_queue_create("audio buffer delegate", DISPATCH_QUEUE_SERIAL)    let audioOutput = AVCaptureAudioDataOutput()    audioOutput.setSampleBufferDelegate(self, queue: bufferAudioQueue)    captureSession.addOutput(audioOutput)    // Always add video last...    let videoOutput = AVCaptureVideoDataOutput()    videoOutput.setSampleBufferDelegate(self, queue: bufferVideoQueue)    captureSession.addOutput(videoOutput)    if let connection = videoOutput.connectionWithMediaType(AVMediaTypeVideo) {        if connection.supportsVideoOrientation { // Force recording to portrait connection.videoOrientation = AVCaptureVideoOrientation.Portrait        }        self.outputConnection = connection    }    captureSession.startRunning()

最后,您需要捕获缓冲区并处理这些东西…确保您使类成为AVCaptureVideoDataOutputSampleBufferDelegate和AVCaptureAudioDataOutputSampleBufferDelegate的委托

//MARK: Implementation for AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAudioDataOutputSampleBufferDelegatefunc captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {    if !self.isRecordingStarted {        return    }    if let audio = self.assetWriterInputAudio where connection.audioChannels.count > 0 && audio.readyForMoreMediaData {        dispatch_async(audioQueue!) { audio.appendSampleBuffer(sampleBuffer)        }        return    }    if let camera = self.assetWriterInputCamera where camera.readyForMoreMediaData {        dispatch_async(videoQueue!) { camera.appendSampleBuffer(sampleBuffer)        }    }}

缺少一些零碎的部分,但希望这足以让您连同文档一起弄清楚。

最后,如果要添加水印,可以通过多种方式实时完成,但一种可能的方式是修改sampleBuffer并将水印写入图像。您会在StackOverflow上找到其他与此相关的问题。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/401534.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号