• 需求

    公司混合开发,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. java身份证号校验

    package com.pt.modules.contract.utils; import java.text.ParseException; import java.text.SimpleDateF ...

  2. RabbitMQ (简单集群部署操作)

    RabbitMQ 集群部署 前期准备 第一步:三台linux系统(centos7.3) 主机名(hostname) 网卡ip node1 192.168.137.138 node2 192.168.1 ...

  3. DolphinScheduler 源码分析之 DAG类

    1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license ...

  4. C++typename的由来和用法

  5. 树的直径&树的重心

    树的直径 定义 那么树上最远的两个点,他们之间的距离,就被称之为树的直径. 树的直径的性质 1. 直径两端点一定是两个叶子节点. 2. 距离任意点最远的点一定是直径的一个端点,这个基于贪心求直径方法的 ...

  6. Codeforces Round #646 (Div. 2) C. Game On Leaves(树上博弈)

    题目链接:https://codeforces.com/contest/1363/problem/C 题意 有一棵 $n$ 个结点的树,每次只能取叶子结点,判断谁能最先取到结点 $x$ . 题解 除非 ...

  7. P1108 低价购买(DP)

    题目描述 "低价购买"这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:"低价购买:再低价购买".每次你购买一支股 ...

  8. Educational Codeforces Round 88 (Rated for Div. 2) D、Yet Another Yet Another Task

    题意: 给你一个含n个数a1,a2...an的数组,你要找到一个区间[l,r],使得al+a(l+1)+...+a(r-1)+ar减去max(al,a(l+1),...,a(r-1),ar)的值尽可能 ...

  9. HDU - 3281 dp

    题意: 给你b个球,m个楼层,你需要找到一个楼层数k,使得从小于k这个楼层上面扔下去球,而球不会碎.求在最糟糕的情况下你最多要尝试多少次 题解: dp[i][j]表示你有b个球,楼层总数为m,你找到那 ...

  10. SQL优化汇总

    今天面某家公司,然后问我SQL优化,感觉有点忘了,今天特此总结一下: 总结得是分两方面:索引优化和查询优化: 一. 索引优化: 1. 独立的列 在进行查询时,索引列不能是表达式的一部分,也不能是函数的 ...