修改output-example,将H.264,AAC帧保存到mp4容器中

URL:http://blog.csdn.net/ajaxhe/article/details/7603333


2012/11/26更新
根据网友反馈,原来的下载链接编译有问题,原因是头文件和库文件的路径没有设置好,已经修改。
下载链接:
http://download.csdn.net/detail/ajaxhe/4810984

2012/10/25更新

在add_video_stream()中添加

c->cqp  = 32;//很关键的参数,不设置的话会导致马赛克,而且它的大小决定着编码的速度和编码后帧的大小


ffmpeg版本:0.8

开发环境:VS2008

遇到的错误:
1. Specified sample_fmt is not supported.
[aac @ 000E6E60] Specified sample_fmt is not supported.
could not open audio codec

设置AVCodecContext *c的sample_fmt位,如:
c ->sample_fmt AV_SAMPLE_FMT_S16 ;

2. Too many bits per frame requested
[aac @ 004D6E60] Too many bits per frame requested
could not open audio codec

设置AVCodecContext *c的bit_rate位,bit_rate = sample_rate * channels * 2,其中的“2”指的是sample_fmt为16位的缘故,相应的设置如下:
c ->sample_rate = 8000;
c-> channels = 1;
c-> bit_rate = 16000;

遇到的warning:
1. [mp4 @ 006C3580] Codec for stream 1 does not use global headers but container format requires global headers
因为保存的文件时mp4格式,需要设置c->flags的CODEC_FLAG_GLOBAL_HEADER,如下:
// some formats want stream headers to be separate
if(! strcmp( oc-> oformat-> name"mp4" ) || !strcmp (oc ->oformat ->name "mov" ) || !strcmp (oc ->oformat ->name "3gp" ))
      c-> flags |= CODEC_FLAG_GLOBAL_HEADER ;

其他情况下可能遇到的错误:
使用这个程序,主要是为了做一个测试,之前是为了将一个RTP音视频H.264+AAC流保存为mp4文件,在保存AAC文件时,执行
if ( av_write_frameoc, & pkt) != 0) {
        fprintf( stderr"Error while writing audio frame\n" );
        exit(1);
    }
总是出错,原因就是因为RTP过来的AAC帧包括的七个字节的AAC Header,而这里写av_write_frame()中pkt.data必须为AAC裸数据(不包括AAC Header),这个需要特别注意.
根据网友反馈,原来的下载链接编译有问题,是头文件和库文件的路径问题,已经修改。
下载链接:http://download.csdn.net/detail/ajaxhe/4810984

