在ios中解码h264

Sho*_*min 5 decoding avfoundation h.264 ios

我是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;
Run Code Online (Sandbox Code Playgroud)

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

谢谢

zeb*_*ton 0

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

另一种选择是使用 AVPlayer 类。

希望能帮助到你,

大卫