在ios中解码h264

时间:2012-01-16 12:30:57

标签: ios avfoundation h.264 decoding

我是AVFoundation和解码过程中的新手。我需要解码h264视频文件并在iphone中播放...任何人都可以给我指导。

我不想使用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;

我对此完全天真,请帮助......从哪里开始///

感谢

2 个答案:

答案 0 :(得分:0)

更简单的解决方案是使用MPMoviePlayerController。它接收输入mov或mv4文件(来自本地文件系统或通过URL)。

另一种选择是使用AVPlayer类。

希望它有所帮助,

大卫

答案 1 :(得分:0)

您可以参考并构建官方示例代码:AVPlayerDemo以查看其工作原理。它使用AV Foundation框架,主要是AVPlayer API来播放视频文件而且性能非常出色。 要通过AVPlayerDemo播放视频文件,您应该通过itunes将视频文件复制到iOS设备,并在AVPlayerDemo应用程序上选择iPod库。