
Recherche avancée
Médias (1)
-
La conservation du net art au musée. Les stratégies à l’œuvre
26 mai 2011
Mis à jour : Juillet 2013
Langue : français
Type : Texte
Autres articles (52)
-
Gestion générale des documents
13 mai 2011, parMédiaSPIP ne modifie jamais le document original mis en ligne.
Pour chaque document mis en ligne il effectue deux opérations successives : la création d’une version supplémentaire qui peut être facilement consultée en ligne tout en laissant l’original téléchargeable dans le cas où le document original ne peut être lu dans un navigateur Internet ; la récupération des métadonnées du document original pour illustrer textuellement le fichier ;
Les tableaux ci-dessous expliquent ce que peut faire MédiaSPIP (...) -
Des sites réalisés avec MediaSPIP
2 mai 2011, parCette page présente quelques-uns des sites fonctionnant sous MediaSPIP.
Vous pouvez bien entendu ajouter le votre grâce au formulaire en bas de page. -
HTML5 audio and video support
13 avril 2011, parMediaSPIP uses HTML5 video and audio tags to play multimedia files, taking advantage of the latest W3C innovations supported by modern browsers.
The MediaSPIP player used has been created specifically for MediaSPIP and can be easily adapted to fit in with a specific theme.
For older browsers the Flowplayer flash fallback is used.
MediaSPIP allows for media playback on major mobile platforms with the above (...)
Sur d’autres sites (6122)
-
simple ffmpeg API example for converting multiple input images into 1 image using tile filter
9 août 2014, par Pragnesh Patelhere is my code.....................
AVFilterContext **inputContexts;
AVFilterContext *outputContext;
AVFilterGraph *graph;
int initFilters(AVFrame *bgFrame, int inputCount, AVCodecContext **codecContexts, char *filters)
{
int i;
int returnCode;
char args[512];
char name[9];
AVFilterInOut **graphInputs = NULL;
AVFilterInOut *graphOutput = NULL;
AVFilter *bufferSrc = avfilter_get_by_name("buffer");
AVFilter *bufferSink = avfilter_get_by_name("buffersink");
graph = avfilter_graph_alloc();
if(graph == NULL)
return -1;
//allocate inputs
graphInputs = av_calloc(inputCount + 1, sizeof(AVFilterInOut *));
for(i = 0; i <= inputCount; i++)
{
graphInputs[i] = avfilter_inout_alloc();
if(graphInputs[i] == NULL)
return -1;
}
//allocate input contexts
inputContexts = av_calloc(inputCount + 1, sizeof(AVFilterContext *));
//first is the background
snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_base=1/1:pixel_aspect=0", bgFrame->width, bgFrame->height, bgFrame->format);
returnCode = avfilter_graph_create_filter(&inputContexts[0], bufferSrc, "background", args, NULL, graph);
if(returnCode < 0)
return returnCode;
graphInputs[0]->filter_ctx = inputContexts[0];
graphInputs[0]->name = av_strdup("background");
graphInputs[0]->next = graphInputs[1];
//allocate the rest
for(i = 1; i <= inputCount; i++)
{
AVCodecContext *codecCtx = codecContexts[i - 1];
snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
codecCtx->width, codecCtx->height, codecCtx->pix_fmt,
codecCtx->time_base.num, codecCtx->time_base.den,
codecCtx->sample_aspect_ratio.num, codecCtx->sample_aspect_ratio.den);
snprintf(name, sizeof(name), "video_%d", i);
returnCode = avfilter_graph_create_filter(&inputContexts[i], bufferSrc, name, args, NULL, graph);
if(returnCode < 0)
return returnCode;
graphInputs[i]->filter_ctx = inputContexts[i];
graphInputs[i]->name = av_strdup(name);
graphInputs[i]->pad_idx = 0;
if(i < inputCount)
{
graphInputs[i]->next = graphInputs[i + 1];
}
else
{
graphInputs[i]->next = NULL;
}
}
//allocate outputs
graphOutput = avfilter_inout_alloc();
returnCode = avfilter_graph_create_filter(&outputContext, bufferSink, "out", NULL, NULL, graph);
if(returnCode < 0)
return returnCode;
graphOutput->filter_ctx = outputContext;
graphOutput->name = av_strdup("out");
graphOutput->next = NULL;
graphOutput->pad_idx = 0;
returnCode = avfilter_graph_parse_ptr(graph, filters, graphInputs, &graphOutput, NULL);
if(returnCode < 0)
return returnCode;
returnCode = avfilter_graph_config(graph, NULL);
return returnCode;
return 0;
}The filters argument of the function is passed on to avfilter_graph_parse_ptr and it can looks like this : [background] scale=512x512 [base] ; [video_1] scale=256x256 [tmp_1] ; [base][tmp_1] tile=1x2 [out]
The call breaks after the call to avfilter_graph_config with the warning : Output pad "default" with type video of the filter instance "background" of buffer not connected to any destination and the error Invalid argument.
please help me
thanks in advance.................
-
closed (H264 track 1 is not valid : sprop-parameter-sets is missing (96 packetization-mode=1)
8 janvier 2024, par MMingYI used FFmpeg6.1 to stream RTSP, but I received the following errors on the server :




closed (H264 track 1 is not valid : sprop-parameter-sets is missing (96 packetization-mode=1),client:Error occurred when opening output Server returned 400 Bad Request.




#include 
#include 
#include 

#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include <libavutil></libavutil>opt.h>
#include <libavutil></libavutil>imgutils.h>
#include <libavutil></libavutil>time.h>

static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
 AVFormatContext *outFormatCtx) {
 int ret;

 /* send the frame to the encoder */
 if (frame)
 printf("Send frame %3"PRId64"\n", frame->pts);

 ret = avcodec_send_frame(enc_ctx, frame);
 if (ret < 0) {
 char errbuf[AV_ERROR_MAX_STRING_SIZE];
 av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);
 fprintf(stderr, "Error sending a frame for encoding ,%s\n", errbuf);
 exit(1);
 }

 while (ret >= 0) {
 ret = avcodec_receive_packet(enc_ctx, pkt);
 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
 return;
 else if (ret < 0) {
 fprintf(stderr, "Error during encoding\n");
 exit(1);
 }

 printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
 av_write_frame(outFormatCtx, pkt); // Write the packet to the RTMP stream
 av_packet_unref(pkt);
 }
}

