Recherche avancée

Médias (0)

Mot : - Tags -/utilisateurs

Aucun média correspondant à vos critères n’est disponible sur le site.

Autres articles (103)

  • Multilang : améliorer l’interface pour les blocs multilingues

    18 février 2011, par

    Multilang est un plugin supplémentaire qui n’est pas activé par défaut lors de l’initialisation de MediaSPIP.
    Après son activation, une préconfiguration est mise en place automatiquement par MediaSPIP init permettant à la nouvelle fonctionnalité d’être automatiquement opérationnelle. Il n’est donc pas obligatoire de passer par une étape de configuration pour cela.

  • Formulaire personnalisable

    21 juin 2013, par

    Cette page présente les champs disponibles dans le formulaire de publication d’un média et il indique les différents champs qu’on peut ajouter. Formulaire de création d’un Media
    Dans le cas d’un document de type média, les champs proposés par défaut sont : Texte Activer/Désactiver le forum ( on peut désactiver l’invite au commentaire pour chaque article ) Licence Ajout/suppression d’auteurs Tags
    On peut modifier ce formulaire dans la partie :
    Administration > Configuration des masques de formulaire. (...)

  • ANNEXE : Les plugins utilisés spécifiquement pour la ferme

    5 mars 2010, par

    Le site central/maître de la ferme a besoin d’utiliser plusieurs plugins supplémentaires vis à vis des canaux pour son bon fonctionnement. le plugin Gestion de la mutualisation ; le plugin inscription3 pour gérer les inscriptions et les demandes de création d’instance de mutualisation dès l’inscription des utilisateurs ; le plugin verifier qui fournit une API de vérification des champs (utilisé par inscription3) ; le plugin champs extras v2 nécessité par inscription3 (...)

