Recherche avancée

Médias (91)

Autres articles (74)

  • Websites made ​​with MediaSPIP

    2 mai 2011, par

    This page lists some websites based on MediaSPIP.

  • Personnaliser en ajoutant son logo, sa bannière ou son image de fond

    5 septembre 2013, par

    Certains thèmes prennent en compte trois éléments de personnalisation : l’ajout d’un logo ; l’ajout d’une bannière l’ajout d’une image de fond ;

  • Creating farms of unique websites

    13 avril 2011, par

    MediaSPIP platforms can be installed as a farm, with a single "core" hosted on a dedicated server and used by multiple websites.
    This allows (among other things) : implementation costs to be shared between several different projects / individuals rapid deployment of multiple unique sites creation of groups of like-minded sites, making it possible to browse media in a more controlled and selective environment than the major "open" (...)

Sur d’autres sites (10987)

  • How to reduce time while writing to output stream

    9 février 2021, par Summit

    I am streaming the render ouput of a opengl application using mpegts.The issue that i am facing is that the time taken to encode the frame is quite long.

    


    The application renders at 60 fps with frame size of 1920 X 1080 , the frame data of the application is pushed to a std::queue.

    


    This is the process for ffmpeg.

    


    I  initialize the stream like this.&#xA;&#xA;   streamerUpd.InitUPD("udp://127.0.0.1:1234", 1920, 1080, rings_);&#xA;&#xA;int StreamUPD::InitUPD(const char* url, int width, int height, std::shared_ptr<ringbuffer2> rings)&#xA;{&#xA;&#xA;    rings_ = rings;&#xA;    width_ = width;&#xA;    height_ = height;&#xA;    filename = url;&#xA;    int ret;&#xA;    av_dict_set(&amp;opt, "pkt_size", "1316", 0);&#xA;    &#xA;        &#xA;    avformat_alloc_output_context2(&amp;oc, nullptr, "mpegts", filename);&#xA;    if (!oc) {&#xA;        return 1;&#xA;    }&#xA;&#xA;    fmt = oc->oformat;&#xA;    /* Add the audio and video streams using the default format codecs&#xA;     * and initialize the codecs. */&#xA;    if (fmt->video_codec != AV_CODEC_ID_NONE) {&#xA;        add_stream(&amp;video_st, oc, &amp;video_codec, fmt->video_codec);&#xA;        have_video = 1;&#xA;        encode_video = 1;&#xA;    }&#xA;&#xA;    /* Write the stream header, if any. */&#xA;    ret = avformat_write_header(oc, &amp;opt);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Error occurred when opening output file: %s\n",&#xA;            av_err2str(ret));&#xA;        return 1;&#xA;    }&#xA;    thr = std::thread(&amp;StreamUPD::output_result, this);&#xA;    return 0;&#xA;}&#xA;</ringbuffer2>

    &#xA;

    ////////////////////////////////////////////////////////////////////////////////////////

    &#xA;

    // Add the output stream

    &#xA;

    void StreamUPD::add_stream(OutputStream* ost, AVFormatContext* oc, AVCodec** codec, enum AVCodecID codec_id)&#xA;{&#xA;    AVCodecContext* c;&#xA;    int i;&#xA;    /* find the encoder */&#xA;    *codec = avcodec_find_encoder(codec_id);&#xA;    if (!(*codec)) {&#xA;        fprintf(stderr, "Could not find encoder for &#x27;%s&#x27;\n",&#xA;            avcodec_get_name(codec_id));&#xA;        exit(1);&#xA;    }&#xA;    ost->st = avformat_new_stream(oc, NULL);&#xA;    if (!ost->st) {&#xA;        fprintf(stderr, "Could not allocate stream\n");&#xA;        exit(1);&#xA;    }&#xA;    ost->st->id = oc->nb_streams - 1;&#xA;    c = avcodec_alloc_context3(*codec);&#xA;    if (!c) {&#xA;        fprintf(stderr, "Could not alloc an encoding context\n");&#xA;        exit(1);&#xA;    }&#xA;    ost->enc = c;&#xA;    switch ((*codec)->type) {&#xA;    case AVMEDIA_TYPE_VIDEO:&#xA;        c->codec_id = codec_id;&#xA;        c->bit_rate = 400000;&#xA;&#xA;        /* Resolution must be a multiple of two. */&#xA;        c->width = width_;&#xA;        c->height = height_;&#xA;        /* timebase: This is the fundamental unit of time (in seconds) in terms&#xA;         * of which frame timestamps are represented. For fixed-fps content,&#xA;         * timebase should be 1/framerate and timestamp increments should be&#xA;         * identical to 1. */&#xA;        ost->st->time_base = { 1, STREAM_FRAME_RATE };&#xA;        c->time_base = ost->st->time_base;&#xA;        c->gop_size = 12; /* emit one intra frame every twelve frames at most */&#xA;        c->pix_fmt = STREAM_PIX_FMT;&#xA;        &#xA;        if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {&#xA;            /* just for testing, we also add B-frames */&#xA;            qDebug() &lt;&lt; "This is MPEG2VIDEO Frame";&#xA;            c->max_b_frames = 2;&#xA;            &#xA;        }&#xA;        if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {&#xA;            /* Needed to avoid using macroblocks in which some coeffs overflow.&#xA;             * This does not happen with normal video, it just happens here as&#xA;             * the motion of the chroma plane does not match the luma plane. */&#xA;            c->mb_decision = 2;&#xA;        }&#xA;        break;&#xA;    default:&#xA;        break;&#xA;    }&#xA;    /* Some formats want stream headers to be separate. */&#xA;    if (oc->oformat->flags &amp; AVFMT_GLOBALHEADER)&#xA;        c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;&#xA;}&#xA;

    &#xA;

    //////////////////////////////////////////////////////////////////////////////////

    &#xA;

    // Open the video

    &#xA;

    void StreamUPD::open_video(AVFormatContext* oc, AVCodec* codec, OutputStream* ost, AVDictionary* opt_arg)&#xA;    {&#xA;        int ret;&#xA;        AVCodecContext* c = ost->enc;&#xA;        AVDictionary* opt = NULL;&#xA;        av_dict_copy(&amp;opt, opt_arg, 0);&#xA;        /* open the codec */&#xA;        ret = avcodec_open2(c, codec, &amp;opt);&#xA;        av_dict_free(&amp;opt);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret));&#xA;            exit(1);&#xA;        }&#xA;        /* allocate and init a re-usable frame */&#xA;        ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);&#xA;        if (!ost->frame) {&#xA;            fprintf(stderr, "Could not allocate video frame\n");&#xA;            exit(1);&#xA;        }&#xA;        /* If the output format is not YUV420P, then a temporary YUV420P&#xA;         * picture is needed too. It is then converted to the required&#xA;         * output format. */&#xA;        ost->tmp_frame = NULL;&#xA;        if (c->pix_fmt != AV_PIX_FMT_YUV420P) {&#xA;            ost->tmp_frame = alloc_picture(AV_PIX_FMT_YUV420P, c->width, c->height);&#xA;            if (!ost->tmp_frame) {&#xA;                fprintf(stderr, "Could not allocate temporary picture\n");&#xA;                exit(1);&#xA;            }&#xA;        }&#xA;        /* copy the stream parameters to the muxer */&#xA;        ret = avcodec_parameters_from_context(ost->st->codecpar, c);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Could not copy the stream parameters\n");&#xA;            exit(1);&#xA;        }&#xA;    }&#xA;

    &#xA;

    Once i have setup the ffmpeg output stream this is how i am streaming the data.

    &#xA;

    This function gets the frame data from the std::queue(pixelsQueue) and sends it for encoding.

    &#xA;

    int StreamUPD::stream_video_frame()&#xA;{   &#xA;    ost = &amp;video_st;&#xA;    c = ost->enc;   &#xA;&#xA;    /* when we pass a frame to the encoder, it may keep a reference to it&#xA;     * internally; make sure we do not overwrite it here */&#xA;    if (av_frame_make_writable(ost->frame) &lt; 0)&#xA;        exit(1);&#xA;    if (!ost->sws_ctx) {&#xA;        ost->sws_ctx = sws_getContext(c->width, c->height,&#xA;            AV_PIX_FMT_RGB24,&#xA;            c->width, c->height,&#xA;            c->pix_fmt,&#xA;            SWS_FAST_BILINEAR, NULL, NULL, NULL);&#xA;        if (!ost->sws_ctx) {&#xA;            fprintf(stderr,&#xA;                "Could not initialize the conversion context\n");&#xA;            exit(1);&#xA;        }&#xA;    }&#xA;    finished_ = true;&#xA;&#xA;    if (pixelsQueue.size() > 0) {       &#xA;        if (pixelsQueue.pop(pixels)) {&#xA;            fill_yuv_image(ost->sws_ctx, frame_data->pixels_.get(), ost->frame, c->width, c->height);&#xA;            ost->frame->pts = ost->next_pts&#x2B;&#x2B;;&#xA;            return write_frame(oc, ost->enc, ost->st, ost->frame);&#xA;        }&#xA;    }&#xA;    return 1;&#xA;}&#xA;

    &#xA;

    Writing the data to the output stream.

    &#xA;

    The function avcodec_receive_packet is the one that takes lot of time.

    &#xA;

    int StreamUPD::write_frame(AVFormatContext* fmt_ctx, AVCodecContext* c,&#xA;    AVStream* st, AVFrame* frame)&#xA;{&#xA;    int ret;&#xA;    // send the frame to the encoder&#xA;    AVPacket pkt = { 0 };&#xA;    ret = avcodec_send_frame(c, frame);&#xA;    ret = avcodec_receive_packet(c, &amp;pkt);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Error sending a frame to the encoder: %s\n",&#xA;            av_err2str(ret));&#xA;        exit(1);&#xA;    }&#xA;    &#xA;    while (ret >= 0) {&#xA;        AVPacket pkt = { 0 };&#xA;        ret = avcodec_receive_packet(c, &amp;pkt);  // This is the function that takes lot of time&#xA;        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)&#xA;            break;&#xA;        else if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error encoding a frame: %s\n", av_err2str(ret));&#xA;            exit(1);&#xA;        }&#xA;        // rescale output packet timestamp values from codec to stream timebase &#xA;        av_packet_rescale_ts(&amp;pkt, c->time_base, st->time_base);&#xA;        pkt.stream_index = st->index;&#xA;        // Write the compressed frame to the media file. &#xA;        ret = av_interleaved_write_frame(fmt_ctx, &amp;pkt);&#xA;        av_packet_unref(&amp;pkt);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error while writing output packet: %s\n", av_err2str(ret));&#xA;            exit(1);&#xA;        }&#xA;    }&#xA;    return ret == AVERROR_EOF ? 1 : 0;&#xA;}&#xA;

    &#xA;

    How can i reduce the outputting time while writing the frames to the stream ?

    &#xA;

    Currently i push more frames in the buffer and the outputting speed is less so the buffer starts to run out of memory in some time.

    &#xA;

  • how to extract audio from video using ffmpeg c++

    29 septembre 2020, par daenerys

    I am trying to write c++ code on how to extract audio from mp4 format file. I have compiled the examples in ffmpeg library and tried to run the demuxing_decoding.c file. The problem is that on running the code, it starts to decode way more than the actual file size and decodes wrong codec format (the decoded files cant be run).

    &#xA;&#xA;

    here is the demuxing_decoding.c that I am trying to run :

    &#xA;&#xA;

    #include &#xA;&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavutil></libavutil>channel_layout.h>&#xA;#include <libavutil></libavutil>common.h>&#xA;#include <libavutil></libavutil>imgutils.h>&#xA;#include <libavutil></libavutil>mathematics.h>&#xA;#include <libavutil></libavutil>samplefmt.h>&#xA;&#xA;#define INBUF_SIZE 4096&#xA;#define AUDIO_INBUF_SIZE 20480&#xA;#define AUDIO_REFILL_THRESH 4096&#xA;&#xA;/* check that a given sample format is supported by the encoder */&#xA;static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)&#xA;{&#xA;    const enum AVSampleFormat *p = codec->sample_fmts;&#xA;&#xA;    while (*p != AV_SAMPLE_FMT_NONE) {&#xA;        if (*p == sample_fmt)&#xA;            return 1;&#xA;        p&#x2B;&#x2B;;&#xA;    }&#xA;    return 0;&#xA;}&#xA;&#xA;/* just pick the highest supported samplerate */&#xA;static int select_sample_rate(AVCodec *codec)&#xA;{&#xA;    const int *p;&#xA;    int best_samplerate = 0;&#xA;&#xA;    if (!codec->supported_samplerates)&#xA;        return 44100;&#xA;&#xA;    p = codec->supported_samplerates;&#xA;    while (*p) {&#xA;        best_samplerate = FFMAX(*p, best_samplerate);&#xA;        p&#x2B;&#x2B;;&#xA;    }&#xA;    return best_samplerate;&#xA;}&#xA;&#xA;/* select layout with the highest channel count */&#xA;static int select_channel_layout(AVCodec *codec)&#xA;{&#xA;    const uint64_t *p;&#xA;    uint64_t best_ch_layout = 0;&#xA;    int best_nb_channels   = 0;&#xA;&#xA;    if (!codec->channel_layouts)&#xA;        return AV_CH_LAYOUT_STEREO;&#xA;&#xA;    p = codec->channel_layouts;&#xA;    while (*p) {&#xA;        int nb_channels = av_get_channel_layout_nb_channels(*p);&#xA;&#xA;        if (nb_channels > best_nb_channels) {&#xA;            best_ch_layout    = *p;&#xA;            best_nb_channels = nb_channels;&#xA;        }&#xA;        p&#x2B;&#x2B;;&#xA;    }&#xA;    return best_ch_layout;&#xA;}&#xA;&#xA;/*&#xA; * Audio encoding example&#xA; */&#xA;static void audio_encode_example(const char *filename)&#xA;{&#xA;    AVCodec *codec;&#xA;    AVCodecContext *c= NULL;&#xA;    AVFrame *frame;&#xA;    AVPacket pkt;&#xA;    int i, j, k, ret, got_output;&#xA;    int buffer_size;&#xA;    FILE *f;&#xA;    uint16_t *samples;&#xA;    float t, tincr;&#xA;&#xA;    printf("Encode audio file %s\n", filename);&#xA;&#xA;    /* find the MP2 encoder */&#xA;    codec = avcodec_find_encoder(AV_CODEC_ID_MP2);&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec not found\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    c = avcodec_alloc_context3(codec);&#xA;    if (!c) {&#xA;        fprintf(stderr, "Could not allocate audio codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* put sample parameters */&#xA;    c->bit_rate = 64000;&#xA;&#xA;    /* check that the encoder supports s16 pcm input */&#xA;    c->sample_fmt = AV_SAMPLE_FMT_S16;&#xA;    if (!check_sample_fmt(codec, c->sample_fmt)) {&#xA;        fprintf(stderr, "Encoder does not support sample format %s",&#xA;                av_get_sample_fmt_name(c->sample_fmt));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* select other audio parameters supported by the encoder */&#xA;    c->sample_rate    = select_sample_rate(codec);&#xA;    c->channel_layout = select_channel_layout(codec);&#xA;    c->channels       = av_get_channel_layout_nb_channels(c->channel_layout);&#xA;&#xA;    /* open it */&#xA;    if (avcodec_open2(c, codec, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    f = fopen(filename, "wb");&#xA;    if (!f) {&#xA;        fprintf(stderr, "Could not open %s\n", filename);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* frame containing input raw audio */&#xA;    frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        fprintf(stderr, "Could not allocate audio frame\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    frame->nb_samples     = c->frame_size;&#xA;    frame->format         = c->sample_fmt;&#xA;    frame->channel_layout = c->channel_layout;&#xA;&#xA;    /* the codec gives us the frame size, in samples,&#xA;     * we calculate the size of the samples buffer in bytes */&#xA;    buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,&#xA;                                             c->sample_fmt, 0);&#xA;    if (buffer_size &lt; 0) {&#xA;        fprintf(stderr, "Could not get sample buffer size\n");&#xA;        exit(1);&#xA;    }&#xA;    samples = av_malloc(buffer_size);&#xA;    if (!samples) {&#xA;        fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",&#xA;                buffer_size);&#xA;        exit(1);&#xA;    }&#xA;    /* setup the data pointers in the AVFrame */&#xA;    ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,&#xA;                                   (const uint8_t*)samples, buffer_size, 0);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not setup audio frame\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* encode a single tone sound */&#xA;    t = 0;&#xA;    tincr = 2 * M_PI * 440.0 / c->sample_rate;&#xA;    for (i = 0; i &lt; 200; i&#x2B;&#x2B;) {&#xA;        av_init_packet(&amp;pkt);&#xA;        pkt.data = NULL; // packet data will be allocated by the encoder&#xA;        pkt.size = 0;&#xA;&#xA;        for (j = 0; j &lt; c->frame_size; j&#x2B;&#x2B;) {&#xA;            samples[2*j] = (int)(sin(t) * 10000);&#xA;&#xA;            for (k = 1; k &lt; c->channels; k&#x2B;&#x2B;)&#xA;                samples[2*j &#x2B; k] = samples[2*j];&#xA;            t &#x2B;= tincr;&#xA;        }&#xA;        /* encode the samples */&#xA;        ret = avcodec_encode_audio2(c, &amp;pkt, frame, &amp;got_output);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error encoding audio frame\n");&#xA;            exit(1);&#xA;        }&#xA;        if (got_output) {&#xA;            fwrite(pkt.data, 1, pkt.size, f);&#xA;            av_free_packet(&amp;pkt);&#xA;        }&#xA;    }&#xA;&#xA;    /* get the delayed frames */&#xA;    for (got_output = 1; got_output; i&#x2B;&#x2B;) {&#xA;        ret = avcodec_encode_audio2(c, &amp;pkt, NULL, &amp;got_output);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error encoding frame\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        if (got_output) {&#xA;            fwrite(pkt.data, 1, pkt.size, f);&#xA;            av_free_packet(&amp;pkt);&#xA;        }&#xA;    }&#xA;    fclose(f);&#xA;&#xA;    av_freep(&amp;samples);&#xA;    av_frame_free(&amp;frame);&#xA;    avcodec_close(c);&#xA;    av_free(c);&#xA;}&#xA;&#xA;/*&#xA; * Audio decoding.&#xA; */&#xA;static void audio_decode_example(const char *outfilename, const char *filename)&#xA;{&#xA;    AVCodec *codec;&#xA;    AVCodecContext *c= NULL;&#xA;    int len;&#xA;    FILE *f, *outfile;&#xA;    uint8_t inbuf[AUDIO_INBUF_SIZE &#x2B; AV_INPUT_BUFFER_PADDING_SIZE];&#xA;    AVPacket avpkt;&#xA;    AVFrame *decoded_frame = NULL;&#xA;&#xA;    av_init_packet(&amp;avpkt);&#xA;&#xA;    printf("Decode audio file %s to %s\n", filename, outfilename);&#xA;&#xA;    /* find the mpeg audio decoder */&#xA;    codec = avcodec_find_decoder(AV_CODEC_ID_MP2);&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec not found\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    c = avcodec_alloc_context3(codec);&#xA;    if (!c) {&#xA;        fprintf(stderr, "Could not allocate audio codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* open it */&#xA;    if (avcodec_open2(c, codec, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    f = fopen(filename, "rb");&#xA;    if (!f) {&#xA;        fprintf(stderr, "Could not open %s\n", filename);&#xA;        exit(1);&#xA;    }&#xA;    outfile = fopen(outfilename, "wb");&#xA;    if (!outfile) {&#xA;        av_free(c);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* decode until eof */&#xA;    avpkt.data = inbuf;&#xA;    avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);&#xA;&#xA;    while (avpkt.size > 0) {&#xA;        int i, ch;&#xA;        int got_frame = 0;&#xA;&#xA;        if (!decoded_frame) {&#xA;            if (!(decoded_frame = av_frame_alloc())) {&#xA;                fprintf(stderr, "Could not allocate audio frame\n");&#xA;                exit(1);&#xA;            }&#xA;        }&#xA;&#xA;        len = avcodec_decode_audio4(c, decoded_frame, &amp;got_frame, &amp;avpkt);&#xA;        if (len &lt; 0) {&#xA;            fprintf(stderr, "Error while decoding\n");&#xA;            exit(1);&#xA;        }&#xA;        if (got_frame) {&#xA;            /* if a frame has been decoded, output it */&#xA;            int data_size = av_get_bytes_per_sample(c->sample_fmt);&#xA;            if (data_size &lt; 0) {&#xA;                /* This should not occur, checking just for paranoia */&#xA;                fprintf(stderr, "Failed to calculate data size\n");&#xA;                exit(1);&#xA;            }&#xA;            for (i=0; inb_samples; i&#x2B;&#x2B;)&#xA;                for (ch=0; chchannels; ch&#x2B;&#x2B;)&#xA;                    fwrite(decoded_frame->data[ch] &#x2B; data_size*i, 1, data_size, outfile);&#xA;        }&#xA;        avpkt.size -= len;&#xA;        avpkt.data &#x2B;= len;&#xA;        avpkt.dts =&#xA;        avpkt.pts = AV_NOPTS_VALUE;&#xA;        if (avpkt.size &lt; AUDIO_REFILL_THRESH) {&#xA;            /* Refill the input buffer, to avoid trying to decode&#xA;             * incomplete frames. Instead of this, one could also use&#xA;             * a parser, or use a proper container format through&#xA;             * libavformat. */&#xA;            memmove(inbuf, avpkt.data, avpkt.size);&#xA;            avpkt.data = inbuf;&#xA;            len = fread(avpkt.data &#x2B; avpkt.size, 1,&#xA;                        AUDIO_INBUF_SIZE - avpkt.size, f);&#xA;            if (len > 0)&#xA;                avpkt.size &#x2B;= len;&#xA;        }&#xA;    }&#xA;&#xA;    fclose(outfile);&#xA;    fclose(f);&#xA;&#xA;    avcodec_close(c);&#xA;    av_free(c);&#xA;    av_frame_free(&amp;decoded_frame);&#xA;}&#xA;&#xA;/*&#xA; * Video encoding example&#xA; */&#xA;static void video_encode_example(const char *filename, int codec_id)&#xA;{&#xA;    AVCodec *codec;&#xA;    AVCodecContext *c= NULL;&#xA;    int i, ret, x, y, got_output;&#xA;    FILE *f;&#xA;    AVFrame *frame;&#xA;    AVPacket pkt;&#xA;    uint8_t endcode[] = { 0, 0, 1, 0xb7 };&#xA;&#xA;    printf("Encode video file %s\n", filename);&#xA;&#xA;    /* find the mpeg1 video encoder */&#xA;    codec = avcodec_find_encoder(codec_id);&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec not found\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    c = avcodec_alloc_context3(codec);&#xA;    if (!c) {&#xA;        fprintf(stderr, "Could not allocate video codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* put sample parameters */&#xA;    c->bit_rate = 400000;&#xA;    /* resolution must be a multiple of two */&#xA;    c->width = 352;&#xA;    c->height = 288;&#xA;    /* frames per second */&#xA;    c->time_base = (AVRational){1,25};&#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;    c->gop_size = 10;&#xA;    c->max_b_frames = 1;&#xA;    c->pix_fmt = AV_PIX_FMT_YUV420P;&#xA;&#xA;    if (codec_id == AV_CODEC_ID_H264)&#xA;        av_opt_set(c->priv_data, "preset", "slow", 0);&#xA;&#xA;    /* open it */&#xA;    if (avcodec_open2(c, codec, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    f = fopen(filename, "wb");&#xA;    if (!f) {&#xA;        fprintf(stderr, "Could not open %s\n", filename);&#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->width  = c->width;&#xA;    frame->height = c->height;&#xA;&#xA;    /* the image can be allocated by any means and av_image_alloc() is&#xA;     * just the most convenient way if av_malloc() is to be used */&#xA;    ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height,&#xA;                         c->pix_fmt, 32);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not allocate raw picture buffer\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* encode 1 second of video */&#xA;    for (i = 0; i &lt; 25; i&#x2B;&#x2B;) {&#xA;        av_init_packet(&amp;pkt);&#xA;        pkt.data = NULL;    // packet data will be allocated by the encoder&#xA;        pkt.size = 0;&#xA;&#xA;        fflush(stdout);&#xA;        /* prepare a dummy image */&#xA;        /* Y */&#xA;        for (y = 0; y &lt; c->height; y&#x2B;&#x2B;) {&#xA;            for (x = 0; x &lt; c->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; c->height/2; y&#x2B;&#x2B;) {&#xA;            for (x = 0; x &lt; c->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;        ret = avcodec_encode_video2(c, &amp;pkt, frame, &amp;got_output);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error encoding frame\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        if (got_output) {&#xA;            printf("Write frame %3d (size=%5d)\n", i, pkt.size);&#xA;            fwrite(pkt.data, 1, pkt.size, f);&#xA;            av_free_packet(&amp;pkt);&#xA;        }&#xA;    }&#xA;&#xA;    /* get the delayed frames */&#xA;    for (got_output = 1; got_output; i&#x2B;&#x2B;) {&#xA;        fflush(stdout);&#xA;&#xA;        ret = avcodec_encode_video2(c, &amp;pkt, NULL, &amp;got_output);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error encoding frame\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        if (got_output) {&#xA;            printf("Write frame %3d (size=%5d)\n", i, pkt.size);&#xA;            fwrite(pkt.data, 1, pkt.size, f);&#xA;            av_free_packet(&amp;pkt);&#xA;        }&#xA;    }&#xA;&#xA;    /* add sequence end code to have a real mpeg file */&#xA;    fwrite(endcode, 1, sizeof(endcode), f);&#xA;    fclose(f);&#xA;&#xA;    avcodec_close(c);&#xA;    av_free(c);&#xA;    av_freep(&amp;frame->data[0]);&#xA;    av_frame_free(&amp;frame);&#xA;    printf("\n");&#xA;}&#xA;&#xA;/*&#xA; * Video decoding example&#xA; */&#xA;&#xA;static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,&#xA;                     char *filename)&#xA;{&#xA;    FILE *f;&#xA;    int i;&#xA;&#xA;    f = fopen(filename,"w");&#xA;    fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);&#xA;    for (i = 0; i &lt; ysize; i&#x2B;&#x2B;)&#xA;        fwrite(buf &#x2B; i * wrap, 1, xsize, f);&#xA;    fclose(f);&#xA;}&#xA;&#xA;static int decode_write_frame(const char *outfilename, AVCodecContext *avctx,&#xA;                              AVFrame *frame, int *frame_count, AVPacket *pkt, int last)&#xA;{&#xA;    int len, got_frame;&#xA;    char buf[1024];&#xA;&#xA;    len = avcodec_decode_video2(avctx, frame, &amp;got_frame, pkt);&#xA;    if (len &lt; 0) {&#xA;        fprintf(stderr, "Error while decoding frame %d\n", *frame_count);&#xA;        return len;&#xA;    }&#xA;    if (got_frame) {&#xA;        printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count);&#xA;        fflush(stdout);&#xA;&#xA;        /* the picture is allocated by the decoder, no need to free it */&#xA;        snprintf(buf, sizeof(buf), outfilename, *frame_count);&#xA;        pgm_save(frame->data[0], frame->linesize[0],&#xA;                 frame->width, frame->height, buf);&#xA;        (*frame_count)&#x2B;&#x2B;;&#xA;    }&#xA;    if (pkt->data) {&#xA;        pkt->size -= len;&#xA;        pkt->data &#x2B;= len;&#xA;    }&#xA;    return 0;&#xA;}&#xA;&#xA;static void video_decode_example(const char *outfilename, const char *filename)&#xA;{&#xA;    AVCodec *codec;&#xA;    AVCodecContext *c= NULL;&#xA;    int frame_count;&#xA;    FILE *f;&#xA;    AVFrame *frame;&#xA;    uint8_t inbuf[INBUF_SIZE &#x2B; AV_INPUT_BUFFER_PADDING_SIZE];&#xA;    AVPacket avpkt;&#xA;&#xA;    av_init_packet(&amp;avpkt);&#xA;&#xA;    /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */&#xA;    memset(inbuf &#x2B; INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);&#xA;&#xA;    printf("Decode video file %s to %s\n", filename, outfilename);&#xA;&#xA;    /* find the mpeg1 video decoder */&#xA;    codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec not found\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    c = avcodec_alloc_context3(codec);&#xA;    if (!c) {&#xA;        fprintf(stderr, "Could not allocate video codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    if (codec->capabilities &amp; AV_CODEC_CAP_TRUNCATED)&#xA;        c->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames&#xA;&#xA;    /* For some codecs, such as msmpeg4 and mpeg4, width and height&#xA;       MUST be initialized there because this information is not&#xA;       available in the bitstream. */&#xA;&#xA;    /* open it */&#xA;    if (avcodec_open2(c, codec, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    f = fopen(filename, "rb");&#xA;    if (!f) {&#xA;        fprintf(stderr, "Could not open %s\n", filename);&#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;&#xA;    frame_count = 0;&#xA;    for (;;) {&#xA;        avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);&#xA;        if (avpkt.size == 0)&#xA;            break;&#xA;&#xA;        /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)&#xA;           and this is the only method to use them because you cannot&#xA;           know the compressed data size before analysing it.&#xA;&#xA;           BUT some other codecs (msmpeg4, mpeg4) are inherently frame&#xA;           based, so you must call them with all the data for one&#xA;           frame exactly. You must also initialize &#x27;width&#x27; and&#xA;           &#x27;height&#x27; before initializing them. */&#xA;&#xA;        /* NOTE2: some codecs allow the raw parameters (frame size,&#xA;           sample rate) to be changed at any frame. We handle this, so&#xA;           you should also take care of it */&#xA;&#xA;        /* here, we use a stream based decoder (mpeg1video), so we&#xA;           feed decoder and see if it could decode a frame */&#xA;        avpkt.data = inbuf;&#xA;        while (avpkt.size > 0)&#xA;            if (decode_write_frame(outfilename, c, frame, &amp;frame_count, &amp;avpkt, 0) &lt; 0)&#xA;                exit(1);&#xA;    }&#xA;&#xA;    /* some codecs, such as MPEG, transmit the I and P frame with a&#xA;       latency of one frame. You must do the following to have a&#xA;       chance to get the last frame of the video */&#xA;    avpkt.data = NULL;&#xA;    avpkt.size = 0;&#xA;    decode_write_frame(outfilename, c, frame, &amp;frame_count, &amp;avpkt, 1);&#xA;&#xA;    fclose(f);&#xA;&#xA;    avcodec_close(c);&#xA;    av_free(c);&#xA;    av_frame_free(&amp;frame);&#xA;    printf("\n");&#xA;}&#xA;&#xA;int main(int argc, char **argv)&#xA;{&#xA;    const char *output_type;&#xA;&#xA;    /* register all the codecs */&#xA;    avcodec_register_all();&#xA;&#xA;    if (argc &lt; 2) {&#xA;        printf("usage: %s output_type\n"&#xA;               "API example program to decode/encode a media stream with libavcodec.\n"&#xA;               "This program generates a synthetic stream and encodes it to a file\n"&#xA;               "named test.h264, test.mp2 or test.mpg depending on output_type.\n"&#xA;               "The encoded stream is then decoded and written to a raw data output.\n"&#xA;               "output_type must be chosen between &#x27;h264&#x27;, &#x27;mp2&#x27;, &#x27;mpg&#x27;.\n",&#xA;               argv[0]);&#xA;        return 1;&#xA;    }&#xA;    output_type = argv[1];&#xA;&#xA;    if (!strcmp(output_type, "h264")) {&#xA;        video_encode_example("test.h264", AV_CODEC_ID_H264);&#xA;    } else if (!strcmp(output_type, "mp2")) {&#xA;        audio_encode_example("test.mp2");&#xA;        audio_decode_example("test.pcm", "test.mp2");&#xA;    } else if (!strcmp(output_type, "mpg")) {&#xA;        video_encode_example("test.mpg", AV_CODEC_ID_MPEG1VIDEO);&#xA;        video_decode_example("test%02d.pgm", "test.mpg");&#xA;    } else {&#xA;        fprintf(stderr, "Invalid output type &#x27;%s&#x27;, choose between &#x27;h264&#x27;, &#x27;mp2&#x27;, or &#x27;mpg&#x27;\n",&#xA;                output_type);&#xA;        return 1;&#xA;    }&#xA;&#xA;    return 0;&#xA;}&#xA;

    &#xA;&#xA;

    What could be wrong ?&#xA;The command I used is :

    &#xA;&#xA;

    ./demuxing_decoding /home/cortana/Burn.mp4 /home/cortana/Desktop/Burn.mp4 /home/cortana/Desktop/Burn.aac&#xA;

    &#xA;

  • FFMPEG API Mp4 H264 Encoding/Muxing - unspecified pixel format

    28 juillet 2020, par Fabrice

    I'm working on a c++ project using ffmpeg. I have to generate an mp4 file with h264 encoding.

    &#xA;

    My problem is that the file generate but when reading the file with VLC I've got no image, and analyzing it with ffprobe give me (log below) the error :

    &#xA;

    &#xA;

    unspecified pixel format

    &#xA;

    &#xA;

    ffprobe version N-93020-g3224d6691c Copyright (c) 2007-2019 the FFmpeg developers&#xA;  built with gcc 8.2.1 (GCC) 20181201&#xA;  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt&#xA;  libavutil      56. 26.100 / 56. 26.100&#xA;  libavcodec     58. 44.100 / 58. 44.100&#xA;  libavformat    58. 26.100 / 58. 26.100&#xA;  libavdevice    58.  6.101 / 58.  6.101&#xA;  libavfilter     7. 48.100 /  7. 48.100&#xA;  libswscale      5.  4.100 /  5.  4.100&#xA;  libswresample   3.  4.100 /  3.  4.100&#xA;  libpostproc    55.  4.100 / 55.  4.100&#xA;[h264 @ 02a46240] non-existing PPS 0 referenced&#xA;[h264 @ 02a46240] decode_slice_header error&#xA;[h264 @ 02a46240] no frame!&#xA;...&#xA;[h264 @ 02a46240] non-existing PPS 0 referenced&#xA;[h264 @ 02a46240] decode_slice_header error&#xA;[h264 @ 02a46240] no frame!&#xA;[mov,mp4,m4a,3gp,3g2,mj2 @ 02a35380] decoding for stream 0 failed&#xA;[mov,mp4,m4a,3gp,3g2,mj2 @ 02a35380] Could not find codec parameters for stream 0 (Video: h264 (avc1 / 0x31637661), none, 352x288, 320 kb/s): unspecified pixel format&#xA;Consider increasing the value for the &#x27;analyzeduration&#x27; and &#x27;probesize&#x27; options&#xA;Input #0, mov,mp4,m4a,3gp,3g2,mj2, from &#x27;C:\Users\Fabrice\Desktop\video\Test.mp4&#x27;:&#xA;  Metadata:&#xA;    major_brand     : isom&#xA;    minor_version   : 512&#xA;    compatible_brands: isomiso2avc1mp41&#xA;    encoder         : Lavf58.26.100&#xA;  Duration: 00:00:09.00, start: 0.000000, bitrate: 323 kb/s&#xA;    Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 352x288, 320 kb/s, 25.11 fps, 25 tbr, 12800 tbn, 25600 tbc (default)&#xA;    Metadata:&#xA;      handler_name    : VideoHandler&#xA;

    &#xA;

    Here is the code I use to genererate my mp4 File, it's based on sample from ffmpeg (see : FFMPEG Muxing sample). I have tried to adapt it without using deprecated function. It works using webm/vp8 encoding, but not mp4/h264.

    &#xA;

    #include &#xA;#include &#xA;#include &#xA;extern "C"&#xA;{&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libswscale></libswscale>swscale.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavutil></libavutil>error.h> &#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libavutil></libavutil>imgutils.h>&#xA;}&#xA;&#xA;#pragma comment(lib, "avcodec.lib")&#xA;#pragma comment(lib, "swscale.lib")&#xA;#pragma comment(lib, "avformat.lib")&#xA;#pragma comment(lib, "avutil.lib")&#xA;&#xA;/* 10 seconds stream duration */&#xA;#define STREAM_DURATION   10.0&#xA;#define STREAM_FRAME_RATE 25 /* 25 images/s */&#xA;#define STREAM_NB_FRAMES  ((int)(STREAM_DURATION * STREAM_FRAME_RATE))&#xA;#define STREAM_PIX_FMT    AV_PIX_FMT_YUV420P /* default pix_fmt */&#xA;&#xA;//#pragma warning(disable : 4996) // TODO: remove&#xA;&#xA;static int sws_flags = SWS_BICUBIC;&#xA;&#xA;/* Add an output stream. */&#xA;static AVStream *add_stream(AVFormatContext *formatContext, AVCodec **codec, enum AVCodecID codecId, AVCodecContext **codecCtx)&#xA;{&#xA;    AVStream *stream;&#xA;&#xA;    // Get the encoder codec&#xA;    *codec = avcodec_find_encoder(codecId);&#xA;    if (!(*codec)) {&#xA;        fprintf(stderr, "Could not find encoder for &#x27;%s&#x27;\n",&#xA;            avcodec_get_name(codecId));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Get the stream for codec&#xA;    stream = avformat_new_stream(formatContext, *codec);&#xA;    if (!stream) {&#xA;        fprintf(stderr, "Could not allocate stream\n");&#xA;        exit(1);&#xA;    }&#xA;    stream->id = formatContext->nb_streams - 1;&#xA;&#xA;    (*codecCtx) = avcodec_alloc_context3(*codec);&#xA;&#xA;    switch ((*codec)->type) {&#xA;    case AVMEDIA_TYPE_VIDEO:&#xA;        stream->codecpar->codec_id = codecId;&#xA;        stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;        stream->codecpar->bit_rate = 400000;&#xA;        stream->codecpar->width = 352;&#xA;        stream->codecpar->height = 288;&#xA;        stream->codecpar->format = STREAM_PIX_FMT;&#xA;        stream->time_base = { 1, STREAM_FRAME_RATE };&#xA;&#xA;        avcodec_parameters_to_context((*codecCtx), stream->codecpar);&#xA;        (*codecCtx)->gop_size = 12; /* emit one intra frame every twelve frames at most */&#xA;        (*codecCtx)->max_b_frames = 2;&#xA;        (*codecCtx)->time_base = { 1, STREAM_FRAME_RATE };&#xA;        if ((*codecCtx)->codec_id == AV_CODEC_ID_MPEG1VIDEO) {&#xA;            /* Needed to avoid using macroblocks in which some coeffs overflow.&#xA;             * This does not happen with normal video, it just happens here as&#xA;             * the motion of the chroma plane does not match the luma plane. */&#xA;            (*codecCtx)->mb_decision = 2;&#xA;        }&#xA;        break;&#xA;&#xA;    default:&#xA;        break;&#xA;    }&#xA;    &#xA;    //if (stream->codecpar->codec_id == AV_CODEC_ID_H264) {&#xA;    //  av_opt_set(codecCtx, "preset", "ultrafast", 0);&#xA;    //}&#xA;    //(*codecCtx)->flags |= AV_CODEC_FLAG_LOW_DELAY;&#xA;&#xA;    /* Some formats want stream headers to be separate. */&#xA;    if (formatContext->oformat->flags &amp; AVFMT_GLOBALHEADER)&#xA;        (*codecCtx)->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;&#xA;&#xA;&#xA;    int ret = avcodec_parameters_from_context(stream->codecpar, (*codecCtx));&#xA;    if (ret &lt; 0) {&#xA;        char error[255];&#xA;        av_strerror(ret, error, 255);&#xA;        fprintf(stderr, "avcodec_parameters_from_context returned (%d) - %s", ret, error);&#xA;        return false;&#xA;    }&#xA;&#xA;    return stream;&#xA;}&#xA;&#xA;/**************************************************************/&#xA;/* video output */&#xA;&#xA;static AVFrame *frame_video;&#xA;static int frame_count;&#xA;&#xA;static void open_video(AVCodec *codec, AVStream *stream, AVCodecContext *codecCtx)&#xA;{&#xA;    int ret;&#xA;&#xA;    /* open the codec */&#xA;    ret = avcodec_open2(codecCtx, codec, NULL);&#xA;    if (ret &lt; 0) {&#xA;        char error[255];&#xA;        av_strerror(ret, error, 255);&#xA;        fprintf(stderr, "Could not open video codec: %s\n", error);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* allocate and init a re-usable frame */&#xA;    frame_video = av_frame_alloc();&#xA;    if (!frame_video) {&#xA;        fprintf(stderr, "Could not allocate video frame\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    frame_video->format = codecCtx->pix_fmt;&#xA;    frame_video->width = codecCtx->width;&#xA;    frame_video->height = codecCtx->height;&#xA;&#xA;    ret = av_frame_get_buffer(frame_video, 32);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not allocate the video frame data\n");&#xA;        exit(1);&#xA;    }&#xA;}&#xA;&#xA;/* Prepare a dummy image. */&#xA;static void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)&#xA;{&#xA;    int x, y, i;&#xA;&#xA;    i = frame_index;&#xA;&#xA;    /* Y */&#xA;    for (y = 0; y &lt; height; y&#x2B;&#x2B;)&#xA;        for (x = 0; x &lt; width; x&#x2B;&#x2B;)&#xA;            pict->data[0][y * pict->linesize[0] &#x2B; x] = x &#x2B; y &#x2B; i * 3;&#xA;&#xA;    /* Cb and Cr */&#xA;    for (y = 0; y &lt; height / 2; y&#x2B;&#x2B;) {&#xA;        for (x = 0; x &lt; width / 2; x&#x2B;&#x2B;) {&#xA;            pict->data[1][y * pict->linesize[1] &#x2B; x] = 128 &#x2B; y &#x2B; i * 2;&#xA;            pict->data[2][y * pict->linesize[2] &#x2B; x] = 64 &#x2B; x &#x2B; i * 5;&#xA;        }&#xA;    }&#xA;}&#xA;&#xA;int timestamp = 0;&#xA;static void write_video_frame(AVFormatContext *formatContext, AVStream *stream, AVCodecContext *codecCtx)&#xA;{&#xA;    int ret;&#xA;    static struct SwsContext *sws_ctx;&#xA;&#xA;    if (frame_count >= STREAM_NB_FRAMES) {&#xA;        /* No more frames to compress. The codec has a latency of a few&#xA;         * frames if using B-frames, so we get the last frames by&#xA;         * passing the same picture again. */&#xA;    }&#xA;    else {&#xA;        if (codecCtx->pix_fmt != AV_PIX_FMT_YUV420P) {&#xA;            /* as we only generate a YUV420P picture, we must convert it&#xA;             * to the codec pixel format if needed */&#xA;            if (!sws_ctx) {&#xA;                sws_ctx = sws_getContext(codecCtx->width, codecCtx->height, AV_PIX_FMT_YUV420P,&#xA;                    codecCtx->width, codecCtx->height, codecCtx->pix_fmt,&#xA;                    sws_flags, NULL, NULL, NULL);&#xA;                if (!sws_ctx) {&#xA;                    fprintf(stderr, "Could not initialize the conversion context\n");&#xA;                    exit(1);&#xA;                }&#xA;            }&#xA;            fill_yuv_image(frame_video, frame_count, codecCtx->width, codecCtx->height);&#xA;            sws_scale(sws_ctx, (const uint8_t * const *)frame_video->data, frame_video->linesize,&#xA;                0, codecCtx->height, frame_video->data, frame_video->linesize);&#xA;        }&#xA;        else {&#xA;            fill_yuv_image(frame_video, frame_count, codecCtx->width, codecCtx->height);&#xA;        }&#xA;    }&#xA;&#xA;    frame_video->format = AV_PIX_FMT_YUV420P;&#xA;    frame_video->width = codecCtx->width;&#xA;    frame_video->height = codecCtx->height;&#xA;&#xA;    if (formatContext->oformat->flags &amp; 0x0020) {&#xA;        /* Raw video case - directly store the picture in the packet */&#xA;        AVPacket pkt;&#xA;        av_init_packet(&amp;pkt);&#xA;&#xA;        pkt.flags |= AV_PKT_FLAG_KEY;&#xA;        pkt.stream_index = stream->index;&#xA;        pkt.data = frame_video->data[0];&#xA;        pkt.size = sizeof(AVPicture);&#xA;&#xA;        ret = av_interleaved_write_frame(formatContext, &amp;pkt);&#xA;    }&#xA;    else {&#xA;        AVPacket pkt = { 0 };&#xA;        av_init_packet(&amp;pkt);&#xA;&#xA;        /* encode the image */&#xA;        fprintf(stderr, "\nFrame type : %c\n", av_get_picture_type_char(frame_video->pict_type));&#xA;        fprintf(stderr, "Frame pts: %lld, \n", frame_video->pts);&#xA;        fprintf(stderr, "Codec timebase: %d/%d\n", codecCtx->time_base.num, codecCtx->time_base.den);&#xA;        fprintf(stderr, "Stream timebase: %d/%d\n", stream->time_base.num, stream->time_base.den);&#xA;        fprintf(stderr, "Resacale: %lld, \n\n", av_rescale_q(1, codecCtx->time_base, stream->time_base));&#xA;        ret = avcodec_send_frame(codecCtx, frame_video);&#xA;        if (ret &lt; 0) {&#xA;            char error[255];&#xA;            av_strerror(ret, error, 255);&#xA;            fprintf(stderr, "Error encoding video frame: %s\n", error);&#xA;            exit(1);&#xA;        }&#xA;        /* If size is zero, it means the image was buffered. */&#xA;        ret = avcodec_receive_packet(codecCtx, &amp;pkt);&#xA;        if (!ret &amp;&amp; pkt.size) {&#xA;            pkt.stream_index = stream->index;&#xA;            fprintf(stderr, "Packet flags : %d\n", pkt.flags);&#xA;            fprintf(stderr, "Packet pts: %lld\n", pkt.pts);&#xA;            fprintf(stderr, "Packet dts: %lld\n", pkt.dts);&#xA;            fprintf(stderr, "Packet duration: %lld\n", pkt.duration);&#xA;            fprintf(stderr, "Packet pos: %lld\n\n", pkt.pos);&#xA;            &#xA;            /* Write the compressed frame to the media file. */&#xA;            ret = av_interleaved_write_frame(formatContext, &amp;pkt);&#xA;        }&#xA;        else {&#xA;            ret = 0;&#xA;        }&#xA;    }&#xA;    if (ret != 0) {&#xA;        char error[255];&#xA;        av_strerror(ret, error, 255);&#xA;        fprintf(stderr, "Error while writing video frame: %s\n", error);&#xA;        exit(1);&#xA;    }&#xA;    frame_count&#x2B;&#x2B;;&#xA;}&#xA;&#xA;static void close_video(AVFormatContext *oc, AVStream *st)&#xA;{&#xA;    av_free(frame_video->data[0]);&#xA;    av_free(frame_video);&#xA;}&#xA;&#xA;/**************************************************************/&#xA;/* media file output */&#xA;&#xA;int main(int argc, char **argv)&#xA;{&#xA;    // The outputed media&#xA;    char filename[100];&#xA;    const char *mediaFormat = "mp4"; AVCodecID mediaVideoCodec = AV_CODEC_ID_H264;&#xA;    //const char *mediaFormat="webm"; AVCodecID mediaVideoCodec = AV_CODEC_ID_VP8;&#xA;    AVOutputFormat *formatOut;&#xA;    AVFormatContext *formatCtx;&#xA;&#xA;    // The video stream&#xA;    AVStream *stream_video;&#xA;    AVCodec *codec_video = nullptr;&#xA;    AVCodecContext *codecCtx_video = nullptr;&#xA;    double time_video = 0;&#xA;&#xA;    // Return code&#xA;    int ret;&#xA;&#xA;    strcpy_s(filename, "C:\\Test.");&#xA;    strcat_s(filename, mediaFormat);&#xA;&#xA;    // allocate the output media context&#xA;    avformat_alloc_output_context2(&amp;formatCtx, NULL, NULL, filename);&#xA;    if (!formatCtx) {&#xA;        return 1;&#xA;    }&#xA;    formatOut = formatCtx->oformat;&#xA;&#xA;    // Add the video stream using H264 codec&#xA;    stream_video = NULL;&#xA;    stream_video = add_stream(formatCtx, &amp;codec_video, mediaVideoCodec, &amp;codecCtx_video);&#xA;&#xA;    // Open video codec and allocate the necessary encode buffers&#xA;    if (stream_video)&#xA;        open_video(codec_video, stream_video, codecCtx_video);&#xA;&#xA;    av_dump_format(formatCtx, 0, filename, 1);&#xA;&#xA;    // Open the output media file, if needed&#xA;    if (!(formatOut->flags &amp; AVFMT_NOFILE)) {&#xA;        ret = avio_open(&amp;formatCtx->pb, filename, AVIO_FLAG_WRITE);&#xA;        if (ret &lt; 0) {&#xA;            char error[255];&#xA;            av_strerror(ret, error, 255);&#xA;            fprintf(stderr, "Could not open &#x27;%s&#x27;: %s\n", filename, error);&#xA;            return 1;&#xA;        }&#xA;    }&#xA;&#xA;    // Write media header&#xA;    ret = avformat_write_header(formatCtx, NULL);&#xA;    if (ret &lt; 0) {&#xA;        char error[255];&#xA;        av_strerror(ret, error, 255);&#xA;        fprintf(stderr, "Error occurred when opening output file: %s\n", error);&#xA;        return 1;&#xA;    }&#xA;&#xA;    if (frame_video)&#xA;        frame_video->pts = 0;&#xA;    for (;;) {&#xA;        // Compute video time from last added video frame&#xA;        time_video = ((double)frame_video->pts) * av_q2d(stream_video->time_base);&#xA;&#xA;        // Stop media if enough time&#xA;        if (!stream_video || time_video >= STREAM_DURATION)&#xA;            break;&#xA;&#xA;        // Add a video frame&#xA;        write_video_frame(formatCtx, stream_video, codecCtx_video);&#xA;&#xA;        // Increase frame pts according to time base&#xA;        frame_video->pts &#x2B;= av_rescale_q(1, codecCtx_video->time_base, stream_video->time_base);&#xA;    }&#xA;&#xA;    // Write media trailer&#xA;    av_write_trailer(formatCtx);&#xA;&#xA;    /* Close each codec. */&#xA;    if (stream_video)&#xA;        close_video(formatCtx, stream_video);&#xA;&#xA;    if (!(formatOut->flags &amp; AVFMT_NOFILE))&#xA;        /* Close the output file. */&#xA;        avio_close(formatCtx->pb);&#xA;&#xA;    /* free the stream */&#xA;    avformat_free_context(formatCtx);&#xA;&#xA;    return 0;&#xA;}&#xA;

    &#xA;

    What am I missing ? Which part give me this error ?

    &#xA;