int main(int argc, char **argv) {
 av_log_set_level(AV_LOG_DEBUG);
 const char *rtmp_url, *codec_name;
 const AVCodec *codec;
 AVCodecContext *codecContext = NULL;
 int i, ret, x, y;
 AVFormatContext *outFormatCtx;
 AVStream *st;
 AVFrame *frame;
 AVPacket *pkt;
 uint8_t endcode[] = {0, 0, 1, 0xb7};

 if (argc <= 3) {
 fprintf(stderr, "Usage: %s <rtmp url="url"> <codec>\n", argv[0]);
 exit(0);
 }
 rtmp_url = argv[1];
 codec_name = argv[2];
 avformat_network_init();
 /* find the mpeg1video encoder */
// codec = avcodec_find_encoder_by_name(codec_name);
// codec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);
// codec = avcodec_find_encoder(AV_CODEC_ID_VP9);
// codec = avcodec_find_encoder(AV_CODEC_ID_MPEG2VIDEO);
 codec = avcodec_find_encoder(AV_CODEC_ID_H264);
// codec = avcodec_find_encoder(AV_CODEC_ID_AV1);
// codec = avcodec_find_encoder(AV_CODEC_ID_H265);
 if (!codec) {
 fprintf(stderr, "Codec '%s' not found\n", codec_name);
 exit(1);
 }
 codecContext = avcodec_alloc_context3(codec);
 if (!codecContext) {
 fprintf(stderr, "Could not allocate video codec context\n");
 exit(1);
 }

 /* ... (rest of the setup code) ... */
/* put sample parameters */
 codecContext->bit_rate = 400000;
 /* resolution must be a multiple of two */
 codecContext->width = 352;
 codecContext->height = 288;
 /* frames per second */
 codecContext->time_base = (AVRational) {1, 25};
 codecContext->framerate = (AVRational) {25, 1};

 /* emit one intra frame every ten frames
 * check frame pict_type before passing frame
 * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
 * then gop_size is ignored and the output of encoder
 * will always be I frame irrespective to gop_size
 */
 codecContext->gop_size = 10;
 codecContext->max_b_frames = 1;
 codecContext->pix_fmt = AV_PIX_FMT_YUV420P;



 /* Open the RTSP output */
// const AVOutputFormat *ofmt = av_guess_format("tcp", NULL, NULL);
 const AVOutputFormat *ofmt = av_guess_format("rtsp", rtmp_url, NULL);
// const AVOutputFormat *ofmt = av_guess_format("flv", NULL, NULL);
// const AVOutputFormat *ofmt = av_guess_format("rtmp", NULL, NULL);
// const AVOutputFormat *ofmt = av_guess_format("mpegts", NULL, NULL);
// const AVOutputFormat *ofmt = av_guess_format("mp4", NULL, NULL);
 if (!ofmt) {
 fprintf(stderr, "Could not find output format\n");
 exit(1);
 }

 /* Allocate the output context */

/* outFormatCtx = avformat_alloc_context();
 if (!outFormatCtx) {
 fprintf(stderr, "Could not allocate output context\n");
 exit(1);
 }*/

 // 打开输出 这个会导致outFormatCtx 中的stream 为空,并且产生这个问题[rtsp @ 00000204f6218b80] No streams to mux were specified
 if (avformat_alloc_output_context2(&outFormatCtx, ofmt, "rtsp", rtmp_url) != 0) {
 fprintf(stderr, "Could not allocate output context\n");
 return 1;
 }


 outFormatCtx->oformat = ofmt;
 outFormatCtx->url = av_strdup(rtmp_url);

 /* Add a video stream */
 st = avformat_new_stream(outFormatCtx, codec);
 if (!st) {
 fprintf(stderr, "Could not allocate stream\n");
 exit(1);
 }
 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
 st->codecpar->codec_id = codec->id;
 st->codecpar->width = 352;
 st->codecpar->height = 288;
 st->codecpar->format = AV_PIX_FMT_YUV420P;
// st->codecpar = c;
// st->codecpar->format = AV_PIX_FMT_YUV420P;
 // Set video stream parameters
// st->codecpar->framerate = (AVRational) {25, 1};
// st->id=outFormatCtx->nb_streams-1;
 /* Set the output URL */
 av_dict_set(&outFormatCtx->metadata, "url", rtmp_url, 0);


 pkt = av_packet_alloc();
 if (!pkt)
 exit(1);


 if (codec->id == AV_CODEC_ID_H264)
 av_opt_set(codecContext->priv_data, "preset", "slow", 0);

 AVDictionary *opt = NULL;
/* av_dict_set(&opt, "rtsp_transport", "udp", 0);
 av_dict_set(&opt, "announce_port", "1935", 0);
 av_dict_set(&opt, "enable-protocol", "rtsp", 0);
 av_dict_set(&opt, "protocol_whitelist", "file,udp,tcp,rtp,rtsp", 0);
 av_dict_set(&opt, "enable-protocol", "rtp", 0);
 av_dict_set(&opt, "enable-protocol", "rtsp", 0);
 av_dict_set(&opt, "enable-protocol", "udp", 0);
 av_dict_set(&opt, "enable-muxer", "rtsp", 0);
 av_dict_set(&opt, "enable-muxer", "rtp", 0);*/
 av_dict_set(&opt, "rtsp_transport", "tcp", 0);
 av_dict_set(&opt, "stimeout", "2000000", 0);
 av_dict_set(&opt, "max_delay", "500000", 0);
 av_dict_set(&opt, "sprop-parameter-sets", "asdgasdfs", AV_DICT_APPEND);
 /* open it */
 ret = avcodec_open2(codecContext, codec, &opt);
 if (ret < 0) {
 fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));
 exit(1);
 }
