Recherche avancée

Médias (0)

Mot : - Tags -/objet éditorial

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

Autres articles (69)

  • Support de tous types de médias

    10 avril 2011

    Contrairement à beaucoup de logiciels et autres plate-formes modernes de partage de documents, MediaSPIP a l’ambition de gérer un maximum de formats de documents différents qu’ils soient de type : images (png, gif, jpg, bmp et autres...) ; audio (MP3, Ogg, Wav et autres...) ; vidéo (Avi, MP4, Ogv, mpg, mov, wmv et autres...) ; contenu textuel, code ou autres (open office, microsoft office (tableur, présentation), web (html, css), LaTeX, Google Earth) (...)

  • Ecrire une actualité

    21 juin 2013, par

    Présentez les changements dans votre MédiaSPIP ou les actualités de vos projets sur votre MédiaSPIP grâce à la rubrique actualités.
    Dans le thème par défaut spipeo de MédiaSPIP, les actualités sont affichées en bas de la page principale sous les éditoriaux.
    Vous pouvez personnaliser le formulaire de création d’une actualité.
    Formulaire de création d’une actualité Dans le cas d’un document de type actualité, les champs proposés par défaut sont : Date de publication ( personnaliser la date de publication ) (...)

  • Librairies et binaires spécifiques au traitement vidéo et sonore

    31 janvier 2010, par

    Les logiciels et librairies suivantes sont utilisées par SPIPmotion d’une manière ou d’une autre.
    Binaires obligatoires FFMpeg : encodeur principal, permet de transcoder presque tous les types de fichiers vidéo et sonores dans les formats lisibles sur Internet. CF ce tutoriel pour son installation ; Oggz-tools : outils d’inspection de fichiers ogg ; Mediainfo : récupération d’informations depuis la plupart des formats vidéos et sonores ;
    Binaires complémentaires et facultatifs flvtool2 : (...)

