• 需求

    公司混合开发,uni端拍小视频不是很理想,为达到仿微信效果,原生插件走起

  • 思路

    第1步:1个AVCaptureSession, 1块AVCaptureVideoPreviewLayer[考虑兼容替换成AVPreView]

    第2步:视频录制需video & audio, 需要对应的AVCaptureDeviceInput,同理对应的AVCaptureVideoDataOutput与AVCaptureAudioDataOutput

    第3步:代理中设置output区分video与audio, 并将对应的CMSampleBufferRef写入到视频文件中

    第4步:写入视频文件中,用到AVAssetWriter, 对应video & audio 需两个AVAssetWriterInput, 加入AVAssetWriter

    第5步:CMSampleBufferRef不断过来,AssetWriter不断写入,直到停止

  • 上菜

    第一步的初始化就不写了,没事可以翻看本人前面的博客

    第2步:两个AVCaptureDeviceInput 两个Output, 且设置Output的代理

    self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];
    if (error) {
    NSLog(@"取得设备摄入videoInput对象时出错, 错误原因: %@", error);
    return;
    } // 设备添加到会话中
    if ([self.session canAddInput:self.videoInput]) {
    [self.session addInput:self.videoInput];
    } [self.videoOutput setSampleBufferDelegate:self queue:self.videoQueue];
    if ([self.session canAddOutput:self.videoOutput]) {
    [self.session addOutput:self.videoOutput];
    } // 音频相关
    AVCaptureDevice *adevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    self.audioInput = [[AVCaptureDeviceInput alloc] initWithDevice:adevice error:&error]; if ([self.session canAddInput:self.audioInput]) {
    [self.session addInput:self.audioInput];
    } [self.audioOutput setSampleBufferDelegate:self queue:self.videoQueue];
    if ([self.session canAddOutput:self.audioOutput]) {
    [self.session addOutput:self.audioOutput];
    } // 视频输出
    - (AVCaptureVideoDataOutput *)videoOutput {
    if (!_videoOutput) {
    _videoOutput = [[AVCaptureVideoDataOutput alloc] init];
    _videoOutput.alwaysDiscardsLateVideoFrames = YES;
    }
    return _videoOutput;
    } // 音频输出
    - (AVCaptureAudioDataOutput *)audioOutput {
    if (!_audioOutput) {
    _audioOutput = [[AVCaptureAudioDataOutput alloc] init];
    }
    return _audioOutput;
    }

    第3步:启动Session,代理里面操作CMSampleBufferRef

    #pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate & AVCaptureAudioDataOutputSampleBufferDelegate
    - (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
    @autoreleasepool {
    // 视频
    if (connection == [self.videoOutput connectionWithMediaType:AVMediaTypeVideo]) {
    if (!self.manager.outputVideoFormatDescription) {
    @synchronized(self) {
    CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);
    self.manager.outputVideoFormatDescription = formatDescription;
    }
    } else {
    @synchronized(self) {
    if (self.manager.state == StateRecording) {
    [self.manager appendBuffer:sampleBuffer type:AVMediaTypeVideo];
    }
    }
    }
    } //音频
    if (connection == [self.audioOutput connectionWithMediaType:AVMediaTypeAudio]) {
    if (!self.manager.outputAudioFormatDescription) {
    @synchronized(self) {
    CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);
    self.manager.outputAudioFormatDescription = formatDescription;
    }
    }
    @synchronized(self) {
    if (self.manager.state == StateRecording) {
    [self.manager appendBuffer:sampleBuffer type:AVMediaTypeAudio];
    }
    }
    }
    }
    }

    第4步:AVAssetWriter以及对应的Input

    // writer初始化
    self.writer = [AVAssetWriter assetWriterWithURL:_videoUrl fileType:AVFileTypeMPEG4 error:nil]; _videoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:_videoSettings];
    //expectsMediaDataInRealTime 必须设为yes,需要从capture session 实时获取数据
    _videoInput.expectsMediaDataInRealTime = YES; _audioInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:_audioSettings];
    _audioInput.expectsMediaDataInRealTime = YES; if ([_writer canAddInput:_videoInput]) {
    [_writer addInput:_videoInput];
    }
    if ([_writer canAddInput:_audioInput]) {
    [_writer addInput:_audioInput];
    }

    第5步:第3步的CMSampleBufferRef通过AVAssetWriter写入到视频文件中

    - (void)appendBuffer:(CMSampleBufferRef)buffer type:(NSString *)mediaType {
    if (buffer == NULL) {
    NSLog(@"empty sampleBuffer");
    return;
    } @synchronized (self) {
    if (self.state < StateRecording) {
    NSLog(@"not ready yet");
    return;
    }
    } CFRetain(buffer);
    dispatch_async(self.queue, ^{
    @autoreleasepool {
    @synchronized (self) {
    if (self.state > StateFinish) {
    CFRelease(buffer);
    return;
    }
    } if (!self.canWrite && mediaType == AVMediaTypeVideo) {
    [self.writer startWriting];
    [self.writer startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(buffer)];
    self.canWrite = YES;
    } if(!self.timer) {
    dispatch_async(dispatch_get_main_queue(), ^{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:TIMER_INTERVAL target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    });
    } // 写入视频数据
    if (mediaType == AVMediaTypeVideo) {
    if (self.videoInput.readyForMoreMediaData) {
    BOOL success = [self.videoInput appendSampleBuffer:buffer];
    if (!success) {
    @synchronized (self) {
    [self stop:^{}];
    [self destroy];
    }
    }
    }
    } // 写入音频数据
    if (mediaType == AVMediaTypeAudio) {
    if (self.audioInput.readyForMoreMediaData) {
    BOOL success = [self.audioInput appendSampleBuffer:buffer];
    if (!success) {
    @synchronized (self) {
    [self stop:^{}];
    [self destroy];
    }
    }
    }
    }
    CFRelease(buffer);
    }
    });
    }
  • 写在末尾:

    1. AVAssetWriterInput设置视频属性时,按照自己的需要设计,其中码率与帧率的设置会影响到拍摄后视频的质量与大小,具体看各自项目的要求

    2. 如果视频视角存在问题,可以从三个方向入手调整

      1.layer的connect设置下videoOrientation

      2.AVCaptureOutput的connect设置下videoOrientation

      3.AVAssetWriterInput针对video是设置下transform,比如Rotation M_PI/2 角度

