Recherche avancée

Médias (91)

Autres articles (101)

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

    18 février 2011, par

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

  • L’agrémenter visuellement

    10 avril 2011

    MediaSPIP est basé sur un système de thèmes et de squelettes. Les squelettes définissent le placement des informations dans la page, définissant un usage spécifique de la plateforme, et les thèmes l’habillage graphique général.
    Chacun peut proposer un nouveau thème graphique ou un squelette et le mettre à disposition de la communauté.

  • Possibilité de déploiement en ferme

    12 avril 2011, par

    MediaSPIP peut être installé comme une ferme, avec un seul "noyau" hébergé sur un serveur dédié et utilisé par une multitude de sites différents.
    Cela permet, par exemple : de pouvoir partager les frais de mise en œuvre entre plusieurs projets / individus ; de pouvoir déployer rapidement une multitude de sites uniques ; d’éviter d’avoir à mettre l’ensemble des créations dans un fourre-tout numérique comme c’est le cas pour les grandes plate-formes tout public disséminées sur le (...)

Sur d’autres sites (8110)

  • H264 decoding using ffmpeg

    18 mai 2019, par Kindermann

    I’m trying to decode a video stream with ffmpeg library, that’s how I do it basically :

    void video_decode(const char *filename)
    {
       AVCodec *codec;
       AVCodecContext *c= NULL;
       int frame_count=0;
       FILE *f;
       AVFrame *frame;
       uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
       AVPacket avpkt;
       av_init_packet(&avpkt);
       memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
       printf("Decoding video file...\n");
       /* find the h264 video decoder */
       codec = avcodec_find_decoder(AV_CODEC_ID_H264);
       c = avcodec_alloc_context3(codec);
       c->bit_rate = 400000;
       c->width = 1920;
       c->height = 1080;

       if (avcodec_open2(c, codec, NULL) < 0) {
           fprintf(stderr, "Could not open codec\n");
           exit(1);
       }
       frame = av_frame_alloc();
       for (;;) {
           avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);        
       if (avpkt.size == 0)
               break;
           avpkt.data = inbuf;
           while(avpkt.size > 0){

           int len, got_frame;
               len = avcodec_decode_video2(c, frame, &got_frame, &avpkt);          
           if (len < 0) {
                   fprintf(stderr, "Errr while decding frame %d\n", frame_count);
                   exit (1);
               }
               if (got_frame) {
                   //Print out frame information..
           }
               if (avpkt.data) {
                   avpkt.size -= len;
                   avpkt.data += len;
               }
       }              
       }  
    }

    But I got the following outputs :

    Decoding video file...
    [h264 @ 0x303c040] decode_slice_header error
    [h264 @ 0x303c040] decode_slice_header error
    [h264 @ 0x303c040] decode_slice_header error
    [h264 @ 0x303c040] no frame!
    Errr while decding frame 0

    Obviously the initiation of codec was incomplete. Do you have experience with h264 api ? Any help would be appreciated.

  • How to encode using the FFMpeg in Android (using H263)

    10 mai 2019, par Kenny910

    I am trying to follow the sample code on encoding in the ffmpeg document and successfully build a application to encode and generate a mp4 file but I face the following problems :

    1) I am using the H263 for encoding but I can only set the width and height of the AVCodecContext to 176x144, for other case (like 720x480 or 640x480) it will return fail.

    2) I can’t play the output mp4 file by using the default Android player, isn’t it support H263 mp4 file ? p.s. I can play it by using other player

    3) Is there any sample code on encoding other video frame to make a new video (which mean decode the video and encode it back in different quality setting, also i would like to modify the frame content) ?

    Here is my code, thanks !

    JNIEXPORT jint JNICALL Java_com_ffmpeg_encoder_FFEncoder_nativeEncoder(JNIEnv* env, jobject thiz, jstring filename){

    LOGI("nativeEncoder()");

    avcodec_register_all();
    avcodec_init();
    av_register_all();

    AVCodec         *codec;
    AVCodecContext  *codecCtx;
    int             i;
    int             out_size;
    int             size;
    int             x;
    int             y;
    int             output_buffer_size;
    FILE            *file;
    AVFrame         *picture;
    uint8_t         *output_buffer;
    uint8_t         *picture_buffer;

    /* Manual Variables */
    int             l;
    int             fps = 30;
    int             videoLength = 5;

    /* find the H263 video encoder */
    codec = avcodec_find_encoder(CODEC_ID_H263);
    if (!codec) {
       LOGI("avcodec_find_encoder() run fail.");
    }

    codecCtx = avcodec_alloc_context();
    picture = avcodec_alloc_frame();

    /* put sample parameters */
    codecCtx->bit_rate = 400000;
    /* resolution must be a multiple of two */
    codecCtx->width = 176;
    codecCtx->height = 144;
    /* frames per second */
    codecCtx->time_base = (AVRational){1,fps};
    codecCtx->pix_fmt = PIX_FMT_YUV420P;
    codecCtx->codec_id = CODEC_ID_H263;
    codecCtx->codec_type = AVMEDIA_TYPE_VIDEO;

    /* open it */
    if (avcodec_open(codecCtx, codec) < 0) {
       LOGI("avcodec_open() run fail.");
    }

    const char* mfileName = (*env)->GetStringUTFChars(env, filename, 0);

    file = fopen(mfileName, "wb");
    if (!file) {
       LOGI("fopen() run fail.");
    }

    (*env)->ReleaseStringUTFChars(env, filename, mfileName);

    /* alloc image and output buffer */
    output_buffer_size = 100000;
    output_buffer = malloc(output_buffer_size);

    size = codecCtx->width * codecCtx->height;
    picture_buffer = malloc((size * 3) / 2); /* size for YUV 420 */

    picture->data[0] = picture_buffer;
    picture->data[1] = picture->data[0] + size;
    picture->data[2] = picture->data[1] + size / 4;
    picture->linesize[0] = codecCtx->width;
    picture->linesize[1] = codecCtx->width / 2;
    picture->linesize[2] = codecCtx->width / 2;

    for(l=0;l/encode 1 second of video
       for(i=0;i/prepare a dummy image YCbCr
           //Y
           for(y=0;yheight;y++) {
               for(x=0;xwidth;x++) {
                   picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
               }
           }

           //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;
               }
           }

           //encode the image
           out_size = avcodec_encode_video(codecCtx, output_buffer, output_buffer_size, picture);
           fwrite(output_buffer, 1, out_size, file);
       }

       //get the delayed frames
       for(; out_size; i++) {
           out_size = avcodec_encode_video(codecCtx, output_buffer, output_buffer_size, NULL);
           fwrite(output_buffer, 1, out_size, file);
       }
    }

    //add sequence end code to have a real mpeg file
    output_buffer[0] = 0x00;
    output_buffer[1] = 0x00;
    output_buffer[2] = 0x01;
    output_buffer[3] = 0xb7;

    fwrite(output_buffer, 1, 4, file);
    fclose(file);
    free(picture_buffer);
    free(output_buffer);
    avcodec_close(codecCtx);
    av_free(codecCtx);
    av_free(picture);

    LOGI("finish");

    return 0; }
  • Creating a usable H.264 video file

    4 mai 2019, par Ethan McTague

    I am trying to use libavcodec to generate an mp4 video file from individual frames. Each input frame is a qt QImage, and the output file is written to using the Qt QFile class.

    I’ve done this through a VideoTarget class which opens the given ’target’ file when initialized, records frames when addFrame(image) is called, and then saves/closes the file when its destructor is called.

    The class has the following fields :

    AVCodec* m_codec = nullptr;
    AVCodecContext *m_context = nullptr;
    AVPacket* m_packet = nullptr;
    AVFrame* m_frame = nullptr;

    QFile m_target;

    And looks like this :

    VideoTarget::VideoTarget(QString target, QObject *parent) : QObject(parent), m_target(target)
    {
       // Find video codec
       m_codec = avcodec_find_encoder_by_name("libx264rgb");
       if (!m_codec) throw std::runtime_error("Unable to find codec.");

       // Make codec context
       m_context = avcodec_alloc_context3(m_codec);
       if (!m_context) throw std::runtime_error("Unable to allocate codec context.");

       // Make codec packet
       m_packet = av_packet_alloc();
       if (!m_packet) throw std::runtime_error("Unable to allocate packet.");

       // Configure context
       m_context->bit_rate = 400000;
       m_context->width = 1280;
       m_context->height = 720;
       m_context->time_base = (AVRational){1, 60};
       m_context->framerate = (AVRational){60, 1};
       m_context->gop_size = 10;
       m_context->max_b_frames = 1;
       m_context->pix_fmt = AV_PIX_FMT_RGB24;

       if (m_codec->id == AV_CODEC_ID_H264)
           av_opt_set(m_context->priv_data, "preset", "slow", 0);

       // Open Codec
       int ret = avcodec_open2(m_context, m_codec, nullptr);
       if (ret < 0) {
           throw std::runtime_error("Unable to open codec.");
       }

       // Open file
       if (!m_target.open(QIODevice::WriteOnly))
           throw std::runtime_error("Unable to open target file.");

       // Allocate frame
       m_frame = av_frame_alloc();
       if (!m_frame) throw std::runtime_error("Unable to allocate frame.");

       m_frame->format = m_context->pix_fmt;
       m_frame->width = m_context->width;
       m_frame->height = m_context->height;
       m_frame->pts = 0;

       ret = av_frame_get_buffer(m_frame, 24);
       if (ret < 0) throw std::runtime_error("Unable to allocate frame buffer.");
    }

    void VideoTarget::addFrame(QImage &image)
    {
       // Ensure frame data is writable
       int ret = av_frame_make_writable(m_frame);
       if (ret < 0) throw std::runtime_error("Unable to make frame writable.");

       // Prepare image
       for (int y = 0; y < m_context->height; y++) {
           for (int x = 0; x < m_context->width; x++) {
               auto pixel = image.pixelColor(x, y);
               int pos = (y * 1024 + x) * 3;
               m_frame->data[0][pos] = pixel.red();
               m_frame->data[0][pos + 1] = pixel.green();
               m_frame->data[0][pos + 2] = pixel.blue();
           }
       }

       m_frame->pts++;

       // Send the frame
       ret = avcodec_send_frame(m_context, m_frame);
       if (ret < 0) throw std::runtime_error("Unable to send AV frame.");

       while (ret >= 0) {
           ret = avcodec_receive_packet(m_context, m_packet);
           if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
               return;
           else if (ret < 0) throw std::runtime_error("Error during encoding.");

           m_target.write((const char*)m_packet->data, m_packet->size);
           av_packet_unref(m_packet);
       }
    }

    VideoTarget::~VideoTarget()
    {
       int ret = avcodec_send_frame(m_context, nullptr);
       if (ret < 0) throw std::runtime_error("Unable to send AV null frame.");

       while (ret >= 0) {
           ret = avcodec_receive_packet(m_context, m_packet);
           if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
               return;
           else if (ret < 0) throw std::runtime_error("Error during encoding.");

           m_target.write((const char*)m_packet->data, m_packet->size);
           av_packet_unref(m_packet);
       }

       // Magic number at the end of the file
       uint8_t endcode[] = { 0, 0, 1, 0xb7 };
       m_target.write((const char*)endcode, sizeof(endcode));
       m_target.close();

       // Free codec stuff
       avcodec_free_context(&m_context);
       av_frame_free(&m_frame);
       av_packet_free(&m_packet);
    }

    When used, the class seems to work, and data is written to the file, except I am unable to play back the resulting file in any application.

    My main suspect is these lines :

       // Prepare image
       for (int y = 0; y < m_context->height; y++) {
           for (int x = 0; x < m_context->width; x++) {
               auto pixel = image.pixelColor(x, y);
               int pos = (y * 1024 + x) * 3;
               m_frame->data[0][pos] = pixel.red();
               m_frame->data[0][pos + 1] = pixel.green();
               m_frame->data[0][pos + 2] = pixel.blue();
           }
       }

    The libavcodec documentation was extremely vague regarding the layout of image data, so I effectively had to guess and be happy with the first thing that didn’t crash, so chances are I’m writing this incorrectly. There’s also the issue of size mismatch between my pixel color data calls (giving int values) and the 24-bits-per-pixel RGB format I have selected.

    How do I tweak this code to output actual, functioning video files ?