ios - AVAssetWriter写入视频但不录制音频

时间:2014-04-22 10:14:18

标签: ios audio video avassetwriter avasset

我成功地制作了viedo(虽然Blur)但是已经成功了 录制音频失败请帮助。

我正在使用以下代码请通过它来建议一些解决方案 或者是一个有用的教程。

我正在使用
附加样本缓冲区 [encoder._writerInput appendSampleBuffer:sampleBuffer]; 并且应用程序崩溃调用以下语句时         [encoder.audioWriterInput appendSampleBuffer:sampleBuffer];

- (void) initPath:(NSString*)path Height:(int) height andWidth:(int) width bitrate:(int)bitrate
{
    self.path = path;
    _bitrate = bitrate;

    [[NSFileManager defaultManager] removeItemAtPath:self.path error:nil];
    NSURL* url = [NSURL fileURLWithPath:self.path];

    //Initialize Video Writer
    _writer = [AVAssetWriter assetWriterWithURL:url fileType:AVFileTypeMPEG4 error:nil];

    //Initialize Audio writer
//    audioWriter = [AVAssetWriter assetWriterWithURL:url fileType:AVFileTypeCoreAudioFormat error:nil];

//    NSDictionary* settings = @{
//        AVVideoCodecKey: AVVideoCodecH264,
//        AVVideoWidthKey: @(width),
//        AVVideoHeightKey: @(height),
//        AVVideoCompressionPropertiesKey: @{
//             AVVideoAverageBitRateKey: @(self.bitrate),
//             AVVideoMaxKeyFrameIntervalKey: @(150),
//             AVVideoProfileLevelKey: AVVideoProfileLevelH264BaselineAutoLevel,
//             AVVideoAllowFrameReorderingKey: @NO,
//             //AVVideoH264EntropyModeKey: AVVideoH264EntropyModeCAVLC,
//             //AVVideoExpectedSourceFrameRateKey: @(30),
//             //AVVideoAverageNonDroppableFrameRateKey: @(30)
//        }
//    };
    NSDictionary* settings = @{
                               AVVideoCodecKey: AVVideoCodecH264,
                               AVVideoWidthKey: [NSNumber numberWithInt:width],
                               AVVideoHeightKey: [NSNumber numberWithInt:height],
                               AVVideoCompressionPropertiesKey: @{
                                       AVVideoAverageBitRateKey: [NSNumber numberWithInt:bitrate],
                                       AVVideoMaxKeyFrameIntervalKey: @(150),
                                       AVVideoProfileLevelKey: AVVideoProfileLevelH264BaselineAutoLevel,
                                       AVVideoAllowFrameReorderingKey: @NO,
                                       AVVideoH264EntropyModeKey: AVVideoH264EntropyModeCAVLC,
                                       AVVideoExpectedSourceFrameRateKey: @(30),
                                       AVVideoAverageNonDroppableFrameRateKey: @(30)
                                       }
                               };

    AudioChannelLayout acl;
    bzero( &acl, sizeof(acl));
    acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;

    NSDictionary *audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                           [ NSNumber numberWithInt: kAudioFormatMPEG4AAC ], AVFormatIDKey,
                           [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
                           [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
                           [ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey,
                           [ NSData dataWithBytes: &acl length: sizeof( acl ) ], AVChannelLayoutKey,
                           nil];

    audioWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioOutputSettings];
    audioWriterInput.expectsMediaDataInRealTime=YES;
    _writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:settings];
    _writerInput.expectsMediaDataInRealTime = YES;

    [_writer addInput:audioWriterInput];
    [_writer addInput:_writerInput];
}

0 个答案:

没有答案
相关问题