avcodec_send_frame(codec_ctx, output_frame) 返回-22
时间: 2023-09-09 14:13:05 浏览: 385
avcodec_send_frame() 函数返回 -22 可能是由于以下原因之一:
1. 传递给 avcodec_send_frame() 的 AVFrame 结构体无效或未初始化。请确保您正确初始化了 AVFrame,例如使用 av_frame_alloc() 函数为其分配内存。
2. AVCodecContext 结构体中的某些参数未正确设置。请检查您使用的 AVCodecContext 结构体是否正确设置了所有必需的参数,例如编码器 ID、采样率、帧率等。
3. 编码器不能接受传递的帧。请确保传递给 avcodec_send_frame() 的帧与编码器支持的格式和分辨率匹配。
您可以尝试使用 av_strerror() 函数将错误代码转换为可读的错误消息,以便更好地了解问题所在。例如:
```
char error_buf[AV_ERROR_MAX_STRING_SIZE];
av_strerror(ret, error_buf, AV_ERROR_MAX_STRING_SIZE);
printf("Error sending frame: %s\n", error_buf);
```
其中,ret 是 avcodec_send_frame() 函数返回的错误代码。
相关问题
ffmpeg_g -re -i /home/tsdl/Downloads/FPV_2021-0-1_12-37-23.h264 -vcodec libx264 -profile:v baseline -r 25 -g 50 -f rtc "http://192.168.114.34:1985/rtc/v1/whip/?app=live&stream=livestream"将上面命令使用FFmpeg最新的API转化成C语言代码
以下是将给定的FFmpeg命令使用最新的API转换为C语言代码的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
int main(int argc, char *argv[]) {
AVFormatContext *fmt_ctx = NULL;
AVCodecContext *video_dec_ctx = NULL, *video_enc_ctx = NULL;
AVCodec *video_dec = NULL, *video_enc = NULL;
AVStream *video_stream = NULL;
AVOutputFormat *output_fmt = NULL;
AVFormatContext *output_fmt_ctx = NULL;
AVPacket pkt;
int ret, i;
// Initialize the AVFormatContext
if ((ret = avformat_open_input(&fmt_ctx, "/home/tsdl/Downloads/FPV_2021-0-1_12-37-23.h264", NULL, NULL)) < 0) {
fprintf(stderr, "Could not open input file\n");
exit(1);
}
if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
fprintf(stderr, "Could not find stream information\n");
exit(1);
}
// Find the video stream
for (i = 0; i < fmt_ctx->nb_streams; i++) {
if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream = fmt_ctx->streams[i];
break;
}
}
if (!video_stream) {
fprintf(stderr, "Could not find video stream\n");
exit(1);
}
// Initialize the video decoder
video_dec = avcodec_find_decoder(video_stream->codecpar->codec_id);
if (!video_dec) {
fprintf(stderr, "Could not find video decoder\n");
exit(1);
}
video_dec_ctx = avcodec_alloc_context3(video_dec);
if (!video_dec_ctx) {
fprintf(stderr, "Could not allocate video decoder context\n");
exit(1);
}
if ((ret = avcodec_parameters_to_context(video_dec_ctx, video_stream->codecpar)) < 0) {
fprintf(stderr, "Could not initialize video decoder context\n");
exit(1);
}
if ((ret = avcodec_open2(video_dec_ctx, video_dec, NULL)) < 0) {
fprintf(stderr, "Could not open video decoder\n");
exit(1);
}
// Initialize the video encoder
video_enc = avcodec_find_encoder_by_name("libx264");
if (!video_enc) {
fprintf(stderr, "Could not find video encoder\n");
exit(1);
}
video_enc_ctx = avcodec_alloc_context3(video_enc);
if (!video_enc_ctx) {
fprintf(stderr, "Could not allocate video encoder context\n");
exit(1);
}
// Set video encoder options
av_opt_set(video_enc_ctx->priv_data, "profile", "baseline", 0);
av_opt_set(video_enc_ctx->priv_data, "tune", "zerolatency", 0);
video_enc_ctx->width = video_dec_ctx->width;
video_enc_ctx->height = video_dec_ctx->height;
video_enc_ctx->sample_aspect_ratio = video_dec_ctx->sample_aspect_ratio;
video_enc_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
video_enc_ctx->time_base = av_inv_q(video_stream->avg_frame_rate);
video_enc_ctx->gop_size = 50;
video_enc_ctx->framerate = video_stream->avg_frame_rate;
if ((ret = avcodec_open2(video_enc_ctx, video_enc, NULL)) < 0) {
fprintf(stderr, "Could not open video encoder\n");
exit(1);
}
// Initialize the output format context
if ((ret = avformat_alloc_output_context2(&output_fmt_ctx, NULL, "rtc", "http://192.168.114.34:1985/rtc/v1/whip/?app=live&stream=livestream")) < 0) {
fprintf(stderr, "Could not allocate output format context\n");
exit(1);
}
output_fmt = output_fmt_ctx->oformat;
// Add the video stream to the output format context
video_stream = avformat_new_stream(output_fmt_ctx, NULL);
if (!video_stream) {
fprintf(stderr, "Could not create video stream\n");
exit(1);
}
video_stream->codecpar->codec_id = video_enc->id;
video_stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
video_stream->codecpar->width = video_enc_ctx->width;
video_stream->codecpar->height = video_enc_ctx->height;
video_stream->codecpar->format = video_enc_ctx->pix_fmt;
video_stream->codecpar->codec_tag = 0;
avcodec_parameters_to_context(video_stream->codec, video_stream->codecpar);
// Write the output format header
if ((ret = avformat_write_header(output_fmt_ctx, NULL)) < 0) {
fprintf(stderr, "Could not write output format header\n");
exit(1);
}
// Read frames from input, encode and write to output
while (1) {
if ((ret = av_read_frame(fmt_ctx, &pkt)) < 0) {
break;
}
if (pkt.stream_index == video_stream->index) {
av_packet_rescale_ts(&pkt, video_stream->time_base, video_enc_ctx->time_base);
pkt.flags |= AV_PKT_FLAG_KEY;
if ((ret = avcodec_send_packet(video_enc_ctx, &pkt)) < 0) {
fprintf(stderr, "Could not send packet to video encoder\n");
exit(1);
}
while (ret >= 0) {
ret = avcodec_receive_packet(video_enc_ctx, &pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
fprintf(stderr, "Error encoding video frame\n");
exit(1);
}
av_packet_rescale_ts(&pkt, video_enc_ctx->time_base, video_stream->time_base);
pkt.stream_index = video_stream->index;
if ((ret = av_interleaved_write_frame(output_fmt_ctx, &pkt)) < 0) {
fprintf(stderr, "Error writing video frame\n");
exit(1);
}
av_packet_unref(&pkt);
}
}
av_packet_unref(&pkt);
}
// Write the output format trailer
av_write_trailer(output_fmt_ctx);
// Cleanup
avformat_close_input(&fmt_ctx);
avcodec_free_context(&video_dec_ctx);
avcodec_free_context(&video_enc_ctx);
avformat_free_context(output_fmt_ctx);
return 0;
}
```
注意:以上示例代码仅供参考,具体实现可能需要根据您的需求进行修改。同时,您需要添加FFmpeg库的链接,并将FFmpeg头文件和库文件添加到编译路径中。
av_interleaved_write_frame 编码h264, 保存为ts格式
你可以使用FFmpeg库中的avformat和avcodec模块来实现将h264编码的视频保存为ts格式的功能。下面是一个简单的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
int main(int argc, char *argv[]) {
const char *input_file = "input.h264";
const char *output_file = "output.ts";
AVFormatContext *in_ctx = NULL, *out_ctx = NULL;
AVCodec *codec = NULL;
AVCodecContext *codec_ctx = NULL;
AVPacket packet;
int ret = 0;
// 打开输入文件
if ((ret = avformat_open_input(&in_ctx, input_file, NULL, NULL)) < 0) {
fprintf(stderr, "Error opening input file: %s\n", av_err2str(ret));
goto end;
}
// 查找视频流
if ((ret = avformat_find_stream_info(in_ctx, NULL)) < 0) {
fprintf(stderr, "Error finding stream information: %s\n", av_err2str(ret));
goto end;
}
// 打开输出文件
if ((ret = avformat_alloc_output_context2(&out_ctx, NULL, NULL, output_file)) < 0) {
fprintf(stderr, "Error opening output file: %s\n", av_err2str(ret));
goto end;
}
// 添加视频流
codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!codec) {
fprintf(stderr, "H.264 codec not found\n");
goto end;
}
codec_ctx = avcodec_alloc_context3(codec);
codec_ctx->width = in_ctx->streams[0]->codecpar->width;
codec_ctx->height = in_ctx->streams[0]->codecpar->height;
codec_ctx->pix_fmt = in_ctx->streams[0]->codecpar->format;
codec_ctx->time_base = in_ctx->streams[0]->time_base;
if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
fprintf(stderr, "Error opening codec: %s\n", av_err2str(ret));
goto end;
}
AVStream *out_stream = avformat_new_stream(out_ctx, codec);
if (!out_stream) {
fprintf(stderr, "Error creating output stream\n");
goto end;
}
out_stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
out_stream->codecpar->codec_id = codec->id;
out_stream->codecpar->width = codec_ctx->width;
out_stream->codecpar->height = codec_ctx->height;
out_stream->codecpar->format = codec_ctx->pix_fmt;
out_stream->codecpar->bit_rate = codec_ctx->bit_rate;
out_stream->codecpar->extradata = codec_ctx->extradata;
out_stream->codecpar->extradata_size = codec_ctx->extradata_size;
avcodec_parameters_to_context(out_stream->codec, out_stream->codecpar);
// 打开输出文件
if (!(out_ctx->oformat->flags & AVFMT_NOFILE)) {
if ((ret = avio_open(&out_ctx->pb, output_file, AVIO_FLAG_WRITE)) < 0) {
fprintf(stderr, "Error opening output file: %s\n", av_err2str(ret));
goto end;
}
}
// 写文件头
if ((ret = avformat_write_header(out_ctx, NULL)) < 0) {
fprintf(stderr, "Error writing header: %s\n", av_err2str(ret));
goto end;
}
// 读取并编码每一帧
while (1) {
AVStream *in_stream = in_ctx->streams[0];
if ((ret = av_read_frame(in_ctx, &packet)) < 0) {
break;
}
if (packet.stream_index != 0) {
av_packet_unref(&packet);
continue;
}
av_packet_rescale_ts(&packet, in_stream->time_base, codec_ctx->time_base);
if ((ret = avcodec_send_packet(codec_ctx, &packet)) < 0) {
fprintf(stderr, "Error sending packet to encoder: %s\n", av_err2str(ret));
av_packet_unref(&packet);
break;
}
while (1) {
AVFrame *frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Error allocating frame\n");
ret = AVERROR(ENOMEM);
goto end;
}
ret = avcodec_receive_frame(codec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
av_frame_free(&frame);
break;
} else if (ret < 0) {
fprintf(stderr, "Error receiving frame from encoder: %s\n", av_err2str(ret));
av_frame_free(&frame);
goto end;
}
frame->pts = frame->best_effort_timestamp;
AVPacket out_packet;
av_init_packet(&out_packet);
out_packet.data = NULL;
out_packet.size = 0;
if ((ret = avcodec_send_frame(codec_ctx, frame)) < 0) {
fprintf(stderr, "Error sending frame to encoder: %s\n", av_err2str(ret));
av_frame_free(&frame);
goto end;
}
while (1) {
ret = avcodec_receive_packet(codec_ctx, &out_packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
av_packet_unref(&out_packet);
break;
} else if (ret < 0) {
fprintf(stderr, "Error receiving packet from encoder: %s\n", av_err2str(ret));
av_packet_unref(&out_packet);
goto end;
}
out_packet.stream_index = 0;
av_packet_rescale_ts(&out_packet, codec_ctx->time_base, out_stream->time_base);
if ((ret = av_interleaved_write_frame(out_ctx, &out_packet)) < 0) {
fprintf(stderr, "Error writing packet to output file: %s\n", av_err2str(ret));
av_packet_unref(&out_packet);
goto end;
}
av_packet_unref(&out_packet);
}
av_frame_free(&frame);
}
av_packet_unref(&packet);
}
// 写文件尾
if ((ret = av_write_trailer(out_ctx)) < 0) {
fprintf(stderr, "Error writing trailer: %s\n", av_err2str(ret));
goto end;
}
end:
if (codec_ctx) {
avcodec_free_context(&codec_ctx);
}
if (in_ctx) {
avformat_close_input(&in_ctx);
}
if (out_ctx) {
if (!(out_ctx->oformat->flags & AVFMT_NOFILE)) {
avio_closep(&out_ctx->pb);
}
avformat_free_context(out_ctx);
}
return ret;
}
```
需要注意的是,这只是一个简单的示例代码,实际使用时需要根据具体的需求进行修改和完善。
阅读全文