2011-04-30 87 views
1

我的應用程序創建一個mp4文件。我驗證過的代碼,我對以下設備的工作原理:AVAssetWriter startWriting在iPod Touch第三代操作系統4.2.1上失敗

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

..但我的iPod Touch第三代運行OS 4.2.1的init失敗。

這與這裏的another question有關,但我在另一個iOS設備上看到了它,而我在這裏包含了我的init代碼。與其他問題一樣,我嘗試過不同的像素格式以及比特率,但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]; 
} 
+0

**下面是我運行的一個額外的測試:**以.mp4並嘗試爲它創建一個AVAssetExportSession,然後查看_ [AVAssetExportSession supportedFileTypes] _返回的內容。結果:在** iPhone4 **和** iPad **上,我可以創建會話並支持com.apple.quicktime-電影文件。在** iPod Touch第3代**上,我甚至無法創建AVAssetExportSession。 _ [AVAssetExportSession exportSessionWithAsset] _只返回NULL。請注意,它_does_將.mp4文件正確加載到iPodT3上的AVAsset中。 – Mike 2011-05-05 22:53:01

+0

**另一個測試:**看看iPodT3是否可以播放這些.mp4視頻。我通過電子郵件(通過gmail)發送了我的iPhone4創建的.mp4文件之一,並在iPodT3上閱讀該電子郵件。我能夠播放.mp4文件就好了。 – Mike 2011-05-05 22:53:43

+0

**另一個測試:**嘗試使用AVAssetWriterInput創建一個.wav文件(使用LinearPCM 16,單聲道,11025hz)或.m4a(kAudioFormatMPEG4AAC)。我可以在iPodT3上創建這兩個。所以我可以用它來創建mpeg4音頻文件,但我無法創建(任何)視頻文件。 – Mike 2011-05-05 22:57:00

回答

2

我得出的結論是,iPod Touch第三代無法創建.mp4視頻,句號。這是有道理的 - 爲什麼包含視頻編碼硬件的設備甚至沒有相機捕捉視頻?

這讓我覺得它可以可能創建.mp4視頻的事情是,我有另一個應用程序,它能夠從我的iPod Touch 3創建它們。但是在分析該應用程序的.mp4視頻時,它實際上是用ffmpeg創建的H.263(MPEG-4 part-2)視頻。 (而AVFoundation將創建H.264視頻,如果它可以在設備上完成的話)。

+0

我在我的iPod Touch 2nd Gen上運行OS 4.2.1時遇到了同樣的問題,試圖創建一個QuickTime電影。你還認爲他們不能製作視頻,因爲他們缺乏編碼硬件? – user258279 2012-01-07 03:21:41

相關問題