iOS:如何设置AVAssetWriterInput以获取基准级别3.0配置文件?

时间:2015-09-02 16:01:26

标签: ios objective-c avassetwriter

某些设备(通常是Android设备)无法读取我在iOS中使用带有ImagePicker API的iphone录制的视频。所以我在搜索视频后搜索了将基线转换为3.0级配置文件的方法。我找到了一个库(https://github.com/rs/SDAVAssetExportSession),提供了使用AVAssetWriterInput的简便方法,我使用了这段代码:

- (IBAction)takeVideo:(id)sender
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
NSArray *availableTypes = [UIImagePickerController
                           availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
ipc.mediaTypes = availableTypes;
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
[ipc setVideoMaximumDuration:10];
ipc.delegate = self;
ipc.videoQuality = UIImagePickerControllerQualityTypeMedium;
if ([availableTypes containsObject:(__bridge NSString *)kUTTypeMovie]) {
    [ipc setMediaTypes:@[(__bridge NSString *)kUTTypeMovie]];
}
[self presentViewController:ipc animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *mediaURL = info[UIImagePickerControllerMediaURL];
if (mediaURL) {
    SDAVAssetExportSession *encoder = [SDAVAssetExportSession.alloc initWithAsset:[AVAsset assetWithURL:mediaURL]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    self.videoPath =  [documentsDirectory stringByAppendingPathComponent:
                        [NSString stringWithFormat:@"myvideo.mov"]];
    self.url = [NSURL fileURLWithPath:self.videoPath];
    encoder.outputURL=self.url;
    encoder.outputFileType = AVFileTypeMPEG4;
    encoder.shouldOptimizeForNetworkUse = YES;

    encoder.videoSettings = @
    {
    AVVideoCodecKey: AVVideoCodecH264,
    AVVideoWidthKey: @1920,
    AVVideoHeightKey: @1080,
    AVVideoCompressionPropertiesKey: @
        {
        AVVideoAverageBitRateKey: @6000000, // Lower bit rate here
        AVVideoProfileLevelKey: AVVideoProfileLevelH264Baseline30,//This is what I want
        },
    };
    encoder.audioSettings = @
    {
    AVFormatIDKey: @(kAudioFormatMPEG4AAC),
    AVNumberOfChannelsKey: @2,
    AVSampleRateKey: @44100,
    AVEncoderBitRateKey: @128000,
    };

    [encoder exportAsynchronouslyWithCompletionHandler:^
     {
         int status = encoder.status;

         if (status == AVAssetExportSessionStatusCompleted)
         {
             AVAssetTrack *videoTrack = nil;
             AVURLAsset *asset = [AVAsset assetWithURL:encoder.outputURL];
             NSArray *videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
             videoTrack = [videoTracks objectAtIndex:0];
             float frameRate = [videoTrack nominalFrameRate];
             float bps = [videoTrack estimatedDataRate];
             NSLog(@"Frame rate == %f",frameRate);
             NSLog(@"bps rate == %f",bps/(1024.0 * 1024.0));
             NSLog(@"Video export succeeded");
                     [self dismissViewControllerAnimated:YES completion:^{
                         //some instructions...
                     }];
         }
         else if (status == AVAssetExportSessionStatusCancelled)
         {
             NSLog(@"Video export cancelled");
         }
         else
         {
             NSLog(@"Video export failed with error: %@ (%ld)", encoder.error.localizedDescription, (long)encoder.error.code);
         }
     }];
}

}

但是我确实收到错误"视频导出失败并显示错误:操作无法完成(-11800)"。 我很确定这是因为我的videoSettings错误/与基线3.0不兼容。

如何让基线3.0正常工作?

1 个答案:

答案 0 :(得分:0)

可能这就是你需要的。 Issue42Solved

AVVideoWidthKey: @720,
AVVideoHeightKey: @480,
AVVideoCompressionPropertiesKey: @
{
    AVVideoAverageBitRateKey: @3000000