/* // 打开RTSP输出URL 微软AI给出的代码
 if (!(outFormatCtx->oformat->flags & AVFMT_NOFILE)) {
 int ret = avio_open(&outFormatCtx->pb, rtmp_url, AVIO_FLAG_WRITE);
 if (ret < 0) {
// std::cerr << "Could not open output URL " << out_url << std::endl;
 fprintf(stderr, "Could not open output URL %s\n", av_err2str(ret));
 return -1;
 }
 }*/

 avcodec_parameters_to_context(codecContext, st->codecpar);

/* AVDictionary *options = NULL;
 av_dict_set(&options, "rtsp_transport", "tcp", 0);
 av_dict_set(&options, "stimeout", "2000000", 0);
 av_dict_set(&options, "max_delay", "500000", 0);
 // 初始化输出
 av_dict_set(&options, "rtsp_transport", "tcp", 0);
//设置 接收包间隔最大延迟,微秒
 av_dict_set(&options, "max_delay", "200000", 0);
// rtmp、rtsp延迟控制到最小
 av_dict_set(&options, "fflags", "nobuffer", 0);
// 在进行网络操作时允许的最大等待时间。5秒
 av_dict_set(&options, "timeout", "5000000", 0);
//设置 阻塞超时,否则可能在流断开时连接发生阻塞,微秒
 av_dict_set(&options, "stimeout", "3000000", 0);
//设置 find_stream_info 最大时长,微秒
 av_dict_set(&options, "analyzeduration", "1000000", 0);*/
 av_dict_set(&opt, "preset", "medium", 0);
 av_dict_set(&opt, "tune", "zerolatency", 0);
 av_dict_set(&opt, "profile", "baseline", 0);
 av_dump_format(outFormatCtx, 0, rtmp_url, 1);

 if (avformat_init_output(outFormatCtx, &opt) != 0) {
 fprintf(stderr, "Error initializing output\n");
 return 1;
 }
 if (!(ofmt->flags & AVFMT_NOFILE)) {
 ret = avio_open(&outFormatCtx->pb, rtmp_url, AVIO_FLAG_WRITE);
 if (ret < 0) {
 fprintf(stderr, "Could not open output file '%s'", rtmp_url);
 exit(1);
 }
 }
 /* 这种方式修改没有效果,无法添加修改SDP
 * av_dict_set(&st->metadata, "title", "Cool video", 0);
 av_dict_set(&st->metadata, "Content-Base", " rtsp://10.45.12.141/h264/ch1/main/av_stream/", 0);
 av_dict_set(&st->metadata, "sprop-parameter-sets", "sdsfwedeo", 0);*/
 AVCodecParameters *codecParams = st->codecpar;
 const char *spropParameterSets = "Z0IACpZTBYmI,aMlWsA=="; // 替换为实际的sprop-parameter-sets值
 av_dict_set(&st->metadata, "sprop-parameter-sets", spropParameterSets, 0);
 avcodec_parameters_to_context(codecContext, st->codecpar);
 AVFormatContext *avFormatContext[1];
 avFormatContext[0] = outFormatCtx;
 char spd[2048];
 av_sdp_create(avFormatContext, 1, spd, sizeof(spd));
 printf("%s\n", spd);
