2013-01-09 42 views
16

我試圖從圖像創建視頻文件圖像magick庫。在像不透明度差異一樣地應用一些效果之後,它會成功創建,但Quick time player會給出錯誤" video file could not be opened. The movie's file format isn't recognized "由於電影的文件格式無法識別,視頻文件無法打開

我使用下面的代碼:

double d = 0.00; 

- (void)posterizeImageWithCompression:(id)sender { 

    // Here we use JPEG compression. 
    NSLog(@"we're using JPEG compression"); 

    MagickWandGenesis(); 
    magick_wand = NewMagickWand(); 
    magick_wand = [self magiWandWithImage:[UIImage imageNamed:@"iphone.png"]]; 

    MagickBooleanType status; 

    status = MagickSetImageOpacity(magick_wand, d); 

    if (status == MagickFalse) { 
     ThrowWandException(magick_wand); 
    } 
    if (status == MagickFalse) { 
     ThrowWandException(magick_wand); 
    } 
    size_t my_size; 
    unsigned char * my_image = MagickGetImageBlob(magick_wand, &my_size); 
    NSData * data = [[NSData alloc] initWithBytes:my_image length:my_size]; 
    free(my_image); 
    magick_wand = DestroyMagickWand(magick_wand); 
    MagickWandTerminus(); 
    UIImage * image = [[UIImage alloc] initWithData:data]; 

    d = d + 0.05; 
    if (status == MagickFalse) { 
     ThrowWandException(magick_wand); 
    } 

    NSData *data1; 

    NSArray *paths; 

    NSString *documentsDirectory,*imagePath ; 

    UIImage *image1 = image; 

    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    documentsDirectory = [paths objectAtIndex:0]; 

    imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%f.png",d]]; 

    data1 = UIImagePNGRepresentation(image1); 

    if (d <= 1.0) { 

     [data1 writeToFile:imagePath atomically:YES]; 

     [imageViewButton setImage:image forState:UIControlStateNormal]; 

     // If ready to have more media data 
     if (assetWriterPixelBufferAdaptor.assetWriterInput.readyForMoreMediaData) { 
      CVReturn cvErr = kCVReturnSuccess; 
      // get screenshot image! 
      CGImageRef image1 = (CGImageRef) image.CGImage; 

      // prepare the pixel buffer 
      CVPixelBufferRef pixelsBuffer = NULL; 

      // Lock pixel buffer address 
      CVPixelBufferLockBaseAddress(pixelsBuffer, 0); 

      // pixelsBuffer = [self pixelBufferFromCGImage:image1]; 

      CVPixelBufferUnlockBaseAddress(pixelsBuffer, 0); 

      CFDataRef imageData= CGDataProviderCopyData(CGImageGetDataProvider(image1)); 
      NSLog (@"copied image data"); 
      cvErr = CVPixelBufferCreateWithBytes(kCFAllocatorDefault, 
                FRAME_WIDTH, 
                FRAME_HEIGHT, 
                kCVPixelFormatType_32BGRA, 
                (void*)CFDataGetBytePtr(imageData), 
                CGImageGetBytesPerRow(image1), 
                NULL, 
                NULL, 
                NULL, 
                &pixelsBuffer); 
      NSLog (@"CVPixelBufferCreateWithBytes returned %d", cvErr); 

      // calculate the time 
      CFAbsoluteTime thisFrameWallClockTime = CFAbsoluteTimeGetCurrent(); 
      CFTimeInterval elapsedTime = thisFrameWallClockTime - firstFrameWallClockTime; 
      NSLog (@"elapsedTime: %f", elapsedTime); 
      CMTime presentationTime = CMTimeMake (elapsedTime * TIME_SCALE, TIME_SCALE); 

      // write the sample 
      BOOL appended = [assetWriterPixelBufferAdaptor appendPixelBuffer:pixelsBuffer withPresentationTime:presentationTime]; 

      if (appended) { 
       NSLog (@"appended sample at time %lf", CMTimeGetSeconds(presentationTime)); 
      } else { 
       NSLog (@"failed to append"); 
       [self stopRecording]; 
      } 

      // Release pixel buffer 
      CVPixelBufferRelease(pixelsBuffer); 
      CFRelease(imageData); 
     } 
    } 


} 

也顯示錯誤,如...

VideoToolbox`vt_Copy_32BGRA_2vuyITU601 + 91 and 
VideoToolbox`vtPixelTransferSession_InvokeBlitter + 574 and 
VideoToolbox`VTPixelTransferSessionTransferImage + 14369 and 
VideoToolbox`VTCompressionSessionEncodeFrame + 1077 and 
MediaToolbox`sbp_vtcs_processSampleBuffer + 599 
+0

所以你想從圖像創建視頻? –

+0

您是否嘗試過使用ffmpeg來執行此功能? – Swati

+0

我認爲這可能是因爲presentationTime不正確。我們賦予'TIME_SCALE'的價值以及您期待您最終電影的FPS是什麼。 – uchiha

回答

1

QT的球員是不是非常豐富。通常,玩家將提供缺少編解碼器的名稱。如果你生成的文件在同一臺計算機上以編程方式播放,而你在QT播放中沒有成功,那麼無論出於何種原因,程序庫顯然可以看到的編解碼器(因爲它使用了它)沒有在操作系統中註冊。在shell中,您可以執行「文件」並獲取文件類型以及可能的編解碼器。如果沒有,您應該能夠使用vlc,transcode或gstreamer找到編解碼器,然後按照Apple的說明直接下載並安裝所需的編解碼器到操作系統。

相關問題