Domanda

La mia app crea un file mp4. Ho verificato che il codice in mio possesso funzioni sui seguenti dispositivi:

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

.. ma l'inizializzazione non riesce sul mio iPod Touch di terza generazione con OS 4.2.1.

È correlato a un'altra domanda qui, ma la vedo su un iOS diverso dispositivo di quanto non fosse e ho incluso il mio codice init qui. Come l'altra domanda, ho provato diversi formati di pixel e bitrate, ma lo stato di AVAssetWriter cambia sempre in AVAssetWriterStatusFailed dopo aver chiamato la sua funzione startWriting.

Qualsiasi idea sarebbe molto apprezzata. So che la creazione di mp4 è possibile su questo dispositivo perché ho scaricato un'altra app che funziona perfettamente sullo stesso dispositivo su cui il mio codice non funziona.

Ecco il codice minimo per eseguire la configurazione del video.

#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];
}
È stato utile?

Soluzione

Sono giunto alla conclusione che l'iPod Touch di terza generazione non può creare video .mp4, punto.Ciò avrebbe senso: perché includere hardware di codifica video su un dispositivo che non dispone nemmeno di una fotocamera con cui acquisire video?

La cosa che mi faceva pensare che potesse creare video .mp4 era che avevo un'altra app in grado di crearli dal mio iPod Touch 3. Ma dopo aver analizzato il video .mp4 di quell'app, in realtà era un video H.263 (MPEG-4 part-2) creato con ffmpeg.(Mentre AVFoundation creerà video H.264 se può farlo su un dispositivo).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top