/* ret = avio_open(&outFormatCtx->pb, rtmp_url, AVIO_FLAG_WRITE);
 if (ret < 0) {
 fprintf(stderr, "Could not open output ,%s\n", av_err2str(ret));
 exit(1);
 }*/

/*// 设置 H264 参数
 AVDictionary *params = NULL;
 av_dict_set(&params, "profile", "main", 0);
 av_dict_set(&params, "level", "3.1", 0);

// 获取 `sprop-parameter-sets` 参数
 AVPacket *extradata = av_packet_alloc();
// avcodec_parameters_from_context(extradata->data, codecContext);

// 获取 `sprop-parameter-sets` 参数的大小
 int sprop_parameter_sets_size = extradata->size;

// 释放资源
 av_packet_free(&extradata);

// 设置 `sprop-parameter-sets` 参数
 uint8_t *sprop_parameter_sets = extradata->data;
 codecContext->extradata = sprop_parameter_sets;
 codecContext->extradata_size = sprop_parameter_sets_size;*/

 /* Write the header */
// ret = avformat_write_header(outFormatCtx, NULL);
 ret = avformat_write_header(outFormatCtx, &opt);
 if (ret != 0) {
 fprintf(stderr, "Error occurred when opening output %s\n", av_err2str(ret));
 exit(1);
 }

 frame = av_frame_alloc();
 if (!frame) {
 fprintf(stderr, "Could not allocate video frame\n");
 exit(1);
 }