源文件:
[cpp]  view plain copy
  1. /* 
  2.  * Libavformat API example: Output a media file in any supported 
  3.  * libavformat format. The default codecs are used. 
  4.  * 
  5.  * Copyright (c) 2003 Fabrice Bellard 
  6.  * 
  7.  * Permission is hereby granted, free of charge, to any person obtaining a copy 
  8.  * of this software and associated documentation files (the "Software"), to deal 
  9.  * in the Software without restriction, including without limitation the rights 
  10.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
  11.  * copies of the Software, and to permit persons to whom the Software is 
  12.  * furnished to do so, subject to the following conditions: 
  13.  * 
  14.  * The above copyright notice and this permission notice shall be included in 
  15.  * all copies or substantial portions of the Software. 
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
  20.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
  23.  * THE SOFTWARE. 
  24.  */  
  25. #include <stdlib.h>  
  26. #include <stdio.h>  
  27. #include <string.h>  
  28. #include <math.h>  
  29. //#include <time.h>  
  30. #include <Windows.h>  
  31.   
  32. #ifndef M_PI  
  33. #define M_PI 3.14159265358979323846  
  34. #endif  
  35.   
  36. extern "C" {  
  37. #include <libavformat/avformat.h>  
  38. #include <libswscale/swscale.h>  
  39. };  
  40.   
  41. #undef exit  
  42.   
  43. /* 5 seconds stream duration */  
  44. #define STREAM_DURATION    5.0  
  45. #define STREAM_FRAME_RATE 25 /* 25 images/s */  
  46. #define STREAM_NB_FRAMES   ((int )(STREAM_DURATION * STREAM_FRAME_RATE ))  
  47. #define STREAM_PIX_FMT PIX_FMT_YUV420P /* default pix_fmt */  
  48.   
  49. static int sws_flags = SWS_BICUBIC ;  
  50.   
  51. /**************************************************************/  
  52. /* audio output */  
  53.   
  54. float t , tincr , tincr2 ;  
  55. int16_t *samples ;  
  56. uint8_t *audio_outbuf ;  
  57. int audio_outbuf_size ;  
  58. int audio_input_frame_size ;  
  59.   
  60. /* 
  61.  * add an audio output stream 
  62.  */  
  63. static AVStream *add_audio_stream (AVFormatContext *oc , int codec_id )  
  64. {  
  65.     AVCodecContext * c;  
  66.     AVStream * st;  
  67.   
  68.     st = av_new_stream (oc , 1);  
  69.     if (! st) {  
  70.         fprintf( stderr, "Could not alloc stream\n" );  
  71.         exit(1);  
  72.     }  
  73.   
  74.     c = st-> codec;  
  75.     c-> codec_id = ( CodecID) codec_id;  
  76.        c-> codec_type = AVMEDIA_TYPE_AUDIO ;  
  77.   
  78.     /* put sample parameters */  
  79.        c-> sample_fmt = AV_SAMPLE_FMT_S16 ;  
  80.     c-> sample_rate = 8000;  
  81.     c-> channels = 1;  
  82.        c-> bit_rate = 16000;  
  83.   
  84.        // some formats want stream headers to be separate  
  85.        if(! strcmp( oc-> oformat-> name, "mp4" ) || !strcmp (oc ->oformat ->name , "mov" ) || !strcmp (oc ->oformat ->name , "3gp" ))  
  86.              c-> flags |= CODEC_FLAG_GLOBAL_HEADER ;  
  87.   
  88.     return st;  
  89. }  
  90.   
  91. static void open_audio (AVFormatContext *oc , AVStream *st )  
  92. {  
  93.     AVCodecContext * c;  
  94.     AVCodec * codec;  
  95.   
  96.     c = st-> codec;  
  97.   
  98.     /* find the audio encoder */  
  99.     codec = avcodec_find_encoder (c ->codec_id );  
  100.     if (! codec) {  
  101.         fprintf( stderr, "codec not found\n" );  
  102.         exit(1);  
  103.     }  
  104.   
  105.     /* open it */  
  106.     if ( avcodec_open( c, codec) < 0) {  
  107.         fprintf( stderr, "could not open audio codec\n" );  
  108.         exit(1);  
  109.     }  
  110.   
  111.     /* init signal generator */  
  112.     t = 0;  
  113.     tincr = 2 * M_PI * 110.0 / c-> sample_rate ;  
  114.     /* increment frequency by 110 Hz per second */  
  115.     tincr2 = 2 * M_PI * 110.0 / c-> sample_rate / c ->sample_rate ;  
  116.   
  117.     audio_outbuf_size = 10000;  
  118.     audio_outbuf = ( uint8_t *) av_malloc( audio_outbuf_size );  
  119.   
  120.     /* ugly hack for PCM codecs (will be removed ASAP with new PCM 
  121.        support to compute the input frame size in samples */  
  122.     if ( c-> frame_size <= 1) {  
  123.         audio_input_frame_size = audio_outbuf_size / c ->channels ;  
  124.         switch( st-> codec-> codec_id) {  
  125.         case CODEC_ID_PCM_S16LE :  
  126.         case CODEC_ID_PCM_S16BE :  
  127.         case CODEC_ID_PCM_U16LE :  
  128.         case CODEC_ID_PCM_U16BE :  
  129.             audio_input_frame_size >>= 1;  
  130.             break;  
  131.         default:  
  132.             break;  
  133.         }  
  134.     } else {  
  135.         audio_input_frame_size = c ->frame_size ;  
  136.     }  
  137.     samples = ( int16_t *) av_malloc( audio_input_frame_size * 2 * c ->channels );  
  138. }  
  139.   
  140. /* prepare a 16 bit dummy audio frame of 'frame_size' samples and 
  141.    'nb_channels' channels */  
  142. static void get_audio_frame (int16_t *samples , int frame_size , int nb_channels )  
  143. {  
  144.     int j, i, v;  
  145.     int16_t * q;  
  146.   
  147.     q = samples;  
  148.     for( j=0; j< frame_size; j++) {  
  149.         v = ( int)( sin( t) * 10000);  
  150.         for( i = 0; i < nb_channels ; i ++)  
  151.             * q++ = v;  
  152.         t += tincr;  
  153.         tincr += tincr2;  
  154.     }  
  155. }  
  156.   
  157. static void write_audio_frame (AVFormatContext *oc , AVStream *st )  
  158. {  
  159.     AVCodecContext * c;  
  160.     AVPacket pkt;  
  161.     av_init_packet(& pkt);  
  162.   
  163.     c = st-> codec;  
  164.   
  165.     get_audio_frame( samples, audio_input_frame_size , c ->channels );  
  166.   
  167.     pkt. size = avcodec_encode_audio (c , audio_outbuf , audio_outbuf_size , samples );  
  168.        printf( "[audio] AAC frame len=%d\n" , pkt .size );  
  169.   
  170.     //pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);  
  171.   
  172.        //pkt.flags |= AV_PKT_FLAG_KEY;  
  173.     pkt. stream_index= st-> index;  
  174.     pkt. data= audio_outbuf ;  
  175.   
  176.     /* write the compressed frame in the media file */  
  177.     if ( av_write_frame( oc, & pkt) != 0) {  
  178.         fprintf( stderr, "Error while writing audio frame\n" );  
  179.         exit(1);  
  180.     }  
  181. }  
  182.   
  183. static void close_audio (AVFormatContext *oc , AVStream *st )  
  184. {  
  185.     avcodec_close( st-> codec);  
  186.   
  187.     av_free( samples);  
  188.     av_free( audio_outbuf );  
  189. }  
  190.   
  191. /**************************************************************/  
  192. /* video output */  
  193.   
  194. AVFrame *picture , *tmp_picture ;  
  195. uint8_t *video_outbuf ;  
  196. int frame_count , video_outbuf_size ;  
  197.   
  198. /* add a video output stream */  
  199. static AVStream *add_video_stream (AVFormatContext *oc , int codec_id )  
  200. {  
  201.     AVCodecContext * c;  
  202.     AVStream * st;  
  203.   
  204.     st = av_new_stream (oc , 0);  
  205.     if (! st) {  
  206.         fprintf( stderr, "Could not alloc stream\n" );  
  207.         exit(1);  
  208.     }  
  209.   
  210.     c = st-> codec;  
  211.     c-> codec_id = ( CodecID) codec_id;  
  212.        c-> codec_type = AVMEDIA_TYPE_VIDEO ;  
  213.   
  214.        c-> me_range = 16;   
  215.        c-> max_qdiff = 4;   
  216.        c-> qmin = 10;   
  217.        c-> qmax = 51;   
  218.        c-> qcompress = 0.6;    
  219.   
  220.     /* put sample parameters */  
  221.     c-> bit_rate = 400000;  
  222.     /* resolution must be a multiple of two */  
  223.     c-> width = 352;  
  224.     c-> height = 288;  
  225.     /* time base: this is the fundamental unit of time (in seconds) in terms 
  226.        of which frame timestamps are represented. for fixed-fps content, 
  227.        timebase should be 1/framerate and timestamp increments should be 
  228.        identically 1. */  
  229.     c-> time_base. den = STREAM_FRAME_RATE ;  
  230.     c-> time_base. num = 1;  
  231.     c-> gop_size = 12; /* emit one intra frame every twelve frames at most */  
  232.     c-> pix_fmt = STREAM_PIX_FMT ;  
  233.     if ( c-> codec_id == CODEC_ID_MPEG2VIDEO ) {  
  234.         /* just for testing, we also add B frames */  
  235.         c-> max_b_frames = 2;  
  236.     }  
  237.     if ( c-> codec_id == CODEC_ID_MPEG1VIDEO ){  
  238.         /* Needed to avoid using macroblocks in which some coeffs overflow. 
  239.            This does not happen with normal video, it just happens here as 
  240.            the motion of the chroma plane does not match the luma plane. */  
  241.         c-> mb_decision =2;  
  242.     }  
  243.   
  244.     // some formats want stream headers to be separate  
  245.     if(! strcmp( oc-> oformat-> name, "mp4" ) || !strcmp (oc ->oformat ->name , "mov" ) || !strcmp (oc ->oformat ->name , "3gp" ))  
  246.         c-> flags |= CODEC_FLAG_GLOBAL_HEADER ;  
  247.   
  248.     return st;  
  249. }  
  250.   
  251. static AVFrame *alloc_picture (int pix_fmt , int width , int height )  
  252. {  
  253.     AVFrame * picture;  
  254.     uint8_t * picture_buf ;  
  255.     int size;  
  256.   
  257.     picture = avcodec_alloc_frame ();  
  258.     if (! picture)  
  259.         return NULL;  
  260.     size = avpicture_get_size ((PixelFormat )pix_fmt , width , height );  
  261.     picture_buf = ( uint8_t *) av_malloc( size);  
  262.     if (! picture_buf ) {  
  263.         av_free( picture);  
  264.         return NULL;  
  265.     }  
  266.     avpicture_fill(( AVPicture *) picture, picture_buf ,  
  267.                    ( PixelFormat )pix_fmt , width , height );  
  268.     return picture;  
  269. }  
  270.   
  271. static void open_video (AVFormatContext *oc , AVStream *st )  
  272. {  
  273.     AVCodec * codec;  
  274.     AVCodecContext * c;  
  275.   
  276.     c = st-> codec;  
  277.   
  278.     /* find the video encoder */  
  279.     codec = avcodec_find_encoder (c ->codec_id );  
  280.     if (! codec) {  
  281.         fprintf( stderr, "codec not found\n" );  
  282.         exit(1);  
  283.     }  
  284.   
  285.     /* open the codec */  
  286.     if ( avcodec_open( c, codec) < 0) {  
  287.         fprintf( stderr, "could not open video codec\n" );  
  288.         exit(1);  
  289.     }  
  290.   
  291.     video_outbuf = NULL;  
  292.     if (!( oc-> oformat-> flags & AVFMT_RAWPICTURE )) {  
  293.         /* allocate output buffer */  
  294.         /* XXX: API change will be done */  
  295.         /* buffers passed into lav* can be allocated any way you prefer, 
  296.            as long as they're aligned enough for the architecture, and 
  297.            they're freed appropriately (such as using av_free for buffers 
  298.            allocated with av_malloc) */  
  299.         video_outbuf_size = 200000;  
  300.         video_outbuf = (uint8_t *)av_malloc (video_outbuf_size );  
  301.     }  
  302.   
  303.     /* allocate the encoded raw picture */  
  304.     picture = alloc_picture (c ->pix_fmt , c ->width , c ->height );  
  305.     if (! picture) {  
  306.         fprintf( stderr, "Could not allocate picture\n" );  
  307.         exit(1);  
  308.     }  
  309.   
  310.     /* if the output format is not YUV420P, then a temporary YUV420P 
  311.        picture is needed too. It is then converted to the required 
  312.        output format */  
  313.     tmp_picture = NULL;  
  314.     if ( c-> pix_fmt != PIX_FMT_YUV420P ) {  
  315.         tmp_picture = alloc_picture (PIX_FMT_YUV420P , c ->width , c ->height );  
  316.         if (! tmp_picture ) {  
  317.             fprintf( stderr, "Could not allocate temporary picture\n" );  
  318.             exit(1);  
  319.         }  
  320.     }  
  321. }  
  322.   
  323. /* prepare a dummy image */  
  324. static void fill_yuv_image (AVFrame *pict , int frame_index , int width , int height )  
  325. {  
  326.     int x, y, i;  
  327.   
  328.     i = frame_index;  
  329.   
  330.     /* Y */  
  331.     for( y=0; y< height; y++) {  
  332.         for( x=0; x< width; x++) {  
  333.             pict-> data[0][ y * pict-> linesize[0] + x] = x + y + i * 3;  
  334.         }  
  335.     }  
  336.   
  337.     /* Cb and Cr */  
  338.     for( y=0; y< height/2; y++) {  
  339.         for( x=0; x< width/2; x++) {  
  340.             pict-> data[1][ y * pict-> linesize[1] + x] = 128 + y + i * 2;  
  341.             pict-> data[2][ y * pict-> linesize[2] + x] = 64 + x + i * 5;  
  342.         }  
  343.     }  
  344. }  
  345.   
  346. static void write_video_frame (AVFormatContext *oc , AVStream *st )  
  347. {  
  348.     int out_size, ret;  
  349.     AVCodecContext * c;  
  350.     static struct SwsContext * img_convert_ctx ;  
  351.   
  352.     c = st-> codec;  
  353.   
  354.     if ( frame_count >= STREAM_NB_FRAMES ) {  
  355.         /* no more frame to compress. The codec has a latency of a few 
  356.            frames if using B frames, so we get the last frames by 
  357.            passing the same picture again */  
  358.     } else {  
  359.         if ( c-> pix_fmt != PIX_FMT_YUV420P ) {  
  360.             /* as we only generate a YUV420P picture, we must convert it 
  361.                to the codec pixel format if needed */  
  362.             if ( img_convert_ctx == NULL ) {  
  363.                 img_convert_ctx = sws_getContext (c ->width , c ->height , PIX_FMT_YUV420P ,  
  364.                                                  c-> width, c-> height, c ->pix_fmt ,  
  365.                                                  sws_flags, NULL, NULL, NULL );  
  366.                 if ( img_convert_ctx == NULL ) {  
  367.                     fprintf( stderr, "Cannot initialize the conversion context\n");  
  368.                     exit(1);  
  369.                 }  
  370.             }  
  371.             fill_yuv_image (tmp_picture , frame_count , c ->width , c ->height );  
  372.             sws_scale( img_convert_ctx , tmp_picture ->data , tmp_picture ->linesize ,  
  373.                       0, c-> height, picture-> data, picture-> linesize);  
  374.         } else {  
  375.             fill_yuv_image (picture , frame_count , c ->width , c ->height );  
  376.         }  
  377.     }  
  378.   
  379.   
  380.     if ( oc-> oformat-> flags & AVFMT_RAWPICTURE ) {  
  381.         /* raw video case. The API will change slightly in the near 
  382.            futur for that */  
  383.         AVPacket pkt;  
  384.         av_init_packet (&pkt );  
  385.   
  386.              pkt. flags |= AV_PKT_FLAG_KEY ;   
  387.         pkt. stream_index = st ->index ;  
  388.         pkt. data= ( uint8_t *) picture;  
  389.         pkt. size= sizeof( AVPicture);  
  390.   
  391.         ret = av_write_frame (oc , &pkt );  
  392.     }  
  393.        else {  
  394.         /* encode the image */  
  395.              //printf("frame count %d\n", frame_count);  
  396.         out_size = avcodec_encode_video (c , video_outbuf , video_outbuf_size , picture );  
  397.         /* if zero size, it means the image was buffered */  
  398.         if ( out_size > 0) {  
  399.             AVPacket pkt;  
  400.             av_init_packet (&pkt );  
  401.                        
  402.                    /* 
  403.                   // 很关键 
  404.                   if (c->codec_id == CODEC_ID_H264) 
  405.                         pkt.pts = av_rescale_q(frame_count, c->time_base, st->time_base); 
  406.                   else 
  407.                         pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base); 
  408.                   */  
  409.                     
  410.                    if( c-> coded_frame ->key_frame ){  
  411.                          pkt. flags |= AV_PKT_FLAG_KEY ;  
  412.                   }  
  413.                  
  414.             pkt. stream_index = st ->index ;  
  415.             pkt. data= video_outbuf ;  
  416.                    // not the video_outbuf_size, note!  
  417.             pkt. size= out_size;  
  418.   
  419.             /* write the compressed frame in the media file */  
  420.             ret = av_write_frame (oc , &pkt );  
  421.   
  422.                    frame_count ++;  
  423.         }  
  424.              else {  
  425.             ret = 0;  
  426.         }  
  427.     }  
  428.     if ( ret != 0) {  
  429.         fprintf( stderr, "Error while writing video frame\n" );  
  430.         exit(1);  
  431.     }  
  432. }  
  433.   
  434. static void close_video (AVFormatContext *oc , AVStream *st )  
  435. {  
  436.     avcodec_close( st-> codec);  
  437.     av_free( picture-> data[0]);  
  438.     av_free( picture);  
  439.     if ( tmp_picture) {  
  440.         av_free( tmp_picture ->data [0]);  
  441.         av_free( tmp_picture );  
  442.     }  
  443.     av_free( video_outbuf );  
  444. }  
  445.   
  446. /**************************************************************/  
  447. /* media file output */  
  448.   
  449. int main (int argc , char **argv )  
  450. {  
  451.     const char filename[] = "output.mp4" ;  
  452.     AVOutputFormat * fmt;  
  453.     AVFormatContext * oc;  
  454.        AVStream * audio_st;  
  455.        double audio_pts;  
  456.        AVStream * video_st;  
  457.        double video_pts;  
  458.     int i;  
  459.   
  460.     /* initialize libavcodec, and register all codecs and formats */  
  461.     av_register_all();  
  462.   
  463. /* 
  464.     if (argc != 2) { 
  465.         printf("usage: %s output_file\n" 
  466.                "API example program to output a media file with libavformat.\n" 
  467.                "The output format is automatically guessed according to the file extension.\n" 
  468.                "Raw images can also be output by using '%%d' in the filename\n" 
  469.                "\n", argv[0]); 
  470.         exit(1); 
  471.     } 
  472.  
  473.     filename = argv[1]; 
  474. */  
  475.   
  476.     /* auto detect the output format from the name. default is mpeg. */  
  477.        fmt = av_guess_format (NULL , filename , NULL );  
  478.        //fmt = av_guess_format("h264", NULL, NULL);  
  479.   
  480.     if (! fmt) {  
  481.         printf( "Could not deduce output format from file extension: using MPEG.\n");  
  482.              fmt = av_guess_format ("mpeg" , NULL , NULL );  
  483.     }  
  484.     if (! fmt) {  
  485.         fprintf( stderr, "Could not find suitable output format\n" );  
  486.         exit(1);  
  487.     }  
  488.   
  489.     /* allocate the output media context */  
  490.        oc = avformat_alloc_context ();  
  491.   
  492.     if (! oc) {  
  493.         fprintf( stderr, "Memory error\n" );  
  494.         exit(1);  
  495.     }  
  496.     oc-> oformat = fmt;  
  497.   
  498.        fmt-> video_codec = (CodecID ) CODEC_ID_H264 ;  
  499.        fmt-> audio_codec = (CodecID ) CODEC_ID_AAC ;  
  500.   
  501.     _snprintf( oc-> filename, sizeof( oc-> filename), "%s" , filename );  
  502.   
  503.     /* add the audio and video streams using the default format codecs 
  504.        and initialize the codecs */  
  505.     video_st = NULL;  
  506.     if ( fmt-> video_codec != CODEC_ID_NONE ) {  
  507.         video_st = add_video_stream (oc , fmt ->video_codec );  
  508.     }  
  509.   
  510.        audio_st = NULL;  
  511.        if ( fmt-> audio_codec != CODEC_ID_NONE ) {  
  512.              audio_st = add_audio_stream (oc , fmt ->audio_codec );  
  513.       }  
  514.      
  515.     /* set the output parameters (must be done even if no parameters). */  
  516.     if ( av_set_parameters( oc, NULL) < 0) {  
  517.         fprintf( stderr, "Invalid output format parameters\n" );  
  518.         exit(1);  
  519.     }  
  520.   
  521.     dump_format( oc, 0, filename, 1);  
  522.   
  523.     /* now that all the parameters are set, we can open the audio and 
  524.        video codecs and allocate the necessary encode buffers */  
  525.     if ( video_st)  
  526.         open_video( oc, video_st);  
  527.     if ( audio_st)  
  528.         open_audio( oc, audio_st);  
  529.   
  530.     /* open the output file */  
  531.     if (!( fmt-> flags & AVFMT_NOFILE )) {  
  532.         if ( url_fopen(& oc-> pb, filename, URL_WRONLY) < 0) {  
  533.             fprintf( stderr, "Could not open '%s'\n" , filename );  
  534.             exit(1);  
  535.         }  
  536.     }  
  537.   
  538.     /* write the stream header, if any */  
  539.        if( av_write_header (oc ) < 0)  
  540.       {  
  541.              fprintf( stderr, "Could not write header for output file\n" );  
  542.              return -1;  
  543.       }  
  544.   
  545.        //long startTime = GetTickCount();  
  546.   
  547.     for(;;) {  
  548.         // compute current audio and video time  
  549.         if ( audio_st)  
  550.             audio_pts = ( double) audio_st-> pts. val * audio_st ->time_base .num / audio_st ->time_base .den ;  
  551.         else  
  552.             audio_pts = 0.0;  
  553.   
  554.         if ( video_st)  
  555.             video_pts = ( double) video_st-> pts. val * video_st ->time_base .num / video_st ->time_base .den ;  
  556.         else  
  557.             video_pts = 0.0;  
  558.   
  559.         if ((! audio_st || audio_pts >= STREAM_DURATION ) &&  
  560.             (! video_st || video_pts >= STREAM_DURATION ))  
  561.             break;  
  562.   
  563.         // write interleaved audio and video frames  
  564.         if (! video_st || ( video_st && audio_st && audio_pts < video_pts )) {  
  565.             write_audio_frame (oc , audio_st );  
  566.         } else {  
  567.             write_video_frame (oc , video_st );  
  568.         }  
  569.     }  
  570.   
  571.        //printf("encode 250 frames, total time:%ld\n", GetTickCount()-startTime);  
  572.   
  573.     /* close each codec */  
  574.     if ( video_st)  
  575.         close_video (oc , video_st );  
  576.   
  577.     if ( audio_st)  
  578.         close_audio (oc , audio_st );  
  579.   
  580.     /* write the trailer, if any */  
  581.     int res = av_write_trailer (oc );  
  582.   
  583.     /* free the streams */  
  584.     for( i = 0; i < oc-> nb_streams; i++) {  
  585.         av_freep(& oc-> streams[ i]-> codec);  
  586.         av_freep(& oc-> streams[ i]);  
  587.     }  
  588.   
  589.     if (!( fmt-> flags & AVFMT_NOFILE )) {  
  590.         /* close the output file */  
  591.         url_fclose( oc-> pb);  
  592.     }  
  593.   
  594.     /* free the stream */  
  595.     av_free( oc);  
  596.   
  597.     return 0;  
  598. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值