【发布时间】:2017-08-15 07:24:39
【问题描述】:
我的内存中已经有一系列图像。图像是 RGB 的,但是是平面的,即非交错的。我希望使用 ffmpeg 将这些无损压缩成视频。
目前我有这个(注意:我已经排除了错误检查和清理):
av_register_all();
AVCodec* codec = avcodec_find_encoder_by_name("libx264rgb");
AVCodecContext* context = avcodec_alloc_context3(codec);
context->width = width;
context->height = paddedHeight;//height padded to a multiple of 8
context->time_base = AVRational { 1,25 };
context->gop_size = 10;
context->max_b_frames = 1;
context->pix_fmt = AV_PIX_FMT_RGB24;
av_opt_set(context->priv_data, "preset", "ultrafast", 0);
av_opt_set(context->priv_data, "crf", 0, 0);//lossless
avcodec_open2(context, codec, NULL);
AVFrame* avFrame = av_frame_alloc();
for (int i = 0; i < AV_NUM_DATA_POINTERS; i++) {
avFrame->data[i] = NULL;
}
avFrame->data[0] = new uint8_t[width*paddedHeight];
avFrame->data[1] = new uint8_t[width*paddedHeight];
avFrame->data[2] = new uint8_t[width*paddedHeight];
AVFormatContext* outputContext;
avformat_alloc_output_context2(&outputContext, NULL, NULL, "filename.webm");
AVStream* outputStream = avformat_new_stream(outputContext, codec);
outputStream->codecpar->width = width;
outputStream->codecpar->height = paddedHeight;
outputStream->codecpar->format = AV_PIX_FMT_GBRP;
outputStream->time_base = AVRational { 1,25 };
outputContext->video_codec = codec;
avio_open2(&outputContext->pb, "filepath\filename.webm", AVIO_FLAG_WRITE, NULL, NULL);//obviously a real filepath is being used
avformat_write_header(outputContext, NULL);
uint8_t* R, G, B;
while(GetNextImageChannels(&R, &G, &B)){
memcpy(R, avFrame->data[0], width * height * sizeof(uint8_t));
memcpy(G, avFrame->data[1], width * height * sizeof(uint8_t));
memcpy(B, avFrame->data[2], width * height * sizeof(uint8_t));
avcodec_send_frame(context, avFrame);
AVPacket encodedPacket;
avcodec_receive_packet(context, &encodedPacket);
av_interleaved_write_frame(outputContext, &encodedPacket);
}
av_write_trailer(outputContext);
目前在 avformat_write_header 失败,给我一个错误代码 -22,我认为这意味着某种无效参数?
我尝试使用 filename.mkv,但在 avformat_alloc_output_context2 失败,错误代码为 -22
我尝试使用AV_PIX_FMT_GBRP 格式,但在avcodec_open2 失败,错误代码也为-22。
我在 ffmpeg 上找到了各种资源,但其中大多数都使用命令行应用程序。少数不是很通用,而且大多已经过时(使用不推荐使用的函数),并且通常从一种视频格式转换为另一种视频格式,而且它们都不是处理平面图像。
如果你能帮我解决我的问题吗?
编辑:修正了数组索引中的错字
【问题讨论】:
标签: c++ image video ffmpeg video-encoding