Recherche avancée

Médias (2)

Mot : - Tags -/plugins

Autres articles (60)

  • La file d’attente de SPIPmotion

    28 novembre 2010, par

    Une file d’attente stockée dans la base de donnée
    Lors de son installation, SPIPmotion crée une nouvelle table dans la base de donnée intitulée spip_spipmotion_attentes.
    Cette nouvelle table est constituée des champs suivants : id_spipmotion_attente, l’identifiant numérique unique de la tâche à traiter ; id_document, l’identifiant numérique du document original à encoder ; id_objet l’identifiant unique de l’objet auquel le document encodé devra être attaché automatiquement ; objet, le type d’objet auquel (...)

  • Des sites réalisés avec MediaSPIP

    2 mai 2011, par

    Cette page présente quelques-uns des sites fonctionnant sous MediaSPIP.
    Vous pouvez bien entendu ajouter le votre grâce au formulaire en bas de page.

  • Gestion des droits de création et d’édition des objets

    8 février 2011, par

    Par défaut, beaucoup de fonctionnalités sont limitées aux administrateurs mais restent configurables indépendamment pour modifier leur statut minimal d’utilisation notamment : la rédaction de contenus sur le site modifiables dans la gestion des templates de formulaires ; l’ajout de notes aux articles ; l’ajout de légendes et d’annotations sur les images ;