// frame->format = c->pix_fmt;
// frame->format = AV_PIX_FMT_YUV420P;
 frame->format = 0;
 frame->width = codecContext->width;
 frame->height = codecContext->height;

 ret = av_frame_get_buffer(frame, 0);
 if (ret < 0) {
 fprintf(stderr, "Could not allocate the video frame data ,%s\n", av_err2str(ret));
 exit(1);
 }

 /* encode 1 second of video */
 for (i = 0; i < 2500; i++) {
 /* ... (rest of the encoding loop) ... */
 fflush(stdout);

 /* make sure the frame data is writable */
 ret = av_frame_make_writable(frame);
 if (ret < 0)
 exit(1);

 /* prepare a dummy image */
 /* Y */
 for (y = 0; y < codecContext->height; y++) {
 for (x = 0; x < codecContext->width; x++) {
 frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
 }
 }

 /* Cb and Cr */
 for (y = 0; y < codecContext->height / 2; y++) {
 for (x = 0; x < codecContext->width / 2; x++) {
 frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
 frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
 }
 }

 frame->pts = i;

 /* encode the image */
 encode(codecContext, frame, pkt, outFormatCtx);
 }

 /* flush the encoder */
 encode(codecContext, NULL, pkt, outFormatCtx);

 /* Write the trailer */
 av_write_trailer(outFormatCtx);

 /* Close the output */
 avformat_free_context(outFormatCtx);

 avcodec_free_context(&codecContext);
 av_frame_free(&frame);
 av_packet_free(&pkt);

 return 0;
}
</codec></rtmp>


I searched online for how to add "prop parameter sets", but I used their method but none of them worked. I also used WireShark to capture packets, but during the communication process, there was still no "prop parameter sets". Here is the method I tried :


AVDictionary *opt = NULL;
 av_dict_set(&opt, "sprop-parameter-sets", "asdgasdfs", 0);



-
FFmpeg:A General error in an external library occurred when using FFmpeg6.1's avcodec_send_frame
4 janvier 2024, par MMingYI have the same code that can successfully push streams (rtmp) in the environment, but in the Android environment, I fail with an error message. The error message method is avcodec_send_frame in ffmpeg6.1. By the way, I compiled the FFmpeg library on Android myself, and I downloaded the official package for Win11. I will provide the code for Android and Win11 below.


android :


static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,

 AVFormatContext *outFormatCtx) {
 int ret;

 /* send the frame to the encoder */
 if (frame)
 LOGE2("Send frame %ld\n", frame->pts);

 ret = avcodec_send_frame(enc_ctx, frame);
 if (ret < 0) {
 char errbuf[AV_ERROR_MAX_STRING_SIZE];
 av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);
 LOGE2("Error sending a frame for encoding ,%s\n", errbuf);
// exit(1);
 return;
 }

 while (ret >= 0) {
 ret = avcodec_receive_packet(enc_ctx, pkt);
 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
 return;
 else if (ret < 0) {
 fprintf(stderr, "Error during encoding\n");
 exit(1);
 }

 printf("Write packet (size=%5d)\n", pkt->pts);
 /* ret = av_interleaved_write_frame(outFormatCtx, pkt);
 if (ret < 0) {
 LOGE2("write frame err=%s", av_err2str(ret));
 break;
 }*/
// printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
 av_write_frame(outFormatCtx, pkt); // Write the packet to the RTMP stream
 av_packet_unref(pkt);
 }
}

