FFmpeg 两个图片合成一张图片的完整C++代码
时间: 2023-03-29 18:02:56 浏览: 180
以下是合成两张图片的完整 C 代码,使用了 FFmpeg 库:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <libavutil/imgutils.h>
#include <libavutil/samplefmt.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#define WIDTH 640
#define HEIGHT 480
int main(int argc, char **argv)
{
AVFormatContext *fmt_ctx = NULL;
AVCodecContext *codec_ctx = NULL;
AVCodec *codec = NULL;
AVFrame *frame = NULL, *frame2 = NULL, *frame_out = NULL;
AVPacket pkt;
int ret, i, j, k;
uint8_t *buffer = NULL;
int buffer_size;
int video_stream_index = -1;
int frame_count = ;
int64_t start_time = ;
struct SwsContext *sws_ctx = NULL;
if (argc < 3) {
fprintf(stderr, "Usage: %s input1 input2 output\n", argv[]);
exit(1);
}
av_register_all();
if ((ret = avformat_open_input(&fmt_ctx, argv[1], NULL, NULL)) < ) {
fprintf(stderr, "Could not open input file '%s'\n", argv[1]);
goto end;
}
if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < ) {
fprintf(stderr, "Could not find stream information\n");
goto end;
}
av_dump_format(fmt_ctx, , argv[1], );
for (i = ; i < fmt_ctx->nb_streams; i++) {
if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream_index = i;
break;
}
}
if (video_stream_index == -1) {
fprintf(stderr, "Could not find video stream\n");
goto end;
}
codec = avcodec_find_decoder(fmt_ctx->streams[video_stream_index]->codecpar->codec_id);
if (!codec) {
fprintf(stderr, "Codec not found\n");
goto end;
}
codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx) {
fprintf(stderr, "Could not allocate codec context\n");
goto end;
}
if ((ret = avcodec_parameters_to_context(codec_ctx, fmt_ctx->streams[video_stream_index]->codecpar)) < ) {
fprintf(stderr, "Could not copy codec parameters to codec context\n");
goto end;
}
if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < ) {
fprintf(stderr, "Could not open codec\n");
goto end;
}
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Could not allocate frame\n");
goto end;
}
frame2 = av_frame_alloc();
if (!frame2) {
fprintf(stderr, "Could not allocate frame\n");
goto end;
}
frame_out = av_frame_alloc();
if (!frame_out) {
fprintf(stderr, "Could not allocate frame\n");
goto end;
}
buffer_size = av_image_get_buffer_size(codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1);
buffer = av_malloc(buffer_size);
if (!buffer) {
fprintf(stderr, "Could not allocate buffer\n");
goto end;
}
av_image_fill_arrays(frame->data, frame->linesize, buffer, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1);
sws_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL);
if (!sws_ctx) {
fprintf(stderr, "Could not initialize sws context\n");
goto end;
}
if ((ret = avformat_open_input(&fmt_ctx, argv[2], NULL, NULL)) < ) {
fprintf(stderr, "Could not open input file '%s'\n", argv[2]);
goto end;
}
if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < ) {
fprintf(stderr, "Could not find stream information\n");
goto end;
}
av_dump_format(fmt_ctx, , argv[2], );
for (i = ; i < fmt_ctx->nb_streams; i++) {
if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream_index = i;
break;
}
}
if (video_stream_index == -1) {
fprintf(stderr, "Could not find video stream\n");
goto end;
}
codec = avcodec_find_decoder(fmt_ctx->streams[video_stream_index]->codecpar->codec_id);
if (!codec) {
fprintf(stderr, "Codec not found\n");
goto end;
}
codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx) {
fprintf(stderr, "Could not allocate codec context\n");
goto end;
}
if ((ret = avcodec_parameters_to_context(codec_ctx, fmt_ctx->streams[video_stream_index]->codecpar)) < ) {
fprintf(stderr, "Could not copy codec parameters to codec context\n");
goto end;
}
if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < ) {
fprintf(stderr, "Could not open codec\n");
goto end;
}
buffer_size = av_image_get_buffer_size(codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1);
buffer = av_malloc(buffer_size);
if (!buffer) {
fprintf(stderr, "Could not allocate buffer\n");
goto end;
}
av_image_fill_arrays(frame2->data, frame2->linesize, buffer, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1);
sws_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL);
if (!sws_ctx) {
fprintf(stderr, "Could not initialize sws context\n");
goto end;
}
if ((ret = avformat_alloc_output_context2(&fmt_ctx, NULL, NULL, argv[3])) < ) {
fprintf(stderr, "Could not create output context\n");
goto end;
}
codec = avcodec_find_encoder(fmt_ctx->oformat->video_codec);
if (!codec) {
fprintf(stderr, "Codec not found\n");
goto end;
}
codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx) {
fprintf(stderr, "Could not allocate codec context\n");
goto end;
}
codec_ctx->width = WIDTH;
codec_ctx->height = HEIGHT;
codec_ctx->time_base = (AVRational){1, 25};
codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < ) {
fprintf(stderr, "Could not open codec\n");
goto end;
}
AVStream *stream = avformat_new_stream(fmt_ctx, codec);
if (!stream) {
fprintf(stderr, "Could not allocate stream\n");
goto end;
}
if ((ret = avcodec_parameters_from_context(stream->codecpar, codec_ctx)) < ) {
fprintf(stderr, "Could not copy codec parameters to stream\n");
goto end;
}
if ((ret = avio_open(&fmt_ctx->pb, argv[3], AVIO_FLAG_WRITE)) < ) {
fprintf(stderr, "Could not open output file '%s'\n", argv[3]);
goto end;
}
if ((ret = avformat_write_header(fmt_ctx, NULL)) < ) {
fprintf(stderr, "Could not write header\n");
goto end;
}
while (1) {
if ((ret = av_read_frame(fmt_ctx, &pkt)) < )
break;
if (pkt.stream_index == video_stream_index) {
ret = avcodec_send_packet(codec_ctx, &pkt);
if (ret < ) {
fprintf(stderr, "Error sending packet to decoder\n");
goto end;
}
while (ret >= ) {
ret = avcodec_receive_frame(codec_ctx, frame_out);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
else if (ret < ) {
fprintf(stderr, "Error receiving frame from decoder\n");
goto end;
}
if (frame_count == ) {
av_image_fill_arrays(frame->data, frame->linesize, pkt.data, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1);
} else if (frame_count == 1) {
av_image_fill_arrays(frame2->data, frame2->linesize, pkt.data, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1);
}
frame_count++;
if (frame_count == 2) {
frame_count = ;
av_image_fill_arrays(frame_out->data, frame_out->linesize, buffer, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1);
sws_scale(sws_ctx, (const uint8_t *const *)frame->data, frame->linesize, , codec_ctx->height, frame_out->data, frame_out->linesize);
sws_scale(sws_ctx, (const uint8_t *const *)frame2->data, frame2->linesize, , codec_ctx->height, frame_out->data, frame_out->linesize);
frame_out->pts = av_rescale_q(frame_out->pts, fmt_ctx->streams[video_stream_index]->time_base, stream->time_base);
frame_out->pkt_dts = av_rescale_q(frame_out->pkt_dts, fmt_ctx->streams[video_stream_index]->time_base, stream->time_base);
frame_out->pkt_duration = av_rescale_q(frame_out->pkt_duration, fmt_ctx->streams[video_stream_index]->time_base, stream->time_base);
ret = avcodec_send_frame(codec_ctx, frame_out);
if (ret < ) {
fprintf(stderr, "Error sending frame to encoder\n");
goto end;
}
while (ret >= ) {
ret = avcodec_receive_packet(codec_ctx, &pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
else if (ret < ) {
fprintf(stderr, "Error receiving packet from encoder\n");
goto end;
}
pkt.stream_index = stream->index;
av_packet_rescale_ts(&pkt, codec_ctx->time_base, stream->time_base);
ret = av_interleaved_write_frame(fmt_ctx, &pkt);
if (ret < ) {
fprintf(stderr, "Error writing packet to output file\n");
goto end;
}
av_packet_unref(&pkt);
}
}
}
}
av_packet_unref(&pkt);
}
ret = av_write_trailer(fmt_ctx);
if (ret < ) {
fprintf(stderr, "Error writing trailer\n");
goto end;
}
end:
avformat_close_input(&fmt_ctx);
avcodec_free_context(&codec_ctx);
av_frame_free(&frame);
av_frame_free(&frame2);
av_frame_free(&frame_out);
av_free(buffer);
sws_freeContext(sws_ctx);
if (ret < ) {
fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
return 1;
}
return ;
}
阅读全文