Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Video 为什么我可以在AVCaptureSession上处理灰度效果,而不能使用AvasseTrader?_Video_Opengl Es_Avcapturesession_Avassetreader - Fatal编程技术网

Video 为什么我可以在AVCaptureSession上处理灰度效果,而不能使用AvasseTrader?

Video 为什么我可以在AVCaptureSession上处理灰度效果,而不能使用AvasseTrader?,video,opengl-es,avcapturesession,avassetreader,Video,Opengl Es,Avcapturesession,Avassetreader,我正在开发一款iPhone应用程序。我想对库中的视频应用一些过滤器。经过研究,我开始写这篇文章和他的色彩追踪来源。 从这段代码中,我可以使用AVCaptureSession实时应用灰度过滤器。正确的。但我想对图书馆的一段视频也这么做。所以我使用Avassetrader来读取源视频。与AVCaptureSession一样,我可以获得CVImageBufferRef。它的宽度、高度和数据大小与AVCaptureSession相同,但我的openGL视图始终为黑色 要捕获帧的源代码: - (v

我正在开发一款iPhone应用程序。我想对库中的视频应用一些过滤器。经过研究,我开始写这篇文章和他的色彩追踪来源。 从这段代码中,我可以使用AVCaptureSession实时应用灰度过滤器。正确的。但我想对图书馆的一段视频也这么做。所以我使用Avassetrader来读取源视频。与AVCaptureSession一样,我可以获得CVImageBufferRef。它的宽度、高度和数据大小与AVCaptureSession相同,但我的openGL视图始终为黑色

要捕获帧的源代码:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {


    [self dismissModalViewControllerAnimated:NO];


    /// incoming video
    NSURL *videoURL = [info valueForKey:UIImagePickerControllerMediaURL];
    NSLog(@"Video : %@", videoURL);

    // AVURLAsset to read input movie (i.e. mov recorded to local storage)
    NSDictionary *inputOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
    AVURLAsset *inputAsset = [[AVURLAsset alloc] initWithURL:videoURL options:inputOptions];

    // Load the input asset tracks information
    [inputAsset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler: ^{


        NSError *error = nil;

        // Check status of "tracks", make sure they were loaded    
        AVKeyValueStatus tracksStatus = [inputAsset statusOfValueForKey:@"tracks" error:&error];
        if (!tracksStatus == AVKeyValueStatusLoaded)
            // failed to load
            return;




        /* Read video samples from input asset video track */
        AVAssetReader *reader = [AVAssetReader assetReaderWithAsset:inputAsset error:&error];

        NSMutableDictionary *outputSettings = [NSMutableDictionary dictionary];
        [outputSettings setObject: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]  forKey: (NSString*)kCVPixelBufferPixelFormatTypeKey];
        AVAssetReaderTrackOutput *readerVideoTrackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[[inputAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] outputSettings:outputSettings];


        // Assign the tracks to the reader and start to read
        [reader addOutput:readerVideoTrackOutput];
        if ([reader startReading] == NO) {
            // Handle error
            NSLog(@"Error reading");
        }

        NSAutoreleasePool *pool = [NSAutoreleasePool new];
        while (reader.status == AVAssetReaderStatusReading) {

            CMSampleBufferRef sampleBufferRef = [readerVideoTrackOutput copyNextSampleBuffer];
            if (sampleBufferRef) {
                CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBufferRef);
                [self processNewCameraFrame:pixelBuffer];

                CMSampleBufferInvalidate(sampleBufferRef);
                CFRelease(sampleBufferRef);
            }
        }
        [pool release];

        NSLog(@"Finished");
    }];
}
以下是处理帧的代码:

- (void)processNewCameraFrame:(CVImageBufferRef)cameraFrame {


    CVPixelBufferLockBaseAddress(cameraFrame, 0);
    int bufferHeight = CVPixelBufferGetHeight(cameraFrame);
    int bufferWidth = CVPixelBufferGetWidth(cameraFrame);

    NSLog(@"Size : %i %i %zu", bufferWidth, bufferHeight, CVPixelBufferGetDataSize(cameraFrame));


    // Create a new texture from the camera frame data, display that using the shaders
    glGenTextures(1, &videoFrameTexture);
    glBindTexture(GL_TEXTURE_2D, videoFrameTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    // This is necessary for non-power-of-two textures
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    // Using BGRA extension to pull in video frame data directly
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(cameraFrame));

    GLenum err = glGetError();
    if (err != GL_NO_ERROR)
        NSLog(@"Error uploading texture. glError: 0x%04X", err);

    [self drawFrame];

    glDeleteTextures(1, &videoFrameTexture);

    CVPixelBufferUnlockBaseAddress(cameraFrame, 0);
}
对于OpenGL视图中的绘图框:

- (void)drawFrame {    
    // Replace the implementation of this method to do your own custom drawing.
    static const GLfloat squareVertices[] = {
        -1.0f, -1.0f,
        1.0f, -1.0f,
        -1.0f,  1.0f,
        1.0f,  1.0f,
    };

    static const GLfloat textureVertices[] = {
        1.0f, 1.0f,
        1.0f, 0.0f,
        0.0f,  1.0f,
        0.0f,  0.0f,
    };

    [glView setDisplayFramebuffer];
    glUseProgram(grayScaleProgram);         

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, videoFrameTexture);

    // Update uniform values
    glUniform1i(uniforms[UNIFORM_VIDEOFRAME], 0);   

    // Update attribute values.
    glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
    glEnableVertexAttribArray(ATTRIB_VERTEX);
    glVertexAttribPointer(ATTRIB_TEXTUREPOSITON, 2, GL_FLOAT, 0, 0, textureVertices);
    glEnableVertexAttribArray(ATTRIB_TEXTUREPOSITON);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);



    [glView presentFramebuffer];
}
我不添加代码,但如果需要帮助,我可以。。。你有办法帮我吗


谢谢

您似乎没有将您的相机相框与videoFrameTexture链接
CVImageBufferRef图像

glBindTexture(CVOpenGLTextureGetTarget(图像)、CVOpenGLTextureGetName(图像))

当你说灰度过滤器时,你的意思是说你使用的是
kCVPixelFormatType\u 420ypcbcr8biplanarvideogrange
视频帧吗?如果是这样,那么我假设您将纹理绑定为
GL\u LUMINANCE
。但是,在
avassetrader
中,使用
kCVPixelFormatType_32BGRA
像素格式,在处理方法中,将纹理绑定为
GL_RGBA

我曾经偶然发现视频捕获像素格式和纹理绑定的错误组合,最终导致了黑屏。检查
AVCaptureSession
设置代码中的
AVCaptureVideoDataOutput
设置。像素格式和纹理绑定应相同

编辑: 黑屏的另一个可能原因是OpenGL上下文没有在线程之间共享。如果您对摄影机视频帧使用的队列不是主队列
dispatch\u get\u main\u queue()
,则表示该方法

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
       fromConnection:(AVCaptureConnection *)connection  
在后台线程上调用,您无法从中更新UI

您可以尝试使用主调度队列设置捕获会话,看看会发生什么

AVCaptureVideoDataOutput *videoOut = [[AVCaptureVideoDataOutput alloc] init];
// set video settings, frame rate .. etc
[videoOut setSampleBufferDelegate:self queue:dispatch_get_main_queue()];