PUSHER_FUNC(int, testPush, jstring yuvPath, jstring outputPath) {
 const char *yvu_path = env->GetStringUTFChars(yuvPath, JNI_FALSE);
 const char *output_path = env->GetStringUTFChars(outputPath, JNI_FALSE);
 const char *rtmp_url = output_path;
 const AVCodec *codec;
 AVCodecContext *codecContext = NULL;
 AVFormatContext *outFormatCtx;
 int ret = 0;
 AVStream *outStream;
 AVFrame *frame;
 AVPacket *pkt;
 int i, x, y;
 avformat_network_init();

 codec = avcodec_find_encoder(AV_CODEC_ID_H264);
// codec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);
// codec = avcodec_find_encoder(AV_CODEC_ID_H265);
 if (!codec) {
 LOGE2("JNI Error finding H.264 encoder");
 return -1;
 }
 codecContext = avcodec_alloc_context3(codec);
 if (!codecContext) {
 fprintf(stderr, "Could not allocate video codec context\n");
 return -1;
 }

 /* Allocate the output context */
 outFormatCtx = avformat_alloc_context();
 if (!outFormatCtx) {
 fprintf(stderr, "Could not allocate output context\n");
 return -1;
 }

 /* Open the RTMP output */
 const AVOutputFormat *ofmt = av_guess_format("flv", NULL, NULL);
// const AVOutputFormat *ofmt = av_guess_format("mpegts", NULL, NULL);
// const AVOutputFormat *ofmt = av_guess_format("mp4", NULL, NULL);
 if (!ofmt) {
 fprintf(stderr, "Could not find output format\n");
 return -1;
 }
 outFormatCtx->oformat = ofmt;
 outFormatCtx->url = av_strdup(rtmp_url);
 /* Add a video stream */
 outStream = avformat_new_stream(outFormatCtx, codec);
 if (!outStream) {
 fprintf(stderr, "Could not allocate stream\n");
 return -1;
 }
 outStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
 outStream->codecpar->codec_id = codec->id;
 outStream->codecpar->width = 352;
 outStream->codecpar->height = 288;

 /* Set the output URL */
 av_dict_set(&outFormatCtx->metadata, "url", rtmp_url, 0);

 pkt = av_packet_alloc();
 if (!pkt)
 return -1;

 /* ... (rest of the setup code) ... */
/* put sample parameters */
 codecContext->bit_rate = 400000;
 /* resolution must be a multiple of two */
 codecContext->width = 352;
 codecContext->height = 288;
 /* frames per second */
 codecContext->time_base = (AVRational) {1, 25};
 codecContext->framerate = (AVRational) {25, 1};

 /* emit one intra frame every ten frames
 * check frame pict_type before passing frame
 * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
 * then gop_size is ignored and the output of encoder
 * will always be I frame irrespective to gop_size
 */
 codecContext->gop_size = 10;
 codecContext->max_b_frames = 1;
 codecContext->pix_fmt = AV_PIX_FMT_YUV420P;

 if (codec->id == AV_CODEC_ID_H264)
 av_opt_set(codecContext->priv_data, "preset", "slow", 0);

 /* open it */
 ret = avcodec_open2(codecContext, codec, NULL);
 if (ret < 0) {
 LOGE2("JNI Error opening codec eer%s", av_err2str(ret));
 return ret;
 }

 avcodec_parameters_to_context(codecContext, outStream->codecpar);

 if (avio_open(&outFormatCtx->pb, rtmp_url, AVIO_FLAG_WRITE)) {
 fprintf(stderr, "Could not open output\n");
 return ret;
 }
 /* Write the header */
 if (avformat_write_header(outFormatCtx, NULL) != 0) {
 fprintf(stderr, "Error occurred when opening output\n");
 return ret;
 }

 frame = av_frame_alloc();
 if (!frame) {
 fprintf(stderr, "Could not allocate video frame\n");
 return -1;
 }
 frame->format = codecContext->pix_fmt;
 frame->format = AV_PIX_FMT_YUV420P;
 frame->format = 0;
 frame->width = codecContext->width;
 frame->height = codecContext->height;

 ret = av_frame_get_buffer(frame, 0);
 if (ret < 0) {
 fprintf(stderr, "Could not allocate the video frame data ,%s\n", av_err2str(ret));
 return ret;
 }

 /* FILE *yuv_file = fopen(yvu_path, "rb");
 if (yuv_file == NULL) {
 LOGE2("cannot open h264 file");
 return -1;
 }*/

 /* encode 1 second of video */
 for (i = 0; i < 25000; i++) {
// for (i = 0; i < 25; i++) {
// fflush(stdout);

 /* make sure the frame data is writable */
 ret = av_frame_make_writable(frame);
 if (ret < 0)
 exit(1);

 /* prepare a dummy image */
 /* Y */
 for (y = 0; y < codecContext->height; y++) {
 for (x = 0; x < codecContext->width; x++) {
 frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
 }
 }

 /* Cb and Cr */
 for (y = 0; y < codecContext->height / 2; y++) {
 for (x = 0; x < codecContext->width / 2; x++) {
 frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
 frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
 }
 }

 frame->pts = i;

 /* encode the image */
 encode(codecContext, frame, pkt, outFormatCtx);
 }

// fclose(yuv_file);

 /* flush the encoder */
 encode(codecContext, NULL, pkt, outFormatCtx);

 /* Write the trailer */
 av_write_trailer(outFormatCtx);

 /* Close the output */
 avformat_free_context(outFormatCtx);

 avcodec_free_context(&codecContext);
 av_frame_free(&frame);
 av_packet_free(&pkt);
}



