<图形图像,动画,多媒体> 读书笔记 --- 录制与编辑视频

使用UIImagePickerController 进行录制

#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

- (IBAction)videoRecod:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)videoRecod:(id)sender {

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

        imagePickerController.mediaTypes = [[NSArray alloc]
                                            initWithObjects:(NSString *)kUTTypeMovie, nil];

        //录制质量设定
        imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;

        //只允许最多录制30秒时间
        imagePickerController.videoMaximumDuration = 30.0f;

        [self presentViewController:imagePickerController animated:YES completion:nil];

    } else {
        NSLog(@"摄像头不可用。");
    }
}

- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void) imagePickerController: (UIImagePickerController *) picker
            didFinishPickingMediaWithInfo: (NSDictionary *) info {

    NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
    NSString *tempFilePath = [url path];

    if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(tempFilePath) ) {
        //video:didFinishSavingWithError:contextInfo: 必须保存成这类回调
        UISaveVideoAtPathToSavedPhotosAlbum( tempFilePath,
                                            self,
                                            @selector(video:didFinishSavingWithError:contextInfo:),
                                            (__bridge void *)(tempFilePath));
    }

    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(NSString *)contextInfo {
    NSString *title; NSString *message;
    if (!error) {
        title = @"视频保存";
        message = @"视频已经保存到设备的相机胶卷中";
    } else {
        title =  @"视频失败";
        message = [error description];
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated
{
    NSLog(@"选择器将要显示。");
}

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated
{
    NSLog(@"选择器显示结束。");
}

使用AVFoundation

AVCaptureSession,捕获会话,是为了实现从摄像头和麦克风捕获数据,需要使用AVCaptureSession对象协调输入输出数据

AVCaptureDevice,捕获设备,代表输入一个设备,例如摄像头和麦克风

AVCaptureDeviceInput,捕获会话的一个输入数据源

AVCaptureOutput,捕获会话的一个输出目标,例如输出的视频文件和静态图片

AVCaptureMovieFileOutput,是AVCaptureOutput的子类,通过它可以将捕获的数据输出到QuickTime视频文件中(MOV)

AVCaptureVideoPreviewLayer,是CALayer子类,可以使用它来显示录制的视频

AVCaptureConnection,捕获连接,在一个捕获会话中输入和输出之间的连接

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>

@interface ViewController ()
    <AVCaptureFileOutputRecordingDelegate>
{
    BOOL isRecording;
}

@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *button;

@property (strong, nonatomic) AVCaptureSession *session;
@property (strong, nonatomic) AVCaptureMovieFileOutput *output;

- (IBAction)recordPressed:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.session =  [[AVCaptureSession alloc] init];
    self.session.sessionPreset = AVCaptureSessionPresetMedium;

    AVCaptureDevice *cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error = nil;
    AVCaptureDeviceInput *camera = [AVCaptureDeviceInput deviceInputWithDevice:cameraDevice error:&error];

    AVCaptureDevice *micDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    AVCaptureDeviceInput *mic = [AVCaptureDeviceInput deviceInputWithDevice:micDevice error:&error];

    if (error || !camera || !mic) {
        NSLog(@"Input Error");
    } else {
        //加入捕获音频视频
        [self.session addInput:camera];
        [self.session addInput:mic];
    }

    self.output = [[AVCaptureMovieFileOutput alloc] init];

    if ([self.session canAddOutput:self.output]) {
        [self.session addOutput:self.output];//输出
    }

    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    previewLayer.frame = CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view.layer insertSublayer:previewLayer atIndex:0];

    [self.session startRunning];
    isRecording = NO;
    self.label.text = @"";

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (![self.session isRunning])
    {
        [self.session startRunning];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if ([self.session isRunning])
    {
        [self.session stopRunning];
    }
}

- (IBAction)recordPressed:(id)sender {
    if (!isRecording)
    {
        [self.button setTitle:@"停止" forState:UIControlStateNormal];
        self.label.text = @"录制中...";
        isRecording = YES;
        NSURL *fileURL = [self fileURL];
        [self.output startRecordingToOutputFileURL:fileURL recordingDelegate:self];
    }
    else
    {
        [self.button setTitle:@"录制" forState:UIControlStateNormal];
        self.label.text = @"停止";
        [self.output stopRecording];
        isRecording = NO;
    }
}

- (NSURL *) fileURL
{
    NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"movie.mov"];
    NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];

    NSFileManager *manager = [[NSFileManager alloc] init];
    if ([manager fileExistsAtPath:outputPath])
    {
        [manager removeItemAtPath:outputPath error:nil];
    }

    return outputURL;
}

