Recherche avancée

Médias (3)

Mot : - Tags -/collection

Autres articles (82)

  • 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 ;

  • Le profil des utilisateurs

    12 avril 2011, par

    Chaque utilisateur dispose d’une page de profil lui permettant de modifier ses informations personnelle. Dans le menu de haut de page par défaut, un élément de menu est automatiquement créé à l’initialisation de MediaSPIP, visible uniquement si le visiteur est identifié sur le site.
    L’utilisateur a accès à la modification de profil depuis sa page auteur, un lien dans la navigation "Modifier votre profil" est (...)

  • Configurer la prise en compte des langues

    15 novembre 2010, par

    Accéder à la configuration et ajouter des langues prises en compte
    Afin de configurer la prise en compte de nouvelles langues, il est nécessaire de se rendre dans la partie "Administrer" du site.
    De là, dans le menu de navigation, vous pouvez accéder à une partie "Gestion des langues" permettant d’activer la prise en compte de nouvelles langues.
    Chaque nouvelle langue ajoutée reste désactivable tant qu’aucun objet n’est créé dans cette langue. Dans ce cas, elle devient grisée dans la configuration et (...)

Sur d’autres sites (13253)

  • FFMPEG RTSP Server using muxing doc example

    11 novembre 2018, par Harshil Makwana

    I am trying to develop RTSP server using FFMPEG. For that I slightly modified muxing file located at doc/example/ folder inside FFMPEG repository.

    Giving my source code of RTSP server example :

    #include
    #include
    #include
    #include

    #include <libavutil></libavutil>avassert.h>
    #include <libavutil></libavutil>channel_layout.h>
    #include <libavutil></libavutil>opt.h>
    #include <libavutil></libavutil>mathematics.h>
    #include <libavutil></libavutil>timestamp.h>
    #include <libavformat></libavformat>avformat.h>
    #include <libswscale></libswscale>swscale.h>
    #include <libswresample></libswresample>swresample.h>

    #define STREAM_DURATION   10.0
    #define STREAM_FRAME_RATE 25 /* 25 images/s */
    #define STREAM_PIX_FMT    AV_PIX_FMT_YUV420P /* default pix_fmt */

    #define SCALE_FLAGS SWS_BICUBIC

    // a wrapper around a single output AVStream
    typedef struct OutputStream {
       AVStream *st;
       AVCodecContext *enc;

       /* pts of the next frame that will be generated */
       int64_t next_pts;
       int samples_count;

       AVFrame *frame;
       AVFrame *tmp_frame;

       float t, tincr, tincr2;

       struct SwsContext *sws_ctx;
       struct SwrContext *swr_ctx;
    } OutputStream;

    static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
    {
       AVRational *time_base = &amp;fmt_ctx->streams[pkt->stream_index]->time_base;

       printf("pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
              av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
              av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
              av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
              pkt->stream_index);
    }

    static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
    {
       /* rescale output packet timestamp values from codec to stream timebase */
       av_packet_rescale_ts(pkt, *time_base, st->time_base);
       pkt->stream_index = st->index;

       /* Write the compressed frame to the media file. */
       log_packet(fmt_ctx, pkt);
       return av_interleaved_write_frame(fmt_ctx, pkt);
    }

    /* Add an output stream. */
    static void add_stream(OutputStream *ost, AVFormatContext *oc,
                          AVCodec **codec,
                          enum AVCodecID codec_id)
    {
       AVCodecContext *c;
       int i;

       /* find the encoder */
       *codec = avcodec_find_encoder(codec_id);
       if (!(*codec)) {
           fprintf(stderr, "Could not find encoder for '%s'\n",
                   avcodec_get_name(codec_id));
           exit(1);
       }

       ost->st = avformat_new_stream(oc, NULL);
       if (!ost->st) {
           fprintf(stderr, "Could not allocate stream\n");
           exit(1);
       }
       ost->st->id = oc->nb_streams-1;
       c = avcodec_alloc_context3(*codec);
       if (!c) {
           fprintf(stderr, "Could not alloc an encoding context\n");
           exit(1);
       }
       ost->enc = c;

       switch ((*codec)->type) {
       case AVMEDIA_TYPE_AUDIO:
           c->sample_fmt  = (*codec)->sample_fmts ?
               (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
           c->bit_rate    = 64000;
           c->sample_rate = 44100;
           if ((*codec)->supported_samplerates) {
               c->sample_rate = (*codec)->supported_samplerates[0];
               for (i = 0; (*codec)->supported_samplerates[i]; i++) {
                   if ((*codec)->supported_samplerates[i] == 44100)
                       c->sample_rate = 44100;
               }
           }
           c->channels        = av_get_channel_layout_nb_channels(c->channel_layout);
           c->channel_layout = AV_CH_LAYOUT_STEREO;
           if ((*codec)->channel_layouts) {
               c->channel_layout = (*codec)->channel_layouts[0];
               for (i = 0; (*codec)->channel_layouts[i]; i++) {
                   if ((*codec)->channel_layouts[i] == AV_CH_LAYOUT_STEREO)
                       c->channel_layout = AV_CH_LAYOUT_STEREO;
               }
           }
           c->channels        = av_get_channel_layout_nb_channels(c->channel_layout);
           ost->st->time_base = (AVRational){ 1, c->sample_rate };
           break;

       case AVMEDIA_TYPE_VIDEO:
           c->codec_id = codec_id;

           c->bit_rate = 400000;
           /* Resolution must be a multiple of two. */
           c->width    = 352;
           c->height   = 288;
           /* timebase: This is the fundamental unit of time (in seconds) in terms
            * of which frame timestamps are represented. For fixed-fps content,
            * timebase should be 1/framerate and timestamp increments should be
            * identical to 1. */
           ost->st->time_base = (AVRational){ 1, STREAM_FRAME_RATE };
           c->time_base       = ost->st->time_base;

           c->gop_size      = 12; /* emit one intra frame every twelve frames at most */
           c->pix_fmt       = STREAM_PIX_FMT;
           if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
               /* just for testing, we also add B-frames */
               c->max_b_frames = 2;
           }
           if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
               /* Needed to avoid using macroblocks in which some coeffs overflow.
                * This does not happen with normal video, it just happens here as
                * the motion of the chroma plane does not match the luma plane. */
               c->mb_decision = 2;
           }
      break;

       default:
           break;
       }

       /* Some formats want stream headers to be separate. */
       if (oc->oformat->flags &amp; AVFMT_GLOBALHEADER)
           c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    }

    /**************************************************************/
    /* audio output */

    static AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt,
                                     uint64_t channel_layout,
                                     int sample_rate, int nb_samples)
    {
       AVFrame *frame = av_frame_alloc();
       int ret;

       if (!frame) {
           fprintf(stderr, "Error allocating an audio frame\n");
           exit(1);
       }

       frame->format = sample_fmt;
       frame->channel_layout = channel_layout;
       frame->sample_rate = sample_rate;
       frame->nb_samples = nb_samples;

       if (nb_samples) {
           ret = av_frame_get_buffer(frame, 0);
           if (ret &lt; 0) {
               fprintf(stderr, "Error allocating an audio buffer\n");
               exit(1);
           }
       }

       return frame;
    }

    static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
    {
       AVCodecContext *c;
       int nb_samples;
       int ret;
      AVDictionary *opt = NULL;

       c = ost->enc;

       /* open it */
       av_dict_copy(&amp;opt, opt_arg, 0);
       ret = avcodec_open2(c, codec, &amp;opt);
       av_dict_free(&amp;opt);
       if (ret &lt; 0) {
           fprintf(stderr, "Could not open audio codec: %s\n", av_err2str(ret));
           exit(1);
       }

       /* init signal generator */
       ost->t     = 0;
       ost->tincr = 2 * M_PI * 110.0 / c->sample_rate;
       /* increment frequency by 110 Hz per second */
       ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;

       if (c->codec->capabilities &amp; AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
           nb_samples = 10000;
       else
           nb_samples = c->frame_size;

       ost->frame     = alloc_audio_frame(c->sample_fmt, c->channel_layout,
                                          c->sample_rate, nb_samples);
       ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c->channel_layout,
                                          c->sample_rate, nb_samples);

       /* copy the stream parameters to the muxer */
       ret = avcodec_parameters_from_context(ost->st->codecpar, c);
       if (ret &lt; 0) {
           fprintf(stderr, "Could not copy the stream parameters\n");
           exit(1);
       }

       /* create resampler context */
           ost->swr_ctx = swr_alloc();
           if (!ost->swr_ctx) {
               fprintf(stderr, "Could not allocate resampler context\n");
               exit(1);
           }

           /* set options */
           av_opt_set_int       (ost->swr_ctx, "in_channel_count",   c->channels,       0);
           av_opt_set_int       (ost->swr_ctx, "in_sample_rate",     c->sample_rate,    0);
           av_opt_set_sample_fmt(ost->swr_ctx, "in_sample_fmt",      AV_SAMPLE_FMT_S16, 0);
           av_opt_set_int       (ost->swr_ctx, "out_channel_count",  c->channels,       0);
           av_opt_set_int       (ost->swr_ctx, "out_sample_rate",    c->sample_rate,    0);
           av_opt_set_sample_fmt(ost->swr_ctx, "out_sample_fmt",     c->sample_fmt,     0);

           /* initialize the resampling context */
           if ((ret = swr_init(ost->swr_ctx)) &lt; 0) {
               fprintf(stderr, "Failed to initialize the resampling context\n");
               exit(1);
           }
    }

    /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
    * 'nb_channels' channels. */
    static AVFrame *get_audio_frame(OutputStream *ost)
    {
       AVFrame *frame = ost->tmp_frame;
       int j, i, v;
       int16_t *q = (int16_t*)frame->data[0];

       /* check if we want to generate more frames */
       if (av_compare_ts(ost->next_pts, ost->enc->time_base,
                         STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
           return NULL;

       for (j = 0; j nb_samples; j++) {
           v = (int)(sin(ost->t) * 10000);
           for (i = 0; i &lt; ost->enc->channels; i++)
               *q++ = v;
           ost->t     += ost->tincr;
           ost->tincr += ost->tincr2;
       }

       frame->pts = ost->next_pts;
       ost->next_pts  += frame->nb_samples;

       return frame;
    }

    /*
    * encode one audio frame and send it to the muxer
    * return 1 when encoding is finished, 0 otherwise
    */
    static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
    {
       AVCodecContext *c;
       AVPacket pkt = { 0 }; // data and size must be 0;
       AVFrame *frame;
       int ret;
       int got_packet;
       int dst_nb_samples;

       av_init_packet(&amp;pkt);
       c = ost->enc;

       frame = get_audio_frame(ost);

       if (frame) {
           /* convert samples from native format to destination codec format, using the resampler */
               /* compute destination number of samples */
               dst_nb_samples = av_rescale_rnd(swr_get_delay(ost->swr_ctx, c->sample_rate) + frame->nb_samples,
                                               c->sample_rate, c->sample_rate, AV_ROUND_UP);
               av_assert0(dst_nb_samples == frame->nb_samples);

           /* when we pass a frame to the encoder, it may keep a reference to it
            * internally;
           * make sure we do not overwrite it here
            */
           ret = av_frame_make_writable(ost->frame);
           if (ret &lt; 0)
               exit(1);

           /* convert to destination format */
           ret = swr_convert(ost->swr_ctx,
                             ost->frame->data, dst_nb_samples,
                             (const uint8_t **)frame->data, frame->nb_samples);
           if (ret &lt; 0) {
               fprintf(stderr, "Error while converting\n");
               exit(1);
           }
           frame = ost->frame;

           frame->pts = av_rescale_q(ost->samples_count, (AVRational){1, c->sample_rate}, c->time_base);
           ost->samples_count += dst_nb_samples;
       }

       ret = avcodec_encode_audio2(c, &amp;pkt, frame, &amp;got_packet);
       if (ret &lt; 0) {
           fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
           exit(1);
       }

       if (got_packet) {
           ret = write_frame(oc, &amp;c->time_base, ost->st, &amp;pkt);
           if (ret &lt; 0) {
               fprintf(stderr, "Error while writing audio frame: %s\n",
                       av_err2str(ret));
               exit(1);
           }
       }

       return (frame || got_packet) ? 0 : 1;
    }

    /**************************************************************/
    /* video output */

    static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
    {
       AVFrame *picture;
       int ret;

       picture = av_frame_alloc();
       if (!picture)
           return NULL;

       picture->format = pix_fmt;
       picture->width  = width;
       picture->height = height;

       /* allocate the buffers for the frame data */
       ret = av_frame_get_buffer(picture, 32);
       if (ret &lt; 0) {
           fprintf(stderr, "Could not allocate frame data.\n");
           exit(1);
       }

       return picture;
    }

    static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
    {
       int ret;
       AVCodecContext *c = ost->enc;
       AVDictionary *opt = NULL;

       av_dict_copy(&amp;opt, opt_arg, 0);

       /* open the codec */
       ret = avcodec_open2(c, codec, &amp;opt);
       av_dict_free(&amp;opt);
       if (ret &lt; 0) {
           fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret));
           exit(1);
       }

       /* allocate and init a re-usable frame */
       ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);
       if (!ost->frame) {
           fprintf(stderr, "Could not allocate video frame\n");
           exit(1);
       }

       /* If the output format is not YUV420P, then a temporary YUV420P
        * picture is needed too. It is then converted to the required
        * output format. */
       ost->tmp_frame = NULL;
       if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
           ost->tmp_frame = alloc_picture(AV_PIX_FMT_YUV420P, c->width, c->height);
           if (!ost->tmp_frame) {
               fprintf(stderr, "Could not allocate temporary picture\n");
               exit(1);
           }
       }

       /* copy the stream parameters to the muxer */
       ret = avcodec_parameters_from_context(ost->st->codecpar, c);
       if (ret &lt; 0) {
           fprintf(stderr, "Could not copy the stream parameters\n");
           exit(1);
       }
    }

    /* Prepare a dummy image. */
    static void fill_yuv_image(AVFrame *pict, int frame_index,
                              int width, int height)
    {
       int x, y, i;

       i = frame_index;

       /* Y */
       for (y = 0; y &lt; height; y++)
           for (x = 0; x &lt; width; x++)
               pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;

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

    static AVFrame *get_video_frame(OutputStream *ost)
    {
       AVCodecContext *c = ost->enc;

       /* check if we want to generate more frames */
       if (av_compare_ts(ost->next_pts, c->time_base,
                         STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
           return NULL;

       /* when we pass a frame to the encoder, it may keep a reference to it
        * internally; make sure we do not overwrite it here */
       if (av_frame_make_writable(ost->frame) &lt; 0)
           exit(1);

       if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
           /* as we only generate a YUV420P picture, we must convert it
            * to the codec pixel format if needed */
           if (!ost->sws_ctx) {
               ost->sws_ctx = sws_getContext(c->width, c->height,
                                             AV_PIX_FMT_YUV420P,
                                             c->width, c->height,
                                             c->pix_fmt,
                                             SCALE_FLAGS, NULL, NULL, NULL);
               if (!ost->sws_ctx) {
                   fprintf(stderr,
                           "Could not initialize the conversion context\n");
                   exit(1);
               }
           }
           fill_yuv_image(ost->tmp_frame, ost->next_pts, c->width, c->height);
           sws_scale(ost->sws_ctx,
                     (const uint8_t * const *)ost->tmp_frame->data, ost->tmp_frame->linesize,
                     0, c->height, ost->frame->data, ost->frame->linesize);
       } else {
           fill_yuv_image(ost->frame, ost->next_pts, c->width, c->height);
       }

       ost->frame->pts = ost->next_pts++;

       return ost->frame;
    }

    /*
    * encode one video frame and send it to the muxer
    * return 1 when encoding is finished, 0 otherwise
    */
    static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
    {
       int ret;
       AVCodecContext *c;
       AVFrame *frame;
       int got_packet = 0;
       AVPacket pkt = { 0 };

       c = ost->enc;

       frame = get_video_frame(ost);

       av_init_packet(&amp;pkt);

       /* encode the image */
       ret = avcodec_encode_video2(c, &amp;pkt, frame, &amp;got_packet);
       if (ret &lt; 0) {
         fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret));
           exit(1);
       }

       if (got_packet) {
           ret = write_frame(oc, &amp;c->time_base, ost->st, &amp;pkt);
       } else {
           ret = 0;
       }

       if (ret &lt; 0) {
           fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
           exit(1);
       }

       return (frame || got_packet) ? 0 : 1;
    }

    static void close_stream(AVFormatContext *oc, OutputStream *ost)
    {
       avcodec_free_context(&amp;ost->enc);
       av_frame_free(&amp;ost->frame);
       av_frame_free(&amp;ost->tmp_frame);
       sws_freeContext(ost->sws_ctx);
       swr_free(&amp;ost->swr_ctx);
    }

    /**************************************************************/
    /* media file output */

    int main(int argc, char **argv)
    {
       OutputStream video_st = { 0 }, audio_st = { 0 };
       const char *filename;
       AVOutputFormat *fmt;
       AVFormatContext *oc;
       AVCodec *audio_codec, *video_codec;
       int ret;
       int have_video = 0, have_audio = 0;
       int encode_video = 0, encode_audio = 0;
       AVDictionary *opt = NULL;
       int i;

       /* Initialize libavcodec, and register all codecs and formats. */
       av_register_all();
       avformat_network_init();
       if (argc &lt; 2) {
           printf("usage: %s output_file\n"
                  "API example program to output a media file with libavformat.\n"
                  "This program generates a synthetic audio and video stream, encodes and\n"
                  "muxes them into a file named output_file.\n"
                  "The output format is automatically guessed according to the file extension.\n"
                  "Raw images can also be output by using '%%d' in the filename.\n"
                  "\n", argv[0]);
           return 1;
       }

       filename = argv[1];
       for (i = 2; i+1 &lt; argc; i+=2) {
           if (!strcmp(argv[i], "-flags") || !strcmp(argv[i], "-fflags"))
               av_dict_set(&amp;opt, argv[i]+1, argv[i+1], 0);
       }
      /* allocate the output media context */
       avformat_alloc_output_context2(&amp;oc, NULL, "rtsp", filename);
       if (!oc) {
           printf("Could not deduce output format from file extension: using MPEG.\n");
           avformat_alloc_output_context2(&amp;oc, NULL, "mpeg", filename);
       }
       if (!oc)
           return 1;

       fmt = oc->oformat;

       /* Add the audio and video streams using the default format codecs
        * and initialize the codecs. */
       if (fmt->video_codec != AV_CODEC_ID_NONE) {
           add_stream(&amp;video_st, oc, &amp;video_codec, fmt->video_codec);
           have_video = 1;
           encode_video = 1;
       }
       if (fmt->audio_codec != AV_CODEC_ID_NONE) {
           add_stream(&amp;audio_st, oc, &amp;audio_codec, fmt->audio_codec);
           have_audio = 1;
           encode_audio = 1;
       }

       /* Now that all the parameters are set, we can open the audio and
        * video codecs and allocate the necessary encode buffers. */
       if (have_video)
           open_video(oc, video_codec, &amp;video_st, opt);

       if (have_audio)
           open_audio(oc, audio_codec, &amp;audio_st, opt);

       av_dump_format(oc, 0, filename, 1);

       /* open the output file, if needed */
       if (!(fmt->flags &amp; AVFMT_NOFILE)) {
           ret = avio_open(&amp;oc->pb, filename, AVIO_FLAG_WRITE);
           if (ret &lt; 0) {
               fprintf(stderr, "Could not open '%s': %s\n", filename,
                       av_err2str(ret));
               return 1;
           }
       }

       /* Write the stream header, if any. */
       ret = avformat_write_header(oc, &amp;opt);
       if (ret &lt; 0) {
           fprintf(stderr, "Error occurred when opening output file: %s\n",
                   av_err2str(ret));
           return 1;
       }

       while (encode_video || encode_audio) {
           /* select the stream to encode */
           if (encode_video &amp;&amp;
              (!encode_audio || av_compare_ts(video_st.next_pts, video_st.enc->time_base,
                                               audio_st.next_pts, audio_st.enc->time_base) &lt;= 0)) {
               encode_video = !write_video_frame(oc, &amp;video_st);
           } else {
               encode_audio = !write_audio_frame(oc, &amp;audio_st);
           }
       }

       /* Write the trailer, if any. The trailer must be written before you
        * close the CodecContexts open when you wrote the header; otherwise
        * av_write_trailer() may try to use memory that was freed on
        * av_codec_close(). */
       av_write_trailer(oc);

       /* Close each codec. */
       if (have_video)
           close_stream(oc, &amp;video_st);
       if (have_audio)
           close_stream(oc, &amp;audio_st);

       if (!(fmt->flags &amp; AVFMT_NOFILE))
           /* Close the output file. */
           avio_closep(&amp;oc->pb);

       /* free the stream */
       avformat_free_context(oc);

       return 0;
    }

    After compiling it, I am running binary :

    $ ./muxing rtsp://127.0.0.1/test
    Output #0, rtsp, to 'rtsp://127.0.0.1/test':
       Stream #0:0: Video: mpeg4, yuv420p, 352x288, q=2-31, 400 kb/s, 25 tbn
       Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp, 64 kb/s
    [tcp @ 0x2b9d220] Connection to tcp://127.0.0.1:554?timeout=0 failed: Connection refused
    Error occurred when opening output file: Connection refused

    But getting Connection refused error,

  • ffmpeg encoded hls makes audio and video progressively out of sync

    21 septembre 2020, par eschie

    When trying to encoding a source .mp4 to multiple bitrates and sizes for adaptive HLSv3 playback the resulting audio and video become progressively out of sync. When scrubbing to a later point it seems to reset, and stay in sync.

    &#xA;&#xA;

    I'm on the latest ffmpeg 4.2.1 via homebrew-ffmpeg, with the libfdk-aac codec. Note, this was also occurring with ffmpeg 4.1.

    &#xA;&#xA;

    ffmpeg version 4.2.1 Copyright (c) 2000-2019 the FFmpeg developers&#xA;  built with Apple LLVM version 9.0.0 (clang-900.0.39.2)&#xA;  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.1-with-options_1 --enable-shared --cc=clang --host-cflags= --host-ldflags= --enable-gpl --enable-libaom --enable-libmp3lame --enable-libopus --enable-libsnappy --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --disable-libjack --disable-indev=jack --enable-opencl --enable-videotoolbox --disable-htmlpages --enable-libfdk-aac --enable-nonfree&#xA;  libavutil      56. 31.100 / 56. 31.100&#xA;  libavcodec     58. 54.100 / 58. 54.100&#xA;  libavformat    58. 29.100 / 58. 29.100&#xA;  libavdevice    58.  8.100 / 58.  8.100&#xA;  libavfilter     7. 57.100 /  7. 57.100&#xA;  libswscale      5.  5.100 /  5.  5.100&#xA;  libswresample   3.  5.100 /  3.  5.100&#xA;  libpostproc    55.  5.100 / 55.  5.100&#xA;

    &#xA;&#xA;

    The audio and video streams are marginally off, would that cause this problem ?

    &#xA;&#xA;

    ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 INPUT_FILE.mp4&#xA;80.480400

    &#xA;&#xA;

    ffprobe -v error -select_streams a:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 INPUT_FILE.mp4&#xA;80.469333

    &#xA;&#xA;

    Here is some metadata on the source file :

    &#xA;&#xA;

    General&#xA;Complete name                            : INPUT_VIDEO.mp4&#xA;Format                                   : MPEG-4&#xA;Format profile                           : Base Media / Version 2&#xA;Codec ID                                 : mp42 (mp42/mp41)&#xA;File size                                : 236 MiB&#xA;Duration                                 : 1 min 21 s&#xA;Overall bit rate mode                    : Variable&#xA;Overall bit rate                         : 24.2 Mb/s&#xA;Encoded date                             : UTC 2019-08-27 22:13:53&#xA;Tagged date                              : UTC 2019-08-27 22:15:15&#xA;TIM                                      : 00;00;00;00&#xA;TSC                                      : 30000&#xA;TSZ                                      : 1001&#xA;&#xA;Video&#xA;ID                                       : 1&#xA;Format                                   : AVC&#xA;Format/Info                              : Advanced Video Codec&#xA;Format profile                           : Main@L4.1&#xA;Format settings                          : CABAC / 2 Ref Frames&#xA;Format settings, CABAC                   : Yes&#xA;Format settings, Reference frames        : 2 frames&#xA;Codec ID                                 : avc1&#xA;Codec ID/Info                            : Advanced Video Coding&#xA;Duration                                 : 1 min 21 s&#xA;Bit rate mode                            : Variable&#xA;Bit rate                                 : 23.8 Mb/s&#xA;Maximum bit rate                         : 768 kb/s&#xA;Width                                    : 1 920 pixels&#xA;Height                                   : 1 080 pixels&#xA;Display aspect ratio                     : 16:9&#xA;Frame rate mode                          : Constant&#xA;Frame rate                               : 29.970 (30000/1001) FPS&#xA;Color space                              : YUV&#xA;Chroma subsampling                       : 4:2:0&#xA;Bit depth                                : 8 bits&#xA;Scan type                                : Progressive&#xA;Bits/(Pixel*Frame)                       : 0.383&#xA;Stream size                              : 233 MiB (99%)&#xA;Language                                 : English&#xA;Encoded date                             : UTC 2019-08-27 22:13:53&#xA;Tagged date                              : UTC 2019-08-27 22:13:53&#xA;Color range                              : Limited&#xA;Color primaries                          : BT.709&#xA;Transfer characteristics                 : BT.709&#xA;Matrix coefficients                      : BT.709&#xA;Codec configuration box                  : avcC&#xA;&#xA;Audio&#xA;ID                                       : 2&#xA;Format                                   : AAC LC&#xA;Format/Info                              : Advanced Audio Codec Low Complexity&#xA;Codec ID                                 : mp4a-40-2&#xA;Duration                                 : 1 min 21 s&#xA;Source duration                          : 1 min 21 s&#xA;Bit rate mode                            : Constant&#xA;Bit rate                                 : 317 kb/s&#xA;Channel(s)                               : 2 channels&#xA;Channel layout                           : L R&#xA;Sampling rate                            : 48.0 kHz&#xA;Frame rate                               : 46.875 FPS (1024 SPF)&#xA;Compression mode                         : Lossy&#xA;Stream size                              : 3.10 MiB (1%)&#xA;Source stream size                       : 3.10 MiB (1%)&#xA;Language                                 : English&#xA;Encoded date                             : UTC 2019-08-27 22:13:53&#xA;Tagged date                              : UTC 2019-08-27 22:13:53&#xA;

    &#xA;&#xA;

    And the encoding command :

    &#xA;&#xA;

    ffmpeg \&#xA;-i INPUT_VIDEO \&#xA;-dn \&#xA;-sn \&#xA;-filter_complex \&#xA;"[0:v]fps=fps=24000/1001, \&#xA;setpts=(PTS-STARTPTS), \&#xA;split=12[vsplit1][vsplit2][vsplit3][vsplit4][vsplit5][vsplit6][vsplit7][vsplit8][vsplit9][vsplit10][vsplit11][vsplit12]; \&#xA;[vsplit1]scale=-1:1080[video_1080_4.1]; \&#xA;[vsplit2]scale=-1:720[video_720_4.1]; \&#xA;[vsplit3]scale=-1:720[video_720_3.1]; \&#xA;[vsplit4]scale=-1:540[video_540_3.1]; \&#xA;[vsplit5]scale=-1:432[video_432_3.1]; \&#xA;[vsplit6]scale=-1:270[video_270_3.0]; \&#xA;[vsplit7]scale=-1:270[video_270_3.1]; \&#xA;[vsplit8]scale=-1:144[video_144_4.1]; \&#xA;[vsplit9]scale=-1:144[video_144_3.0]; \&#xA;[vsplit10]scale=1920:1080[base_1080]; \&#xA;[vsplit11]scale=1280:720[base_720]; \&#xA;[vsplit12]scale=640:360[base_360]" \&#xA;\&#xA;-map "[video_1080_4.1]" \&#xA;-r:v:0 "24000/1001" \&#xA;-c:v:0 "libx264" \&#xA;-x264-params:0 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \&#xA;-preset:v:0 "slow" \&#xA;-profile:v:0 "high" \&#xA;-level:v:0 "4.1" \&#xA;-refs:v:0 "2" \&#xA;-b-pyramid:v:0 "strict" \&#xA;-tune:v:0 "film" \&#xA;-b:v:0 "4800000" \&#xA;-maxrate:v:0 "4800000" \&#xA;-bufsize:v:0 "6*4800000/8" \&#xA;-vsync:v:0 "cfr" \&#xA;-bsf:v:0 "h264_metadata=fixed_frame_rate_flag=1" \&#xA;\&#xA;-map "[video_720_4.1]" \&#xA;-r:v:1 "24000/1001" \&#xA;-c:v:1 "libx264" \&#xA;-x264-params:1 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \&#xA;-preset:v:1 "slow" \&#xA;-profile:v:1 "main" \&#xA;-level:v:1 "4.1" \&#xA;-refs:v:1 "2" \&#xA;-b-pyramid:v:1 "strict" \&#xA;-tune:v:1 "film" \&#xA;-b:v:1 "3200000" \&#xA;-maxrate:v:1 "3200000" \&#xA;-bufsize:v:1 "6*3200000/8" \&#xA;-vsync:v:1 "cfr" \&#xA;-bsf:v:1 "h264_metadata=fixed_frame_rate_flag=1" \&#xA;\&#xA;-map "[video_720_3.1]" \&#xA;-r:v:2 "24000/1001" \&#xA;-c:v:2 "libx264" \&#xA;-x264-params:2 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \&#xA;-preset:v:2 "slow" \&#xA;-profile:v:2 "main" \&#xA;-level:v:2 "3.1" \&#xA;-refs:v:2 "2" \&#xA;-b-pyramid:v:2 "strict" \&#xA;-tune:v:2 "film" \&#xA;-b:v:2 "2200000" \&#xA;-maxrate:v:2 "2200000" \&#xA;-bufsize:v:2 "6*2200000/8" \&#xA;-vsync:v:2 "cfr" \&#xA;-bsf:v:2 "h264_metadata=fixed_frame_rate_flag=1" \&#xA;\&#xA;-map "[video_540_3.1]" \&#xA;-r:v:3 "24000/1001" \&#xA;-c:v:3 "libx264" \&#xA;-x264-params:3 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \&#xA;-preset:v:3 "slow" \&#xA;-profile:v:3 "main" \&#xA;-level:v:3 "3.1" \&#xA;-refs:v:3 "2" \&#xA;-b-pyramid:v:3 "strict" \&#xA;-tune:v:3 "film" \&#xA;-b:v:3 "1400000" \&#xA;-maxrate:v:3 "1400000" \&#xA;-bufsize:v:3 "6*1400000/8" \&#xA;-vsync:v:3 "cfr" \&#xA;-bsf:v:3 "h264_metadata=fixed_frame_rate_flag=1" \&#xA;\&#xA;-map "[video_432_3.1]" \&#xA;-r:v:4 "24000/1001" \&#xA;-c:v:4 "libx264" \&#xA;-x264-params:4 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \&#xA;-preset:v:4 "slow" \&#xA;-profile:v:4 "main" \&#xA;-level:v:4 "3.1" \&#xA;-refs:v:4 "2" \&#xA;-b-pyramid:v:4 "strict" \&#xA;-tune:v:4 "film" \&#xA;-b:v:4 "900000" \&#xA;-maxrate:v:4 "900000" \&#xA;-bufsize:v:4 "6*900000/8" \&#xA;-vsync:v:4 "cfr" \&#xA;-bsf:v:4 "h264_metadata=fixed_frame_rate_flag=1" \&#xA;\&#xA;-map "[video_270_3.0]" \&#xA;-r:v:5 "24000/1001" \&#xA;-c:v:5 "libx264" \&#xA;-x264-params:5 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \&#xA;-preset:v:5 "slow" \&#xA;-profile:v:5 "baseline" \&#xA;-level:v:5 "3.0" \&#xA;-refs:v:5 "2" \&#xA;-b-pyramid:v:5 "strict" \&#xA;-tune:v:5 "film" \&#xA;-b:v:5 "400000" \&#xA;-maxrate:v:5 "400000" \&#xA;-bufsize:v:5 "6*400000/8" \&#xA;-vsync:v:5 "cfr" \&#xA;-bsf:v:5 "h264_metadata=fixed_frame_rate_flag=1" \&#xA;\&#xA;-map "[video_270_3.1]" \&#xA;-r:v:6 "24000/1001" \&#xA;-c:v:6 "libx264" \&#xA;-x264-params:6 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \&#xA;-preset:v:6 "slow" \&#xA;-profile:v:6 "main" \&#xA;-level:v:6 "3.1" \&#xA;-refs:v:6 "2" \&#xA;-b-pyramid:v:6 "strict" \&#xA;-tune:v:6 "film" \&#xA;-b:v:6 "200000" \&#xA;-maxrate:v:6 "200000" \&#xA;-bufsize:v:6 "6*200000/8" \&#xA;-vsync:v:6 "cfr" \&#xA;-bsf:v:6 "h264_metadata=fixed_frame_rate_flag=1" \&#xA;\&#xA;-map "[video_144_4.1]" \&#xA;-r:v:7 "24000/1001" \&#xA;-c:v:7 "libx264" \&#xA;-x264-params:7 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \&#xA;-preset:v:7 "slow" \&#xA;-profile:v:7 "high" \&#xA;-level:v:7 "4.1" \&#xA;-refs:v:7 "2" \&#xA;-b-pyramid:v:7 "strict" \&#xA;-tune:v:7 "film" \&#xA;-b:v:7 "64000" \&#xA;-maxrate:v:7 "64000" \&#xA;-bufsize:v:7 "6*64000/8" \&#xA;-vsync:v:7 "cfr" \&#xA;-bsf:v:7 "h264_metadata=fixed_frame_rate_flag=1" \&#xA;\&#xA;-map "[video_144_3.0]" \&#xA;-r:v:8 "24000/1001" \&#xA;-c:v:8 "libx264" \&#xA;-x264-params:8 "keyint=144:min-keyint=144:scenecut=0:open_gop=0" \&#xA;-preset:v:8 "slow" \&#xA;-profile:v:8 "baseline" \&#xA;-level:v:8 "3.0" \&#xA;-refs:v:8 "2" \&#xA;-b-pyramid:v:8 "strict" \&#xA;-tune:v:8 "film" \&#xA;-b:v:8 "56000" \&#xA;-maxrate:v:8 "56000" \&#xA;-bufsize:v:8 "6*56000/8" \&#xA;-vsync:v:8 "cfr" \&#xA;-bsf:v:8 "h264_metadata=fixed_frame_rate_flag=1" \&#xA;\&#xA;-map "[base_1080]" \&#xA;-c:v:9 "libx264" \&#xA;-preset:v:9 "slow" \&#xA;\&#xA;-map "[base_720]" \&#xA;-c:v:10 "libx264" \&#xA;-preset:v:10 "slow" \&#xA;\&#xA;-map "[base_360]" \&#xA;-c:v:11 "libx264" \&#xA;-preset:v:11 "slow" \&#xA;\&#xA;-map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 -map a:0 \&#xA;-c:a "libfdk_aac" \&#xA;-ar "48000" \&#xA;-ab "128k" \&#xA;-af "aresample=async=1:min_hard_comp=0.100000:first_pts=0" \&#xA;\&#xA;-f tee \&#xA;-flags &#x2B;global_header \&#xA;"[select=\&#x27;v:0,a:0,v:1,a:1,v:2,a:2,v:3,a:3,v:4,a:4,v:5,a:5,v:6,a:6,v:7,a:7,v:8,a:8\&#x27;:f=hls:hls_flags=discont_start&#x2B;temp_file:hls_time=6:hls_list_size=0:var_stream_map=\&#x27;v:0,a:0 v:1,a:1 v:2,a:2 v:3,a:3 v:4,a:4 v:5,a:5 v:6,a:6 v:7,a:7 v:8,a:8\&#x27;:master_pl_name=playlist.m3u8:hls_segment_filename=INPUT_VIDEO__%v_%03d.ts]INPUT_VIDEO/out_%v.m3u8|[select=\&#x27;v:9,a:9\&#x27;:f=mp4:movflags=&#x2B;faststart]INPUT_VIDEO/large-1920-1080.mp4|[select=\&#x27;v:10,a:10\&#x27;:f=mp4:movflags=&#x2B;faststart]INPUT_VIDEO/med-1280-720.mp4|[select=\&#x27;v:11,a:11\&#x27;:f=mp4:movflags=&#x2B;faststart]INPUT_VIDEO/sm-640-360.mp4"&#xA;

    &#xA;

  • Understanding FFMPEG Video Encoding

    20 juin 2014, par SetSlapShot

    Got this from the encoding example in ffmpeg. I can somewhat follow the authors example for audio encoding, but I find myself befuddled looking at the C code (I commented in block numbers to help me reference what I’m talking about)...

    static void video_encode_example(const char *filename)
    {
    AVCodec *codec;
    AVCodecContext *c= NULL;
    int i, out_size, size, x, y, outbuf_size;
    FILE *f;
    AVFrame *picture;
    uint8_t *outbuf, *picture_buf;              //BLOCK ONE
    printf("Video encoding\n");

    /* find the mpeg1 video encoder */
    codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
    if (!codec) {
       fprintf(stderr, "codec not found\n");
       exit(1);                                //BLOCK TWO
    }

    c= avcodec_alloc_context();
    picture= avcodec_alloc_frame();
    /* put sample parameters */
    c->bit_rate = 400000;
    /* resolution must be a multiple of two */
    c->width = 352;
    c->height = 288;
    /* frames per second */
    c->time_base= (AVRational){1,25};
    c->gop_size = 10; /* emit one intra frame every ten frames */
    c->max_b_frames=1;
    c->pix_fmt = PIX_FMT_YUV420P;                   //BLOCK THREE

    /* open it */
    if (avcodec_open(c, codec) &lt; 0) {
       fprintf(stderr, "could not open codec\n");
       exit(1);
    }
    f = fopen(filename, "wb");
    if (!f) {
       fprintf(stderr, "could not open %s\n", filename);
       exit(1);
    }                                               //BLOCK FOUR

    /* alloc image and output buffer */
    outbuf_size = 100000;
    outbuf = malloc(outbuf_size);
    size = c->width * c->height;
    picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
    picture->data[0] = picture_buf;
    picture->data[1] = picture->data[0] + size;
    picture->data[2] = picture->data[1] + size / 4;
    picture->linesize[0] = c->width;
    picture->linesize[1] = c->width / 2;
    picture->linesize[2] = c->width / 2;              //BLOCK FIVE

    /* encode 1 second of video */
    for(i=0;i&lt;25;i++) {
       fflush(stdout);
       /* prepare a dummy image */
       /* Y */
       for(y=0;yheight;y++) {
           for(x=0;xwidth;x++) {
               picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
           }
       }                                            //BLOCK SIX

       /* Cb and Cr */
       for(y=0;yheight/2;y++) {
           for(x=0;xwidth/2;x++) {
               picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
               picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
           }
       }                                           //BLOCK SEVEN

       /* encode the image */
       out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
       printf("encoding frame %3d (size=%5d)\n", i, out_size);
       fwrite(outbuf, 1, out_size, f);
    }                                              //BLOCK EIGHT

    /* get the delayed frames */
    for(; out_size; i++) {
       fflush(stdout);
       out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
       printf("write frame %3d (size=%5d)\n", i, out_size);
       fwrite(outbuf, 1, out_size, f);
    }                                             //BLOCK NINE

    /* add sequence end code to have a real mpeg file */
    outbuf[0] = 0x00;
    outbuf[1] = 0x00;
    outbuf[2] = 0x01;
    outbuf[3] = 0xb7;
    fwrite(outbuf, 1, 4, f);
    fclose(f);
    free(picture_buf);
    free(outbuf);
    avcodec_close(c);
    av_free(c);
    av_free(picture);
    }                                            //BLOCK TEN

    Here’s what I can get from the authors code block by block...

    BLOCK ONE : Initializing Variables and pointers. I couldn’t find the AVFrame struct yet in the ffmpeg source code so I don’t know what its referencing

    BLOCK TWO : Uses a codec from the file, if not found close.

    BLOCK THREE : Sets sample video parameters. Only thing I don’t really get is gop size. I read about intra frames and I still don’t get what they are.

    BLOCK FOUR : Open the file for writing...

    BLOCK FIVE : Here’s where they really start losing me. Part is probably because I don’t know exactly what AVFrame is, but why do they only use 3/2 of the image size ?

    BLOCK SIX & SEVEN : I don’t understand what they are trying to accomplish with this math.

    BLOCK EIGHT : It looks like the avcodec function does all the work here, not concerned with that for the time being..

    BLOCK NINE : Since it’s outside the 25 frame for loop I assume it gets the leftover frames ?

    BLOCK TEN : Close, free mem, etc...

    I know this is a large block of code to be confused with, any input would be helpful. I got put in over my head at work. Thanks in advance SO.