Sur d’autres sites (11620)

  • closed (H264 track 1 is not valid : sprop-parameter-sets is missing (96 packetization-mode=1)

    8 janvier 2024, par MMingY

    I 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 &#xA;#include &#xA;#include &#xA;&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libavutil></libavutil>imgutils.h>&#xA;#include <libavutil></libavutil>time.h>&#xA;&#xA;static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,&#xA;                   AVFormatContext *outFormatCtx) {&#xA;    int ret;&#xA;&#xA;    /* send the frame to the encoder */&#xA;    if (frame)&#xA;        printf("Send frame %3"PRId64"\n", frame->pts);&#xA;&#xA;    ret = avcodec_send_frame(enc_ctx, frame);&#xA;    if (ret &lt; 0) {&#xA;        char errbuf[AV_ERROR_MAX_STRING_SIZE];&#xA;        av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);&#xA;        fprintf(stderr, "Error sending a frame for encoding ,%s\n", errbuf);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    while (ret >= 0) {&#xA;        ret = avcodec_receive_packet(enc_ctx, pkt);&#xA;        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)&#xA;            return;&#xA;        else if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error during encoding\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);&#xA;        av_write_frame(outFormatCtx, pkt); // Write the packet to the RTMP stream&#xA;        av_packet_unref(pkt);&#xA;    }&#xA;}&#xA;&#xA;int main(int argc, char **argv) {&#xA;    av_log_set_level(AV_LOG_DEBUG);&#xA;    const char *rtmp_url, *codec_name;&#xA;    const AVCodec *codec;&#xA;    AVCodecContext *codecContext = NULL;&#xA;    int i, ret, x, y;&#xA;    AVFormatContext *outFormatCtx;&#xA;    AVStream *st;&#xA;    AVFrame *frame;&#xA;    AVPacket *pkt;&#xA;    uint8_t endcode[] = {0, 0, 1, 0xb7};&#xA;&#xA;    if (argc &lt;= 3) {&#xA;        fprintf(stderr, "Usage: %s <rtmp url="url"> <codec>\n", argv[0]);&#xA;        exit(0);&#xA;    }&#xA;    rtmp_url = argv[1];&#xA;    codec_name = argv[2];&#xA;    avformat_network_init();&#xA;    /* find the mpeg1video encoder */&#xA;//    codec = avcodec_find_encoder_by_name(codec_name);&#xA;//    codec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);&#xA;//    codec = avcodec_find_encoder(AV_CODEC_ID_VP9);&#xA;//    codec = avcodec_find_encoder(AV_CODEC_ID_MPEG2VIDEO);&#xA;    codec = avcodec_find_encoder(AV_CODEC_ID_H264);&#xA;//    codec = avcodec_find_encoder(AV_CODEC_ID_AV1);&#xA;//    codec = avcodec_find_encoder(AV_CODEC_ID_H265);&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec &#x27;%s&#x27; not found\n", codec_name);&#xA;        exit(1);&#xA;    }&#xA;    codecContext = avcodec_alloc_context3(codec);&#xA;    if (!codecContext) {&#xA;        fprintf(stderr, "Could not allocate video codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* ... (rest of the setup code) ... */&#xA;/* put sample parameters */&#xA;    codecContext->bit_rate = 400000;&#xA;    /* resolution must be a multiple of two */&#xA;    codecContext->width = 352;&#xA;    codecContext->height = 288;&#xA;    /* frames per second */&#xA;    codecContext->time_base = (AVRational) {1, 25};&#xA;    codecContext->framerate = (AVRational) {25, 1};&#xA;&#xA;    /* emit one intra frame every ten frames&#xA;     * check frame pict_type before passing frame&#xA;     * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I&#xA;     * then gop_size is ignored and the output of encoder&#xA;     * will always be I frame irrespective to gop_size&#xA;     */&#xA;    codecContext->gop_size = 10;&#xA;    codecContext->max_b_frames = 1;&#xA;    codecContext->pix_fmt = AV_PIX_FMT_YUV420P;&#xA;&#xA;&#xA;&#xA;    /* Open the RTSP output */&#xA;//    const AVOutputFormat *ofmt = av_guess_format("tcp", NULL, NULL);&#xA;    const AVOutputFormat *ofmt = av_guess_format("rtsp", rtmp_url, NULL);&#xA;//    const AVOutputFormat *ofmt = av_guess_format("flv", NULL, NULL);&#xA;//    const AVOutputFormat *ofmt = av_guess_format("rtmp", NULL, NULL);&#xA;//    const AVOutputFormat *ofmt = av_guess_format("mpegts", NULL, NULL);&#xA;//    const AVOutputFormat *ofmt = av_guess_format("mp4", NULL, NULL);&#xA;    if (!ofmt) {&#xA;        fprintf(stderr, "Could not find output format\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* Allocate the output context */&#xA;&#xA;/*    outFormatCtx = avformat_alloc_context();&#xA;    if (!outFormatCtx) {&#xA;        fprintf(stderr, "Could not allocate output context\n");&#xA;        exit(1);&#xA;    }*/&#xA;&#xA;    // 打开输出  这个会导致outFormatCtx 中的stream 为空,并且产生这个问题[rtsp @ 00000204f6218b80] No streams to mux were specified&#xA;    if (avformat_alloc_output_context2(&amp;outFormatCtx, ofmt, "rtsp", rtmp_url) != 0) {&#xA;        fprintf(stderr, "Could not allocate output context\n");&#xA;        return 1;&#xA;    }&#xA;&#xA;&#xA;    outFormatCtx->oformat = ofmt;&#xA;    outFormatCtx->url = av_strdup(rtmp_url);&#xA;&#xA;    /* Add a video stream */&#xA;    st = avformat_new_stream(outFormatCtx, codec);&#xA;    if (!st) {&#xA;        fprintf(stderr, "Could not allocate stream\n");&#xA;        exit(1);&#xA;    }&#xA;    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;    st->codecpar->codec_id = codec->id;&#xA;    st->codecpar->width = 352;&#xA;    st->codecpar->height = 288;&#xA;    st->codecpar->format = AV_PIX_FMT_YUV420P;&#xA;//    st->codecpar = c;&#xA;//    st->codecpar->format = AV_PIX_FMT_YUV420P;&#xA;    // Set video stream parameters&#xA;//    st->codecpar->framerate = (AVRational) {25, 1};&#xA;//    st->id=outFormatCtx->nb_streams-1;&#xA;    /* Set the output URL */&#xA;    av_dict_set(&amp;outFormatCtx->metadata, "url", rtmp_url, 0);&#xA;&#xA;&#xA;    pkt = av_packet_alloc();&#xA;    if (!pkt)&#xA;        exit(1);&#xA;&#xA;&#xA;    if (codec->id == AV_CODEC_ID_H264)&#xA;        av_opt_set(codecContext->priv_data, "preset", "slow", 0);&#xA;&#xA;    AVDictionary *opt = NULL;&#xA;/*    av_dict_set(&amp;opt, "rtsp_transport", "udp", 0);&#xA;    av_dict_set(&amp;opt, "announce_port", "1935", 0);&#xA;    av_dict_set(&amp;opt, "enable-protocol", "rtsp", 0);&#xA;    av_dict_set(&amp;opt, "protocol_whitelist", "file,udp,tcp,rtp,rtsp", 0);&#xA;    av_dict_set(&amp;opt, "enable-protocol", "rtp", 0);&#xA;    av_dict_set(&amp;opt, "enable-protocol", "rtsp", 0);&#xA;    av_dict_set(&amp;opt, "enable-protocol", "udp", 0);&#xA;    av_dict_set(&amp;opt, "enable-muxer", "rtsp", 0);&#xA;    av_dict_set(&amp;opt, "enable-muxer", "rtp", 0);*/&#xA;    av_dict_set(&amp;opt, "rtsp_transport", "tcp", 0);&#xA;    av_dict_set(&amp;opt, "stimeout", "2000000", 0);&#xA;    av_dict_set(&amp;opt, "max_delay", "500000", 0);&#xA;    av_dict_set(&amp;opt, "sprop-parameter-sets", "asdgasdfs", AV_DICT_APPEND);&#xA;    /* open it */&#xA;    ret = avcodec_open2(codecContext, codec, &amp;opt);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));&#xA;        exit(1);&#xA;    }&#xA;/*    // 打开RTSP输出URL  微软AI给出的代码&#xA;    if (!(outFormatCtx->oformat->flags &amp; AVFMT_NOFILE)) {&#xA;        int ret = avio_open(&amp;outFormatCtx->pb, rtmp_url, AVIO_FLAG_WRITE);&#xA;        if (ret &lt; 0) {&#xA;//            std::cerr &lt;&lt; "Could not open output URL " &lt;&lt; out_url &lt;&lt; std::endl;&#xA;            fprintf(stderr, "Could not open output URL  %s\n", av_err2str(ret));&#xA;            return -1;&#xA;        }&#xA;    }*/&#xA;&#xA;    avcodec_parameters_to_context(codecContext, st->codecpar);&#xA;&#xA;/*    AVDictionary *options = NULL;&#xA;    av_dict_set(&amp;options, "rtsp_transport", "tcp", 0);&#xA;    av_dict_set(&amp;options, "stimeout", "2000000", 0);&#xA;    av_dict_set(&amp;options, "max_delay", "500000", 0);&#xA;    // 初始化输出&#xA;    av_dict_set(&amp;options, "rtsp_transport", "tcp", 0);&#xA;//设置 接收包间隔最大延迟,微秒&#xA;    av_dict_set(&amp;options, "max_delay", "200000", 0);&#xA;// rtmp、rtsp延迟控制到最小&#xA;    av_dict_set(&amp;options, "fflags", "nobuffer", 0);&#xA;// 在进行网络操作时允许的最大等待时间。5秒&#xA;    av_dict_set(&amp;options, "timeout", "5000000", 0);&#xA;//设置 阻塞超时,否则可能在流断开时连接发生阻塞,微秒&#xA;    av_dict_set(&amp;options, "stimeout", "3000000", 0);&#xA;//设置 find_stream_info 最大时长,微秒&#xA;    av_dict_set(&amp;options, "analyzeduration", "1000000", 0);*/&#xA;    av_dict_set(&amp;opt, "preset", "medium", 0);&#xA;    av_dict_set(&amp;opt, "tune", "zerolatency", 0);&#xA;    av_dict_set(&amp;opt, "profile", "baseline", 0);&#xA;    av_dump_format(outFormatCtx, 0, rtmp_url, 1);&#xA;&#xA;    if (avformat_init_output(outFormatCtx, &amp;opt) != 0) {&#xA;        fprintf(stderr, "Error initializing output\n");&#xA;        return 1;&#xA;    }&#xA;    if (!(ofmt->flags &amp; AVFMT_NOFILE)) {&#xA;        ret = avio_open(&amp;outFormatCtx->pb, rtmp_url, AVIO_FLAG_WRITE);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Could not open output file &#x27;%s&#x27;", rtmp_url);&#xA;            exit(1);&#xA;        }&#xA;    }&#xA;    /*   这种方式修改没有效果,无法添加修改SDP&#xA;     * av_dict_set(&amp;st->metadata, "title", "Cool video", 0);&#xA;       av_dict_set(&amp;st->metadata, "Content-Base", " rtsp://10.45.12.141/h264/ch1/main/av_stream/", 0);&#xA;       av_dict_set(&amp;st->metadata, "sprop-parameter-sets", "sdsfwedeo", 0);*/&#xA;    AVCodecParameters *codecParams = st->codecpar;&#xA;    const char *spropParameterSets = "Z0IACpZTBYmI,aMlWsA==";  // 替换为实际的sprop-parameter-sets值&#xA;    av_dict_set(&amp;st->metadata, "sprop-parameter-sets", spropParameterSets, 0);&#xA;    avcodec_parameters_to_context(codecContext, st->codecpar);&#xA;    AVFormatContext *avFormatContext[1];&#xA;    avFormatContext[0] = outFormatCtx;&#xA;    char spd[2048];&#xA;    av_sdp_create(avFormatContext, 1, spd, sizeof(spd));&#xA;    printf("%s\n", spd);&#xA;/*    ret = avio_open(&amp;outFormatCtx->pb, rtmp_url, AVIO_FLAG_WRITE);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not open output ,%s\n", av_err2str(ret));&#xA;        exit(1);&#xA;    }*/&#xA;&#xA;/*// 设置 H264 参数&#xA;    AVDictionary *params = NULL;&#xA;    av_dict_set(&amp;params, "profile", "main", 0);&#xA;    av_dict_set(&amp;params, "level", "3.1", 0);&#xA;&#xA;// 获取 `sprop-parameter-sets` 参数&#xA;    AVPacket *extradata = av_packet_alloc();&#xA;//    avcodec_parameters_from_context(extradata->data, codecContext);&#xA;&#xA;// 获取 `sprop-parameter-sets` 参数的大小&#xA;    int sprop_parameter_sets_size = extradata->size;&#xA;&#xA;// 释放资源&#xA;    av_packet_free(&amp;extradata);&#xA;&#xA;// 设置 `sprop-parameter-sets` 参数&#xA;    uint8_t *sprop_parameter_sets = extradata->data;&#xA;    codecContext->extradata = sprop_parameter_sets;&#xA;    codecContext->extradata_size = sprop_parameter_sets_size;*/&#xA;&#xA;    /* Write the header */&#xA;//    ret = avformat_write_header(outFormatCtx, NULL);&#xA;    ret = avformat_write_header(outFormatCtx, &amp;opt);&#xA;    if (ret != 0) {&#xA;        fprintf(stderr, "Error occurred when opening output %s\n", av_err2str(ret));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        fprintf(stderr, "Could not allocate video frame\n");&#xA;        exit(1);&#xA;    }&#xA;//    frame->format = c->pix_fmt;&#xA;//    frame->format = AV_PIX_FMT_YUV420P;&#xA;    frame->format = 0;&#xA;    frame->width = codecContext->width;&#xA;    frame->height = codecContext->height;&#xA;&#xA;    ret = av_frame_get_buffer(frame, 0);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not allocate the video frame data ,%s\n", av_err2str(ret));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* encode 1 second of video */&#xA;    for (i = 0; i &lt; 2500; i&#x2B;&#x2B;) {&#xA;        /* ... (rest of the encoding loop) ... */&#xA;        fflush(stdout);&#xA;&#xA;        /* make sure the frame data is writable */&#xA;        ret = av_frame_make_writable(frame);&#xA;        if (ret &lt; 0)&#xA;            exit(1);&#xA;&#xA;        /* prepare a dummy image */&#xA;        /* Y */&#xA;        for (y = 0; y &lt; codecContext->height; y&#x2B;&#x2B;) {&#xA;            for (x = 0; x &lt; codecContext->width; x&#x2B;&#x2B;) {&#xA;                frame->data[0][y * frame->linesize[0] &#x2B; x] = x &#x2B; y &#x2B; i * 3;&#xA;            }&#xA;        }&#xA;&#xA;        /* Cb and Cr */&#xA;        for (y = 0; y &lt; codecContext->height / 2; y&#x2B;&#x2B;) {&#xA;            for (x = 0; x &lt; codecContext->width / 2; x&#x2B;&#x2B;) {&#xA;                frame->data[1][y * frame->linesize[1] &#x2B; x] = 128 &#x2B; y &#x2B; i * 2;&#xA;                frame->data[2][y * frame->linesize[2] &#x2B; x] = 64 &#x2B; x &#x2B; i * 5;&#xA;            }&#xA;        }&#xA;&#xA;        frame->pts = i;&#xA;&#xA;        /* encode the image */&#xA;        encode(codecContext, frame, pkt, outFormatCtx);&#xA;    }&#xA;&#xA;    /* flush the encoder */&#xA;    encode(codecContext, NULL, pkt, outFormatCtx);&#xA;&#xA;    /* Write the trailer */&#xA;    av_write_trailer(outFormatCtx);&#xA;&#xA;    /* Close the output */&#xA;    avformat_free_context(outFormatCtx);&#xA;&#xA;    avcodec_free_context(&amp;codecContext);&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;&#xA;    return 0;&#xA;}&#xA;</codec></rtmp>

    &#xA;

    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 :

    &#xA;

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

    &#xA;

  • FFmpeg:A General error in an external library occurred when using FFmpeg6.1's avcodec_send_frame

    4 janvier 2024, par MMingY

    I 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.

    &#xA;

    android :

    &#xA;

    static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,&#xA;&#xA;                   AVFormatContext *outFormatCtx) {&#xA;    int ret;&#xA;&#xA;    /* send the frame to the encoder */&#xA;    if (frame)&#xA;        LOGE2("Send frame %ld\n", frame->pts);&#xA;&#xA;    ret = avcodec_send_frame(enc_ctx, frame);&#xA;    if (ret &lt; 0) {&#xA;        char errbuf[AV_ERROR_MAX_STRING_SIZE];&#xA;        av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);&#xA;        LOGE2("Error sending a frame for encoding ,%s\n", errbuf);&#xA;//        exit(1);&#xA;        return;&#xA;    }&#xA;&#xA;    while (ret >= 0) {&#xA;        ret = avcodec_receive_packet(enc_ctx, pkt);&#xA;        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)&#xA;            return;&#xA;        else if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error during encoding\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        printf("Write packet (size=%5d)\n", pkt->pts);&#xA;        /*    ret = av_interleaved_write_frame(outFormatCtx, pkt);&#xA;            if (ret &lt; 0) {&#xA;                LOGE2("write frame err=%s", av_err2str(ret));&#xA;                break;&#xA;            }*/&#xA;//        printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);&#xA;        av_write_frame(outFormatCtx, pkt); // Write the packet to the RTMP stream&#xA;        av_packet_unref(pkt);&#xA;    }&#xA;}&#xA;&#xA;PUSHER_FUNC(int, testPush, jstring yuvPath, jstring outputPath) {&#xA;    const char *yvu_path = env->GetStringUTFChars(yuvPath, JNI_FALSE);&#xA;    const char *output_path = env->GetStringUTFChars(outputPath, JNI_FALSE);&#xA;    const char *rtmp_url = output_path;&#xA;    const AVCodec *codec;&#xA;    AVCodecContext *codecContext = NULL;&#xA;    AVFormatContext *outFormatCtx;&#xA;    int ret = 0;&#xA;    AVStream *outStream;&#xA;    AVFrame *frame;&#xA;    AVPacket *pkt;&#xA;    int i, x, y;&#xA;    avformat_network_init();&#xA;&#xA;    codec = avcodec_find_encoder(AV_CODEC_ID_H264);&#xA;//    codec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);&#xA;//    codec = avcodec_find_encoder(AV_CODEC_ID_H265);&#xA;    if (!codec) {&#xA;        LOGE2("JNI Error finding H.264 encoder");&#xA;        return -1;&#xA;    }&#xA;    codecContext = avcodec_alloc_context3(codec);&#xA;    if (!codecContext) {&#xA;        fprintf(stderr, "Could not allocate video codec context\n");&#xA;        return -1;&#xA;    }&#xA;&#xA;    /* Allocate the output context */&#xA;    outFormatCtx = avformat_alloc_context();&#xA;    if (!outFormatCtx) {&#xA;        fprintf(stderr, "Could not allocate output context\n");&#xA;        return -1;&#xA;    }&#xA;&#xA;    /* Open the RTMP output */&#xA;    const AVOutputFormat *ofmt = av_guess_format("flv", NULL, NULL);&#xA;//    const AVOutputFormat *ofmt = av_guess_format("mpegts", NULL, NULL);&#xA;//    const AVOutputFormat *ofmt = av_guess_format("mp4", NULL, NULL);&#xA;    if (!ofmt) {&#xA;        fprintf(stderr, "Could not find output format\n");&#xA;        return -1;&#xA;    }&#xA;    outFormatCtx->oformat = ofmt;&#xA;    outFormatCtx->url = av_strdup(rtmp_url);&#xA;    /* Add a video stream */&#xA;    outStream = avformat_new_stream(outFormatCtx, codec);&#xA;    if (!outStream) {&#xA;        fprintf(stderr, "Could not allocate stream\n");&#xA;        return -1;&#xA;    }&#xA;    outStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;    outStream->codecpar->codec_id = codec->id;&#xA;    outStream->codecpar->width = 352;&#xA;    outStream->codecpar->height = 288;&#xA;&#xA;    /* Set the output URL */&#xA;    av_dict_set(&amp;outFormatCtx->metadata, "url", rtmp_url, 0);&#xA;&#xA;    pkt = av_packet_alloc();&#xA;    if (!pkt)&#xA;        return -1;&#xA;&#xA;    /* ... (rest of the setup code) ... */&#xA;/* put sample parameters */&#xA;    codecContext->bit_rate = 400000;&#xA;    /* resolution must be a multiple of two */&#xA;    codecContext->width = 352;&#xA;    codecContext->height = 288;&#xA;    /* frames per second */&#xA;    codecContext->time_base = (AVRational) {1, 25};&#xA;    codecContext->framerate = (AVRational) {25, 1};&#xA;&#xA;    /* emit one intra frame every ten frames&#xA;     * check frame pict_type before passing frame&#xA;     * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I&#xA;     * then gop_size is ignored and the output of encoder&#xA;     * will always be I frame irrespective to gop_size&#xA;     */&#xA;    codecContext->gop_size = 10;&#xA;    codecContext->max_b_frames = 1;&#xA;    codecContext->pix_fmt = AV_PIX_FMT_YUV420P;&#xA;&#xA;    if (codec->id == AV_CODEC_ID_H264)&#xA;        av_opt_set(codecContext->priv_data, "preset", "slow", 0);&#xA;&#xA;    /* open it */&#xA;    ret = avcodec_open2(codecContext, codec, NULL);&#xA;    if (ret &lt; 0) {&#xA;        LOGE2("JNI Error opening codec eer%s", av_err2str(ret));&#xA;        return ret;&#xA;    }&#xA;&#xA;    avcodec_parameters_to_context(codecContext, outStream->codecpar);&#xA;&#xA;    if (avio_open(&amp;outFormatCtx->pb, rtmp_url, AVIO_FLAG_WRITE)) {&#xA;        fprintf(stderr, "Could not open output\n");&#xA;        return ret;&#xA;    }&#xA;    /* Write the header */&#xA;    if (avformat_write_header(outFormatCtx, NULL) != 0) {&#xA;        fprintf(stderr, "Error occurred when opening output\n");&#xA;        return ret;&#xA;    }&#xA;&#xA;    frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        fprintf(stderr, "Could not allocate video frame\n");&#xA;        return -1;&#xA;    }&#xA;    frame->format = codecContext->pix_fmt;&#xA;    frame->format = AV_PIX_FMT_YUV420P;&#xA;    frame->format = 0;&#xA;    frame->width = codecContext->width;&#xA;    frame->height = codecContext->height;&#xA;&#xA;    ret = av_frame_get_buffer(frame, 0);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not allocate the video frame data ,%s\n", av_err2str(ret));&#xA;        return ret;&#xA;    }&#xA;&#xA;    /*  FILE *yuv_file = fopen(yvu_path, "rb");&#xA;      if (yuv_file == NULL) {&#xA;          LOGE2("cannot open h264 file");&#xA;          return -1;&#xA;      }*/&#xA;&#xA;    /* encode 1 second of video */&#xA;    for (i = 0; i &lt; 25000; i&#x2B;&#x2B;) {&#xA;//    for (i = 0; i &lt; 25; i&#x2B;&#x2B;) {&#xA;//        fflush(stdout);&#xA;&#xA;        /* make sure the frame data is writable */&#xA;        ret = av_frame_make_writable(frame);&#xA;        if (ret &lt; 0)&#xA;            exit(1);&#xA;&#xA;        /* prepare a dummy image */&#xA;        /* Y */&#xA;        for (y = 0; y &lt; codecContext->height; y&#x2B;&#x2B;) {&#xA;            for (x = 0; x &lt; codecContext->width; x&#x2B;&#x2B;) {&#xA;                frame->data[0][y * frame->linesize[0] &#x2B; x] = x &#x2B; y &#x2B; i * 3;&#xA;            }&#xA;        }&#xA;&#xA;        /* Cb and Cr */&#xA;        for (y = 0; y &lt; codecContext->height / 2; y&#x2B;&#x2B;) {&#xA;            for (x = 0; x &lt; codecContext->width / 2; x&#x2B;&#x2B;) {&#xA;                frame->data[1][y * frame->linesize[1] &#x2B; x] = 128 &#x2B; y &#x2B; i * 2;&#xA;                frame->data[2][y * frame->linesize[2] &#x2B; x] = 64 &#x2B; x &#x2B; i * 5;&#xA;            }&#xA;        }&#xA;&#xA;        frame->pts = i;&#xA;&#xA;        /* encode the image */&#xA;        encode(codecContext, frame, pkt, outFormatCtx);&#xA;    }&#xA;&#xA;//    fclose(yuv_file);&#xA;&#xA;    /* flush the encoder */&#xA;    encode(codecContext, NULL, pkt, outFormatCtx);&#xA;&#xA;    /* Write the trailer */&#xA;    av_write_trailer(outFormatCtx);&#xA;&#xA;    /* Close the output */&#xA;    avformat_free_context(outFormatCtx);&#xA;&#xA;    avcodec_free_context(&amp;codecContext);&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;}&#xA;

    &#xA;

    win11:

    &#xA;

    #include &#xA;#include &#xA;#include &#xA;&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libavutil></libavutil>imgutils.h>&#xA;#include <libavutil></libavutil>time.h>&#xA;&#xA;static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,&#xA;                   AVFormatContext *outFormatCtx) {&#xA;    int ret;&#xA;&#xA;    /* send the frame to the encoder */&#xA;    if (frame)&#xA;        printf("Send frame %3"PRId64"\n", frame->pts);&#xA;&#xA;    ret = avcodec_send_frame(enc_ctx, frame);&#xA;    if (ret &lt; 0) {&#xA;        char errbuf[AV_ERROR_MAX_STRING_SIZE];&#xA;        av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);&#xA;        fprintf(stderr, "Error sending a frame for encoding ,%s\n", errbuf);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    while (ret >= 0) {&#xA;        ret = avcodec_receive_packet(enc_ctx, pkt);&#xA;        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)&#xA;            return;&#xA;        else if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error during encoding\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);&#xA;        av_write_frame(outFormatCtx, pkt); // Write the packet to the RTMP stream&#xA;        av_packet_unref(pkt);&#xA;    }&#xA;}&#xA;&#xA;int main(int argc, char **argv) {&#xA;    av_log_set_level(AV_LOG_DEBUG);&#xA;    const char *rtmp_url, *codec_name;&#xA;    const AVCodec *codec;&#xA;    AVCodecContext *codecContext = NULL;&#xA;    int i, ret, x, y;&#xA;    AVFormatContext *outFormatCtx;&#xA;    AVStream *st;&#xA;    AVFrame *frame;&#xA;    AVPacket *pkt;&#xA;    uint8_t endcode[] = {0, 0, 1, 0xb7};&#xA;&#xA;    if (argc &lt;= 3) {&#xA;        fprintf(stderr, "Usage: %s <rtmp url="url"> <codec>\n", argv[0]);&#xA;        exit(0);&#xA;    }&#xA;    rtmp_url = argv[1];&#xA;    codec_name = argv[2];&#xA;    avformat_network_init();&#xA;    /* find the mpeg1video encoder */&#xA;//    codec = avcodec_find_encoder_by_name(codec_name);&#xA;//    codec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);&#xA;//    codec = avcodec_find_encoder(AV_CODEC_ID_VP9);&#xA;//    codec = avcodec_find_encoder(AV_CODEC_ID_MPEG2VIDEO);&#xA;//    codec = avcodec_find_encoder(AV_CODEC_ID_H264);&#xA;    codec = avcodec_find_encoder(AV_CODEC_ID_H264);&#xA;//    codec = avcodec_find_encoder(AV_CODEC_ID_AV1);&#xA;//    codec = avcodec_find_encoder(AV_CODEC_ID_H265);&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec &#x27;%s&#x27; not found\n", codec_name);&#xA;        exit(1);&#xA;    }&#xA;    codecContext = avcodec_alloc_context3(codec);&#xA;    if (!codecContext) {&#xA;        fprintf(stderr, "Could not allocate video codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* Allocate the output context */&#xA;    outFormatCtx = avformat_alloc_context();&#xA;    if (!outFormatCtx) {&#xA;        fprintf(stderr, "Could not allocate output context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* Open the RTMP output */&#xA;    const AVOutputFormat *ofmt = av_guess_format("flv", NULL, NULL);&#xA;//    const AVOutputFormat *ofmt = av_guess_format("MKV", NULL, NULL);&#xA;//    const AVOutputFormat *ofmt = av_guess_format("rtmp", NULL, NULL);&#xA;//    const AVOutputFormat *ofmt = av_guess_format("mpegts", NULL, NULL);&#xA;//    const AVOutputFormat *ofmt = av_guess_format("mp4", NULL, NULL);&#xA;    if (!ofmt) {&#xA;        fprintf(stderr, "Could not find output format\n");&#xA;        exit(1);&#xA;    }&#xA;    outFormatCtx->oformat = ofmt;&#xA;    outFormatCtx->url = av_strdup(rtmp_url);&#xA;    /* Add a video stream */&#xA;    st = avformat_new_stream(outFormatCtx, codec);&#xA;    if (!st) {&#xA;        fprintf(stderr, "Could not allocate stream\n");&#xA;        exit(1);&#xA;    }&#xA;    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;    st->codecpar->codec_id = codec->id;&#xA;    st->codecpar->width = 352;&#xA;    st->codecpar->height = 288;&#xA;//    st->codecpar = c;&#xA;//    st->codecpar->format = AV_PIX_FMT_YUV420P;&#xA;    // Set video stream parameters&#xA;//    st->codecpar->framerate = (AVRational){25, 1};&#xA;&#xA;    /* Set the output URL */&#xA;    av_dict_set(&amp;outFormatCtx->metadata, "url", rtmp_url, 0);&#xA;&#xA;&#xA;    pkt = av_packet_alloc();&#xA;    if (!pkt)&#xA;        exit(1);&#xA;&#xA;    /* ... (rest of the setup code) ... */&#xA;/* put sample parameters */&#xA;    codecContext->bit_rate = 400000;&#xA;    /* resolution must be a multiple of two */&#xA;    codecContext->width = 352;&#xA;    codecContext->height = 288;&#xA;    /* frames per second */&#xA;    codecContext->time_base = (AVRational) {1, 25};&#xA;    codecContext->framerate = (AVRational) {25, 1};&#xA;&#xA;    /* emit one intra frame every ten frames&#xA;     * check frame pict_type before passing frame&#xA;     * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I&#xA;     * then gop_size is ignored and the output of encoder&#xA;     * will always be I frame irrespective to gop_size&#xA;     */&#xA;    codecContext->gop_size = 10;&#xA;    codecContext->max_b_frames = 1;&#xA;    codecContext->pix_fmt = AV_PIX_FMT_YUV420P;&#xA;&#xA;    if (codec->id == AV_CODEC_ID_H264)&#xA;        av_opt_set(codecContext->priv_data, "preset", "slow", 0);&#xA;&#xA;    /* open it */&#xA;    ret = avcodec_open2(codecContext, codec, NULL);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    avcodec_parameters_to_context(codecContext, st->codecpar);&#xA;&#xA;    if (avio_open(&amp;outFormatCtx->pb, rtmp_url, AVIO_FLAG_WRITE)) {&#xA;        fprintf(stderr, "Could not open output\n");&#xA;        exit(1);&#xA;    }&#xA;    /* Write the header */&#xA;    if (avformat_write_header(outFormatCtx, NULL) != 0) {&#xA;        fprintf(stderr, "Error occurred when opening output\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        fprintf(stderr, "Could not allocate video frame\n");&#xA;        exit(1);&#xA;    }&#xA;//    frame->format = c->pix_fmt;&#xA;//    frame->format = AV_PIX_FMT_YUV420P;&#xA;    frame->format = 0;&#xA;    frame->width = codecContext->width;&#xA;    frame->height = codecContext->height;&#xA;&#xA;    ret = av_frame_get_buffer(frame, 0);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not allocate the video frame data ,%s\n", av_err2str(ret));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* encode 1 second of video */&#xA;    for (i = 0; i &lt; 2500; i&#x2B;&#x2B;) {&#xA;        /* ... (rest of the encoding loop) ... */&#xA;        fflush(stdout);&#xA;&#xA;        /* make sure the frame data is writable */&#xA;        ret = av_frame_make_writable(frame);&#xA;        if (ret &lt; 0)&#xA;            exit(1);&#xA;&#xA;        /* prepare a dummy image */&#xA;        /* Y */&#xA;        for (y = 0; y &lt; codecContext->height; y&#x2B;&#x2B;) {&#xA;            for (x = 0; x &lt; codecContext->width; x&#x2B;&#x2B;) {&#xA;                frame->data[0][y * frame->linesize[0] &#x2B; x] = x &#x2B; y &#x2B; i * 3;&#xA;            }&#xA;        }&#xA;&#xA;        /* Cb and Cr */&#xA;        for (y = 0; y &lt; codecContext->height / 2; y&#x2B;&#x2B;) {&#xA;            for (x = 0; x &lt; codecContext->width / 2; x&#x2B;&#x2B;) {&#xA;                frame->data[1][y * frame->linesize[1] &#x2B; x] = 128 &#x2B; y &#x2B; i * 2;&#xA;                frame->data[2][y * frame->linesize[2] &#x2B; x] = 64 &#x2B; x &#x2B; i * 5;&#xA;            }&#xA;        }&#xA;&#xA;        frame->pts = i;&#xA;&#xA;        /* encode the image */&#xA;        encode(codecContext, frame, pkt, outFormatCtx);&#xA;    }&#xA;&#xA;    /* flush the encoder */&#xA;    encode(codecContext, NULL, pkt, outFormatCtx);&#xA;&#xA;    /* Write the trailer */&#xA;    av_write_trailer(outFormatCtx);&#xA;&#xA;    /* Close the output */&#xA;    avformat_free_context(outFormatCtx);&#xA;&#xA;    avcodec_free_context(&amp;codecContext);&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;&#xA;    return 0;&#xA;}&#xA;</codec></rtmp>

    &#xA;

    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.

    &#xA;

  • Problems with outputting stream format as RTMP via FFmpeg C-API

    9 janvier 2024, par dongrixinyu

    I am using FFmpeg's C API to push video streams rtmp://.... into an SRS server.
    &#xA;The input stream is an MP4 file named juren-30s.mp4.
    &#xA;The output stream is also an MP4 file named juren-30s-5.mp4.

    &#xA;

    My piece of code (see further down) works fine when used in the following steps :
    &#xA;mp4 -> demux -> decode -> rgb images -> encode -> mux -> mp4.

    &#xA;

    Problem :

    &#xA;

    When I changed the output stream to an online RTMP url named rtmp://ip:port/live/stream_nb_23 (just an example, you can change it according to your server and rules.)

    &#xA;

    result : This code would be corrupted mp4 -> rtmp(flv).

    &#xA;

    What I've tried :

    &#xA;

    Changing the output format
    &#xA;I changed the output format param to become flv when I initialized the avformat_alloc_output_context2. But this didn't help.

    &#xA;

    Debugging the output
    &#xA;When I executed ffprobe rtmp://ip:port/live/xxxxxxx, I got the following errors and did not know why :

    &#xA;

    [h264 @ 0x55a925e3ba80] luma_log2_weight_denom 12 is out of range&#xA;[h264 @ 0x55a925e3ba80] Missing reference picture, default is 2&#xA;[h264 @ 0x55a925e3ba80] concealing 8003 DC, 8003 AC, 8003 MV errors in P frame&#xA;[h264 @ 0x55a925e3ba80] QP 4294966938 out of range&#xA;[h264 @ 0x55a925e3ba80] decode_slice_header error&#xA;[h264 @ 0x55a925e3ba80] no frame!&#xA;[h264 @ 0x55a925e3ba80] luma_log2_weight_denom 21 is out of range&#xA;[h264 @ 0x55a925e3ba80] luma_log2_weight_denom 10 is out of range&#xA;[h264 @ 0x55a925e3ba80] chroma_log2_weight_denom 12 is out of range&#xA;[h264 @ 0x55a925e3ba80] Missing reference picture, default is 0&#xA;[h264 @ 0x55a925e3ba80] decode_slice_header error&#xA;[h264 @ 0x55a925e3ba80] QP 4294967066 out of range&#xA;[h264 @ 0x55a925e3ba80] decode_slice_header error&#xA;[h264 @ 0x55a925e3ba80] no frame!&#xA;[h264 @ 0x55a925e3ba80] QP 341 out of range&#xA;[h264 @ 0x55a925e3ba80] decode_slice_header error&#xA;

    &#xA;

    I am confused about the difference between MP4 and RTMP of how to use FFmpeg C-API to produce a correct output stream format.

    &#xA;

    Besides, I also wanna learn how to convert video and audio streams into other formats using FFmpeg C-api, such as flv, ts, rtsp, etc.

    &#xA;

    Code to reproduce the problem :

    &#xA;

    &#xA;

    So how to make this code output to RTMP without getting issue of an unplayable video ?

    &#xA;

    #include &#xA;#include "libavformat/avformat.h"&#xA;int main()&#xA;{&#xA;    int ret = 0; int err;&#xA;&#xA;    //Open input file&#xA;    char filename[] = "juren-30s.mp4";&#xA;    AVFormatContext *fmt_ctx = avformat_alloc_context();&#xA;    if (!fmt_ctx) {&#xA;        printf("error code %d \n",AVERROR(ENOMEM));&#xA;        return ENOMEM;&#xA;    }&#xA;    if((err = avformat_open_input(&amp;fmt_ctx, filename,NULL,NULL)) &lt; 0){&#xA;        printf("can not open file %d \n",err);&#xA;        return err;&#xA;    }&#xA;&#xA;    //Open the decoder&#xA;    AVCodecContext *avctx = avcodec_alloc_context3(NULL);&#xA;    ret = avcodec_parameters_to_context(avctx, fmt_ctx->streams[0]->codecpar);&#xA;    if (ret &lt; 0){&#xA;        printf("error code %d \n",ret);&#xA;        return ret;&#xA;    }&#xA;    AVCodec *codec = avcodec_find_decoder(avctx->codec_id);&#xA;    if ((ret = avcodec_open2(avctx, codec, NULL)) &lt; 0) {&#xA;        printf("open codec faile %d \n",ret);&#xA;        return ret;&#xA;    }&#xA;&#xA;    //Open the output file container&#xA;    char filename_out[] = "juren-30s-5.mp4";&#xA;    AVFormatContext *fmt_ctx_out = NULL;&#xA;    err = avformat_alloc_output_context2(&amp;fmt_ctx_out, NULL, NULL, filename_out);&#xA;    if (!fmt_ctx_out) {&#xA;        printf("error code %d \n",AVERROR(ENOMEM));&#xA;        return ENOMEM;&#xA;    }&#xA;    //Add all the way to the container context&#xA;    AVStream *st = avformat_new_stream(fmt_ctx_out, NULL);&#xA;    st->time_base = fmt_ctx->streams[0]->time_base;&#xA;&#xA;    AVCodecContext *enc_ctx = NULL;&#xA;    &#xA;    AVPacket *pt = av_packet_alloc();&#xA;    AVFrame *frame = av_frame_alloc();&#xA;    AVPacket *pkt_out = av_packet_alloc();&#xA;&#xA;    int frame_num = 0; int read_end = 0;&#xA;    &#xA;    for(;;){&#xA;        if( 1 == read_end ){ break;}&#xA;&#xA;        ret = av_read_frame(fmt_ctx, pkt);&#xA;        //Skip and do not process audio packets&#xA;        if( 1 == pkt->stream_index ){&#xA;            av_packet_unref(pt);&#xA;            continue;&#xA;        }&#xA;&#xA;        if ( AVERROR_EOF == ret) {&#xA;            //After reading the file, the data and size of pkt should be null at this time&#xA;            avcodec_send_packet(avctx, NULL);&#xA;        }else {&#xA;            if( 0 != ret){&#xA;                printf("read error code %d \n",ret);&#xA;                return ENOMEM;&#xA;            }else{&#xA;                retry:&#xA;                if (avcodec_send_packet(avctx, pkt) == AVERROR(EAGAIN)) {&#xA;                    printf("Receive_frame and send_packet both returned EAGAIN, which is an API violation.\n");&#xA;                    //Here you can consider sleeping for 0.1 seconds and returning EAGAIN. This is usually because there is a bug in ffmpeg&#x27;s internal API.&#xA;                    goto retry;&#xA;                }&#xA;                //Release the encoded data in pkt&#xA;                av_packet_unref(pt);&#xA;            }&#xA;&#xA;        }&#xA;&#xA;        //The loop keeps reading data from the decoder until there is no more data to read.&#xA;        for(;;){&#xA;            //Read AVFrame&#xA;            ret = avcodec_receive_frame(avctx, frame);&#xA;            /* Release the YUV data in the frame,&#xA;             * Since av_frame_unref is called in the avcodec_receive_frame function, the following code can be commented.&#xA;             * So we don&#x27;t need to manually unref this AVFrame&#xA;             * */&#xA;            //off_frame_unref(frame);&#xA;&#xA;            if( AVERROR(EAGAIN) == ret ){&#xA;                //Prompt EAGAIN means the decoder needs more AVPackets&#xA;                //Jump out of the first layer of for and let the decoder get more AVPackets&#xA;                break;&#xA;            }else if( AVERROR_EOF == ret ){&#xA;                /* The prompt AVERROR_EOF means that an AVPacket with both data and size NULL has been sent to the decoder before.&#xA;                 * Sending NULL AVPacket prompts the decoder to flush out all cached frames.&#xA;                 * Usually a NULL AVPacket is sent only after reading the input file, or when another video stream needs to be decoded with an existing decoder.&#xA;                 *&#xA;                 * */&#xA;&#xA;                /* Send null AVFrame to the encoder and let the encoder flush out the remaining data.&#xA;                 * */&#xA;                ret = avcodec_send_frame(enc_ctx, NULL);&#xA;                for(;;){&#xA;                    ret = avcodec_receive_packet(enc_ctx, pkt_out);&#xA;                    //It is impossible to return EAGAIN here, if there is any, exit directly.&#xA;                    if (ret == AVERROR(EAGAIN)){&#xA;                        printf("avcodec_receive_packet error code %d \n",ret);&#xA;                        return ret;&#xA;                    }&#xA;                    &#xA;                    if ( AVERROR_EOF == ret ){ break; }&#xA;                    &#xA;                    //Encode the AVPacket, print some information first, and then write it to the file.&#xA;                    printf("pkt_out size : %d \n",pkt_out->size);&#xA;                    //Set the stream_index of AVPacket so that you know which stream it is.&#xA;                    pkt_out->stream_index = st->index;&#xA;                    //Convert the time base of AVPacket to the time base of the output stream.&#xA;                    pkt_out->pts = av_rescale_q_rnd(pkt_out->pts, fmt_ctx->streams[0]->time_base, st->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);&#xA;                    pkt_out->dts = av_rescale_q_rnd(pkt_out->dts, fmt_ctx->streams[0]->time_base, st->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);&#xA;                    pkt_out->duration = av_rescale_q_rnd(pkt_out->duration, fmt_ctx->streams[0]->time_base, st->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);&#xA;&#xA;&#xA;                    ret = av_interleaved_write_frame(fmt_ctx_out, pkt_out);&#xA;                    if (ret &lt; 0) {&#xA;                        printf("av_interleaved_write_frame faile %d \n",ret);&#xA;                        return ret;&#xA;                    }&#xA;                    av_packet_unref(pt_out);&#xA;                }&#xA;                av_write_trailer(fmt_ctx_out);&#xA;                //Jump out of the second layer of for, the file has been decoded.&#xA;                read_end = 1;&#xA;                break;&#xA;            }else if( ret >= 0 ){&#xA;                //Only when a frame is decoded can the encoder be initialized.&#xA;                if( NULL == enc_ctx ){&#xA;                    //Open the encoder and set encoding information.&#xA;                    AVCodec *encode = avcodec_find_encoder(AV_CODEC_ID_H264);&#xA;                    enc_ctx = avcodec_alloc_context3(encode);&#xA;                    enc_ctx->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;                    enc_ctx->bit_rate = 400000;&#xA;                    enc_ctx->framerate = avctx->framerate;&#xA;                    enc_ctx->gop_size = 30;&#xA;                    enc_ctx->max_b_frames = 10;&#xA;                    enc_ctx->profile = FF_PROFILE_H264_MAIN;&#xA;                   &#xA;                    /*&#xA;                     * In fact, the following information is also available in the container. You can also open the encoder directly in the container at the beginning.&#xA;                     * I took these encoder parameters from AVFrame because the difference in the container is final.&#xA;                     * Because the AVFrame you decoded may go through a filter, the information will be transformed after the filter, but this article does not use filters.&#xA;                     */&#xA;                     &#xA;                    //The time base of the encoder should be the time base of AVFrame, because AVFrame is the input. The time base of AVFrame is the time base of the stream.&#xA;                    enc_ctx->time_base = fmt_ctx->streams[0]->time_base;&#xA;                    enc_ctx->width = fmt_ctx->streams[0]->codecpar->width;&#xA;                    enc_ctx->height = fmt_ctx->streams[0]->codecpar->height;&#xA;                    enc_ctx->sample_aspect_ratio = st->sample_aspect_ratio = frame->sample_aspect_ratio;&#xA;                    enc_ctx->pix_fmt = frame->format;&#xA;                    enc_ctx->color_range            = frame->color_range;&#xA;                    enc_ctx->color_primaries        = frame->color_primaries;&#xA;                    enc_ctx->color_trc              = frame->color_trc;&#xA;                    enc_ctx->colorspace             = frame->colorspace;&#xA;                    enc_ctx->chroma_sample_location = frame->chroma_location;&#xA;&#xA;                    /* Note that the value of this field_order is different for different videos. I have written it here.&#xA;                     * Because the video in this article is AV_FIELD_PROGRESSIVE&#xA;                     * The production environment needs to process different videos&#xA;                     */&#xA;                    enc_ctx->field_order = AV_FIELD_PROGRESSIVE;&#xA;&#xA;                    /* Now we need to copy the encoder parameters to the stream. When decoding, assign parameters from the stream to the decoder.&#xA;                     * Now let’s do it in reverse.&#xA;                     * */&#xA;                    ret = avcodec_parameters_from_context(st->codecpar,enc_ctx);&#xA;                    if (ret &lt; 0){&#xA;                        printf("error code %d \n",ret);&#xA;                        return ret;&#xA;                    }&#xA;                    if ((ret = avcodec_open2(enc_ctx, encode, NULL)) &lt; 0) {&#xA;                        printf("open codec faile %d \n",ret);&#xA;                        return ret;&#xA;                    }&#xA;&#xA;                    //Formally open the output file&#xA;                    if ((ret = avio_open2(&amp;fmt_ctx_out->pb, filename_out, AVIO_FLAG_WRITE,&amp;fmt_ctx_out->interrupt_callback,NULL)) &lt; 0) {&#xA;                        printf("avio_open2 fail %d \n",ret);&#xA;                        return ret;&#xA;                    }&#xA;&#xA;                    //Write the file header first.&#xA;                    ret = avformat_write_header(fmt_ctx_out,NULL);&#xA;                    if (ret &lt; 0) {&#xA;                        printf("avformat_write_header fail %d \n",ret);&#xA;                        return ret;&#xA;                    }&#xA;&#xA;                }&#xA;&#xA;                //Send AVFrame to the encoder, and then continuously read AVPacket&#xA;                ret = avcodec_send_frame(enc_ctx, frame);&#xA;                if (ret &lt; 0) {&#xA;                    printf("avcodec_send_frame fail %d \n",ret);&#xA;                    return ret;&#xA;                }&#xA;                for(;;){&#xA;                    ret = avcodec_receive_packet(enc_ctx, pkt_out);&#xA;                    if (ret == AVERROR(EAGAIN)){ break; }&#xA;                    &#xA;                    if (ret &lt; 0){&#xA;                    printf("avcodec_receive_packet fail %d \n",ret);&#xA;                    return ret;&#xA;                    }&#xA;                    &#xA;                    //Encode the AVPacket, print some information first, and then write it to the file.&#xA;                    printf("pkt_out size : %d \n",pkt_out->size);&#xA;&#xA;                    //Set the stream_index of AVPacket so that you know which stream it is.&#xA;                    pkt_out->stream_index = st->index;&#xA;                    &#xA;                    //Convert the time base of AVPacket to the time base of the output stream.&#xA;                    pkt_out->pts = av_rescale_q_rnd(pkt_out->pts, fmt_ctx->streams[0]->time_base, st->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);&#xA;                    pkt_out->dts = av_rescale_q_rnd(pkt_out->dts, fmt_ctx->streams[0]->time_base, st->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);&#xA;                    pkt_out->duration = av_rescale_q_rnd(pkt_out->duration, fmt_ctx->streams[0]->time_base, st->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);&#xA;&#xA;                    ret = av_interleaved_write_frame(fmt_ctx_out, pkt_out);&#xA;                    if (ret &lt; 0) {&#xA;                        printf("av_interleaved_write_frame faile %d \n",ret);&#xA;                        return ret;&#xA;                    }&#xA;                    av_packet_unref(pt_out);&#xA;                }&#xA;&#xA;            }&#xA;            else{ printf("other fail \n"); return ret;}&#xA;        }&#xA;    }&#xA;    &#xA;    av_frame_free(&amp;frame); av_packet_free(&amp;pt); av_packet_free(&amp;pkt_out);&#xA;    &#xA;    //Close the encoder and decoder.&#xA;    avcodec_close(avctx); avcodec_close(enc_ctx);&#xA;&#xA;    //Release container memory.&#xA;    avformat_free_context(fmt_ctx);&#xA;&#xA;    //Must adjust avio_closep, otherwise the data may not be written in, it will be 0kb&#xA;    avio_closep(&amp;fmt_ctx_out->pb);&#xA;    avformat_free_context(fmt_ctx_out);&#xA;    printf("done \n");&#xA;&#xA;    return 0;&#xA;}&#xA;

    &#xA;

    This problem has haunted over my head for about three weeks. I still have no idea where the key bug exists. Really appreciate it if any FFmpeg expert could help me.

    &#xA;