win11:


#include 
#include 
#include 

#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include <libavutil></libavutil>opt.h>
#include <libavutil></libavutil>imgutils.h>
#include <libavutil></libavutil>time.h>

static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
 AVFormatContext *outFormatCtx) {
 int ret;

 /* send the frame to the encoder */
 if (frame)
 printf("Send frame %3"PRId64"\n", frame->pts);

 ret = avcodec_send_frame(enc_ctx, frame);
 if (ret < 0) {
 char errbuf[AV_ERROR_MAX_STRING_SIZE];
 av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);
 fprintf(stderr, "Error sending a frame for encoding ,%s\n", errbuf);
 exit(1);
 }

 while (ret >= 0) {
 ret = avcodec_receive_packet(enc_ctx, pkt);
 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
 return;
 else if (ret < 0) {
 fprintf(stderr, "Error during encoding\n");
 exit(1);
 }

 printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
 av_write_frame(outFormatCtx, pkt); // Write the packet to the RTMP stream
 av_packet_unref(pkt);
 }
}

int main(int argc, char **argv) {
 av_log_set_level(AV_LOG_DEBUG);
 const char *rtmp_url, *codec_name;
 const AVCodec *codec;
 AVCodecContext *codecContext = NULL;
 int i, ret, x, y;
 AVFormatContext *outFormatCtx;
 AVStream *st;
 AVFrame *frame;
 AVPacket *pkt;
 uint8_t endcode[] = {0, 0, 1, 0xb7};

 if (argc <= 3) {
 fprintf(stderr, "Usage: %s <rtmp url="url"> <codec>\n", argv[0]);
 exit(0);
 }
 rtmp_url = argv[1];
 codec_name = argv[2];
 avformat_network_init();
 /* find the mpeg1video encoder */
// codec = avcodec_find_encoder_by_name(codec_name);
// codec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);
// codec = avcodec_find_encoder(AV_CODEC_ID_VP9);
// codec = avcodec_find_encoder(AV_CODEC_ID_MPEG2VIDEO);
// codec = avcodec_find_encoder(AV_CODEC_ID_H264);
 codec = avcodec_find_encoder(AV_CODEC_ID_H264);
// codec = avcodec_find_encoder(AV_CODEC_ID_AV1);
// codec = avcodec_find_encoder(AV_CODEC_ID_H265);
 if (!codec) {
 fprintf(stderr, "Codec '%s' not found\n", codec_name);
 exit(1);
 }
 codecContext = avcodec_alloc_context3(codec);
 if (!codecContext) {
 fprintf(stderr, "Could not allocate video codec context\n");
 exit(1);
 }

 /* Allocate the output context */
 outFormatCtx = avformat_alloc_context();
 if (!outFormatCtx) {
 fprintf(stderr, "Could not allocate output context\n");
 exit(1);
 }

 /* Open the RTMP output */
 const AVOutputFormat *ofmt = av_guess_format("flv", NULL, NULL);
// const AVOutputFormat *ofmt = av_guess_format("MKV", NULL, NULL);
// const AVOutputFormat *ofmt = av_guess_format("rtmp", NULL, NULL);
// const AVOutputFormat *ofmt = av_guess_format("mpegts", NULL, NULL);
// const AVOutputFormat *ofmt = av_guess_format("mp4", NULL, NULL);
 if (!ofmt) {
 fprintf(stderr, "Could not find output format\n");
 exit(1);
 }
 outFormatCtx->oformat = ofmt;
 outFormatCtx->url = av_strdup(rtmp_url);
 /* Add a video stream */
 st = avformat_new_stream(outFormatCtx, codec);
 if (!st) {
 fprintf(stderr, "Could not allocate stream\n");
 exit(1);
 }
 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
 st->codecpar->codec_id = codec->id;
 st->codecpar->width = 352;
 st->codecpar->height = 288;
// st->codecpar = c;
// st->codecpar->format = AV_PIX_FMT_YUV420P;
 // Set video stream parameters
// st->codecpar->framerate = (AVRational){25, 1};

 /* Set the output URL */
 av_dict_set(&outFormatCtx->metadata, "url", rtmp_url, 0);


 pkt = av_packet_alloc();
 if (!pkt)
 exit(1);

 /* ... (rest of the setup code) ... */
/* put sample parameters */
 codecContext->bit_rate = 400000;
 /* resolution must be a multiple of two */
 codecContext->width = 352;
 codecContext->height = 288;
 /* frames per second */
 codecContext->time_base = (AVRational) {1, 25};
 codecContext->framerate = (AVRational) {25, 1};

 /* emit one intra frame every ten frames
 * check frame pict_type before passing frame
 * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
 * then gop_size is ignored and the output of encoder
 * will always be I frame irrespective to gop_size
 */
 codecContext->gop_size = 10;
 codecContext->max_b_frames = 1;
 codecContext->pix_fmt = AV_PIX_FMT_YUV420P;

 if (codec->id == AV_CODEC_ID_H264)
 av_opt_set(codecContext->priv_data, "preset", "slow", 0);

 /* open it */
 ret = avcodec_open2(codecContext, codec, NULL);
 if (ret < 0) {
 fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));
 exit(1);
 }

 avcodec_parameters_to_context(codecContext, st->codecpar);

 if (avio_open(&outFormatCtx->pb, rtmp_url, AVIO_FLAG_WRITE)) {
 fprintf(stderr, "Could not open output\n");
 exit(1);
 }
 /* Write the header */
 if (avformat_write_header(outFormatCtx, NULL) != 0) {
 fprintf(stderr, "Error occurred when opening output\n");
 exit(1);
 }

 frame = av_frame_alloc();
 if (!frame) {
 fprintf(stderr, "Could not allocate video frame\n");
 exit(1);
 }