#pragma  mark-- AVCaptureFileOutputRecordingDelegate委托协议实现方法

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
    didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
    fromConnections:(NSArray *)connections error:(NSError *)error
{

    if (error == nil) {
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

        [library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
                                    completionBlock:^(NSURL *assetURL, NSError *error)
         {
             if (error)
             {
                 NSLog(@"写入错误。") ;
             }

         }];
    }

}

使用UIVideoEditorController

#import "ViewController.h"

@interface ViewController ()
<UIVideoEditorControllerDelegate,UINavigationControllerDelegate>

- (IBAction)editButtonPress:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)editButtonPress:(id)sender {

    NSBundle *bundle = [NSBundle mainBundle];
	NSString *moviePath = [bundle pathForResource:@"YY"
                                           ofType:@"mp4"];

    //判断设备是否支持编辑视频
    if ([UIVideoEditorController canEditVideoAtPath:moviePath]){

        UIVideoEditorController *videoEditor =
        [[UIVideoEditorController alloc] init];

        videoEditor.delegate = self;
        videoEditor.videoPath = moviePath;

        [self presentViewController:videoEditor animated:YES completion:NULL];

    } else {
        NSLog(@"不能编辑这个视频");
    }

}

- (void)videoEditorController:(UIVideoEditorController *)editor
     didSaveEditedVideoToPath:(NSString *)editedVideoPath{

    [editor dismissViewControllerAnimated:YES completion:NULL];

    if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(editedVideoPath) ) {
        UISaveVideoAtPathToSavedPhotosAlbum(editedVideoPath, self,
                                            @selector(video:didFinishSavingWithError:contextInfo:),
                                            (__bridge void *)(editedVideoPath));
    }
}

- (void)videoEditorController:(UIVideoEditorController *)editor
             didFailWithError:(NSError *)error{
    NSLog(@"编辑视频出错");
    NSLog(@"Video editor error occurred = %@", error);
    [editor dismissViewControllerAnimated:YES completion:NULL];
}

- (void)videoEditorControllerDidCancel:(UIVideoEditorController *)editor{
    NSLog(@"视频编辑取消");
    [editor dismissViewControllerAnimated:YES completion:NULL];
}

