4

我正在尝试制作一个应用程序来记录 UIImageView 的内容以及麦克风音频并将它们实时录制到视频中。(有点像会说话的汤姆猫应用程序)

我正在使用 AVAssetWriterInputPixelBufferAdaptor 轻松记录 UIImageView 的内容,但我根本不知道如何将音频与此合并。这也不是因为缺乏尝试,我已经在这个特定问题上工作了一个多星期来梳理这个网站、谷歌、iphone 开发者论坛等。这不是我的一杯茶。

对此最接近的参考是: 如何将 UIImage 数组导出为电影?

我可以分别捕捉视频和音频,然后在事后将它们一起编码(我真的很努力解决这个问题),但是让这个应用程序实时录制真的很理想。

这是一些框架代码,演示了我到目前为止的录音:

这是.h文件

//
//  RecordingTestProjectViewController.h
//  RecordingTestProject
//
//  Created by Sean Luck on 7/26/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <CoreMedia/CoreMedia.h>
#import <CoreVideo/CoreVideo.h>
#import <QuartzCore/QuartzCore.h>

@interface RecordingTestProjectViewController : UIViewController 
    {
        UIImageView *testView;
        NSTimer *theTimer;

        NSTimer *assetWriterTimer;
        AVMutableComposition *mutableComposition;
        AVAssetWriter *assetWriter;
        AVAssetWriterInput *assetWriterInput;

        AVAssetWriterInput *_audioWriterInput;
        AVCaptureDeviceInput *audioInput;
        AVCaptureSession *_capSession;

        AVAssetWriterInputPixelBufferAdaptor *assetWriterPixelBufferAdaptor;
        CFAbsoluteTime firstFrameWallClockTime;
        int count;
    }

-(void) writeSample: (NSTimer*) _timer; 
-(void) startRecording;
-(void) pauseRecording;
-(void) stopRecording;
-(NSString*) pathToDocumentsDirectory; 


@end

和 .m 文件

//
//  RecordingTestProjectViewController.m
//  RecordingTestProject
//
//  Created by Sean Luck on 7/26/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RecordingTestProjectViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <CoreMedia/CoreMedia.h>
#import <CoreVideo/CoreVideo.h>
#import <QuartzCore/QuartzCore.h>

#define OUTPUT_FILE_NAME @"screen.mov"

#define TIME_SCALE 600

@implementation RecordingTestProjectViewController

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

- (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];

        // Release any cached data, images, etc that aren't in use.
    }

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
    {

        testView = [[UIImageView alloc] initWithImage:nil];
        testView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        testView.userInteractionEnabled=NO;
        testView.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:testView];
        [testView release];



        [super viewDidLoad];
        [self startRecording];
    }







-(void) writeSample: (NSTimer*) _timer 
    {

        if ([assetWriterInput isReadyForMoreMediaData]) 
            {


                NSLog(@"count=%.i",count);
                count=count+10;

                UIGraphicsBeginImageContext(testView.frame.size);
                [testView.image drawInRect:CGRectMake(0, 0, testView.frame.size.width, testView.frame.size.height)];
                CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
                CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0,1,1,1);
                CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5);
                CGContextBeginPath(UIGraphicsGetCurrentContext());
                CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 10+count/2, 10+count);
                CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 20+count*2, 20+count);
                CGContextStrokePath(UIGraphicsGetCurrentContext());
                testView.image = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();


                CVReturn cvErr = kCVReturnSuccess;
                CGImageRef image = (CGImageRef) [testView.image CGImage];

                // prepare the pixel buffer
                CVPixelBufferRef pixelBuffer = NULL;
                CFDataRef imageData= CGDataProviderCopyData(CGImageGetDataProvider(image));
                cvErr = CVPixelBufferCreateWithBytes(kCFAllocatorDefault,testView.frame.size.width,testView.frame.size.height,kCVPixelFormatType_32BGRA,(void*)CFDataGetBytePtr(imageData),CGImageGetBytesPerRow(image),NULL,NULL,NULL,&pixelBuffer);


                // calculate the time
                CFAbsoluteTime thisFrameWallClockTime = CFAbsoluteTimeGetCurrent();
                CFTimeInterval elapsedTime = thisFrameWallClockTime - firstFrameWallClockTime;
                CMTime presentationTime =  CMTimeMake (elapsedTime * TIME_SCALE, TIME_SCALE);

                if (!cvErr) 
                    {
                        // write the sample
                        BOOL appended = [assetWriterPixelBufferAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:presentationTime];

                        if (appended) 
                            {

                            } 
                        else 
                            {
                                NSLog (@"failed to append");
                                [self stopRecording];
                            }
                    }


                CVPixelBufferRelease(pixelBuffer);
                CFRelease(imageData);

            }

        if (count>1000) 
            {
                [self stopRecording];
            }
    }