Sur d’autres sites (7574)

  • cpp code to use ffmpeg static library, compress and convert from one format to another [on hold]

    3 juillet 2017, par Herdesh Verma

    I am new to ffmpeg library and don’t know much about ffmpeg library. I need to write a cpp code that could use static ffmpeg library and do compression along with conversion of format. Till now i managed to get below program working which can convert .mov file to .mpeg format and compress 125MB file to 18MB.

    extern "C"
    {
    #include <libavutil></libavutil>imgutils.h>
    #include <libavutil></libavutil>opt.h>
    #include <libavcodec></libavcodec>avcodec.h>
    #include <libavutil></libavutil>mathematics.h>
    #include <libavutil></libavutil>samplefmt.h>
    #include <libavutil></libavutil>timestamp.h>
    #include <libavformat></libavformat>avformat.h>
    #include <libavfilter></libavfilter>avfiltergraph.h>
    }
    #include
    static AVFormatContext *fmt_ctx = NULL;
    static AVCodecContext *video_dec_ctx = NULL, *audio_dec_ctx;
    static int width, height;
    static enum AVPixelFormat pix_fmt;
    static AVStream *video_stream = NULL, *audio_stream = NULL;
    static const char *src_filename = NULL;
    static const char *video_dst_filename = NULL;
    static const char *audio_dst_filename = NULL;
    static FILE *video_dst_file = NULL;
    static FILE *audio_dst_file = NULL;
    static uint8_t *video_dst_data[4] = {NULL};
    static int      video_dst_linesize[4];
    static int video_dst_bufsize;
    static int video_stream_idx = -1, audio_stream_idx = -1;
    static AVPacket *pkt=NULL;
    static AVPacket *pkt1=NULL;
    static AVFrame *frame = NULL;
    //static AVPacket pkt;
    static int video_frame_count = 0;
    static int audio_frame_count = 0;
    static int refcount = 0;
    AVCodec *codec;
    AVCodecContext *c= NULL;
    int i, out_size, size, x, y, outbuf_size;
    AVFrame *picture;
    uint8_t *outbuf, *picture_buf;

    static void encode_packet(int *got_frame)
    {
    int ret, got_output;
    /* send the frame to the encoder */
    if (frame)
    frame->pts=video_dec_ctx->frame_number;
       printf("Send frame %3"PRId64"\n", frame->pts);
    ret = avcodec_encode_video2(c, pkt1, frame, got_frame);
    printf("ret=%d\n", ret);
    if(ret &lt; 0)
    {
       printf("Error while encoding frame\n");
       exit(1);
    }
    if (*got_frame) {
            printf("Write frame %3d (size=%5d)\n", i, pkt1->size);
            fwrite(pkt1->data, 1, pkt1->size, video_dst_file);
           av_free_packet(pkt1);
       }
    }

    static int decode_packet(int *got_frame, int cached)
    {
    AVPacket pkt1;
    int ret = 0;
    int decoded = pkt->size;
    *got_frame = 0;
    if (pkt->stream_index == video_stream_idx) {
       /* decode video frame */
       ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, pkt);
    printf("ret=%d and got_frame=%d\n", ret, *got_frame);

       if (ret &lt; 0) {
           printf("Error decoding video frame (%s)\n", av_err2str(ret));
           return ret;
       }
       if (*got_frame) {
           if (frame->width != width || frame->height != height ||
               frame->format != pix_fmt) {
               /* To handle this change, one could call av_image_alloc again
    and
                * decode the following frames into another rawvideo file. */
               return -1;
           }
           printf("video_frame%s n:%d coded_n:%d\n",
                  cached ? "(cached)" : "",
                  video_frame_count++, frame->coded_picture_number);
    }
    encode_packet(got_frame);
    }
    else if (pkt->stream_index == audio_stream_idx) {
       ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, pkt);
       if (ret &lt; 0) {
           printf("Error decoding audio frame (%s)\n", av_err2str(ret));
           return ret;
       }
       /* Some audio decoders decode only part of the packet, and have to be
        * called again with the remainder of the packet data.
        * Sample: fate-suite/lossless-audio/luckynight-partial.shn
        * Also, some decoders might over-read the packet. */
       decoded = FFMIN(ret, pkt->size);
       if (*got_frame) {
           size_t unpadded_linesize = frame->nb_samples *
    av_get_bytes_per_sample((AVSampleFormat)frame->format);
           printf("audio_frame%s n:%d nb_samples:%d pts:%s\n",
                  cached ? "(cached)" : "",
                  audio_frame_count++, frame->nb_samples,
                  av_ts2timestr(frame->pts, &amp;audio_dec_ctx->time_base));
           /* Write the raw audio data samples of the first plane. This works
            * fine for packed formats (e.g. AV_SAMPLE_FMT_S16). However,
            * most audio decoders output planar audio, which uses a separate
            * plane of audio samples for each channel (e.g.
    AV_SAMPLE_FMT_S16P).
            * In other words, this code will write only the first audio channel
            * in these cases.
            * You should use libswresample or libavfilter to convert the frame
            * to packed data. */
           fwrite(frame->extended_data[0], 1, unpadded_linesize,
    audio_dst_file);
       }
    }
    /* If we use frame reference counting, we own the data and need
    * to de-reference it when we don't use it anymore */
    if (*got_frame &amp;&amp; refcount)
       av_frame_unref(frame);
    return decoded;
    }

    static int open_codec_context(int *stream_idx,
                             AVCodecContext **dec_ctx, AVFormatContext
    *fmt_ctx, enum AVMediaType type)
    {
    int ret, stream_index;
    AVStream *st;
    AVCodec *dec = NULL;
    AVDictionary *opts = NULL;
    ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
    if (ret &lt; 0) {
       printf("Could not find %s stream in input file '%s'\n",
               av_get_media_type_string(type), src_filename);
       return ret;
    } else {
       stream_index = ret;
       st = fmt_ctx->streams[stream_index];
       /* find decoder for the stream */
       dec = avcodec_find_decoder(st->codecpar->codec_id);
       if (!dec) {
           printf("Failed to find %s codec\n",
                   av_get_media_type_string(type));
           return AVERROR(EINVAL);
       }
       /* Allocate a codec context for the decoder */
       *dec_ctx = avcodec_alloc_context3(dec);
       if (!*dec_ctx) {
           printf("Failed to allocate the %s codec context\n",
                   av_get_media_type_string(type));
           return AVERROR(ENOMEM);
       }
       /* Copy codec parameters from input stream to output codec context */
       if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) &lt; 0) {
           printf("Failed to copy %s codec parameters to decoder context\n",
                   av_get_media_type_string(type));
           return ret;
       }
       /* Init the decoders, with or without reference counting */
       av_dict_set(&amp;opts, "refcounted_frames", refcount ? "1" : "0", 0);
       if ((ret = avcodec_open2(*dec_ctx, dec, &amp;opts)) &lt; 0) {
           printf("Failed to open %s codec\n",
                   av_get_media_type_string(type));
           return ret;
       }
       *stream_idx = stream_index;
    }
    return 0;
    }



    int main (int argc, char **argv)
    {
    int ret = 0, got_frame;
    src_filename = argv[1];
    video_dst_filename = argv[2];
    audio_dst_filename = argv[3];
    av_register_all();
    avcodec_register_all();
    printf("Registered all\n");

    /* open input file, and allocate format context */
    if (avformat_open_input(&amp;fmt_ctx, src_filename, NULL, NULL) &lt; 0) {
       printf("Could not open source file %s\n", src_filename);
       exit(1);
    }

    /* retrieve stream information */
    if (avformat_find_stream_info(fmt_ctx, NULL) &lt; 0) {
       printf("Could not find stream information\n");
       exit(1);
    }

    if (open_codec_context(&amp;video_stream_idx, &amp;video_dec_ctx, fmt_ctx,
    AVMEDIA_TYPE_VIDEO) >= 0) {
       video_stream = fmt_ctx->streams[video_stream_idx];
       video_dst_file = fopen(video_dst_filename, "wb");
       if (!video_dst_file) {
           printf("Could not open destination file %s\n", video_dst_filename);
           ret = 1;
    }

    /* allocate image where the decoded image will be put */
    width = video_dec_ctx->width;
    height = video_dec_ctx->height;
    pix_fmt = video_dec_ctx->pix_fmt;
    ret = av_image_alloc(video_dst_data, video_dst_linesize,
                       width, height, pix_fmt, 1);
    if (ret &lt; 0) {
       printf("Could not allocate raw video buffer\n");
       goto end;
    }
    video_dst_bufsize = ret;
    }
    if (open_codec_context(&amp;audio_stream_idx, &amp;audio_dec_ctx, fmt_ctx,
    AVMEDIA_TYPE_AUDIO) >= 0) {
       audio_stream = fmt_ctx->streams[audio_stream_idx];
       audio_dst_file = fopen(audio_dst_filename, "wb");
       if (!audio_dst_file) {
           printf("Could not open destination file %s\n", audio_dst_filename);
           ret = 1;
           goto end;
       }
    }
    /* dump input information to stderr */
    av_dump_format(fmt_ctx, 0, src_filename, 0);

    if (!audio_stream &amp;&amp; !video_stream) {
       printf("Could not find audio or video stream in the input, aborting\n");
       ret = 1;
       goto end;
    }

    frame = av_frame_alloc();
    if (!frame) {
       printf("Could not allocate frame\n");
       ret = AVERROR(ENOMEM);
       goto end;
    }

    pkt = av_packet_alloc();
    if (!pkt)
       exit(1);
    /* initialize packet, set data to NULL, let the demuxer fill it */
    av_init_packet(pkt);
    printf("after dump\n");
    pkt->data = NULL;
    pkt->size = 0;
    if (video_stream)
       printf("Demuxing video from file '%s' into '%s'\n", src_filename,
    video_dst_filename);
    if (audio_stream)
       printf("Demuxing audio from file '%s' into '%s'\n", src_filename,
    audio_dst_filename);


       codec = avcodec_find_encoder(AV_CODEC_ID_MPEG1VIDEO);
    if (!codec) {
       printf("Codec AV_CODEC_ID_MPEG1VIDEO not found\n");
       exit(1);
    }

    c = avcodec_alloc_context3(codec);
    if (!c) {
       printf("Could not allocate video codec context\n");
       exit(1);
    }

    /* put sample parameters */
    c->bit_rate = 400000;
    /* resolution must be a multiple of two */
    c->width = width;
    c->height = height;
    /* frames per second */
    c->time_base = (AVRational){1, 25};
    c->framerate = (AVRational){25, 1};
    /* emit one intra frame every ten frames
    * check frame pict_type before passing frame
    * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
    * then gop_size is ignored and the output of encoder
    * will always be I frame irrespective to gop_size
    */
    c->gop_size = 10;
    c->max_b_frames = 1;
    c->pix_fmt = AV_PIX_FMT_YUV420P;

    pkt1 = av_packet_alloc();
    if (!pkt1)
       exit(1);
    av_init_packet(pkt1);
    pkt1->data=NULL;
    pkt1->size=0;
    ret = avcodec_open2(c, codec, NULL);
    if (ret &lt; 0) {
       printf("Could not open codec: %s\n", av_err2str(ret));
       exit(1);
    }
       /* read frames from the file */
    while (av_read_frame(fmt_ctx, pkt) >= 0) {
       AVPacket orig_pkt = *pkt;
       do {
           ret = decode_packet(&amp;got_frame, 0);
           if (ret &lt; 0)
               break;
           pkt->data += ret;
           pkt->size -= ret;

       } while (pkt->size > 0);
       av_packet_unref(&amp;orig_pkt);
    }
    end:
       avcodec_free_context(&amp;video_dec_ctx);
       avcodec_free_context(&amp;audio_dec_ctx);
       avformat_close_input(&amp;fmt_ctx);
       if (video_dst_file)
           fclose(video_dst_file);
       if (audio_dst_file)
           fclose(audio_dst_file);
       //    av_frame_free(&amp;frame);
       av_free(video_dst_data[0]);
       return ret &lt; 0;
    }

    I want above program to support compress and conversion of different format also.
    Please help me getting the program ready.

  • Video rotating to left by 90 degree when converted using ffmpeg

    15 juillet 2017, par Herdesh Verma

    I developed a below code :

    extern "C"
    {
    #include <libavutil></libavutil>imgutils.h>
    #include <libavutil></libavutil>opt.h>
    #include <libavcodec></libavcodec>avcodec.h>
    #include <libavutil></libavutil>mathematics.h>
    #include <libavutil></libavutil>samplefmt.h>
    #include <libavutil></libavutil>timestamp.h>
    #include <libavformat></libavformat>avformat.h>
    #include <libavfilter></libavfilter>avfiltergraph.h>
    #include <libswscale></libswscale>swscale.h>
    }
    #include
    static AVFormatContext *fmt_ctx = NULL;

    static int frame_index = 0;

    static int j = 0, nbytes=0;
    uint8_t *video_outbuf = NULL;
    static AVPacket *pAVPacket=NULL;
    static int value=0;
    static AVFrame *pAVFrame=NULL;
    static AVFrame *outFrame=NULL;
    static AVStream *video_st=NULL;
    static AVFormatContext *outAVFormatContext=NULL;
    static AVCodec *outAVCodec=NULL;
    static AVOutputFormat *output_format=NULL;
    static AVCodecContext *video_dec_ctx = NULL, *audio_dec_ctx;
    static AVCodecContext *outAVCodecContext=NULL;
    static int width, height;
    static enum AVPixelFormat pix_fmt;
    static AVStream *video_stream = NULL, *audio_stream = NULL;
    static const char *src_filename = NULL;
    static const char *video_dst_filename = NULL;
    static const char *audio_dst_filename = NULL;
    static FILE *video_dst_file = NULL;
    static FILE *audio_dst_file = NULL;
    static uint8_t *video_dst_data[4] = {NULL};
    static int      video_dst_linesize[4];
    static int video_dst_bufsize;
    static int video_stream_idx = -1, audio_stream_idx = -1;
    static AVPacket *pkt=NULL;
    static AVPacket *pkt1=NULL;
    static AVFrame *frame = NULL;
    //static AVPacket pkt;
    static int video_frame_count = 0;
    static int audio_frame_count = 0;
    static int refcount = 0;
    AVCodec *codec;
    static struct SwsContext *sws_ctx;
    AVCodecContext *c= NULL;
    int i, out_size, size, x, y, outbuf_size;
    AVFrame *picture;
    uint8_t *outbuf, *picture_buf;
    int video_outbuf_size;
    int w, h;
    AVPixelFormat pixFmt;
    uint8_t *data[4];
    int linesize[4];

    static int open_codec_context(int *stream_idx,
                             AVCodecContext **dec_ctx, AVFormatContext
    *fmt_ctx, enum AVMediaType type)
    {
    int ret, stream_index;
    AVStream *st;
    AVCodec *dec = NULL;
    AVDictionary *opts = NULL;
    ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
    if (ret &lt; 0) {
       printf("Could not find %s stream in input file '%s'\n",
               av_get_media_type_string(type), src_filename);
       return ret;
    } else {
       stream_index = ret;
       st = fmt_ctx->streams[stream_index];
       /* find decoder for the stream */
       dec = avcodec_find_decoder(st->codecpar->codec_id);
       if (!dec) {
           printf("Failed to find %s codec\n",
                   av_get_media_type_string(type));
           return AVERROR(EINVAL);
       }
       /* Allocate a codec context for the decoder */
       *dec_ctx = avcodec_alloc_context3(dec);
       if (!*dec_ctx) {
           printf("Failed to allocate the %s codec context\n",
                   av_get_media_type_string(type));
           return AVERROR(ENOMEM);
       }
       /* Copy codec parameters from input stream to output codec context */
       if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) &lt; 0) {
           printf("Failed to copy %s codec parameters to decoder context\n",
                   av_get_media_type_string(type));
           return ret;
       }
       /* Init the decoders, with or without reference counting */
       av_dict_set(&amp;opts, "refcounted_frames", refcount ? "1" : "0", 0);
       if ((ret = avcodec_open2(*dec_ctx, dec, &amp;opts)) &lt; 0) {
           printf("Failed to open %s codec\n",
                   av_get_media_type_string(type));
           return ret;
       }
       *stream_idx = stream_index;
    }
    return 0;
    }



    int main (int argc, char **argv)
    {
    int ret = 0, got_frame;
    src_filename = argv[1];
    video_dst_filename = argv[2];
    audio_dst_filename = argv[3];
    av_register_all();
    avcodec_register_all();
    printf("Registered all\n");

    /* open input file, and allocate format context */
    if (avformat_open_input(&amp;fmt_ctx, src_filename, NULL, NULL) &lt; 0) {
       printf("Could not open source file %s\n", src_filename);
       exit(1);
    }

    /* retrieve stream information */
    if (avformat_find_stream_info(fmt_ctx, NULL) &lt; 0) {
       printf("Could not find stream information\n");
       exit(1);
    }

    if (open_codec_context(&amp;video_stream_idx, &amp;video_dec_ctx, fmt_ctx,
    AVMEDIA_TYPE_VIDEO) >= 0) {
       video_stream = fmt_ctx->streams[video_stream_idx];
       avformat_alloc_output_context2(&amp;outAVFormatContext, NULL, NULL,
    video_dst_filename);
       if (!outAVFormatContext)
       {
               printf("\n\nError : avformat_alloc_output_context2()");
               return -1;
       }
    }

    if (open_codec_context(&amp;audio_stream_idx, &amp;audio_dec_ctx, fmt_ctx,
    AVMEDIA_TYPE_AUDIO) >= 0) {
       audio_stream = fmt_ctx->streams[audio_stream_idx];
       audio_dst_file = fopen(audio_dst_filename, "wb");
       if (!audio_dst_file) {
           printf("Could not open destination file %s\n", audio_dst_filename);
           ret = 1;
           goto end;
       }
    }
    /* dump input information to stderr */
    av_dump_format(fmt_ctx, 0, src_filename, 0);

    if (!audio_stream &amp;&amp; !video_stream) {
       printf("Could not find audio or video stream in the input, aborting\n");
       ret = 1;
       goto end;
    }

       output_format = av_guess_format(NULL, video_dst_filename, NULL);
       if( !output_format )
       {
        printf("\n\nError : av_guess_format()");
        return -1;
       }

       video_st = avformat_new_stream(outAVFormatContext ,NULL);
       if( !video_st )
       {
               printf("\n\nError : avformat_new_stream()");
         return -1;
       }

       outAVCodecContext = avcodec_alloc_context3(outAVCodec);
       if( !outAVCodecContext )
       {
         printf("\n\nError : avcodec_alloc_context3()");
         return -1;
       }


       outAVCodecContext = video_st->codec;
       outAVCodecContext->codec_id = AV_CODEC_ID_MPEG4;// AV_CODEC_ID_MPEG4; //
    AV_CODEC_ID_H264 // AV_CODEC_ID_MPEG1VIDEO
       outAVCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
       outAVCodecContext->pix_fmt  = AV_PIX_FMT_YUV420P;
       outAVCodecContext->bit_rate = 400000; // 2500000
       outAVCodecContext->width = 1920;
       //outAVCodecContext->width = 500;
       outAVCodecContext->height = 1080;
       //outAVCodecContext->height = 500;
       outAVCodecContext->gop_size = 3;
       outAVCodecContext->max_b_frames = 2;
       outAVCodecContext->time_base.num = 1;
       outAVCodecContext->time_base.den = 30; // 15fps

       if (outAVCodecContext->codec_id == AV_CODEC_ID_H264)
       {
        av_opt_set(outAVCodecContext->priv_data, "preset", "slow", 0);
       }

       outAVCodec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);
       if( !outAVCodec )
       {
        printf("\n\nError : avcodec_find_encoder()");
        return -1;
       }

       /* Some container formats (like MP4) require global headers to be
    present
          Mark the encoder so that it behaves accordingly. */

       if ( outAVFormatContext->oformat->flags &amp; AVFMT_GLOBALHEADER)
       {
               outAVCodecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
       }

       value = avcodec_open2(outAVCodecContext, outAVCodec, NULL);
       if( value &lt; 0)
       {
               printf("\n\nError : avcodec_open2()");
               return -1;
       }

    /* create empty video file */
       if ( !(outAVFormatContext->flags &amp; AVFMT_NOFILE) )
       {
        if( avio_open2(&amp;outAVFormatContext->pb , video_dst_filename,
    AVIO_FLAG_WRITE ,NULL, NULL) &lt; 0 )
        {
         printf("\n\nError : avio_open2()");
        }
       }

       if(!outAVFormatContext->nb_streams)
       {
               printf("\n\nError : Output file dose not contain any stream");
         return -1;
       }

       /* imp: mp4 container or some advanced container file required header
    information*/
       value = avformat_write_header(outAVFormatContext , NULL);
       if(value &lt; 0)
       {
               printf("\n\nError : avformat_write_header()");
               return -1;
       }

       printf("\n\nOutput file information :\n\n");
       av_dump_format(outAVFormatContext , 0 ,video_dst_filename ,1);


       int flag;
       int frameFinished;


       value = 0;

       pAVPacket = (AVPacket *)av_malloc(sizeof(AVPacket));
       av_init_packet(pAVPacket);

       pAVFrame = av_frame_alloc();
       if( !pAVFrame )
       {
        printf("\n\nError : av_frame_alloc()");
        return -1;
       }

       outFrame = av_frame_alloc();//Allocate an AVFrame and set its fields to
    default values.
       if( !outFrame )
       {
        printf("\n\nError : av_frame_alloc()");
        return -1;
       }

       nbytes = av_image_get_buffer_size(outAVCodecContext-
    >pix_fmt,outAVCodecContext->width,outAVCodecContext->height,32);
       video_outbuf = (uint8_t*)av_malloc(nbytes);
       if( video_outbuf == NULL )
       {
       printf("\n\nError : av_malloc()");
       }


       value = av_image_fill_arrays( outFrame->data, outFrame->linesize,
    video_outbuf , AV_PIX_FMT_YUV420P, outAVCodecContext-
    >width,outAVCodecContext->height,1 ); // returns : the size in bytes
    required for src
       if(value &lt; 0)
       {
       printf("\n\nError : av_image_fill_arrays()");
       }

       SwsContext* swsCtx_ ;

       // Allocate and return swsContext.
       // a pointer to an allocated context, or NULL in case of error
       // Deprecated : Use sws_getCachedContext() instead.
       swsCtx_ = sws_getContext(video_dec_ctx->width,
                               video_dec_ctx->height,
                               video_dec_ctx->pix_fmt,
                               video_dec_ctx->width,
                               video_dec_ctx->height,
                               video_dec_ctx->pix_fmt,
                               SWS_BICUBIC, NULL, NULL, NULL);


       AVPacket outPacket;

       int got_picture;

       while( av_read_frame( fmt_ctx , pAVPacket ) >= 0 )
       {
               if(pAVPacket->stream_index == video_stream_idx)
               {
                       value = avcodec_decode_video2(video_dec_ctx , pAVFrame ,
     &amp;frameFinished , pAVPacket );
                       if( value &lt; 0)
                       {
                               printf("Error : avcodec_decode_video2()");
                       }

                       if(frameFinished)// Frame successfully decoded :)
                       {
                               sws_scale(swsCtx_, pAVFrame->data, pAVFrame-
    >linesize,0, video_dec_ctx->height, outFrame->data,outFrame->linesize);
    //                              sws_scale(swsCtx_, pAVFrame->data, pAVFrame-
    >linesize,0, video_dec_ctx->height, outFrame->data,outFrame->linesize);
                               av_init_packet(&amp;outPacket);
                               outPacket.data = NULL;    // packet data will be
    allocated by the encoder
                               outPacket.size = 0;

                               avcodec_encode_video2(outAVCodecContext ,
    &amp;outPacket ,outFrame , &amp;got_picture);

                               if(got_picture)
                               {
                                       if(outPacket.pts != AV_NOPTS_VALUE)
                                               outPacket.pts =
    av_rescale_q(outPacket.pts, video_st->codec->time_base, video_st-
    >time_base);
                                       if(outPacket.dts != AV_NOPTS_VALUE)
                                               outPacket.dts =
    av_rescale_q(outPacket.dts, video_st->codec->time_base, video_st-
    >time_base);

                                       printf("Write frame %3d (size= %2d)\n",
    j++, outPacket.size/1000);
                                       if(av_write_frame(outAVFormatContext ,
    &amp;outPacket) != 0)
                                       {
                                               printf("\n\nError :
    av_write_frame()");
                                       }

                               av_packet_unref(&amp;outPacket);
                               } // got_picture

                       av_packet_unref(&amp;outPacket);
                       } // frameFinished

               }
       }// End of while-loop

       value = av_write_trailer(outAVFormatContext);
       if( value &lt; 0)
       {
               printf("\n\nError : av_write_trailer()");
       }


       //THIS WAS ADDED LATER
       av_free(video_outbuf);

    end:
       avcodec_free_context(&amp;video_dec_ctx);
       avcodec_free_context(&amp;audio_dec_ctx);
       avformat_close_input(&amp;fmt_ctx);
       if (video_dst_file)
           fclose(video_dst_file);
      if (audio_dst_file)
          fclose(audio_dst_file);
      //av_frame_free(&amp;frame);
      av_free(video_dst_data[0]);
      return ret &lt; 0;
    }

    Problem with above code is that it rotates a video to left by 90 degree.

    Snapshot of video given as input to above program

    Snapshot of output video. It is rotated by 90 degree to left.

    I compiled program using below command :

    g++ -D__STDC_CONSTANT_MACROS -Wall -g ScreenRecorder.cpp -I/home/harry/Documents/compressor/ffmpeg-3.3/ -I/root/android-ndk-r14b/platforms/android-21/arch-x86_64/usr/include/ -c -o ScreenRecorder.o -w

    And linked it using below command :

    g++ -Wall -g ScreenRecorder.o -I/home/harry/Documents/compressor/ffmpeg-3.3/ -I/root/android-ndk-r14b/platforms/android-21/arch-x86_64/usr/include/ -L/usr/lib64 -L/lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.4.7/ -L/home/harry/Documents/compressor/ffmpeg-3.3/ffmpeg-build -L/root/android-ndk-r14b/platforms/android-21/arch-x86_64/usr/lib64 -o ScreenRecorder.exe -lavformat -lavcodec -lavutil -lavdevice -lavfilter -lswscale -lx264 -lswresample -lm -lpthread -ldl -lstdc++ -lc -lrt

    Program is being run using below command :

    ./ScreenRecorder.exe vertical.MOV videoH.mp4 audioH.mp3

    Note :
    - Source video is taken from iphone and is of .mov format.
    - Output video is being stored in .mp4 file.

    Can anyone please tell me why it is rotating video by 90 degree ?

    One thing i noticed in dump is shown below :

     Duration: 00:00:06.04, start: 0.000000, bitrate: 17087 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 17014 kb/s, 29.98 fps, 29.97 tbr, 600 tbn, 1200 tbc (default)
    Metadata:
     rotate          : 90
     creation_time   : 2017-07-09T10:56:42.000000Z
     handler_name    : Core Media Data Handler
     encoder         : H.264
    Side data:
     displaymatrix: rotation of -90.00 degrees

    it says displaymatrix: rotation of -90.00 degrees. Is it responsible for rotating video by 90 degree ?

  • FFmpeg mux video use libavformat avcodec but output couldn't be played

    10 août 2017, par tqn

    I’m trying write a app that take an input video and crop it to square video and ignore audio stream. Because bad performance if using command, I’m trying to use libavcodec and libavformat to do it. But the output isn’t playable by any video player and duration is 0 although I wrote all frame. Here are my code.

    void convert_video(char* input) {
       AVFormatContext *pFormatCtx = NULL;
       int             i, videoStreamIndex;
       AVCodecContext  *pCodecCtx = NULL;
       AVCodec         *pCodec = NULL;
       AVFrame         *pFrame = NULL;
       AVFrame         *pFrameSquare = NULL;
       AVPacket        packet, outPacket;
       int             frameFinished;
       int             numBytes;
       uint8_t         *buffer = NULL;
       AVCodec         *pEncodec = NULL;
       AVFormatContext *poFormatCxt = NULL;
       MuxOutputStream    videoStream = {0}, audioStream = {0};
       int tar_w, tar_h;

       const enum AVPixelFormat pic_format = AV_PIX_FMT_YUV420P;
       const enum AVCodecID codec_id = AV_CODEC_ID_H264;
       AVDictionary    *optionsDict = NULL;
       char output[50];
       sprintf(output, "%soutput.mp4", ANDROID_SDCARD);

       // Register all formats and codecs
       av_register_all();

       // Open video file
       if(avformat_open_input(&amp;pFormatCtx, input, NULL, NULL)!=0)
           return; // Couldn't open file
       avformat_alloc_output_context2(&amp;poFormatCxt, NULL, NULL, output);

       // Retrieve stream information
       if(avformat_find_stream_info(pFormatCtx, NULL)&lt;0)
           return; // Couldn't find stream information

       // Find the first video stream
       videoStreamIndex=-1;
       for(i=0; inb_streams; i++)
           if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
               videoStreamIndex=i;
               break;
           }
       if(videoStreamIndex==-1)
           return; // Didn't find a video stream

       // Get a pointer to the codec context for the video stream
       pCodecCtx = pFormatCtx->streams[videoStreamIndex]->codec;
       tar_w = pCodecCtx->width > pCodecCtx->height ? pCodecCtx->height : pCodecCtx->width;
       tar_h = tar_w;

       // Find the decoder for the video stream
       pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
       pEncodec = avcodec_find_encoder(codec_id);

       add_stream_mux(&amp;videoStream, poFormatCxt, &amp;pEncodec, codec_id, tar_w, tar_h);
       videoStream.st[0].time_base = pFormatCtx->streams[videoStreamIndex]->time_base;
       videoStream.st[0].codec->time_base = videoStream.st[0].time_base;
       videoStream.st[0].codec->time_base.den *= videoStream.st[0].codec->ticks_per_frame;
    //    add_stream(&amp;audioStream, poFormatCxt, &amp;)
       open_video(poFormatCxt, pEncodec, &amp;videoStream, optionsDict);
       int ret = avio_open(&amp;poFormatCxt->pb, output, AVIO_FLAG_WRITE);

       // Open codec
       if(avcodec_open2(pCodecCtx, pCodec, &amp;optionsDict) &lt; 0)
           return; // Could not open codec

       ret = avformat_write_header(poFormatCxt, &amp;optionsDict);
       if (ret != 0) {
           ANDROID_LOG("Died");
       }

       // Allocate video frame
       pFrame=av_frame_alloc();
       pFrame->format = videoStream.st->codec->pix_fmt;
       pFrame->width = pCodecCtx->width;
       pFrame->height = pCodecCtx->height;
       av_frame_get_buffer(pFrame, 32);

       // Allocate an AVFrame structure
       pFrameSquare=av_frame_alloc();
       if(pFrameSquare==NULL)
           return;

       // Determine required buffer size and allocate buffer
       numBytes=avpicture_get_size(pic_format, tar_w,
                                   tar_h);
       buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

       // Assign appropriate parts of buffer to image planes in pFrameSquare
       // Note that pFrameSquare is an AVFrame, but AVFrame is a superset
       // of AVPicture
       ret = avpicture_fill((AVPicture *)pFrameSquare, buffer, pic_format,
                      tar_w, tar_h);
       if (ret &lt; 0) {
           ANDROID_LOG("Can't fill picture");
           return;
       }

       // Read frames and save first five frames to disk
       i=0;
       ret = av_read_frame(pFormatCtx, &amp;packet);
       while(ret >= 0) {
           // Is this a packet from the video stream?
           if(packet.stream_index == videoStreamIndex) {
               // Decode video frame
    //            av_packet_rescale_ts(&amp;packet, videoStream.st->time_base, videoStream.st->codec->time_base);
               avcodec_decode_video2(pCodecCtx, pFrame, &amp;frameFinished,
                                     &amp;packet);
    //            while (!frameFinished) {
    //                avcodec_decode_video2(videoStream.st->codec, pFrame, &amp;frameFinished, NULL);
    //            }
               ANDROID_LOG("Trying to decode frame %d with result %d", i, frameFinished);
               ret = av_picture_crop((AVPicture*) pFrameSquare, (AVPicture*) pFrame, pic_format, 0, 0);
               if (ret &lt; 0) {
                   ANDROID_LOG("Can't crop image");
               }
    //            av_frame_get_best_effort_timestamp(pFrame);
    //            av_rescale_q()

               if(frameFinished) {

                   // Save the frame to disk
                   av_init_packet(&amp;outPacket);
    //                av_packet_rescale_ts(&amp;outPacket, videoStream.st->codec->time_base, videoStream.st->time_base);
                   pFrameSquare->width = tar_w;
                   pFrameSquare->height = tar_h;
                   pFrameSquare->format = pic_format;
                   pFrameSquare->pts = ++videoStream.next_pts;
                   ret = avcodec_encode_video2(videoStream.st->codec, &amp;outPacket, pFrameSquare, &amp;frameFinished);

    //                int count = 0;
    //                while (!frameFinished &amp;&amp; count++ &lt; 6) {
    //                    ret = avcodec_encode_video2(videoStream.st->codec, &amp;outPacket, NULL, &amp;frameFinished);
    //                }
                   if (frameFinished) {
                       ANDROID_LOG("Writing frame %d", i);
                       outPacket.stream_index = videoStreamIndex;
                       av_interleaved_write_frame(poFormatCxt, &amp;outPacket);
                   }
                   av_free_packet(&amp;outPacket);
               }
           }

           // Free the packet that was allocated by av_read_frameav_free_packet(&amp;packet);
           ret = av_read_frame(pFormatCtx, &amp;packet);
       }

       ret = av_write_trailer(poFormatCxt);
       if (ret &lt; 0) {
           ANDROID_LOG("Couldn't write trailer");
       } else {
           ANDROID_LOG("Video convert finished");
       }

       // Free the RGB image
       av_free(buffer);
       av_free(pFrameSquare);

       // Free the YUV frame
       av_free(pFrame);

       // Close the codec
       avcodec_close(pCodecCtx);
    //    avcodec_close(pEncodecCtx);

       // Close the video file
       avformat_close_input(&amp;pFormatCtx);

       return;
    }

    Helper

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

    /* Add an output stream. */
    void add_stream_mux(MuxOutputStream *ost, AVFormatContext *oc,
                          AVCodec **codec,
                          enum AVCodecID codec_id, int width, int heigh)
    {
       AVCodecContext *codecCtx;
       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, *codec);
       if (!ost->st) {
           fprintf(stderr, "Could not allocate stream\n");
           exit(1);
       }
       ost->st->id = oc->nb_streams-1;
       codecCtx = ost->st->codec;
       switch ((*codec)->type) {
           case AVMEDIA_TYPE_AUDIO:
               codecCtx->sample_fmt  = (*codec)->sample_fmts ?
                                (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
               codecCtx->bit_rate    = 64000;
               codecCtx->sample_rate = 44100;
               if ((*codec)->supported_samplerates) {
                   codecCtx->sample_rate = (*codec)->supported_samplerates[0];
                   for (i = 0; (*codec)->supported_samplerates[i]; i++) {
                       if ((*codec)->supported_samplerates[i] == 44100)
                           codecCtx->sample_rate = 44100;
                   }
               }
               codecCtx->channels        = av_get_channel_layout_nb_channels(codecCtx->channel_layout);
               codecCtx->channel_layout = AV_CH_LAYOUT_STEREO;
               if ((*codec)->channel_layouts) {
                   codecCtx->channel_layout = (*codec)->channel_layouts[0];
                   for (i = 0; (*codec)->channel_layouts[i]; i++) {
                       if ((*codec)->channel_layouts[i] == AV_CH_LAYOUT_STEREO)
                           codecCtx->channel_layout = AV_CH_LAYOUT_STEREO;
                   }
               }
               codecCtx->channels        = av_get_channel_layout_nb_channels(codecCtx->channel_layout);
               ost->st->time_base = (AVRational){ 1, codecCtx->sample_rate };
               break;
           case AVMEDIA_TYPE_VIDEO:
               codecCtx->codec_id = codec_id;
               codecCtx->bit_rate = 400000;
               /* Resolution must be a multiple of two. */
               codecCtx->width    = width;
               codecCtx->height   = heigh;
               /* 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 };
               codecCtx->time_base       = ost->st->time_base;
               codecCtx->gop_size      = 12; /* emit one intra frame every twelve frames at most */
               codecCtx->pix_fmt       = STREAM_PIX_FMT;
               if (codecCtx->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
                   /* just for testing, we also add B frames */
                   codecCtx->max_b_frames = 2;
               }
               if (codecCtx->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. */
                   codecCtx->mb_decision = 2;
               }
               break;
           default:
               break;
       }
       /* Some formats want stream headers to be separate. */
       if (oc->oformat->flags &amp; AVFMT_GLOBALHEADER)
           codecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    }
    static void open_video(AVFormatContext *oc, AVCodec *codec, MuxOutputStream *ost, AVDictionary *opt_arg)
    {
       int ret;
       AVCodecContext *c = ost->st->codec;
       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);
           }
       }
    }

    I’m afraid that I set wrong pts or time_base of frame, and also when decoding or encoding, I see that some first frame is lost, frameFinished is 0. See a post that I’ve to flush decoder by avcodec_decode_video2(videoStream.st->codec, pFrame, &amp;frameFinished, NULL) but after try a few times, frameFinished still is 0, and with avcodec_encode_video2(videoStream.st->codec, &amp;outPacket, NULL, &amp;frameFinished) will throw error in next encode frame. So how I can get all frame that lost ? I’m using FFmpeg version 3.0.1