Recherche avancée

Médias (1)

Mot : - Tags -/book

Autres articles (96)

  • Publier sur MédiaSpip

    13 juin 2013

    Puis-je poster des contenus à partir d’une tablette Ipad ?
    Oui, si votre Médiaspip installé est à la version 0.2 ou supérieure. Contacter au besoin l’administrateur de votre MédiaSpip pour le savoir

  • MediaSPIP Core : La Configuration

    9 novembre 2010, par

    MediaSPIP Core fournit par défaut trois pages différentes de configuration (ces pages utilisent le plugin de configuration CFG pour fonctionner) : une page spécifique à la configuration générale du squelettes ; une page spécifique à la configuration de la page d’accueil du site ; une page spécifique à la configuration des secteurs ;
    Il fournit également une page supplémentaire qui n’apparait que lorsque certains plugins sont activés permettant de contrôler l’affichage et les fonctionnalités spécifiques (...)

  • Supporting all media types

    13 avril 2011, par

    Unlike most software and media-sharing platforms, MediaSPIP aims to manage as many different media types as possible. The following are just a few examples from an ever-expanding list of supported formats : images : png, gif, jpg, bmp and more audio : MP3, Ogg, Wav and more video : AVI, MP4, OGV, mpg, mov, wmv and more text, code and other data : OpenOffice, Microsoft Office (Word, PowerPoint, Excel), web (html, CSS), LaTeX, Google Earth and (...)