-(void) startRecording 
    {
        // Doesn't record audio at all. Needs to be implemented.

        // create the AVAssetWriter
        NSString *moviePath = [[self pathToDocumentsDirectory] stringByAppendingPathComponent:OUTPUT_FILE_NAME];
        if ([[NSFileManager defaultManager] fileExistsAtPath:moviePath]) 
            {
                [[NSFileManager defaultManager] removeItemAtPath:moviePath error:nil];
            }

        NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
        NSError *movieError = nil;

        [assetWriter release];
        assetWriter = [[AVAssetWriter alloc] initWithURL:movieURL fileType: AVFileTypeQuickTimeMovie error: &movieError];
        NSDictionary *assetWriterInputSettings = [NSDictionary dictionaryWithObjectsAndKeys: AVVideoCodecH264, AVVideoCodecKey,[NSNumber numberWithInt:testView.frame.size.width], AVVideoWidthKey,[NSNumber numberWithInt:testView.frame.size.height], AVVideoHeightKey,nil];

        [assetWriterInput release];
        assetWriterInput =[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:assetWriterInputSettings];
        assetWriterInput.expectsMediaDataInRealTime = YES;
        [assetWriter addInput:assetWriterInput];


        [assetWriterPixelBufferAdaptor release];
        assetWriterPixelBufferAdaptor = [[AVAssetWriterInputPixelBufferAdaptor  alloc] initWithAssetWriterInput:assetWriterInput sourcePixelBufferAttributes:nil];
        [assetWriter startWriting];

        firstFrameWallClockTime = CFAbsoluteTimeGetCurrent();
        [assetWriter startSessionAtSourceTime: CMTimeMake(0, TIME_SCALE)];

        // start writing samples to it
        [assetWriterTimer release];
        assetWriterTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector (writeSample:) userInfo:nil repeats:YES];

        [movieURL release];
        [assetWriterInputSettings release];

    }


-(void) stopRecording 
    {
        if (assetWriterTimer!=nil) 
            {
                [assetWriterTimer invalidate];
                assetWriterTimer = nil;
                [assetWriter finishWriting];


                NSString *moviePath = [[self pathToDocumentsDirectory] stringByAppendingPathComponent:OUTPUT_FILE_NAME];
                UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);

            }

    }





-(void) pauseRecording
    {
        // needs to be implemented
    }




-(NSString*) pathToDocumentsDirectory 
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        return documentsDirectory;

    }


- (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return YES;
    }

@end

就是这样。如果有人可以修改此代码来录制麦克风音频,我相信除了我之外还有很多人会很感激。一旦添加了音频,这将为开发人员提供一个直接的框架来在应用程序屏幕投射中进行演示,以演示他们的应用程序。

仅供参考:我的代码是开源的,主要模仿来自http://www.subfurther.com/blog/2011/04/12/voices-that-matter-iphone-spring-2011/的 VTMScreenRecorderTest 项目。我进行了小幅修改并清理了内存泄漏。

4

1 回答 1

1

我刚刚发现,如果您想将麦克风与录制的视频一起使用,CMTimestamps 必须同步。我试图通过使用 CMSampleBufferSetOutputPresentationTimeStamp 更改音频上的采样时间来同步它们……但我遗漏了一些东西,或者它不起作用。如果您捕获麦克风开始的时间并将其添加为视频的偏移量,则 StackOverflow 上的 AVAssetWriter 的每个示例似乎都可以正常工作-

这是我为使视频与麦克风保持同步所做的工作:

Img = Image
Snd = Sound
Avmedia = AVAssetWriter
etc.
#define Cmtm__Pref_Timescale_uk 10000
          CMTime Duration_cmtm = CMTimeMakeWithSeconds( Avmedia_v->Img_Duration__Sec_df, Cmtm__Pref_Timescale_uk );
          Avmedia_v->Img_cmtm = CMTimeAdd( Duration_cmtm, Avmedia_v->Exprt__Begin_cmtm );

          if( [ Avmedia_v->Clrbuf__Adaptor_v appendPixelBuffer: Clrbuf_v withPresentationTime:
               Avmedia_v->Img_cmtm ] is no )
          {
            //If the operation was unsuccessful,
            //invoke the AVAssetWriter object’s finishWriting method in order to save a partially completed asset.
            [ Avmedia_v->Avwriter_v finishWriting ];

            CLog( Lv_Minor, "Movie Img exprt failed" );
          }
于 2013-08-19T17:24:23.950 回答