2012-01-16 72 views
5

我是AVFoundation和解碼過程中的新手。我需要解碼h264視頻文件並在iphone中播放它...任何人都可以給我指導來做到這一點。在ios中解碼h264

我不想使用ffmpeg或任何第三方庫來做到這一點。據我所知使用Avfoundation編碼是可能的...這是我想到的是用於編碼,但不知道在所有的代碼...

float bitsPerPixel; 
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(currentFormatDescription); 
int numPixels = dimensions.width * dimensions.height; 
int bitsPerSecond; 

// Assume that lower-than-SD resolutions are intended for streaming, and use a lower bitrate 
if (numPixels < (640 * 480)) 
    bitsPerPixel = 4.05; // This bitrate matches the quality produced by AVCaptureSessionPresetMedium or Low. 
else 
    bitsPerPixel = 11.4; // This bitrate matches the quality produced by AVCaptureSessionPresetHigh. 

bitsPerSecond = numPixels * bitsPerPixel; 

NSDictionary *videoCompressionSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
              AVVideoCodecH264, AVVideoCodecKey, 
              [NSNumber numberWithInteger:dimensions.width], AVVideoWidthKey, 
              [NSNumber numberWithInteger:dimensions.height], AVVideoHeightKey, 
              [NSDictionary dictionaryWithObjectsAndKeys: 
              [NSNumber numberWithInteger:bitsPerSecond], AVVideoAverageBitRateKey, 
              [NSNumber numberWithInteger:30], AVVideoMaxKeyFrameIntervalKey, 
              nil], AVVideoCompressionPropertiesKey, 
              nil]; 
if ([assetWriter canApplyOutputSettings:videoCompressionSettings forMediaType:AVMediaTypeVideo]) { 
    assetWriterVideoIn = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:videoCompressionSettings]; 
    assetWriterVideoIn.expectsMediaDataInRealTime = YES; 
    assetWriterVideoIn.transform = [self transformFromCurrentVideoOrientationToOrientation:self.referenceOrientation]; 
    if ([assetWriter canAddInput:assetWriterVideoIn]) 
     [assetWriter addInput:assetWriterVideoIn]; 
    else { 
     NSLog(@"Couldn't add asset writer video input."); 
     return NO; 
    } 
} 
else { 
    NSLog(@"Couldn't apply video output settings."); 
    return NO; 
} 

return YES; 

我完全天真關於這個,請幫助.. 。從哪裏開始///

感謝

+0

我試圖做同樣的事情你有沒有找到解決辦法? – 2012-04-29 17:07:04

+0

該項目被取消......我們沒有找到解決方案... – 2013-05-01 07:46:38

回答

0

更簡單的解決方案是使用的MPMoviePlayerController。它需要輸入mov或mv4文件(從本地文件系統或通過URL)。

另一種選擇是使用AVPlayer類。

希望它能幫助,

大衛

0

您可以參考並建立官方的示例代碼:AVPlayerDemo,看看它是如何工作的。它使用AV Foundation框架,主要是AVPlayer API播放視頻文件,性能非常好。 要通過AVPlayerDemo播放視頻文件,您應該將視頻文件複製到iOS設備上,並在AVPlayerDemo App上選擇iPod庫。