Recherche avancée

Médias (1)

Mot : - Tags -/publier

Autres articles (35)

  • Installation en mode ferme

    4 février 2011, par

    Le mode ferme permet d’héberger plusieurs sites de type MediaSPIP en n’installant qu’une seule fois son noyau fonctionnel.
    C’est la méthode que nous utilisons sur cette même plateforme.
    L’utilisation en mode ferme nécessite de connaïtre un peu le mécanisme de SPIP contrairement à la version standalone qui ne nécessite pas réellement de connaissances spécifique puisque l’espace privé habituel de SPIP n’est plus utilisé.
    Dans un premier temps, vous devez avoir installé les mêmes fichiers que l’installation (...)

  • Emballe médias : à quoi cela sert ?

    4 février 2011, par

    Ce plugin vise à gérer des sites de mise en ligne de documents de tous types.
    Il crée des "médias", à savoir : un "média" est un article au sens SPIP créé automatiquement lors du téléversement d’un document qu’il soit audio, vidéo, image ou textuel ; un seul document ne peut être lié à un article dit "média" ;

  • Ajouter notes et légendes aux images

    7 février 2011, par

    Pour pouvoir ajouter notes et légendes aux images, la première étape est d’installer le plugin "Légendes".
    Une fois le plugin activé, vous pouvez le configurer dans l’espace de configuration afin de modifier les droits de création / modification et de suppression des notes. Par défaut seuls les administrateurs du site peuvent ajouter des notes aux images.
    Modification lors de l’ajout d’un média
    Lors de l’ajout d’un média de type "image" un nouveau bouton apparait au dessus de la prévisualisation (...)

Sur d’autres sites (5773)

  • How can i mux H264 stream into MP4 file via libavformat

    14 août, par Brian

    I want to realize an application that firstly decode a multi-media file(such as test.mp4 file, video codec id is H264), get a video stream and an audio stream, then make some different in the audio stream, at last encode the video stream (use libx264) and audio stream into a result file (result.mp4). To promote the efficiency, I omitted the decode and encode of video stream, i get the video packet via function av_read_frame, then output it directly into the result file via function av_write_frame. But there is no picture in the output file, and the size of output file is fairly small.

    


    I tracked the ffmpeg code and found that in the function av_write_frame->mov_write_packet->ff_mov_write_packet, it will call function ff_avc_parse_nal_units to obtain the size of nal unit, but the return value is very small (such as 208 bytes).

    


    I find that the H264 stream in the MP4 file is not stored in Annex-B format, so it can't find start code (0x000001).

    


    Now my problem is how can I change the H264 stream to Annex-B format, and make it work ?

    


    I added start code at the beginning of every frame manually, but it still not work.

    


    Following is the codes similar with my :

    


    // write the stream header, if any
av_write_header(pFormatCtxEnc);

.........
/**
* Init of Encoder and Decoder
*/

bool KeyFlag = false;
bool KeyFlagEx = false;
// Read frames and save frames to disk
int iPts = 1;
av_init_packet(&packet);
while(av_read_frame(pFormatCtxDec, &packet)>=0)
{
        if (packet.flags == 1)
                KeyFlag = true;

        if (!KeyFlag)
                continue;

        if (m_bStop)
        {
                break;
        }

        // Is this a packet from the video stream?
        if(packet.stream_index == videoStream)
        {
                currentframeNum ++;
                if (progressCB != NULL && currentframeNum%20 == 0)
                {
                        float fpercent = (float)currentframeNum/frameNum;
                        progressCB(fpercent,m_pUser);
                }

                if (currentframeNum >= beginFrame && currentframeNum <= endFrane)
                {
                        if (packet.flags == 1)
                                KeyFlagEx = true;

                        if (!KeyFlagEx)
                                continue;
                        packet.dts = iPts ++;
                        av_write_frame(pFormatCtxEnc, &packet);

                }
        }
        // Free the packet that was allocated by av_read_frame
}

// write the trailer, if any
av_write_trailer(pFormatCtxEnc);

/**
* Release of encoder and decoder
*/

