iPod Touch 3rd Gen / OS 4.2.1上的AVAssetWriter startWriting失败

时间:2011-04-30 07:21:03

标签: ios4 avfoundation avassetwriter

我的应用会创建一个mp4文件。我已经验证了我在以下设备上运行的代码:

  • iPad(OS 4.3.2)
  • iPhone4(OS 4.2.1)
  • iPhone 3GS(OS 4.2.1)

..但运行OS 4.2.1的iPod Touch 3rd Gen上的init失败。

这与此处的another question有关,但我在不同的iOS设备上看到它并且我在此处包含了我的初始化代码。和其他问题一样,我尝试过不同的像素格式和比特率,但在调用startWriting函数后,AVAssetWriter的状态总是变为 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];
}

1 个答案:

答案 0 :(得分:2)

我得出结论,iPod Touch第三代无法制作.mp4视频,期间。这是有道理的 - 为什么在一个甚至没有相机来捕捉视频的设备上包含视频编码硬件?

让我觉得可能创建.mp4视频的事情就是我有另一个应用程序能够从我的iPod Touch 3创建它们。但是在分析该应用程序的.mp4视频时,它实际上是用ffmpeg创建的H.263(MPEG-4 part-2)视频。 (如果AVFoundation可以在设备上完成它,那么它将创建H.264视频。)