Sur d’autres sites (8639)

  • iPhone camera shooting video using the AVCaptureSession and using ffmpeg CMSampleBufferRef a change in h.264 format is the issue. please advice

    4 janvier 2012, par isaiah

    My goal is h.264/AAC , mpeg2-ts streaming to server from iphone device.

    Current my source is FFmpeg+libx264 compile success. I Know gnu License. I want the demo program.

    I'm want to know that

    1.CMSampleBufferRef to AVPicture data is success ?

    avpicture_fill((AVPicture*)pFrame, rawPixelBase, PIX_FMT_RGB32, width, height);
     pFrame linesize and data is not null but pst -9233123123 . outpic also .
    Because of this I have to guess 'non-strictly-monotonic PTS' message

    2.This log is repeat.

    encoding frame (size= 0)
    encoding frame = "" , 'avcodec_encode_video' return 0 is success but always 0 .

    I don't know what to do...

    2011-06-01 15:15:14.199 AVCam[1993:7303] pFrame = avcodec_alloc_frame();
    2011-06-01 15:15:14.207 AVCam[1993:7303] avpicture_fill = 1228800
    Video encoding
    2011-0601 15:5:14.215 AVCam[1993:7303] codec = 5841844
    [libx264 @ 0x1441e00] using cpu capabilities: ARMv6 NEON
    [libx264 @ 0x1441e00] profile Constrained Baseline, level 2.0[libx264 @ 0x1441e00] non-strictly-monotonic PTS
    encoding frame (size=    0)
    encoding frame
    [libx264 @ 0x1441e00] final ratefactor: 26.74

    3.I have to guess 'non-strictly-monotonic PTS' message is the cause of all problems.
    what is this 'non-strictly-monotonic PTS' .

     this is source 

    (void)        captureOutput:(AVCaptureOutput *)captureOutput
           didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
                  fromConnection:(AVCaptureConnection *)connection
    {

       if( !CMSampleBufferDataIsReady(sampleBuffer) )
       {
           NSLog( @"sample buffer is not ready. Skipping sample" );
           return;
       }


       if( [isRecordingNow isEqualToString:@"YES"] )
       {
           lastSampleTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
           if( videoWriter.status != AVAssetWriterStatusWriting  )
           {
               [videoWriter startWriting];
               [videoWriter startSessionAtSourceTime:lastSampleTime];
           }

           if( captureOutput == videooutput )
           {
               [self newVideoSample:sampleBuffer];

               CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
               CVPixelBufferLockBaseAddress(pixelBuffer, 0);

               // access the data
               int width = CVPixelBufferGetWidth(pixelBuffer);
               int height = CVPixelBufferGetHeight(pixelBuffer);
               unsigned char *rawPixelBase = (unsigned char *)CVPixelBufferGetBaseAddress(pixelBuffer);

               AVFrame *pFrame;
               pFrame = avcodec_alloc_frame();
               pFrame->quality = 0;

               NSLog(@"pFrame = avcodec_alloc_frame(); ");

    //          int bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0);

    //          int bytesSize = height * bytesPerRow ;  

    //          unsigned char *pixel = (unsigned char*)malloc(bytesSize);

    //          unsigned char *rowBase = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);

    //          memcpy (pixel, rowBase, bytesSize);


               int avpicture_fillNum = avpicture_fill((AVPicture*)pFrame, rawPixelBase, PIX_FMT_RGB32, width, height);//PIX_FMT_RGB32//PIX_FMT_RGB8
               //NSLog(@"rawPixelBase = %i , rawPixelBase -s = %s",rawPixelBase, rawPixelBase);
               NSLog(@"avpicture_fill = %i",avpicture_fillNum);
               //NSLog(@"width = %i,height = %i",width, height);



               // Do something with the raw pixels here

               CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);

               //avcodec_init();
               //avdevice_register_all();
               av_register_all();





               AVCodec *codec;
               AVCodecContext *c= NULL;
               int  out_size, size, outbuf_size;
               //FILE *f;
               uint8_t *outbuf;

               printf("Video encoding\n");

               /* find the mpeg video encoder */
               codec =avcodec_find_encoder(CODEC_ID_H264);//avcodec_find_encoder_by_name("libx264"); //avcodec_find_encoder(CODEC_ID_H264);//CODEC_ID_H264);
               NSLog(@"codec = %i",codec);
               if (!codec) {
                   fprintf(stderr, "codec not found\n");
                   exit(1);
               }

               c= avcodec_alloc_context();

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

               c ->me_range = 16;
               c ->max_qdiff = 4;
               c ->qmin = 10;
               c ->qmax = 51;
               c ->qcompress = 0.6f;

               /* open it */
               if (avcodec_open(c, codec) < 0) {
                   fprintf(stderr, "could not open codec\n");
                   exit(1);
               }


               /* alloc image and output buffer */
               outbuf_size = 100000;
               outbuf = malloc(outbuf_size);
               size = c->width * c->height;

               AVFrame* outpic = avcodec_alloc_frame();
               int nbytes = avpicture_get_size(PIX_FMT_YUV420P, c->width, c->height);

               //create buffer for the output image
               uint8_t* outbuffer = (uint8_t*)av_malloc(nbytes);

    #pragma mark -  

               fflush(stdout);

    <pre>//         int numBytes = avpicture_get_size(PIX_FMT_YUV420P, c->width, c->height);
    //          uint8_t *buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
    //          
    //          //UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"10%d", i]];
    //          CGImageRef newCgImage = [self imageFromSampleBuffer:sampleBuffer];//[image CGImage];
    //          
    //          CGDataProviderRef dataProvider = CGImageGetDataProvider(newCgImage);
    //          CFDataRef bitmapData = CGDataProviderCopyData(dataProvider);
    //          buffer = (uint8_t *)CFDataGetBytePtr(bitmapData);  
    //          
    //          avpicture_fill((AVPicture*)pFrame, buffer, PIX_FMT_RGB8, c->width, c->height);
               avpicture_fill((AVPicture*)outpic, outbuffer, PIX_FMT_YUV420P, c->width, c->height);

               struct SwsContext* fooContext = sws_getContext(c->width, c->height,
                                                              PIX_FMT_RGB8,
                                                              c->width, c->height,
                                                              PIX_FMT_YUV420P,
                                                              SWS_FAST_BILINEAR, NULL, NULL, NULL);

               //perform the conversion
               sws_scale(fooContext, pFrame->data, pFrame->linesize, 0, c->height, outpic->data, outpic->linesize);
               // Here is where I try to convert to YUV

               /* encode the image */

               out_size = avcodec_encode_video(c, outbuf, outbuf_size, outpic);
               printf("encoding frame (size=%5d)\n", out_size);
               printf("encoding frame %s\n", outbuf);


               //fwrite(outbuf, 1, out_size, f);

               //              free(buffer);
               //              buffer = NULL;      



               /* 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(outbuf);

               avcodec_close(c);
               av_free(c);
               av_free(pFrame);
               printf("\n");
    </pre>
  • Conversion from mp3 to aac/mp4 container (FFmpeg/c++)

    1er juillet 2013, par taansari

    I have made a small application to extract audio from an mp4 file, or simply convert an existing audio file to AAC/mp4 format (both raw AAC, or inside mp4 container). I have run this application with existing mp4 files as input, and it properly extracts audio, and encodes to mp4 (audio only:AAC), or even directly in AAC format (i.e. test.aac also works). But when I tried running it on mp3 files, output clip plays faster than it should be (a clip of 1:12 seconds plays back till 1:05 seconds only).

    Edit : I have made improvements in code - now, it no longer plays back faster, but is still only converted till 1:05 seconds, remaining clip is missing (this is about 89% conversion done, and remaining 11% remaining).

    Here is the code I have written to achieve this :

    ////////////////////////////////////////////////
       #include "stdafx.h"

    #include <iostream>
    #include <fstream>

    #include <string>
    #include <vector>
    #include <map>

    #include <deque>
    #include <queue>

    #include
    #include
    #include
    #include

    extern "C"
    {
    #include "libavcodec/avcodec.h"
    #include "libavformat/avformat.h"
    #include "libavdevice/avdevice.h"
    #include "libswscale/swscale.h"
    #include "libavutil/dict.h"
    #include "libavutil/error.h"
    #include "libavutil/opt.h"
    #include <libavutil></libavutil>fifo.h>
    #include <libavutil></libavutil>imgutils.h>
    #include <libavutil></libavutil>samplefmt.h>
    #include <libswresample></libswresample>swresample.h>
    }

    AVFormatContext*    fmt_ctx= NULL;
    int                    audio_stream_index = -1;
    AVCodecContext *    codec_ctx_audio = NULL;
    AVCodec*            codec_audio = NULL;
    AVFrame*            decoded_frame = NULL;
    uint8_t**            audio_dst_data = NULL;
    int                    got_frame = 0;
    int                    audiobufsize = 0;
    AVPacket            input_packet;
    int                    audio_dst_linesize = 0;
    int                    audio_dst_bufsize = 0;
    SwrContext *        swr = NULL;

    AVOutputFormat *    output_format = NULL ;
    AVFormatContext *    output_fmt_ctx= NULL;
    AVStream *            audio_st = NULL;
    AVCodec *            audio_codec = NULL;
    double                audio_pts = 0.0;
    AVFrame *            out_frame = avcodec_alloc_frame();

    int                    audio_input_frame_size = 0;

    uint8_t *            audio_data_buf = NULL;
    uint8_t *            audio_out = NULL;
    int                    audio_bit_rate;
    int                    audio_sample_rate;
    int                    audio_channels;

    int decode_packet();
    int open_audio_input(char* src_filename);
    int decode_frame();

    int open_encoder(char* output_filename);
    AVStream *add_audio_stream(AVFormatContext *oc, AVCodec **codec,
       enum AVCodecID codec_id);
    int open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st);
    void close_audio(AVFormatContext *oc, AVStream *st);
    void write_audio_frame(uint8_t ** audio_src_data, int audio_src_bufsize);

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

       // Retrieve stream information
       if(avformat_find_stream_info(fmt_ctx, NULL)&lt;0)
           return -1; // Couldn&#39;t find stream information

       // Dump information about file onto standard error
       av_dump_format(fmt_ctx, 0, src_filename, 0);

       // Find the first video stream
       for(i=0; inb_streams; i++)
       {
           if(fmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO)
           {
               audio_stream_index=i;
               break;
           }
       }
       if ( audio_stream_index != -1 )
       {
           // Get a pointer to the codec context for the audio stream
           codec_ctx_audio=fmt_ctx->streams[audio_stream_index]->codec;

           // Find the decoder for the video stream
           codec_audio=avcodec_find_decoder(codec_ctx_audio->codec_id);
           if(codec_audio==NULL) {
               fprintf(stderr, "Unsupported audio codec!\n");
               return -1; // Codec not found
           }

           // Open codec
           AVDictionary *codecDictOptions = NULL;
           if(avcodec_open2(codec_ctx_audio, codec_audio, &amp;codecDictOptions)&lt;0)
               return -1; // Could not open codec

           // Set up SWR context once you&#39;ve got codec information
           swr = swr_alloc();
           av_opt_set_int(swr, "in_channel_layout",  codec_ctx_audio->channel_layout, 0);
           av_opt_set_int(swr, "out_channel_layout", codec_ctx_audio->channel_layout,  0);
           av_opt_set_int(swr, "in_sample_rate",     codec_ctx_audio->sample_rate, 0);
           av_opt_set_int(swr, "out_sample_rate",    codec_ctx_audio->sample_rate, 0);
           av_opt_set_sample_fmt(swr, "in_sample_fmt",  codec_ctx_audio->sample_fmt, 0);
           av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16,  0);
           swr_init(swr);

           // Allocate audio frame
           if ( decoded_frame == NULL ) decoded_frame = avcodec_alloc_frame();
           int nb_planes = 0;
           AVStream* audio_stream = fmt_ctx->streams[audio_stream_index];
           nb_planes = av_sample_fmt_is_planar(codec_ctx_audio->sample_fmt) ? codec_ctx_audio->channels : 1;
           int tempSize =  sizeof(uint8_t *) * nb_planes;
           audio_dst_data = (uint8_t**)av_mallocz(tempSize);
           if (!audio_dst_data)
           {
               fprintf(stderr, "Could not allocate audio data buffers\n");
           }
           else
           {
               for ( int i = 0 ; i &lt; nb_planes ; i ++ )
               {
                   audio_dst_data[i] = NULL;
               }
           }
       }
    }


    int decode_frame()
    {
       int rv = 0;
       got_frame = 0;
       if ( fmt_ctx == NULL  )
       {
           return rv;
       }
       int ret = 0;
       audiobufsize = 0;
       rv = av_read_frame(fmt_ctx, &amp;input_packet);
       if ( rv &lt; 0 )
       {
           return rv;
       }
       rv = decode_packet();
       // Free the input_packet that was allocated by av_read_frame
       //av_free_packet(&amp;input_packet);
       return rv;
    }

    int decode_packet()
    {
       int rv = 0;
       int ret = 0;

       //audio stream?
       if(input_packet.stream_index == audio_stream_index)
       {
           /* decode audio frame */
           rv = avcodec_decode_audio4(codec_ctx_audio, decoded_frame, &amp;got_frame, &amp;input_packet);
           if (rv &lt; 0)
           {
               fprintf(stderr, "Error decoding audio frame\n");
               //return ret;
           }
           else
           {
               if (got_frame)
               {
                   if ( audio_dst_data[0] == NULL )
                   {
                        ret = av_samples_alloc(audio_dst_data, &amp;audio_dst_linesize, decoded_frame->channels,
                           decoded_frame->nb_samples, (AVSampleFormat)decoded_frame->format, 1);
                       if (ret &lt; 0)
                       {
                           fprintf(stderr, "Could not allocate audio buffer\n");
                           return AVERROR(ENOMEM);
                       }
                       /* TODO: extend return code of the av_samples_* functions so that this call is not needed */
                       audio_dst_bufsize = av_samples_get_buffer_size(NULL, audio_st->codec->channels,
                           decoded_frame->nb_samples, (AVSampleFormat)decoded_frame->format, 1);

                       //int16_t* outputBuffer = ...;
                       swr_convert( swr, audio_dst_data, out_frame->nb_samples, (const uint8_t**) decoded_frame->extended_data, decoded_frame->nb_samples );
                   }
                   /* copy audio data to destination buffer:
                   * this is required since rawaudio expects non aligned data */
                   //av_samples_copy(audio_dst_data, decoded_frame->data, 0, 0,
                   //    decoded_frame->nb_samples, decoded_frame->channels, (AVSampleFormat)decoded_frame->format);
               }
           }
       }
       return rv;
    }


    int open_encoder(char* output_filename )
    {
       int rv = 0;

       /* allocate the output media context */
       AVOutputFormat *opfmt = NULL;

       avformat_alloc_output_context2(&amp;output_fmt_ctx, opfmt, NULL, output_filename);
       if (!output_fmt_ctx) {
           printf("Could not deduce output format from file extension: using MPEG.\n");
           avformat_alloc_output_context2(&amp;output_fmt_ctx, NULL, "mpeg", output_filename);
       }
       if (!output_fmt_ctx) {
           rv = -1;
       }
       else
       {
           output_format = output_fmt_ctx->oformat;
       }

       /* Add the audio stream using the default format codecs
       * and initialize the codecs. */
       audio_st = NULL;

       if ( output_fmt_ctx )
       {
           if (output_format->audio_codec != AV_CODEC_ID_NONE)
           {
               audio_st = add_audio_stream(output_fmt_ctx, &amp;audio_codec, output_format->audio_codec);
           }

           /* Now that all the parameters are set, we can open the audio and
           * video codecs and allocate the necessary encode buffers. */
           if (audio_st)
           {
               rv = open_audio(output_fmt_ctx, audio_codec, audio_st);
               if ( rv &lt; 0 ) return rv;
           }

           av_dump_format(output_fmt_ctx, 0, output_filename, 1);
           /* open the output file, if needed */
           if (!(output_format->flags &amp; AVFMT_NOFILE))
           {
               if (avio_open(&amp;output_fmt_ctx->pb, output_filename, AVIO_FLAG_WRITE) &lt; 0) {
                   fprintf(stderr, "Could not open &#39;%s&#39;\n", output_filename);
                   rv = -1;
               }
               else
               {
                   /* Write the stream header, if any. */
                   if (avformat_write_header(output_fmt_ctx, NULL) &lt; 0)
                   {
                       fprintf(stderr, "Error occurred when opening output file\n");
                       rv = -1;
                   }
               }
           }
       }

       return rv;
    }

    AVStream *add_audio_stream(AVFormatContext *oc, AVCodec **codec,
       enum AVCodecID codec_id)
    {
       AVCodecContext *c;
       AVStream *st;

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

       st = avformat_new_stream(oc, *codec);
       if (!st) {
           fprintf(stderr, "Could not allocate stream\n");
           exit(1);
       }
       st->id = 1;

       c = st->codec;

       /* put sample parameters */
       c->sample_fmt  = AV_SAMPLE_FMT_S16;
       c->bit_rate    = audio_bit_rate;
       c->sample_rate = audio_sample_rate;
       c->channels    = audio_channels;

       // some formats want stream headers to be separate
       if (oc->oformat->flags &amp; AVFMT_GLOBALHEADER)
           c->flags |= CODEC_FLAG_GLOBAL_HEADER;

       return st;
    }

    int open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st)
    {
       int ret=0;
       AVCodecContext *c;

       st->duration = fmt_ctx->duration;
       c = st->codec;

       /* open it */
       ret = avcodec_open2(c, codec, NULL) ;
       if ( ret &lt; 0)
       {
           fprintf(stderr, "could not open codec\n");
           return -1;
           //exit(1);
       }

       if (c->codec->capabilities &amp; CODEC_CAP_VARIABLE_FRAME_SIZE)
           audio_input_frame_size = 10000;
       else
           audio_input_frame_size = c->frame_size;
       int tempSize = audio_input_frame_size *
           av_get_bytes_per_sample(c->sample_fmt) *
           c->channels;
       return ret;
    }

    void close_audio(AVFormatContext *oc, AVStream *st)
    {
       avcodec_close(st->codec);
    }

    void write_audio_frame(uint8_t ** audio_src_data, int audio_src_bufsize)
    {
       AVFormatContext *oc = output_fmt_ctx;
       AVStream *st = audio_st;
       if ( oc == NULL || st == NULL ) return;
       AVCodecContext *c;
       AVPacket pkt = { 0 }; // data and size must be 0;
       int got_packet;

       av_init_packet(&amp;pkt);
       c = st->codec;

       out_frame->nb_samples = audio_input_frame_size;
       int buf_size =         audio_src_bufsize *
           av_get_bytes_per_sample(c->sample_fmt) *
           c->channels;
       avcodec_fill_audio_frame(out_frame, c->channels, c->sample_fmt,
           (uint8_t *) *audio_src_data,
           buf_size, 1);
       avcodec_encode_audio2(c, &amp;pkt, out_frame, &amp;got_packet);
       if (!got_packet)
       {
       }
       else
       {
           if (pkt.pts != AV_NOPTS_VALUE)
               pkt.pts =  av_rescale_q(pkt.pts, st->codec->time_base, st->time_base);
           if (pkt.dts != AV_NOPTS_VALUE)
               pkt.dts = av_rescale_q(pkt.dts, st->codec->time_base, st->time_base);
           if ( c &amp;&amp; c->coded_frame &amp;&amp; c->coded_frame->key_frame)
               pkt.flags |= AV_PKT_FLAG_KEY;

            pkt.stream_index = st->index;
           pkt.flags |= AV_PKT_FLAG_KEY;
           /* Write the compressed frame to the media file. */
           if (av_interleaved_write_frame(oc, &amp;pkt) != 0)
           {
               fprintf(stderr, "Error while writing audio frame\n");
               exit(1);
           }
       }
       av_free_packet(&amp;pkt);
    }


    void write_delayed_frames(AVFormatContext *oc, AVStream *st)
    {
       AVCodecContext *c = st->codec;
       int got_output = 0;
       int ret = 0;
       AVPacket pkt;
       pkt.data = NULL;
       pkt.size = 0;
       av_init_packet(&amp;pkt);
       int i = 0;
       for (got_output = 1; got_output; i++)
       {
           ret = avcodec_encode_audio2(c, &amp;pkt, NULL, &amp;got_output);
           if (ret &lt; 0)
           {
               fprintf(stderr, "error encoding frame\n");
               exit(1);
           }
           static int64_t tempPts = 0;
           static int64_t tempDts = 0;
           /* If size is zero, it means the image was buffered. */
           if (got_output)
           {
               if (pkt.pts != AV_NOPTS_VALUE)
                   pkt.pts =  av_rescale_q(pkt.pts, st->codec->time_base, st->time_base);
               if (pkt.dts != AV_NOPTS_VALUE)
                   pkt.dts = av_rescale_q(pkt.dts, st->codec->time_base, st->time_base);
               if ( c &amp;&amp; c->coded_frame &amp;&amp; c->coded_frame->key_frame)
                   pkt.flags |= AV_PKT_FLAG_KEY;

               pkt.stream_index = st->index;
               /* Write the compressed frame to the media file. */
               ret = av_interleaved_write_frame(oc, &amp;pkt);
           }
           else
           {
               ret = 0;
           }
           av_free_packet(&amp;pkt);
       }
    }

    int main(int argc, char **argv)
    {
       /* register all formats and codecs */
       av_register_all();
       avcodec_register_all();
       avformat_network_init();
       avdevice_register_all();
       int i =0;
       char src_filename[90] = "mp3.mp3";
       char dst_filename[90] = "test.mp4";
       open_audio_input(src_filename);
       audio_bit_rate        = codec_ctx_audio->bit_rate;
       audio_sample_rate    = codec_ctx_audio->sample_rate;
       audio_channels        = codec_ctx_audio->channels;
       open_encoder( dst_filename );
       while(1)
       {
           int rv = decode_frame();
           if ( rv &lt; 0 )
           {
               break;
           }

           if (audio_st)
           {
               audio_pts = (double)audio_st->pts.val * audio_st->time_base.num /
                   audio_st->time_base.den;
           }
           else
           {
               audio_pts = 0.0;
           }
           if ( codec_ctx_audio )
           {
               if ( got_frame)
               {
                   write_audio_frame( audio_dst_data, audio_dst_bufsize );
               }
           }
           if ( audio_dst_data[0] )
           {
               av_freep(&amp;audio_dst_data[0]);
               audio_dst_data[0] = NULL;
           }
           av_free_packet(&amp;input_packet);
           printf("\naudio_pts: %.3f", audio_pts);
       }
       write_delayed_frames( output_fmt_ctx, audio_st );
       av_write_trailer(output_fmt_ctx);
       close_audio( output_fmt_ctx, audio_st);
       swr_free(&amp;swr);
       avcodec_free_frame(&amp;out_frame);
       return 0;
    }
    ///////////////////////////////////////////////
    </queue></deque></map></vector></string></fstream></iostream>

    I have been looking at this problem from many angles since about two days now, but cant seem to figure out what I'm doing wrong.

    Note also : the printf() statement I've inserted shows audio_pts up to 64.551 (that's about 1:05 seconds that also proves encoder is not going to full duration of input file : 1:12 secs).

    Can anyone please guide me what I may be doing wrong ?

    Thanks in advance for any guidance !

    p.s. when run through command line like : ffmpeg -i test.mp3 test.mp4, it converts the file just fine.

  • FFMPEG : cannot play MPEG4 video encoded from images. Duration and bitrate undefined

    17 juin 2013, par KaiK

    I've been trying to set a H264 video stream created from images, into an MPEG4 container. I've been able to get the video stream from images successfully. But when muxing it in the container, I must do something wrong because no player is able to reproduce it, despite ffplay - that plays the video until the end and after that, the image gets frozen until the eternity -.

    The ffplay cannot identify Duration neither bitrate, so I supose it might be an issue related with dts and pts, but I've searched about how to solve it with no success.

    Here's the ffplay output :

    ~$ ffplay testContainer.mp4
    ffplay version git-2012-01-31-c673671 Copyright (c) 2003-2012 the FFmpeg developers
     built on Feb  7 2012 20:32:12 with gcc 4.4.3
     configuration: --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-        libfaac --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-x11grab --enable-libvpx --enable-libmp3lame --enable-debug=3
     libavutil      51. 36.100 / 51. 36.100
     libavcodec     54.  0.102 / 54.  0.102
     libavformat    54.  0.100 / 54.  0.100
     libavdevice    53.  4.100 / 53.  4.100
     libavfilter     2. 60.100 /  2. 60.100
     libswscale      2.  1.100 /  2.  1.100
     libswresample   0.  6.100 /  0.  6.100
     libpostproc    52.  0.100 / 52.  0.100
    [h264 @ 0xa4849c0] max_analyze_duration 5000000 reached at 5000000
    [h264 @ 0xa4849c0] Estimating duration from bitrate, this may be inaccurate
    Input #0, h264, from &#39;testContainer.mp4&#39;:
     Duration: N/A, bitrate: N/A
       Stream #0:0: Video: h264 (High), yuv420p, 512x512, 25 fps, 25 tbr, 1200k tbn, 50 tbc
          2.74 A-V:  0.000 fd=   0 aq=    0KB vq=  160KB sq=    0B f=0/0   0/0

    Structure

    My code is C++ styled, so I've a class that handles all the encoding, and then a main that initilize it, passes some images in a bucle, and finally notify the end of the process as following :

    int main (int argc, const char * argv[])
    {

    MyVideoEncoder* videoEncoder = new MyVideoEncoder(512, 512, 512, 512, "output/testContainer.mp4", 25, 20);
    if(!videoEncoder->initWithCodec(MyVideoEncoder::H264))
    {
       std::cout &lt;&lt; "something really bad happened. Exit!!" &lt;&lt; std::endl;
       exit(-1);
    }

    /* encode 1 second of video */
    for(int i=0;i&lt;228;i++) {

       std::stringstream filepath;
       filepath &lt;&lt; "input2/image" &lt;&lt; i &lt;&lt; ".jpg";

       videoEncoder->encodeFrameFromJPG(const_cast(filepath.str().c_str()));

    }

    videoEncoder->endEncoding();

    }

    Hints

    I've seen a lot of examples about decoding of a video and encoding into another, but no working example of muxing a video from the scratch, so I'm not sure how to proceed with the pts and dts packet values. That's the reason why I suspect the issue must be in the following method :

    bool MyVideoEncoder::encodeImageAsFrame(){
       bool res = false;


       pTempFrame->pts = frameCount * frameRate * 90; //90Hz by the standard for PTS-values
       frameCount++;

       /* encode the image */
       out_size = avcodec_encode_video(pVideoStream->codec, outbuf, outbuf_size, pTempFrame);


       if (out_size > 0) {
           AVPacket pkt;
           av_init_packet(&amp;pkt);
           pkt.pts = pkt.dts = 0;

           if (pVideoStream->codec->coded_frame->pts != AV_NOPTS_VALUE) {
               pkt.pts = av_rescale_q(pVideoStream->codec->coded_frame->pts,
                       pVideoStream->codec->time_base, pVideoStream->time_base);
               pkt.dts = pTempFrame->pts;

           }
           if (pVideoStream->codec->coded_frame->key_frame) {
               pkt.flags |= AV_PKT_FLAG_KEY;
           }
           pkt.stream_index = pVideoStream->index;
           pkt.data = outbuf;
           pkt.size = out_size;

           res = (av_interleaved_write_frame(pFormatContext, &amp;pkt) == 0);
       }


       return res;
    }

    Any help or insight would be appreciated. Thanks in advance !!

    P.S. The rest of the code, where config is done, is the following :

    // MyVideoEncoder.cpp

    #include "MyVideoEncoder.h"
    #include "Image.hpp"
    #include <cstring>
    #include <sstream>
    #include

    #define MAX_AUDIO_PACKET_SIZE (128 * 1024)



    MyVideoEncoder::MyVideoEncoder(int inwidth, int inheight,
           int outwidth, int outheight, char* fileOutput, int framerate,
           int compFactor) {
       inWidth = inwidth;
       inHeight = inheight;
       outWidth = outwidth;
       outHeight = outheight;
       pathToMovie = fileOutput;
       frameRate = framerate;
       compressionFactor = compFactor;
       frameCount = 0;

    }

    MyVideoEncoder::~MyVideoEncoder() {

    }

    bool MyVideoEncoder::initWithCodec(
           MyVideoEncoder::encoderType type) {
       if (!initializeEncoder(type))
           return false;

       if (!configureFrames())
           return false;

       return true;

    }

    bool MyVideoEncoder::encodeFrameFromJPG(char* filepath) {

       setJPEGImage(filepath);
       return encodeImageAsFrame();
    }



    bool MyVideoEncoder::encodeDelayedFrames(){
       bool res = false;

       while(out_size > 0)
       {
           pTempFrame->pts = frameCount * frameRate * 90; //90Hz by the standard for PTS-values
           frameCount++;

           out_size = avcodec_encode_video(pVideoStream->codec, outbuf, outbuf_size, NULL);

           if (out_size > 0)
           {
               AVPacket pkt;
               av_init_packet(&amp;pkt);
               pkt.pts = pkt.dts = 0;

               if (pVideoStream->codec->coded_frame->pts != AV_NOPTS_VALUE) {
                   pkt.pts = av_rescale_q(pVideoStream->codec->coded_frame->pts,
                           pVideoStream->codec->time_base, pVideoStream->time_base);
                   pkt.dts = pTempFrame->pts;
               }
               if (pVideoStream->codec->coded_frame->key_frame) {
                   pkt.flags |= AV_PKT_FLAG_KEY;
               }
               pkt.stream_index = pVideoStream->index;
               pkt.data = outbuf;
               pkt.size = out_size;


               res = (av_interleaved_write_frame(pFormatContext, &amp;pkt) == 0);
           }

       }

       return res;
    }






    void MyVideoEncoder::endEncoding() {
       encodeDelayedFrames();
       closeEncoder();
    }

    bool MyVideoEncoder::setJPEGImage(char* imgFilename) {
       Image* rgbImage = new Image();
       rgbImage->read_jpeg_image(imgFilename);

       bool ret = setImageFromRGBArray(rgbImage->get_data());

       delete rgbImage;

       return ret;
    }

    bool MyVideoEncoder::setImageFromRGBArray(unsigned char* data) {

       memcpy(pFrameRGB->data[0], data, 3 * inWidth * inHeight);

       int ret = sws_scale(img_convert_ctx, pFrameRGB->data, pFrameRGB->linesize,
               0, inHeight, pTempFrame->data, pTempFrame->linesize);

       pFrameRGB->pts++;
       if (ret)
           return true;
       else
           return false;
    }

    bool MyVideoEncoder::initializeEncoder(encoderType type) {

       av_register_all();

       pTempFrame = avcodec_alloc_frame();
       pTempFrame->pts = 0;
       pOutFormat = NULL;
       pFormatContext = NULL;
       pVideoStream = NULL;
       pAudioStream = NULL;
       bool res = false;

       // Create format
       switch (type) {
           case MyVideoEncoder::H264:
               pOutFormat = av_guess_format("h264", NULL, NULL);
               break;
           case MyVideoEncoder::MPEG1:
               pOutFormat = av_guess_format("mpeg", NULL, NULL);
               break;
           default:
               pOutFormat = av_guess_format(NULL, pathToMovie.c_str(), NULL);
               break;
       }

       if (!pOutFormat) {
           pOutFormat = av_guess_format(NULL, pathToMovie.c_str(), NULL);
           if (!pOutFormat) {
               std::cout &lt;&lt; "output format not found" &lt;&lt; std::endl;
               return false;
           }
       }


       // allocate context
       pFormatContext = avformat_alloc_context();
       if(!pFormatContext)
       {
           std::cout &lt;&lt; "cannot alloc format context" &lt;&lt; std::endl;
           return false;
       }

       pFormatContext->oformat = pOutFormat;

       memcpy(pFormatContext->filename, pathToMovie.c_str(), min( (const int) pathToMovie.length(), (const int)sizeof(pFormatContext->filename)));


       //Add video and audio streams
       pVideoStream = AddVideoStream(pFormatContext,
               pOutFormat->video_codec);

       // Set the output parameters
       av_dump_format(pFormatContext, 0, pathToMovie.c_str(), 1);

       // Open Video stream
       if (pVideoStream) {
           res = openVideo(pFormatContext, pVideoStream);
       }


       if (res &amp;&amp; !(pOutFormat->flags &amp; AVFMT_NOFILE)) {
           if (avio_open(&amp;pFormatContext->pb, pathToMovie.c_str(), AVIO_FLAG_WRITE) &lt; 0) {
               res = false;
               std::cout &lt;&lt; "Cannot open output file" &lt;&lt; std::endl;
           }
       }

       if (res) {
           avformat_write_header(pFormatContext,NULL);
       }
       else{
           freeMemory();
           std::cout &lt;&lt; "Cannot init encoder" &lt;&lt; std::endl;
       }


       return res;

    }



    AVStream *MyVideoEncoder::AddVideoStream(AVFormatContext *pContext, CodecID codec_id)
    {
     AVCodecContext *pCodecCxt = NULL;
     AVStream *st    = NULL;

     st = avformat_new_stream(pContext, NULL);
     if (!st)
     {
         std::cout &lt;&lt; "Cannot add new video stream" &lt;&lt; std::endl;
         return NULL;
     }
     st->id = 0;

     pCodecCxt = st->codec;
     pCodecCxt->codec_id = (CodecID)codec_id;
     pCodecCxt->codec_type = AVMEDIA_TYPE_VIDEO;
     pCodecCxt->frame_number = 0;


     // Put sample parameters.
     pCodecCxt->bit_rate = outWidth * outHeight * 3 * frameRate/ compressionFactor;

     pCodecCxt->width  = outWidth;
     pCodecCxt->height = outHeight;

     /* frames per second */
     pCodecCxt->time_base= (AVRational){1,frameRate};

     /* pixel format must be YUV */
     pCodecCxt->pix_fmt = PIX_FMT_YUV420P;


     if (pCodecCxt->codec_id == CODEC_ID_H264)
     {
         av_opt_set(pCodecCxt->priv_data, "preset", "slow", 0);
         av_opt_set(pCodecCxt->priv_data, "vprofile", "baseline", 0);
         pCodecCxt->max_b_frames = 16;
     }
     if (pCodecCxt->codec_id == CODEC_ID_MPEG1VIDEO)
     {
         pCodecCxt->mb_decision = 1;
     }

     if(pContext->oformat->flags &amp; AVFMT_GLOBALHEADER)
     {
         pCodecCxt->flags |= CODEC_FLAG_GLOBAL_HEADER;
     }

     pCodecCxt->coder_type = 1;  // coder = 1
     pCodecCxt->flags|=CODEC_FLAG_LOOP_FILTER;   // flags=+loop
     pCodecCxt->me_range = 16;   // me_range=16
     pCodecCxt->gop_size = 50;  // g=250
     pCodecCxt->keyint_min = 25; // keyint_min=25


     return st;
    }


    bool MyVideoEncoder::openVideo(AVFormatContext *oc, AVStream *pStream)
    {
       AVCodec *pCodec;
       AVCodecContext *pContext;

       pContext = pStream->codec;

       // Find the video encoder.
       pCodec = avcodec_find_encoder(pContext->codec_id);
       if (!pCodec)
       {
           std::cout &lt;&lt; "Cannot found video codec" &lt;&lt; std::endl;
           return false;
       }

       // Open the codec.
       if (avcodec_open2(pContext, pCodec, NULL) &lt; 0)
       {
           std::cout &lt;&lt; "Cannot open video codec" &lt;&lt; std::endl;
           return false;
       }


       return true;
    }



    bool MyVideoEncoder::configureFrames() {

       /* alloc image and output buffer */
       outbuf_size = outWidth*outHeight*3;
       outbuf = (uint8_t*) malloc(outbuf_size);

       av_image_alloc(pTempFrame->data, pTempFrame->linesize, pVideoStream->codec->width,
               pVideoStream->codec->height, pVideoStream->codec->pix_fmt, 1);

       //Alloc RGB temp frame
       pFrameRGB = avcodec_alloc_frame();
       if (pFrameRGB == NULL)
           return false;
       avpicture_alloc((AVPicture *) pFrameRGB, PIX_FMT_RGB24, inWidth, inHeight);

       pFrameRGB->pts = 0;

       //Set SWS context to convert from RGB images to YUV images
       if (img_convert_ctx == NULL) {
           img_convert_ctx = sws_getContext(inWidth, inHeight, PIX_FMT_RGB24,
                   outWidth, outHeight, pVideoStream->codec->pix_fmt, /*SWS_BICUBIC*/
                   SWS_FAST_BILINEAR, NULL, NULL, NULL);
           if (img_convert_ctx == NULL) {
               fprintf(stderr, "Cannot initialize the conversion context!\n");
               return false;
           }
       }

       return true;

    }

    void MyVideoEncoder::closeEncoder() {
       av_write_frame(pFormatContext, NULL);
       av_write_trailer(pFormatContext);
       freeMemory();
    }


    void MyVideoEncoder::freeMemory()
    {
     bool res = true;

     if (pFormatContext)
     {
       // close video stream
       if (pVideoStream)
       {
         closeVideo(pFormatContext, pVideoStream);
       }

       // Free the streams.
       for(size_t i = 0; i &lt; pFormatContext->nb_streams; i++)
       {
         av_freep(&amp;pFormatContext->streams[i]->codec);
         av_freep(&amp;pFormatContext->streams[i]);
       }

       if (!(pFormatContext->flags &amp; AVFMT_NOFILE) &amp;&amp; pFormatContext->pb)
       {
         avio_close(pFormatContext->pb);
       }

       // Free the stream.
       av_free(pFormatContext);
       pFormatContext = NULL;
     }
    }

    void MyVideoEncoder::closeVideo(AVFormatContext *pContext, AVStream *pStream)
    {
     avcodec_close(pStream->codec);
     if (pTempFrame)
     {
       if (pTempFrame->data)
       {
         av_free(pTempFrame->data[0]);
         pTempFrame->data[0] = NULL;
       }
       av_free(pTempFrame);
       pTempFrame = NULL;
     }

     if (pFrameRGB)
     {
       if (pFrameRGB->data)
       {
         av_free(pFrameRGB->data[0]);
         pFrameRGB->data[0] = NULL;
       }
       av_free(pFrameRGB);
       pFrameRGB = NULL;
     }

    }
    </sstream></cstring>