1

我的应用程序创建一个 mp4 文件。我已验证我的代码适用于以下设备:

  • iPad (操作系统 4.3.2)
  • iPhone4(操作系统 4.2.1)
  • iPhone 3GS (操作系统 4.2.1)

.. 但在运行 OS 4.2.1 的我的 iPod Touch 3rd Gen 上初始化失败。

这与此处的另一个问题有关,但我在与他不同的 iOS 设备上看到它,并且我在这里包含了我的初始化代码。像另一个问题一样,我尝试了不同的像素格式和比特率,但是 AVAssetWriter 的状态在调用其 startWriting 函数后总是更改为AVAssetWriterStatusFailed 。

任何想法将不胜感激。我知道可以在此设备上创建 mp4,因为我已经下载了另一个应用程序,该应用程序在我的代码失败的同一设备上运行良好。

这是进行视频设置的最少代码。

#import "AVFoundation/AVFoundation.h"
#import "AVFoundation/AVAssetWriterInput.h"
#import "AVFoundation/AVAssetReader.h"
#import "Foundation/NSUrl.h"


void VideoSetupTest()
{
    int width = 320;
    int height = 480;

    // Setup the codec settings.
    int nBitsPerSecond = 100000;
    NSDictionary *codecSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithInt: nBitsPerSecond], AVVideoAverageBitRateKey, 
                                   nil];        

    // Create the AVAssetWriterInput.
    NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                    AVVideoCodecH264, AVVideoCodecKey,
                                    codecSettings, AVVideoCompressionPropertiesKey,
                                    [NSNumber numberWithInt: width], AVVideoWidthKey,
                                    [NSNumber numberWithInt: height], AVVideoHeightKey,
                                    nil];

    AVAssetWriterInput *assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType: AVMediaTypeVideo outputSettings: outputSettings];
    [assetWriterInput retain];

    // Create the AVAssetWriterPixelBufferAdaptor.
    NSDictionary *pixelBufferAdaptorAttribs = [NSDictionary dictionaryWithObjectsAndKeys:
                                               [NSNumber numberWithInt: kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey, 
                                               [NSNumber numberWithInt: width], kCVPixelBufferWidthKey,
                                               [NSNumber numberWithInt: height], kCVPixelBufferHeightKey,
                                               nil];
    AVAssetWriterInputPixelBufferAdaptor *pixelBufferAdaptor = [AVAssetWriterInputPixelBufferAdaptor alloc];
    [pixelBufferAdaptor initWithAssetWriterInput: assetWriterInput sourcePixelBufferAttributes: pixelBufferAdaptorAttribs];



    // Figure out a filename.
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
    NSString *documentsDirectory = [paths objectAtIndex:0];
    char szFilename[256];
    sprintf( szFilename, "%s/test.mp4", [documentsDirectory UTF8String] );
    unlink( szFilename );
    printf( "Filename:\n%s\n\n", szFilename );


    // Create the AVAssetWriter.
    NSError *pError;
    NSURL *url = [NSURL fileURLWithPath: [NSString stringWithUTF8String: szFilename]];
    AVAssetWriter *assetWriter = [AVAssetWriter alloc];
    [assetWriter initWithURL:url  fileType:AVFileTypeMPEG4  error:&pError];


    // Bind these things together and start!
    assetWriterInput.expectsMediaDataInRealTime = YES;
    [assetWriter addInput:assetWriterInput];


    //
    // ** NOTE: Here's the call where [assetWriter status] starts returning AVAssetWriterStatusFailed
    //          on an iPod 3rd Gen running iOS 4.2.1.
    //
    [assetWriter startWriting];
}
4

1 回答 1

2

我得出的结论是 iPod Touch 第 3 代无法创建 .mp4 视频,期间。这是有道理的——为什么在一个甚至没有摄像头来捕捉视频的设备上包含视频编码硬件?

让我认为它可以创建 .mp4 视频的原因是我有另一个应用程序可以从我的 iPod Touch 3 上创建它们。但是在分析该应用程序的 .mp4 视频后,它实际上是一个 H.263(MPEG -4 part-2) 使用 ffmpeg 创建的视频。(而 AVFoundation 将创建 H.264 视频,如果它可以在设备上完成的话)。

于 2011-05-07T17:39:49.717 回答