weixin_41905118
weixin_41905118
采纳率100%
2019-03-16 11:33 阅读 554

如何将FFmpeg读取frame转化为png?

如何将FFmpeg读取frame转化为png?不写文件,直接在内存中后续使用。我要curl发png图片到http服务器,还有其他方法吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    weixin_41905118 weixin_41905118 2019-03-16 11:53

    继续看文档,png_create_write_struct_2()这个函数是自己分配png处理内存的方法,不是写内存的初始化,正确的接口是:
    png_init_io()初始化读取或写入文件,这是默认方式。
    也可以使用 png_set_read_fn
    () and png_set_write_fn() 来自定义读写方式。

    typedef struct {
        char * data;
        size_t size;
        size_t offset;
    }ImageSource;
    
    //写入数据函数,将所有的数据写入缓冲区
    void PNGAPI png_own_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
    {
        ImageSource* isource = (ImageSource*)png_get_io_ptr(png_ptr);
        if(isource->offset + length <= isource->size)
        {
           memcpy(isource->data + isource->offset,data,  length);
           isource->offset += length;
        }
        else
        {
            png_error(png_ptr,"pngReaderCallback failed");
        }
    }
    static bool png_creat(AVFrame * pFrame,char * png_buf,size_t date_size)
    {
        png_structp png_ptr;
        png_infop info_ptr;
        png_colorp palette;
        int width=pFrame->width,height=pFrame->height;
    
        png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
        if (png_ptr == NULL)
        {
           return false;
        }
    
        /* Allocate/initialize the image information data.  REQUIRED */
        info_ptr = png_create_info_struct(png_ptr);
        if (info_ptr == NULL)
        {
            png_destroy_write_struct(&png_ptr,  NULL);
            return false;
        }
        if (setjmp(png_jmpbuf(png_ptr)))
        {
           /* If we get here, we had a problem writing the file */
           png_destroy_write_struct(&png_ptr, &info_ptr);
           return false;
        }
        ImageSource imgsource;
        imgsource.data = png_buf;
        imgsource.size = date_size;
        imgsource.offset = 0;
        png_set_write_fn(png_ptr,&imgsource,png_own_write_data,NULL);
    
    
        /* 设置png文件的属性 */
        png_set_IHDR(png_ptr, info_ptr, width, height, 8,
                        PNG_COLOR_TYPE_RGB,
                        PNG_INTERLACE_NONE,
                        PNG_COMPRESSION_TYPE_BASE,
                        PNG_FILTER_TYPE_BASE);
    
        /* 分配调色板空间。常数 PNG_MAX_PALETTE_LENGTH 的值是256 */
        palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH * sizeof(png_color));
    
        png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH);
    
           /* Write the file header information.  REQUIRED */
        png_write_info(png_ptr, info_ptr);
    
        /* The easiest way to write the image (you may have a different memory
        * layout, however, so choose what fits your needs best).  You need to
        * use the first method if you aren't handling interlacing yourself.
        */
        png_uint_32 k;
        png_byte *image;
        png_bytep row_pointers[height];
    
        image = pFrame->data[0];
    
        if (height > PNG_UINT_32_MAX/sizeof(png_bytep))
            png_error (png_ptr, "Image is too tall to process in memory");
    
        for (k = 0; k < height; k++)
            row_pointers[k] = image + k*width*3;
    
        /* One of the following output methods is REQUIRED */
        png_write_image(png_ptr, row_pointers);
    
        //end,进行必要的扫尾工作:
        png_write_end(png_ptr, info_ptr);
        png_free(png_ptr, palette);
        png_destroy_write_struct(&png_ptr, &info_ptr);
    
    }
    
    点赞 评论 复制链接分享

相关推荐