// frame->format = c->pix_fmt;
// frame->format = AV_PIX_FMT_YUV420P;
 frame->format = 0;
 frame->width = codecContext->width;
 frame->height = codecContext->height;

 ret = av_frame_get_buffer(frame, 0);
 if (ret < 0) {
 fprintf(stderr, "Could not allocate the video frame data ,%s\n", av_err2str(ret));
 exit(1);
 }

 /* encode 1 second of video */
 for (i = 0; i < 2500; i++) {
 /* ... (rest of the encoding loop) ... */
 fflush(stdout);

 /* make sure the frame data is writable */
 ret = av_frame_make_writable(frame);
 if (ret < 0)
 exit(1);

 /* prepare a dummy image */
 /* Y */
 for (y = 0; y < codecContext->height; y++) {
 for (x = 0; x < codecContext->width; x++) {
 frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
 }
 }

 /* Cb and Cr */
 for (y = 0; y < codecContext->height / 2; y++) {
 for (x = 0; x < codecContext->width / 2; x++) {
 frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
 frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
 }
 }

 frame->pts = i;

 /* encode the image */
 encode(codecContext, frame, pkt, outFormatCtx);
 }

 /* flush the encoder */
 encode(codecContext, NULL, pkt, outFormatCtx);

 /* Write the trailer */
 av_write_trailer(outFormatCtx);

 /* Close the output */
 avformat_free_context(outFormatCtx);

 avcodec_free_context(&codecContext);
 av_frame_free(&frame);
 av_packet_free(&pkt);

 return 0;
}
</codec></rtmp>


I suspect it's an issue with the ffmpeg library I compiled, so I searched for a step to compile ffmpeg on GitHub, but the package it compiled still has the same problem. I don't know what to do now.