AVAssetWriter startWriting ล้มเหลวบน iPod Touch 3rd Gen / OS 4.2.1

แอพของฉันสร้างไฟล์ mp4 ฉันตรวจสอบแล้วว่ารหัสที่ฉันใช้งานได้บนอุปกรณ์ต่อไปนี้:

  • ไอแพด (ระบบปฏิบัติการ 4.3.2)
  • ไอโฟน 4 (ระบบปฏิบัติการ 4.2.1)
  • ไอโฟน 3GS (ระบบปฏิบัติการ 4.2.1)

.. แต่ init ล้มเหลวใน iPod Touch 3rd Gen ของฉันที่ใช้ OS 4.2.1

สิ่งนี้เกี่ยวข้องกับคำถามอื่นที่นี่ แต่ฉันเห็นมันบนอุปกรณ์ iOS ที่แตกต่างจากเขา เป็นและฉันได้รวมรหัสเริ่มต้นของฉันไว้ที่นี่ เช่นเดียวกับคำถามอื่นๆ ฉันได้ลองใช้รูปแบบพิกเซลและบิตเรตที่แตกต่างกัน แต่สถานะของ AVAssetWriter จะเปลี่ยนเป็น AVAssetWriterStatusFailed เสมอหลังจากเรียกใช้ฟังก์ชัน startWriting

ความคิดใด ๆ ที่จะได้รับการชื่นชมอย่างมาก. ฉันรู้ว่าการสร้าง 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];
}

person Mike    schedule 30.04.2011    source แหล่งที่มา
comment
นี่คือการทดสอบเพิ่มเติมที่ฉันได้ทำ: ใช้ .mp4 แล้วลองสร้าง AVAssetExportSession สำหรับมัน จากนั้นดูว่า [AVAssetExportSession รองรับ FileTypes] ส่งคืนอะไร ผลลัพธ์: บน iPhone4 และ iPad ฉันสามารถสร้างเซสชันและรองรับไฟล์ com.apple.quicktime-movie ใน iPod Touch 3rd Gen ฉันไม่สามารถสร้าง AVAssetExportSession ได้ [AVAssetExportSession ExportSessionWithAsset] เพิ่งคืนค่า NULL โปรดทราบว่า จะ โหลดไฟล์ .mp4 ลงใน AVAsset อย่างถูกต้องบน iPodT3   -  person Mike    schedule 06.05.2011
comment
การทดสอบอื่น: ดูว่า iPodT3 สามารถเล่นวิดีโอ .mp4 เหล่านี้ได้หรือไม่ ฉันส่งอีเมลถึงตัวเอง (ผ่าน gmail) หนึ่งในไฟล์ .mp4 ที่ iPhone4 ของฉันสร้างและอ่านอีเมลนั้นบน iPodT3 ฉันสามารถเล่นไฟล์ .mp4 ได้ดี   -  person Mike    schedule 06.05.2011
comment
การทดสอบอื่น: ลองใช้ AVAssetWriterInput เพื่อสร้างไฟล์ .wav (ด้วย LinearPCM 16, โมโน, 11025hz) หรือ .m4a (kAudioFormatMPEG4AAC) ฉันสามารถสร้างทั้งสองสิ่งนี้ได้ดีบน iPodT3 ดังนั้นฉันจึงสามารถสร้างไฟล์เสียง mpeg4 ได้ แต่ฉันไม่สามารถสร้างไฟล์วิดีโอ (ใดๆ ก็ได้)   -  person Mike    schedule 06.05.2011


คำตอบ (1)


ฉันได้ข้อสรุปว่า iPod Touch 3rd Gen ไม่สามารถสร้างวิดีโอ .mp4 ได้ นี่คงจะสมเหตุสมผล - เหตุใดจึงรวมฮาร์ดแวร์การเข้ารหัสวิดีโอไว้ในอุปกรณ์ที่ไม่มีกล้องสำหรับถ่ายวิดีโอด้วยซ้ำ

สิ่งที่ทำให้ฉันคิดว่า สามารถ สร้างวิดีโอ .mp4 ได้ก็คือ ฉันมีแอปอื่นที่สามารถสร้างวิดีโอเหล่านั้นจาก iPod Touch 3 ของฉันได้ แต่เมื่อวิเคราะห์วิดีโอ .mp4 ของแอปนั้นแล้ว จริงๆ แล้วเป็นวิดีโอ H.263 (MPEG-4 ตอนที่ 2) ที่สร้างด้วย ffmpeg (ในขณะที่ AVFoundation จะสร้างวิดีโอ H.264 หากสามารถทำได้บนอุปกรณ์)

person Mike    schedule 07.05.2011
comment
ฉันพบปัญหาเดียวกันนี้ใน iPod Touch 2nd Gen ที่ใช้ OS 4.2.1 โดยพยายามสร้างภาพยนตร์ QuickTime คุณยังคิดว่าพวกเขาไม่สามารถสร้างวิดีโอได้เนื่องจากขาดฮาร์ดแวร์การเข้ารหัสใช่หรือไม่ - person user258279; 07.01.2012