iOS拍个小视频的更多相关文章

  1. IOS版微信小视频导出方法

    1.在电脑上连接手机,打开iTools 选择 应用-应用-文件共享. 2.依次打开/Library/WechatPrivate/6e2809aac61608de6a6cc55d9570d25b/Sig ...

  2. [iOS]手把手教你实现微信小视频

    本文个人原创,转载请注明出处,谢谢. 前段时间项目要求需要在聊天模块中加入类似微信的小视频功能,这边博客主要是为了总结遇到的问题和解决方法,希望能够对有同样需求的朋友有所帮助. 效果预览: 这里先罗列 ...

  3. ios设备突破微信小视频6S限制的方法

    刷微信朋友圈只发文字和图片怎能意犹未竟,微信小视频是一个很好的补充,音视频到位,流行流行最流行.但小视频时长不能超过6S,没有滤镜等是很大的遗憾.but有人突破限制玩出了花样,用ios设备在朋友圈晒出 ...

  4. Android 仿微信朋友圈拍小视频上传到服务器

    这个接上一个写的实现拍小视频和传到服务器的  界面是这个样子滴. 我也知不知道怎么给图片搞小一点o(╯□╰)o 布局文件是这样的[认真脸] <?xml version="1.0&quo ...

  5. iOS微信小视频优化心得

    小视频是微信6.0版本重大功能之一,在开发过程中遇到不少问题.本文先叙述小视频的产品需求,介绍了几个实现方案,分析每个方案的优缺点,最后总结出最优的解决方案. 小视频播放需求 可以同时播放多个视频 用 ...

  6. iOS燃烧动画、3D视图框架、天气动画、立体相册、微信朋友圈小视频等源码

    iOS精选源码 iOS天气动画,包括太阳,云,雨,雷暴,雪动画. 较为美观的多级展开列表 3D立体相册,可以旋转的立方体 一个仪表盘Demo YGDashboardView 一个基于UIScrollV ...

  7. 如何保存微信的小视频 How to keep WeChat 'Sights'

    微信小视频非常方便,但很难将其下载到本地电脑长期保存.网上有介绍方法,如百度经验上办法,但目前看来它可能只适用安卓系统,而且或已失效(可能由于版本更新).对Windows Phone无效,而对于更加封 ...

  8. 利用ffmpeg给小视频结尾增加logo水印

    背景 1.app有类似微信拍摄小视频功能,时长上限8s,视频文件保存在第三方云存储,app直接上传,后端数据库只记录视频的存放地址. 2.最近一次功能迭代,增加了小视频下载功能,小视频有可能在别的社交 ...

  9. 微信小视频复制到手机本地Android APP 分享

    因为需要将拍的宝宝的微信小视频上传到亲宝宝软件,每次去手动找文件比较麻烦,所以做了个微信视频复制到手机本地的APP,做工虽然粗糙,但是绝对实用, 下载地址 http://pan.baidu.com/s ...