Sur d’autres sites (5945)

  • conversion from cv::mat to avframe, encode in h264 dont writing correctly [on hold]

    15 novembre 2014, par danics

    i create a method for converting between cv::mat to avframe with PIX_FMT_YUV420P format (oframe format) but _convCtx always return null, whats wrong ? thanks in advance ! ((i solve this)) but i have another probelm in end of this question please

    void processToFrame(cv::Mat* _mat, AVFrame* oframe)
     {
         // Create and allocate the conversion frame.
         if (_oframe == nullptr) {
             _oframe = avcodec_alloc_frame();  
             if (_oframe == nullptr)
                 throw std::runtime_error("Matrix Converter: Could not allocate the output frame.");

             avpicture_alloc(reinterpret_cast(_oframe),
                 PIX_FMT_BGR24, _mat->cols, _mat->rows);            
         }

         avpicture_fill(reinterpret_cast(_oframe),
             (uint8_t *)_mat->data,
             AV_PIX_FMT_BGR24,
             _mat->cols,
             _mat->rows);

         // Convert the image from its native format to BGR.
         if (_convCtx == nullptr) {
             _convCtx = sws_getContext(
                 oframe->width, oframe->height, (enum AVPixelFormat) oframe->format,
                 _oframe->width, _oframe->height, PIX_FMT_BGR24,
                 SWS_BICUBIC, nullptr, nullptr, nullptr);
         }
         if (_convCtx == nullptr)
             throw std::runtime_error("Matrix Converter: Unable to initialize the conversion context.");  

         // Scales the source data according to our SwsContext settings.
         if (sws_scale(_convCtx,
             _oframe->data, _oframe->linesize, 0, _oframe->height,
             oframe->data, oframe->linesize) < 0)
             throw std::runtime_error("Matrix Converter: Pixel format conversion not supported.");
     }

    create oframe

    void createVideoFile(const char* filename, int w, int h, int codec_id, int fps)
     {
         /* find the mpeg1 video encoder */
         if(codec == nullptr)
         {
             /* find the mpeg1 video encoder */
             codec = avcodec_find_encoder((AVCodecID)codec_id);
             if (!codec) {
                 throw std::runtime_error("codec not found\n");
             }
         }

         if(c == nullptr)
         {
             c = avcodec_alloc_context3(codec);
             if(!c)
             {
                 throw std::runtime_error("Could not allocate video codec context\n");
             }
             /* put sample parameters */
             c->bit_rate = 400000;
             /* resolution must be a multiple of two */
             c->width = 2 * (w / 2);
             c->height = 2 * (h / 2);
             /* frames per second */
             AVRational ar;
             ar.den = 1;
             ar.num = fps;

             c->time_base= ar; //(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;

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

             /* open it */
             if (avcodec_open2(c, codec, NULL) < 0) {
                 throw std::runtime_error("Could not open codec\n");
             }

             f = fopen(filename, "wb");
             if (!f) {
                 throw std::runtime_error("could not open file\n");
             }

             frame = avcodec_alloc_frame();
             if (!frame) {
                 throw std::runtime_error("Could not allocate video frame\n");
             }
             frame->format = c->pix_fmt;
             frame->width  = c->width;
             frame->height = c->height;

             /* the image can be allocated by any means and av_image_alloc() is
              * just the most convenient way if av_malloc() is to be used */

             int ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height, c->pix_fmt, 32);

             if (ret < 0) {
                 throw std::runtime_error("Could not allocate raw picture buffer\n");
             }
         }
    }

    read mat images

    void video_encode_example(const cv::Mat& fin)
    {
       AVPacket pkt;
       /* encode 1 second of video */
       av_init_packet(&pkt);
       pkt.data = NULL;    // packet data will be allocated by the encoder
       pkt.size = 0;

       fflush(stdout);

       cv::Mat res;
       cv::resize(fin, res, cv::Size(c->width, c->height));

       processToFrame(&res, frame);
       frame->pts = i;

       /* encode the image */
       int ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
       if (ret < 0) {
       throw std::runtime_error("Error encoding frame\n");
       }

       if (got_output) {
           printf("Write frame %3d (size=%5d)\n", i, pkt.size);
           fwrite(pkt.data, 1, pkt.size, f);
           av_free_packet(&pkt);
       }
       i++;
    }

    update :

    i can solve the conversion problem by setting of _oframe.width = _mat.cols and _oframe.height = _mat.rows, avpicture_alloc dont set this value itself.

    now i have the other problem my videowriting class seems write ok because windows slideshow show one of frames, but i cant view video in my players, this is my release function, i dont know whats wrong endcoding or endcoder structure parameter like pts and else.

    void video_release()
       {
           /* get the delayed frames */
           for (got_output = 1; got_output; i++) {
               fflush(stdout);

               int ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
               if (ret < 0) {
                   throw std::runtime_error("Error encoding frame\n");
               }

               if (got_output) {
                   printf("Write frame %3d (size=%5d)\n", i, pkt.size);
                   fwrite(pkt.data, 1, pkt.size, f);
                   av_free_packet(&pkt);
               }
           }

           uint8_t endcode[] = { 0, 0, 1, 0xb7 };

           /* add sequence end code to have a real mpeg file */
           fwrite(endcode, 1, sizeof(endcode), f);
           fclose(f);

           avcodec_close(c);
           av_free(c);
           av_freep(&frame->data[0]);
           avcodec_free_frame(&frame);

           c = nullptr;
           codec = nullptr;
           frame = nullptr;

           if (_convCtx)
               sws_freeContext(_convCtx);

           if (_oframe) {
               //if (_oframe->data)
                   //av_free(_oframe->data[0]);
               av_free(_oframe);
           }

           _oframe = nullptr;
           _convCtx = nullptr;
       }
  • conversion from cv::mat to avframe

    14 novembre 2014, par danics

    i create a method for converting between cv::mat to avframe with PIX_FMT_YUV420P format (oframe format) but _convCtx always return null, whats wrong ? thanks in advance !

    void processToFrame(cv::Mat* _mat, AVFrame* oframe)
     {
         // Create and allocate the conversion frame.
         if (_oframe == nullptr) {
             _oframe = avcodec_alloc_frame();  
             if (_oframe == nullptr)
                 throw std::runtime_error("Matrix Converter: Could not allocate the output frame.");

             avpicture_alloc(reinterpret_cast(_oframe),
                 PIX_FMT_BGR24, _mat->cols, _mat->rows);            
         }

         avpicture_fill(reinterpret_cast(_oframe),
             (uint8_t *)_mat->data,
             AV_PIX_FMT_BGR24,
             _mat->cols,
             _mat->rows);

         // Convert the image from its native format to BGR.
         if (_convCtx == nullptr) {
             _convCtx = sws_getContext(
                 oframe->width, oframe->height, (enum AVPixelFormat) oframe->format,
                 _oframe->width, _oframe->height, PIX_FMT_BGR24,
                 SWS_BICUBIC, nullptr, nullptr, nullptr);
         }
         if (_convCtx == nullptr)
             throw std::runtime_error("Matrix Converter: Unable to initialize the conversion context.");  

         // Scales the source data according to our SwsContext settings.
         if (sws_scale(_convCtx,
             _oframe->data, _oframe->linesize, 0, _oframe->height,
             oframe->data, oframe->linesize) < 0)
             throw std::runtime_error("Matrix Converter: Pixel format conversion not supported.");
     }

    edit : create oframe

    void createVideoFile(const char* filename, int w, int h, int codec_id, int fps)
     {
         /* find the mpeg1 video encoder */
         if(codec == nullptr)
         {
             /* find the mpeg1 video encoder */
             codec = avcodec_find_encoder((AVCodecID)codec_id);
             if (!codec) {
                 throw std::runtime_error("codec not found\n");
             }
         }

         if(c == nullptr)
         {
             c = avcodec_alloc_context3(codec);
             if(!c)
             {
                 throw std::runtime_error("Could not allocate video codec context\n");
             }
             /* put sample parameters */
             c->bit_rate = 400000;
             /* resolution must be a multiple of two */
             c->width = 2 * (w / 2);
             c->height = 2 * (h / 2);
             /* frames per second */
             AVRational ar;
             ar.den = 1;
             ar.num = fps;

             c->time_base= ar; //(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;

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

             /* open it */
             if (avcodec_open2(c, codec, NULL) < 0) {
                 throw std::runtime_error("Could not open codec\n");
             }

             f = fopen(filename, "wb");
             if (!f) {
                 throw std::runtime_error("could not open file\n");
             }

             frame = avcodec_alloc_frame();
             if (!frame) {
                 throw std::runtime_error("Could not allocate video frame\n");
             }
             frame->format = c->pix_fmt;
             frame->width  = c->width;
             frame->height = c->height;

             /* the image can be allocated by any means and av_image_alloc() is
              * just the most convenient way if av_malloc() is to be used */

             int ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height, c->pix_fmt, 32);

             if (ret < 0) {
                 throw std::runtime_error("Could not allocate raw picture buffer\n");
             }
         }
    }

    read mat images

    void video_encode_example(const cv::Mat& fin)
    {
       AVPacket pkt;
       /* encode 1 second of video */
       av_init_packet(&pkt);
       pkt.data = NULL;    // packet data will be allocated by the encoder
       pkt.size = 0;

       fflush(stdout);

       cv::Mat res;
       cv::resize(fin, res, cv::Size(c->width, c->height));

       processToFrame(&res, frame);
       frame->pts = i;

       /* encode the image */
       int ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
       if (ret < 0) {
       throw std::runtime_error("Error encoding frame\n");
       }

       if (got_output) {
           printf("Write frame %3d (size=%5d)\n", i, pkt.size);
           fwrite(pkt.data, 1, pkt.size, f);
           av_free_packet(&pkt);
       }
       i++;
       /* get the delayed frames */
       /*for (got_output = 1; got_output; i++) {
           fflush(stdout);

           ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
           if (ret < 0) {
               throw std::runtime_error("Error encoding frame\n");
           }

           if (got_output) {
               printf("Write frame %3d (size=%5d)\n", i, pkt.size);
               fwrite(pkt.data, 1, pkt.size, f);
               av_free_packet(&pkt);
           }
       }*/
    }
  • Convert YUV frames into RGBA frames with FFMPEG

    28 mai 2015, par Dave_Dev

    I would like to develop an application which would be able to convert YUV frames into RGBA frames using the ffmpeg library.
    I have begun writing this code :

    void Decode::video_encode_example(const char *filename, int codec_id)
    {
       AVCodec *codec;
       AVCodecContext *c= NULL;
       int i, ret, x, y, got_output;
       FILE *f;
       AVFrame *frame;
       AVPacket pkt;
       uint8_t endcode[] = { 0, 0, 1, 0xb7 };

       printf("Encode video file %s\n", filename);

       /* find the mpeg1 video encoder */
       codec = avcodec_find_encoder((enum AVCodecID)codec_id);

       if (!codec) {
           fprintf(stderr, "Codec not found\n");
           exit(1);
       }

       c = avcodec_alloc_context3(codec);
       if (!c) {
           fprintf(stderr, "Could not allocate video codec context\n");
           exit(2);
       }

       /* put sample parameters */
       c->bit_rate = 400000;
       /* resolution must be a multiple of two */
       c->width = 352; // Avant c'était du 352x288
       c->height = 288;
       /* frames per second */
       c->time_base = (AVRational){1,25};
       /* 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;
       printf("Avant\n");
       c->pix_fmt = PIX_FMT_RGBA;// Avant c'était AV_PIX_FMT_YUV420P
       printf("Après\n");
       if (codec_id == AV_CODEC_ID_H264)
           av_opt_set(c->priv_data, "preset", "slow", 0);

       /* open it */
       if (avcodec_open2(c, codec, NULL) < 0) {
           fprintf(stderr, "Could not open codec\n");
           exit(3);
       }

       f = fopen(filename, "wb");
       if (!f) {
           fprintf(stderr, "Could not open %s\n", filename);
           exit(4);
       }

       frame = avcodec_alloc_frame();// Dans une version plus récente c'est av_frame_alloc
       if (!frame) {
           fprintf(stderr, "Could not allocate video frame\n");
           exit(5);
       }
       frame->format = c->pix_fmt;
       frame->width  = c->width;
       frame->height = c->height;

    However, each time I run this application, the following error appears in my Linux terminal :

    [mpeg2video @ 0x10c7040] Specified pix_fmt is not supported

    Could you help me please ?