2019独角兽企业重金招聘Python工程师标准>>>

分类: iphone 2012-10-07 16:37  583人阅读  评论(0)  收藏  举报
iphone 图像处理 string 算法 工作

转自:http://flhs-wdw.blog.sohu.com/207300574.html

iphone提供了AVFoundation库来方便的操作多媒体设备,AVAssetWriter这个类可以方便的将图像和音频写成一个完整的视频文件。甚至将整个应用的操作录制下来,也不是什么困难的事情。

这里先说一下如何将录像的视频写到指定文件中去:
首先先准备好AVCaptureSession,当录制开始后,可以控制调用相关回调来取音视频的每一贞数据。
[cpp]  view plain copy
  1. NSError * error;
  2. session = [[AVCaptureSession alloc] init];
  3. [session beginConfiguration];
  4. [session setSessionPreset:AVCaptureSessionPreset640x480];
  5. [self initVideoAudioWriter];
  6. AVCaptureDevice * videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  7. AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
  8. AVCaptureDevice * audioDevice1 = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
  9. AVCaptureDeviceInput *audioInput1 = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice1 error:&error];
  10. videoOutput = [[AVCaptureVideoDataOutput alloc] init];
  11. [videoOutput setAlwaysDiscardsLateVideoFrames:YES];
  12. [videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
  13. [videoOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
  14. audioOutput = [[AVCaptureAudioDataOutput alloc] init];
  15. numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
  16. [audioOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
  17. [session addInput:videoInput];
  18. [session addInput:audioInput1];
  19. [session addOutput:videoOutput];
  20. [session addOutput:audioOutput];
  21. [session commitConfiguration];
  22. [session startRunning];

回调函数:

[cpp]  view plain copy
  1. -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
  2. //CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
  3. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  4. static int frame = 0;
  5. CMTime lastSampleTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
  6. if( frame == 0 && videoWriter.status != AVAssetWriterStatusWriting  )
  7. {
  8. [videoWriter startWriting];
  9. [videoWriter startSessionAtSourceTime:lastSampleTime];
  10. }
  11. if (captureOutput == videoOutput)
  12. {
  13. /           if( videoWriter.status > AVAssetWriterStatusWriting )
  14. {
  15. NSLog(@"Warning: writer status is %d", videoWriter.status);
  16. if( videoWriter.status == AVAssetWriterStatusFailed )
  17. NSLog(@"Error: %@", videoWriter.error);
  18. return;
  19. }
  20. if ([videoWriterInput isReadyForMoreMediaData])
  21. if( ![videoWriterInput appendSampleBuffer:sampleBuffer] )
  22. NSLog(@"Unable to write to video input");
  23. else
  24. NSLog(@"already write vidio");
  25. }
  26. }
  27. else if (captureOutput == audioOutput)
  28. {
  29. if( videoWriter.status > AVAssetWriterStatusWriting )
  30. {
  31. NSLog(@"Warning: writer status is %d", videoWriter.status);
  32. if( videoWriter.status == AVAssetWriterStatusFailed )
  33. NSLog(@"Error: %@", videoWriter.error);
  34. return;
  35. }
  36. if ([audioWriterInput isReadyForMoreMediaData])
  37. if( ![audioWriterInput appendSampleBuffer:sampleBuffer] )
  38. NSLog(@"Unable to write to audio input");
  39. else
  40. NSLog(@"already write audio");
  41. }
  42. if (frame == FrameCount)
  43. {
  44. [self closeVideoWriter];
  45. }
  46. frame ++;
  47. [pool drain];
  48. }

剩下的工作就是初始化AVAssetWriter,包括音频与视频输入输出:

[cpp]  view plain copy
  1. -(void) initVideoAudioWriter
  2. {
  3. CGSize size = CGSizeMake(480, 320);
  4. NSString *betaCompressionDirectory = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents/Movie.mp4"];
  5. NSError *error = nil;
  6. unlink([betaCompressionDirectory UTF8String]);
  7. //----initialize compression engine
  8. self.videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURLfileURLWithPath:betaCompressionDirectory]
  9. fileType:AVFileTypeQuickTimeMovie
  10. error:&error];
  11. NSParameterAssert(videoWriter);
  12. if(error)
  13. NSLog(@"error = %@", [error localizedDescription]);
  14. NSDictionary *videoCompressionProps = [NSDictionary dictionaryWithObjectsAndKeys:
  15. [NSNumber numberWithDouble:128.0*1024.0],AVVideoAverageBitRateKey,
  16. nil ];
  17. NSDictionary *videoSettings = [NSDictionarydictionaryWithObjectsAndKeys:AVVideoCodecH264, AVVideoCodecKey,
  18. [NSNumber numberWithInt:size.width], AVVideoWidthKey,
  19. [NSNumber numberWithInt:size.height],AVVideoHeightKey,videoCompressionProps, AVVideoCompressionPropertiesKey, nil];
  20. self.videoWriterInput = [AVAssetWriterInputassetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
  21. NSParameterAssert(videoWriterInput);
  22. videoWriterInput.expectsMediaDataInRealTime = YES;
  23. NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionarydictionaryWithObjectsAndKeys:
  24. [NSNumbernumberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey, nil];
  25. self.adaptor = [AVAssetWriterInputPixelBufferAdaptorassetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
  26. sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];
  27. NSParameterAssert(videoWriterInput);
  28. NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
  29. if ([videoWriter canAddInput:videoWriterInput])
  30. NSLog(@"I can add this input");
  31. else
  32. NSLog(@"i can't add this input");
  33. // Add the audio input
  34. AudioChannelLayout acl;
  35. bzero( &acl, sizeof(acl));
  36. acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
  37. NSDictionary* audioOutputSettings = nil;
  38. //    audioOutputSettings = [ NSDictionary dictionaryWithObjectsAndKeys:
  39. //                           [ NSNumber numberWithInt: kAudioFormatAppleLossless ], AVFormatIDKey,
  40. //                           [ NSNumber numberWithInt: 16 ], AVEncoderBitDepthHintKey,
  41. //                           [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
  42. //                           [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
  43. //                           [ NSData dataWithBytes: &acl length: sizeof( acl ) ], AVChannelLayoutKey,
  44. //                           nil ];
  45. audioOutputSettings = [ NSDictionary dictionaryWithObjectsAndKeys:
  46. [ NSNumber numberWithInt: kAudioFormatMPEG4AAC ], AVFormatIDKey,
  47. [ NSNumber numberWithInt:64000], AVEncoderBitRateKey,
  48. [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
  49. [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
  50. [ NSData dataWithBytes: &acl length: sizeof( acl ) ], AVChannelLayoutKey,
  51. nil ];
  52. audioWriterInput = [[AVAssetWriterInput
  53. assetWriterInputWithMediaType: AVMediaTypeAudio
  54. outputSettings: audioOutputSettings ] retain];
  55. audioWriterInput.expectsMediaDataInRealTime = YES;
  56. // add input
  57. [videoWriter addInput:audioWriterInput];
  58. [videoWriter addInput:videoWriterInput];
  59. }

转载于:https://my.oschina.net/u/1049180/blog/137802

iOS 开发中 通过AVAssetWriter将录像视频写到指定文件相关推荐

  1. iOS开发中的神兵利器 [实战系列]-李发展-专题视频课程

    iOS开发中的神兵利器 [实战系列]-11758人已学习 课程介绍         - 140节课程讲解GitHub中近百个过千star的iOS热门开源项目 - 市面上唯一大规模讲解热门的iOS开源项 ...

  2. iOS开发中didSelectRowAtIndexPath tap事件响应延迟

    iOS开发中didSelectRowAtIndexPath tap事件响应延迟 为UITableViewCell添加tapped事件,代码如下: class VideoViewController: ...

  3. iOS开发中的Web应用概述

    为了更好的阅读体验,建议阅读原文 插播广告 -- 几十行代码完成资讯类App多种形式内容页 HybridPageKit :一个针对资讯类App高性能.易扩展.组件化的通用内容页实现框架. 想和我一起全 ...

  4. FFmpeg在iOS开发中编译并使用

    FFmpeg简介 FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.读作:爱服爱服爱母派格.全称:Fast Forward Mpeg.直译:快速转换图像.FFmpe ...

  5. iOS开发中遇到的一些问题及解决方案【转载】

    iOS开发中遇到的一些问题及解决方案[转载] 2015-12-29 [385][scrollView不接受点击事件,是因为事件传递失败] // //  MyScrollView.m //  Creat ...

  6. iOS开发中屏幕旋转(一)

    Morris_ 2018.11.24 前言 最近做一个关于在线视频互动的iPad项目,部分界面只要横屏,部分界面可以横竖屏转换.看了看别家做的项目,有些是只做竖/横屏,有些是支持了横竖屏转换,在iPa ...

  7. ios 开发中 动态库 与静态库的区别

    使用静态库的好处 1,模块化,分工合作 2,避免少量改动经常导致大量的重复编译连接 3,也可以重用,注意不是共享使用 动态库使用有如下好处: 1使用动态库,可以将最终可执行文件体积缩小 2使用动态库, ...

  8. iOS 开发中的多线程

    线程.进程 什么是线程.进程   有的人说进程就像是人的脑袋,线程就是脑袋上的头发~~.其实这么比方不算错,但是更简单的来说,用迅雷下载文件,迅雷这个程序就是一个进程,下载的文件就是一个线程,同时下载 ...

  9. iOS开发中使用[[UIApplication sharedApplication] openURL:]加载其它应用

    iOS 应用程序之间(1) 在iOS开发中,经常需要调用其它App,如拨打电话.发送邮件等.UIApplication:openURL:方法是实现这一目的的最简单方法,该方法一般通过提供的url参数的 ...

  10. iOS开发中各种关键字的区别

    1.一些概念 1.浅Copy:指针的复制,只是多了一个指向这块内存的指针,共用一块内存. 深Copy:内存的复制,两块内存是完全不同的, 也就是两个对象指针分别指向不同的内存,互不干涉. 2.atom ...

最新文章

  1. 串口 能 按位传输吗_六类网线能传输多少米?家装六类网线有必要吗?
  2. plotly可视化绘制多图(multiplot)
  3. 总结FormsAuthentication的使用
  4. legend3---4、lavarel中session使用注意
  5. OSI模型中的数据链路层和物理层的区分
  6. Android NDK 内存泄露检测
  7. java集合框架03
  8. 配置MGR时修改了/etc/hosts但映射后的hostname不起作用
  9. 错误ORA-04091: table is mutating, trigger/function may not see it的原因以及解决办法
  10. 本地---tcpserver与tcpclient
  11. jQuery Ajax POST方法
  12. 基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码
  13. LNMP详解(六)——Nginx location语法详解
  14. python3.x编程模板总结
  15. java int 位_java int是几位
  16. MATLAB代码:基于分布式ADMM算法的考虑碳排放交易的电力系统优化调度研究
  17. 【DIY】通达信DIY添加扫雷宝、地图和复盘(1)
  18. ps去水印教程_Adobe Photoshop CS2去除水印方法 PS去水印教程
  19. SmartUpload文件上传
  20. mysql ndb_MySQL NDB Cluster概述

热门文章

  1. yolov4网络结构_上达最高精度,下到最快速度,Scaled-YOLOv4:模型缩放显神威
  2. 八爪鱼导出到mysql数据库_数据导出到Oracle数据库的方法 - 八爪鱼采集器
  3. android连mysql注册界面代码_Android实现注册登录界面的实例代码
  4. Vite 配置项目别名-最新版
  5. React Native 介绍
  6. MTK平台修改分区大小之后,通过fastboot工具烧录的说明
  7. 播报哥架构运行异常提示
  8. Java Servlet(十一):一个servlet被10个浏览器客户端访问时会创建几个servlet实例?...
  9. Spring常用注解用法总结
  10. javascript简介和基本语法