2015-11-24 17 views
-1

代碼:缺少所需的關鍵AVVideoHeightKey例外的iOS

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.When拿起我壓縮視頻它給人的異常爲「[AVAssetWriterInput initWithMediaType:outputSettings:sourceFormatHint:]缺少所需的關鍵AVVideoHeightKey」。我不知道如何解決這個problem..any幫助將提前appreicated.thanks

+0

在'encoder.videoSettings = @ {...}'你必須有'AVVideoHeightKey:@(someHeighValue)'。我猜你還需要添加'AVVideoHeightKey:@(someWidthValue)'。 – Larme

回答

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, 
    }, 
}; 
+0

斷開的鏈接,修復它 – Loxx

+0

@Larcerax:鏈接固定。在URL中缺少「n」。 – Larme

+0

感謝噸,這工作 – Loxx