return true;


    


  • FFmpeg - Output has only the first frame

    30 novembre 2018, par natario

    I am working on a simple 3 frames animated GIF and trying to get the same GIF as output after scaling down each frame.

    The program seems to work well, except for the fact that the output has a single frame only (no animation, just the first frame). What coding error can be causing this issue ?

    • I am setting the encoder time_base to the input AVStream.time_base, which in my case happens to be 1/100
    • I am writing each frame with av_interleaved_write_frame, setting the packet pts to the input AVFrame.pts (which happen to be 0, 40 and 80)
    • I know for sure that write function is called three times with these pts. The output GIF, analized at https://ezgif.com/split , actually contains 3 frames. But they are all identical.

    It’s sure this is an error within my program, so I am hoping that someone can give some clues as to why this could happen while muxing (incorrectly reusing some buffer, ....).

    Below are the most relevant parts (I am a beginner) :

    The main function :

    extern "C" int compress(Context* context) {
       context->error = avformat_write_header(context->outFormat, &context->inFormat->metadata);
       if (hasError(context, "Writing headers")) return context->error;

       while (av_read_frame(context->inFormat, context->inPacket) >= 0) {
           AVPacket origPacket = *context->inPacket;
           int temp;
           do {
               context->error = decodePacket(context);
               if (hasError(context, "Decoding packet")) return context->error;
               temp = context->error;
               context->inPacket->data += temp;
               context->inPacket->size -= temp;
           } while (context->inPacket->size > 0);
           av_packet_unref(&origPacket);
       }

       // flush cached frames and the decoder
       context->inPacket->data = NULL;
       context->inPacket->size = 0;
       do {} while (decodePacket(context) > 0);

       // Write the trailer
       context->error = av_write_trailer(context->outFormat);
       if (hasError(context, "Writing trailers")) return context->error;
       return 0;
    }

    The decodePacket function :

    extern "C" int decodePacket(Context* context) {
       int size = context->inPacket->size;
       int used = avcodec_send_packet(context->decoder, context->inPacket);
       if (used < 0) return used;
       int error;
       while (used >= 0) {
           used = avcodec_receive_frame(context->decoder, context->inFrame);
           if (used == AVERROR(EAGAIN) || used == AVERROR_EOF) {
               return size;
           } else if (used < 0) {
               return used;
           }
           error = encodeFrame(context);
           if (error < 0) return error;
       }
       return size;
    }

    The writeFrame function :

    extern "C" int encodeFrame(Context* context) {
       sws_scale(context->scale, context->inFrame->data,
                 context->inFrame->linesize, 0,
                 context->inFrame->height, context->outFrame->data,
                 context->outFrame->linesize);
       AVPacket packet = { 0 };
       av_init_packet(&packet);
       context->outFrame->pts = context->inFrame->pts;
       int temp = avcodec_send_frame(context->encoder, context->outFrame);
       if (temp < 0) return temp;
       while (temp >= 0) {
           temp = avcodec_receive_packet(context->encoder, &packet);
           if (temp == AVERROR(EAGAIN) || temp == AVERROR_EOF) {
               return 0;
           } else if (temp < 0) {
               return temp;
           }

           packet.pts = context->inFrame->pts;
           int error = av_interleaved_write_frame(context->outFormat, &packet);
           if (error < 0) {
               return error;
           }    
       }
       return 0;
    }

    And finally this context struct that is being passed around :

    typedef struct Context {
       Context() {}
       AVFrame* inFrame = NULL;
       AVFrame* outFrame = NULL;
       AVStream* inStream = NULL;
       AVStream* outStream = NULL;
       AVFormatContext* outFormat = NULL;
       AVFormatContext* inFormat = NULL;
       struct SwsContext* scale = NULL;
       AVCodecContext* decoder = NULL;
       AVCodecContext* encoder = NULL;
       AVCodec* decoderCodec = NULL;
       AVCodec* encoderCodec = NULL;
       AVPacket* inPacket = NULL;
       int error = 0;
       const char* inFilename = NULL;
       const char* outFilename = NULL;
       int inStreamId = -1; // Video
       AVPixelFormat sourceFormat;
       AVPixelFormat destFormat;
       int sourceWidth, sourceHeight;
       int destWidth, destHeight;
    } Context;
  • FFMPEG AAC encoder issue

    28 novembre 2018, par Harshil Makwana

    I am trying to capture and encode audio data, I am encoding audio using
    FFMPEG AAC and to capture PCM data I used ALSA, Capturing part is working in my case, However, AAC encoder is not working.

    I am trying to play test.aac file using

    ffplay test.aac

    but it contains lots of noise.

    Attaching code for aac encoder :

    #include "AudioEncoder.h"

    void* AudioEncoder::run(void *ctx)
    {
       return ((AudioEncoder *)ctx)->execute();
    }

    static int frameCount = 0;

    void* AudioEncoder::execute(void)
    {
       float buf[size], *temp;
       int totalSize = 0;
       int fd = open("in.pcm", O_CREAT| O_RDWR, 0666);
       int frameSize = 128 * snd_pcm_format_width(SND_PCM_FORMAT_FLOAT) / 8 * 2;
       av_new_packet(&pkt,size);
       cout << size << endl;
       while (!Main::stopThread)
       {
           temp = (Main::fbAudio)->dequeue();
           memcpy(buf + totalSize, temp, frameSize);
           write(fd, temp, frameSize); // Can play in.pcm with no noise in it.
           totalSize += frameSize;
           delete temp;
           if (totalSize >= size)
           {
               totalSize = 0;
               //frame_buf = (uint8_t *) buf;
               pFrame->data[0] = (uint8_t *)buf;  //PCM Data

               pFrame->pts=frameCount;
               frameCount++;
               got_frame=0;
               //Encode
               ret = avcodec_encode_audio2(pCodecCtx, &pkt,pFrame, &got_frame);
               if(ret < 0){
                   cerr << "Failed to encode!\n";
                   return NULL;
               }
               if (got_frame==1){
                   printf("Succeed to encode 1 frame! \tsize:%5d\n",pkt.size);
                   pkt.stream_index = audio_st->index;
    #ifdef DUMP_TEST
                   ret = av_write_frame(pFormatCtx, &pkt);
    #endif
                   av_free_packet(&pkt);
               }
               //memset(buf, 0, sizeof(float)*size);
           }
           //delete temp;
           //if (buf.size() >= m_audio_output_decoder_ctx->frame_size)
           /* encode the audio*/
       }
           close(fd);
       Main::stopThread = true;
       return NULL;
    }

    int AudioEncoder::flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index){
       int ret;
       int got_frame;
       AVPacket enc_pkt;
       if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &
           CODEC_CAP_DELAY))
           return 0;
       while (1) {
           enc_pkt.data = NULL;
           enc_pkt.size = 0;
           av_init_packet(&enc_pkt);
           ret = avcodec_encode_audio2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,
               NULL, &got_frame);
           av_frame_free(NULL);
           if (ret < 0)
               break;
           if (!got_frame){
               ret=0;
               break;
           }
           printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n",enc_pkt.size);
           /* mux encoded frame */
    #ifdef DUMP_TEST
           ret = av_write_frame(fmt_ctx, &enc_pkt);
           if (ret < 0)
               break;
    #endif
       }
       return ret;
    }

    void AudioEncoder::start(void)
    {
       pthread_t encoder;
       pthread_create(&encoder, NULL, &AudioEncoder::run, this);
    }

    AudioEncoder::AudioEncoder() : out_file("test.aac")
    {
       got_frame = 0;
       ret = 0;
       size = 0;
       av_register_all();
       avcodec_register_all();
       //Method 1.
       pFormatCtx = avformat_alloc_context();
       fmt = av_guess_format(NULL, out_file, NULL);
       pFormatCtx->oformat = fmt;

    #ifdef DUMP_TEST
       if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0){
           cerr << "Failed to open output file!\n";
           return;
       }
    #endif
       audio_st = avformat_new_stream(pFormatCtx, 0);
       if (audio_st==NULL){
           return;
       }
       pCodecCtx = audio_st->codec;
       pCodecCtx->codec_id = fmt->audio_codec;
       pCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
       pCodecCtx->sample_fmt = AV_SAMPLE_FMT_FLTP;
       pCodecCtx->sample_rate= 8000;
       pCodecCtx->channel_layout=AV_CH_LAYOUT_STEREO;
       pCodecCtx->channels = av_get_channel_layout_nb_channels(pCodecCtx->channel_layout);
    //    pCodecCtx->bit_rate = 64000;

    #ifdef DUMP_TEST
       //Show some information
       av_dump_format(pFormatCtx, 0, out_file, 1);
    #endif

       pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
       if (!pCodec){
           printf("Can not find encoder!\n");
           return;
       }
       if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0){
           printf("Failed to open encoder!\n");
           return;
       }
       pFrame = av_frame_alloc();
       pFrame->nb_samples= pCodecCtx->frame_size;
       pFrame->format= pCodecCtx->sample_fmt;

       size = av_samples_get_buffer_size(NULL, pCodecCtx->channels,pCodecCtx->frame_size,pCodecCtx->sample_fmt, 1);
       frame_buf = (uint8_t *)av_malloc(size);
       avcodec_fill_audio_frame(pFrame, pCodecCtx->channels, pCodecCtx->sample_fmt,(const uint8_t*)frame_buf, size, 1);
       //Write Header
    #ifdef DUMP_TEST
       avformat_write_header(pFormatCtx,NULL);
    #endif
    }

    AudioEncoder::~AudioEncoder()
    {
       //Flush Encoder
       ret = flush_encoder(pFormatCtx,0);
       if (ret < 0) {
           cerr << "Flushing encoder failed\n";
           return;
       }

    #ifdef DUMP_TEST
       //Write Trailer
       av_write_trailer(pFormatCtx);
    #endif
       //Clean
       if (audio_st){
           avcodec_close(audio_st->codec);
           av_free(pFrame);
           av_free(frame_buf);
       }
       avio_close(pFormatCtx->pb);
       avformat_free_context(pFormatCtx);
    }

    Here, please ignore DUMP_TEST flag, I already enabled it.

    Can some one tell me what is issue ?

    Thanks,
    Harshil