- (void)video:(NSString *)videoPath
didFinishSavingWithError:(NSError *)error
  contextInfo:(NSString *)contextInfo {

    NSString *title; NSString *message;
    if (!error) {
        title = @"视频保存";
        message = @"视频已经保存到设备的相机胶卷中";
    } else {
        title =  @"视频失败";
        message = [error description];
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

个人感言,其实视频我之前做过,也有类似文章,在国外的一些网站上完全也能找到比这更好的更有技术含量的代码和文章,不过总之,算是一个学习记录吧~

原书:http://item.jd.com/11522516.html

时间: 2024-10-24 09:55:43

<图形图像,动画,多媒体> 读书笔记 --- 录制与编辑视频的相关文章

&lt;图形图像,动画,多媒体&gt; 读书笔记 --- 图像处理

为了便于操作图像iOS中定义图像类,UIImage是UIKit框架中定义的图像类,其封装了高层次图像类,可以通过多种方式创建这些对象.在Core Graphics框架(或Quartz 2D)中也定义了CGImage,他表示位图图像,因为CGImage被封装起来了,所以通常通过CGImageRef来使用CGImage. 除了UIImage和CGImage外,在Core Image框架中也有一个图像类 CIImage,CIImage封装了图像类能够很好地进行图像效果处理.例如,滤镜的使用.UIIma

&lt;图形图像,动画,多媒体&gt; 读书笔记 --- AirPlay

AirPlay技术是之前一直没有接触过的技术,正好这次做一个笔记 共用: 1.可以通过AirPlay将iOS和MAC设备上的视频或音频输出到高清电视上或高保真音响 2.可以通过AirPlay将iOS和MAC设备屏幕镜像显示到高清电视上 3.可以通过AirPlay将iOS和MAC设备屏幕与高清电视实现双屏幕显示 AirPlay对能够播放的媒体编码有要求 1.音频数据采用AAC或MP3编码压缩 2.视频数据采用H264编码压缩 可以通过AirPlay播放的技术有 1.MPMoviePlayerCon

&lt;图形图像,动画,多媒体&gt; 读书笔记 --- 音效

音频多媒体文件主要是存放音频数据信息,音频文件在录制的过程中把声音信号,通过音频编码,变成音频数字信号保存到某种格式文件中.在播放过程中在对音频文件解码,解码出的信号通过扬声器等设备就可以转成音波.音频文件在编码的过程中数据量很大,所以有的文件格式对于数据进行了压缩,因此音频文件可以分为: 无损格式,是非压缩数据格式,文件很大一般不适合移动设备,例如:WAV,AU,APE等文件 有损格式,对于数据进行了压缩,压缩后丢掉了一些数据,例如MP3,WMA等文件. 1.WAV文件 WAV文件目前是最流行

&lt;图形图像,动画,多媒体&gt; 读书笔记 --- 视频

1.AVI文件 AVI是音频视频交错的英文缩写,它是微软公司开发的一种符合RIFF文件规范的数字音频与视频文件格式,是将音频与视频同步组合在一起的文件格式,他对视频文件采用了一种有损压缩方式. 2.WMV文件 WMV也是微软公司推出的一种流媒体格式,在同等视频质量下,WMV格式的体积非常小,因此很适合在网上播放和传输. 3.RMVB文件 RMVB是一种视频文件格式,RMVB中的VB指他打破了压缩的平均比特率,使在静态画面下的比特率降低,来达到优化整个视频中比特率,提高效率节约资源的目的.RMVB

&lt;图形图像,动画,多媒体&gt; 读书笔记 --- 力学行为特性

UIKit力学行为包含了:重力(UIGravityBehavior),碰撞(UICollisionBehavior),吸附(UIAttachmentBehavior),推(UIPushBehavior),甩(UISnapBehavior)和行为限制(UIDynamicItemBehavior). - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; _animator = [[UIDynamicAnimato

&lt;图形图像,动画,多媒体&gt; 读书笔记 --- 图像处理之人脸识别

人脸识别过程一般分为以下3个步骤: 1.首先建立人脸的面纹数据库.可以通过照相机或摄像机采集人脸的面相图片,将这些面相图片生成面纹编码保存到数据库中. 2.获取当前人脸面相图片.即通过照相机或摄像机采集人脸的面相图片,将当前的面相文件生成面纹编码 3.用当前的面纹编码与数据库中的面纹编码进行对比 在iOS5之后提供人脸识别的API,通过提供的CIDetector类可以进行人脸特征识别,CIDetector是CoreImage框架中的一个特征识别滤镜,CIDetector主要用于人脸特征识别.通过

&amp;lt;图形图像,动画,多媒体&amp;gt; 读书笔记 --- 录制与编辑视频

使用UIImagePickerController 进行录制 #import "ViewController.h" #import <MobileCoreServices/MobileCoreServices.h> #import <QuartzCore/QuartzCore.h> @interface ViewController () <UIImagePickerControllerDelegate,UINavigationControllerDele

&amp;lt;图形图像,动画,多媒体&amp;gt; 读书笔记 --- 音效

音频多媒体文件主要是存放音频数据信息,音频文件在录制的过程中把声音信号,通过音频编码,变成音频数字信号保存到某种格式文件里.在播放过程中在对音频文件解码,解码出的信号通过扬声器等设备就能够转成音波.音频文件在编码的过程中数据量非常大,所以有的文件格式对于数据进行了压缩,因此音频文件能够分为: 无损格式,是非压缩数据格式,文件非常大一般不适合移动设备,比如:WAV,AU,APE等文件 有损格式,对于数据进行了压缩,压缩后丢掉了一些数据,比如MP3,WMA等文件. 1.WAV文件 WAV文件眼下是最

&amp;lt;图形图像,动画,多媒体&amp;gt; 读书笔记 --- AirPlay

AirPlay技术是之前一直没有接触过的技术,正好这次做一个笔记 共用: 1.能够通过AirPlay将iOS和MAC设备上的视频或音频输出到高清电视上或高保真音响 2.能够通过AirPlay将iOS和MAC设备屏幕镜像显示到高清电视上 3.能够通过AirPlay将iOS和MAC设备屏幕与高清电视实现双屏幕显示 AirPlay对可以播放的媒体编码有要求 1.音频数据採用AAC或MP3编码压缩 2.视频数据採用H264编码压缩 能够通过AirPlay播放的技术有 1.MPMoviePlayerCon