随机推荐

  1. spring MVC 3.2中@ResponseBody(Post接口)返回乱码的完美解决方案

    本来因为ajax跨域http远程调用时有问题,在服务端响应时用以下方式解决了,但IE8及下有问题. response.addHeader("Access-Control-Allow-Orig ...

  2. Flink-v1.12官方网站翻译-P028-Custom Serialization for Managed State

    管理状态的自定义序列化 本页面的目标是为需要使用自定义状态序列化的用户提供指导,涵盖了如何提供自定义状态序列化器,以及实现允许状态模式演化的序列化器的指南和最佳实践. 如果你只是简单地使用Flink自 ...

  3. LCA算法——倍增

    概况 LCA(Lowest Common Ancestors),即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. 实现过程 预处理:通过dfs遍历,记录每个节点到根节点的距离dis ...

  4. HDU6321 Dynamic Graph Matching【状压DP 子集枚举】

    HDU6321 Dynamic Graph Matching 题意: 给出\(N\)个点,一开始没有边,然后有\(M\)次操作,每次操作加一条无向边或者删一条已经存在的边,问每次操作后图中恰好匹配\( ...

  5. 2019icpc徐州站 Cat 计蒜客 - 42540 && The Answer to the Ultimate Question of Life, The Universe, and Everything. 计蒜客 - 42545

    VJ链接:https://vjudge.net/contest/412095#problem/A Cat 计蒜客 - 42540 题意: 给你一个区间[L,R],给你现在拥有的钱S.你需要从[L,R] ...

  6. Codeforces Round #646 (Div. 2) B. Subsequence Hate(前缀和)

    题目链接:https://codeforces.com/contest/1363/problem/B 题意 可以将 $01$ 串中的 $0$ 变为 $1$.$1$ 变为 $0$,问至少需要变换多少字符 ...

  7. codeforces 632F. Magic Matrix (最小生成树)

    You're given a matrix A of size n × n. Let's call the matrix with nonnegative elements magic if it i ...

  8. LINUX - 文件读写缓存

    遇到一个进程core掉后日志打印不出来的问题: 参考如下: [引用] 只有正常退出,才能做到flush.否则将写失败. 之后有百度了下中文资料,发现同样的结论. "fflush库函数的作用是 ...

  9. codeforces 869A

    A. The Artful Expedient time limit per test 1 second memory limit per test 256 megabytes input stand ...

  10. 攻防世界-Web-lottery(.git泄露、php源码审计、弱类型利用)

    扫描目录,发现.git泄露: 提取.git泄露的源码,得到许多文件: 网站这里: 这就要审计一下代码,找找漏洞了. 经过一番审计,猜数字对应的函数在api.php中: 我们要绕过这个$win_numb ...