iOS中缺少必需的密钥AVVideoHeightKey异常

时间:2015-11-24 10:54:51

标签: ios uiimagepickercontroller avassetexportsession

代码:

SDAVAssetExportSession *encoder = [SDAVAssetExportSession.alloc initWithAsset:[AVAsset assetWithURL:[info objectForKey:UIImagePickerControllerMediaURL]]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    myPathDocs =  [documentsDirectory stringByAppendingPathComponent:
                        [NSString stringWithFormat:@"lowerBitRate-%d.mov",arc4random() % 1000]];
    NSURL *url = [NSURL fileURLWithPath:myPathDocs];
    encoder.outputURL=url;
    encoder.outputFileType = AVFileTypeMPEG4;
    encoder.shouldOptimizeForNetworkUse = YES;

    encoder.videoSettings = @
    {
    AVVideoCodecKey: AVVideoCodecH264,
    AVVideoCompressionPropertiesKey: @
        {
        AVVideoAverageBitRateKey: @2300000, // Lower bit rate here
        AVVideoProfileLevelKey: AVVideoProfileLevelH264High40,
        },
    };
    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");
             // encoder.outputURL <- this is what you want!!
         }
         else if (status == AVAssetExportSessionStatusCancelled)
         {
             NSLog(@"Video export cancelled");
         }
         else
         {
             NSLog(@"Video export failed with error: %@ (%d)", encoder.error.localizedDescription, encoder.error.code);
         }
     }];

我尝试压缩UIImagePickerController挑选的视频。当我压缩视频时,它会出现异常,因为&#34; [AVAssetWriterInput initWithMediaType:outputSettings:sourceFormatHint:]缺少必需的密钥AVVideoHeightKey &#34; .I我不知道如何解决这个问题。任何帮助都会被提前感谢。谢谢你提前

1 个答案:

答案 0 :(得分:1)

如果查看example of SDAVAssetExportSession,您会看到:

encoder.videoSettings = @
{
    AVVideoCodecKey: AVVideoCodecH264,
    AVVideoWidthKey: @1920,  //******* 
    AVVideoHeightKey: @1080, //*******
    AVVideoCompressionPropertiesKey: @
    {
        AVVideoAverageBitRateKey: @6000000,
        AVVideoProfileLevelKey: AVVideoProfileLevelH264High40,
    },
};

我没有检查,但我猜SDAVAssetExportSession继承自AVAssetExportSessionencoder.videoSettings显然缺少高度和宽度值。这就是告诉你错误信息的原因。您没有为视频设置高度或宽度。

所以你必须这样做:

encoder.videoSettings = @
{
AVVideoCodecKey: AVVideoCodecH264,
AVVideoWidthKey: @someWidthYouWant,
AVVideoHeightKey: @someHeightYouWant,
AVVideoCompressionPropertiesKey: @
    {
    AVVideoAverageBitRateKey: @2300000, // Lower bit rate here
    AVVideoProfileLevelKey: AVVideoProfileLevelH264High40,
    },
};