织梦CMS - 轻松建站从此开始!

罗索

基于ffmpeg的简单音视频编解码的例子(代码转自ffmpeg官方网站)

落鹤生 发布于 2013-01-30 14:28 点击:次 
该例子代码包含音频的解码/编码和视频的编码/解码,其中主要用到编解码的libavcodec组件。以下是完整的例子,代码自身的注释足够清晰了。
TAG:

近日需要做一个视频转码服务器,对我这样一个在该领域的新手来说却是够我折腾一番,在别人的建议下开始研究开源ffmpeg项目,下面是在代码中看到的一 段例子代码,对我的学习非常有帮助。该例子代码包含音频的解码/编码和视频的编码/解码,其中主要用到编解码的libavcodec组件。以下是完整的例 子,代码自身的注释足够清晰了。

  1. /** 
  2.  * @file 
  3.  * libavcodec API use example. 
  4.  * 
  5.  * Note that libavcodec only handles codecs (mpeg, mpeg4, etc...), 
  6.  * not file formats (avi, vob, mp4, mov, mkv, mxf, flv, mpegts, mpegps, etc...).  
  7.  * See library 'libavformat' for the format handling 
  8.  * @example doc/examples/decoding_encoding.c 
  9.  */ 
  10.  
  11. #include <math.h> 
  12.  
  13. #include <libavutil/opt.h> 
  14. #include <libavcodec/avcodec.h> 
  15. #include <libavutil/channel_layout.h> 
  16. #include <libavutil/common.h> 
  17. #include <libavutil/imgutils.h> 
  18. #include <libavutil/mathematics.h> 
  19. #include <libavutil/samplefmt.h> 
  20.  
  21. #define INBUF_SIZE 4096 
  22. #define AUDIO_INBUF_SIZE 20480 
  23. #define AUDIO_REFILL_THRESH 4096 
  24.  
  25. /* check that a given sample format is supported by the encoder */ 
  26. static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt) 
  27.     const enum AVSampleFormat *p = codec->sample_fmts; 
  28.  
  29.     while (*p != AV_SAMPLE_FMT_NONE) { 
  30.         if (*p == sample_fmt) 
  31.             return 1; 
  32.         p++; 
  33.     } 
  34.     return 0; 
  35.  
  36. /* just pick the highest supported samplerate */ 
  37. static int select_sample_rate(AVCodec *codec) 
  38.     const int *p; 
  39.     int best_samplerate = 0; 
  40.  
  41.     if (!codec->supported_samplerates) 
  42.         return 44100; 
  43.  
  44.     p = codec->supported_samplerates; 
  45.     while (*p) { 
  46.         best_samplerate = FFMAX(*p, best_samplerate); 
  47.         p++; 
  48.     } 
  49.     return best_samplerate; 
  50.  
  51. /* select layout with the highest channel count */ 
  52. static int select_channel_layout(AVCodec *codec) 
  53.     const uint64_t *p; 
  54.     uint64_t best_ch_layout = 0; 
  55.     int best_nb_channells   = 0; 
  56.  
  57.     if (!codec->channel_layouts) 
  58.         return AV_CH_LAYOUT_STEREO; 
  59.  
  60.     p = codec->channel_layouts; 
  61.     while (*p) { 
  62.         int nb_channels = av_get_channel_layout_nb_channels(*p); 
  63.  
  64.         if (nb_channels > best_nb_channells) { 
  65.             best_ch_layout    = *p; 
  66.             best_nb_channells = nb_channels; 
  67.         } 
  68.         p++; 
  69.     } 
  70.     return best_ch_layout; 
  71.  
  72. /* 
  73.  * Audio encoding example 
  74.  */ 
  75. static void audio_encode_example(const char *filename) 
  76.     AVCodec *codec; 
  77.     AVCodecContext *c= NULL; 
  78.     AVFrame *frame; 
  79.     AVPacket pkt; 
  80.     int i, j, k, ret, got_output; 
  81.     int buffer_size; 
  82.     FILE *f; 
  83.     uint16_t *samples; 
  84.     float t, tincr; 
  85.  
  86.     printf("Encode audio file %s\n", filename); 
  87.  
  88.     /* find the MP2 encoder */ 
  89.     codec = avcodec_find_encoder(AV_CODEC_ID_MP2); 
  90.     if (!codec) { 
  91.         fprintf(stderr, "Codec not found\n"); 
  92.         exit(1); 
  93.     } 
  94.  
  95.     c = avcodec_alloc_context3(codec); 
  96.     if (!c) { 
  97.         fprintf(stderr, "Could not allocate audio codec context\n"); 
  98.         exit(1); 
  99.     } 
  100.  
  101.     /* put sample parameters */ 
  102.     c->bit_rate = 64000; 
  103.  
  104.     /* check that the encoder supports s16 pcm input */ 
  105.     c->sample_fmt = AV_SAMPLE_FMT_S16; 
  106.     if (!check_sample_fmt(codec, c->sample_fmt)) { 
  107.         fprintf(stderr, "Encoder does not support sample format %s"
  108.                 av_get_sample_fmt_name(c->sample_fmt)); 
  109.         exit(1); 
  110.     } 
  111.  
  112.     /* select other audio parameters supported by the encoder */ 
  113.     c->sample_rate    = select_sample_rate(codec); 
  114.     c->channel_layout = select_channel_layout(codec); 
  115.     c->channels       = av_get_channel_layout_nb_channels(c->channel_layout); 
  116.  
  117.     /* open it */ 
  118.     if (avcodec_open2(c, codec, NULL) < 0) { 
  119.         fprintf(stderr, "Could not open codec\n"); 
  120.         exit(1); 
  121.     } 
  122.  
  123.     f = fopen(filename, "wb"); 
  124.     if (!f) { 
  125.         fprintf(stderr, "Could not open %s\n", filename); 
  126.         exit(1); 
  127.     } 
  128.  
  129.     /* frame containing input raw audio */ 
  130.     frame = avcodec_alloc_frame(); 
  131.     if (!frame) { 
  132.         fprintf(stderr, "Could not allocate audio frame\n"); 
  133.         exit(1); 
  134.     } 
  135.  
  136.     frame->nb_samples     = c->frame_size; 
  137.     frame->format         = c->sample_fmt; 
  138.     frame->channel_layout = c->channel_layout; 
  139.  
  140.     /* the codec gives us the frame size, in samples, 
  141.      * we calculate the size of the samples buffer in bytes */ 
  142.     buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size, 
  143.                                              c->sample_fmt, 0); 
  144.     samples = av_malloc(buffer_size); 
  145.     if (!samples) { 
  146.         fprintf(stderr, "Could not allocate %d bytes for samples buffer\n"
  147.                 buffer_size); 
  148.         exit(1); 
  149.     } 
  150.     /* setup the data pointers in the AVFrame */ 
  151.     ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt, 
  152.                                    (const uint8_t*)samples, buffer_size, 0); 
  153.     if (ret < 0) { 
  154.         fprintf(stderr, "Could not setup audio frame\n"); 
  155.         exit(1); 
  156.     } 
  157.  
  158.     /* encode a single tone sound */ 
  159.     t = 0; 
  160.     tincr = 2 * M_PI * 440.0 / c->sample_rate; 
  161.     for(i=0;i<200;i++) { 
  162.         av_init_packet(&pkt); 
  163.         pkt.data = NULL; // packet data will be allocated by the encoder 
  164.         pkt.size = 0; 
  165.  
  166.         for (j = 0; j < c->frame_size; j++) { 
  167.             samples[2*j] = (int)(sin(t) * 10000); 
  168.  
  169.             for (k = 1; k < c->channels; k++) 
  170.                 samples[2*j + k] = samples[2*j]; 
  171.             t += tincr; 
  172.         } 
  173.         /* encode the samples */ 
  174.         ret = avcodec_encode_audio2(c, &pkt, frame, &got_output); 
  175.         if (ret < 0) { 
  176.             fprintf(stderr, "Error encoding audio frame\n"); 
  177.             exit(1); 
  178.         } 
  179.         if (got_output) { 
  180.             fwrite(pkt.data, 1, pkt.size, f); 
  181.             av_free_packet(&pkt); 
  182.         } 
  183.     } 
  184.  
  185.     /* get the delayed frames */ 
  186.     for (got_output = 1; got_output; i++) { 
  187.         ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output); 
  188.         if (ret < 0) { 
  189.             fprintf(stderr, "Error encoding frame\n"); 
  190.             exit(1); 
  191.         } 
  192.  
  193.         if (got_output) { 
  194.             fwrite(pkt.data, 1, pkt.size, f); 
  195.             av_free_packet(&pkt); 
  196.         } 
  197.     } 
  198.     fclose(f); 
  199.  
  200.     av_freep(&samples); 
  201.     avcodec_free_frame(&frame); 
  202.     avcodec_close(c); 
  203.     av_free(c); 
  204.  
  205. /* 
  206.  * Audio decoding. 
  207.  */ 
  208. static void audio_decode_example(const char *outfilename, const char *filename) 
  209.     AVCodec *codec; 
  210.     AVCodecContext *c= NULL; 
  211.     int len; 
  212.     FILE *f, *outfile; 
  213.     uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; 
  214.     AVPacket avpkt; 
  215.     AVFrame *decoded_frame = NULL; 
  216.  
  217.     av_init_packet(&avpkt); 
  218.  
  219.     printf("Decode audio file %s to %s\n", filename, outfilename); 
  220.  
  221.     /* find the mpeg audio decoder */ 
  222.     codec = avcodec_find_decoder(AV_CODEC_ID_MP2); 
  223.     if (!codec) { 
  224.         fprintf(stderr, "Codec not found\n"); 
  225.         exit(1); 
  226.     } 
  227.  
  228.     c = avcodec_alloc_context3(codec); 
  229.     if (!c) { 
  230.         fprintf(stderr, "Could not allocate audio codec context\n"); 
  231.         exit(1); 
  232.     } 
  233.  
  234.     /* open it */ 
  235.     if (avcodec_open2(c, codec, NULL) < 0) { 
  236.         fprintf(stderr, "Could not open codec\n"); 
  237.         exit(1); 
  238.     } 
  239.  
  240.     f = fopen(filename, "rb"); 
  241.     if (!f) { 
  242.         fprintf(stderr, "Could not open %s\n", filename); 
  243.         exit(1); 
  244.     } 
  245.     outfile = fopen(outfilename, "wb"); 
  246.     if (!outfile) { 
  247.         av_free(c); 
  248.         exit(1); 
  249.     } 
  250.  
  251.     /* decode until eof */ 
  252.     avpkt.data = inbuf; 
  253.     avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); 
  254.  
  255.     while (avpkt.size > 0) { 
  256.         int got_frame = 0; 
  257.  
  258.         if (!decoded_frame) { 
  259.             if (!(decoded_frame = avcodec_alloc_frame())) { 
  260.                 fprintf(stderr, "Could not allocate audio frame\n"); 
  261.                 exit(1); 
  262.             } 
  263.         } else 
  264.             avcodec_get_frame_defaults(decoded_frame); 
  265.  
  266.         len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt); 
  267.         if (len < 0) { 
  268.             fprintf(stderr, "Error while decoding\n"); 
  269.             exit(1); 
  270.         } 
  271.         if (got_frame) { 
  272.             /* if a frame has been decoded, output it */ 
  273.             int data_size = av_samples_get_buffer_size(NULL, c->channels, 
  274.                                                        decoded_frame->nb_samples, 
  275.                                                        c->sample_fmt, 1); 
  276.             fwrite(decoded_frame->data[0], 1, data_size, outfile); 
  277.         } 
  278.         avpkt.size -= len; 
  279.         avpkt.data += len; 
  280.         avpkt.dts = 
  281.         avpkt.pts = AV_NOPTS_VALUE; 
  282.         if (avpkt.size < AUDIO_REFILL_THRESH) { 
  283.             /* Refill the input buffer, to avoid trying to decode 
  284.              * incomplete frames. Instead of this, one could also use 
  285.              * a parser, or use a proper container format through 
  286.              * libavformat. */ 
  287.             memmove(inbuf, avpkt.data, avpkt.size); 
  288.             avpkt.data = inbuf; 
  289.             len = fread(avpkt.data + avpkt.size, 1, 
  290.                         AUDIO_INBUF_SIZE - avpkt.size, f); 
  291.             if (len > 0) 
  292.                 avpkt.size += len; 
  293.         } 
  294.     } 
  295.  
  296.     fclose(outfile); 
  297.     fclose(f); 
  298.  
  299.     avcodec_close(c); 
  300.     av_free(c); 
  301.     avcodec_free_frame(&decoded_frame); 
  302.  
  303. /* 
  304.  * Video encoding example 
  305.  */ 
  306. static void video_encode_example(const char *filename, int codec_id) 
  307.     AVCodec *codec; 
  308.     AVCodecContext *c= NULL; 
  309.     int i, ret, x, y, got_output; 
  310.     FILE *f; 
  311.     AVFrame *frame; 
  312.     AVPacket pkt; 
  313.     uint8_t endcode[] = { 0, 0, 1, 0xb7 }; 
  314.  
  315.     printf("Encode video file %s\n", filename); 
  316.  
  317.     /* find the mpeg1 video encoder */ 
  318.     codec = avcodec_find_encoder(codec_id); 
  319.     if (!codec) { 
  320.         fprintf(stderr, "Codec not found\n"); 
  321.         exit(1); 
  322.     } 
  323.  
  324.     c = avcodec_alloc_context3(codec); 
  325.     if (!c) { 
  326.         fprintf(stderr, "Could not allocate video codec context\n"); 
  327.         exit(1); 
  328.     } 
  329.  
  330.     /* put sample parameters */ 
  331.     c->bit_rate = 400000; 
  332.     /* resolution must be a multiple of two */ 
  333.     c->width = 352; 
  334.     c->height = 288; 
  335.     /* frames per second */ 
  336.     c->time_base= (AVRational){1,25}; 
  337.     c->gop_size = 10; /* emit one intra frame every ten frames */ 
  338.     c->max_b_frames=1; 
  339.     c->pix_fmt = AV_PIX_FMT_YUV420P; 
  340.  
  341.     if(codec_id == AV_CODEC_ID_H264) 
  342.         av_opt_set(c->priv_data, "preset""slow", 0); 
  343.  
  344.     /* open it */ 
  345.     if (avcodec_open2(c, codec, NULL) < 0) { 
  346.         fprintf(stderr, "Could not open codec\n"); 
  347.         exit(1); 
  348.     } 
  349.  
  350.     f = fopen(filename, "wb"); 
  351.     if (!f) { 
  352.         fprintf(stderr, "Could not open %s\n", filename); 
  353.         exit(1); 
  354.     } 
  355.  
  356.     frame = avcodec_alloc_frame(); 
  357.     if (!frame) { 
  358.         fprintf(stderr, "Could not allocate video frame\n"); 
  359.         exit(1); 
  360.     } 
  361.     frame->format = c->pix_fmt; 
  362.     frame->width  = c->width; 
  363.     frame->height = c->height; 
  364.  
  365.     /* the image can be allocated by any means and av_image_alloc() is 
  366.      * just the most convenient way if av_malloc() is to be used */ 
  367.     ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height, 
  368.                          c->pix_fmt, 32); 
  369.     if (ret < 0) { 
  370.         fprintf(stderr, "Could not allocate raw picture buffer\n"); 
  371.         exit(1); 
  372.     } 
  373.  
  374.     /* encode 1 second of video */ 
  375.     for(i=0;i<25;i++) { 
  376.         av_init_packet(&pkt); 
  377.         pkt.data = NULL;    // packet data will be allocated by the encoder 
  378.         pkt.size = 0; 
  379.  
  380.         fflush(stdout); 
  381.         /* prepare a dummy image */ 
  382.         /* Y */ 
  383.         for(y=0;y<c->height;y++) { 
  384.             for(x=0;x<c->width;x++) { 
  385.                 frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3; 
  386.             } 
  387.         } 
  388.  
  389.         /* Cb and Cr */ 
  390.         for(y=0;y<c->height/2;y++) { 
  391.             for(x=0;x<c->width/2;x++) { 
  392.                 frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2; 
  393.                 frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5; 
  394.             } 
  395.         } 
  396.  
  397.         frame->pts = i; 
  398.  
  399.         /* encode the image */ 
  400.         ret = avcodec_encode_video2(c, &pkt, frame, &got_output); 
  401.         if (ret < 0) { 
  402.             fprintf(stderr, "Error encoding frame\n"); 
  403.             exit(1); 
  404.         } 
  405.  
  406.         if (got_output) { 
  407.             printf("Write frame %3d (size=%5d)\n", i, pkt.size); 
  408.             fwrite(pkt.data, 1, pkt.size, f); 
  409.             av_free_packet(&pkt); 
  410.         } 
  411.     } 
  412.  
  413.     /* get the delayed frames */ 
  414.     for (got_output = 1; got_output; i++) { 
  415.         fflush(stdout); 
  416.  
  417.         ret = avcodec_encode_video2(c, &pkt, NULL, &got_output); 
  418.         if (ret < 0) { 
  419.             fprintf(stderr, "Error encoding frame\n"); 
  420.             exit(1); 
  421.         } 
  422.  
  423.         if (got_output) { 
  424.             printf("Write frame %3d (size=%5d)\n", i, pkt.size); 
  425.             fwrite(pkt.data, 1, pkt.size, f); 
  426.             av_free_packet(&pkt); 
  427.         } 
  428.     } 
  429.  
  430.     /* add sequence end code to have a real mpeg file */ 
  431.     fwrite(endcode, 1, sizeof(endcode), f); 
  432.     fclose(f); 
  433.  
  434.     avcodec_close(c); 
  435.     av_free(c); 
  436.     av_freep(&frame->data[0]); 
  437.     avcodec_free_frame(&frame); 
  438.     printf("\n"); 
  439.  
  440. /* 
  441.  * Video decoding example 
  442.  */ 
  443.  
  444. static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, 
  445.                      char *filename) 
  446.     FILE *f; 
  447.     int i; 
  448.  
  449.     f=fopen(filename,"w"); 
  450.     fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255); 
  451.     for(i=0;i<ysize;i++) 
  452.         fwrite(buf + i * wrap,1,xsize,f); 
  453.     fclose(f); 
  454.  
  455. static int decode_write_frame(const char *outfilename, AVCodecContext *avctx, 
  456.                               AVFrame *frame, int *frame_count, AVPacket *pkt, int last) 
  457.     int len, got_frame; 
  458.     char buf[1024]; 
  459.  
  460.     len = avcodec_decode_video2(avctx, frame, &got_frame, pkt); 
  461.     if (len < 0) { 
  462.         fprintf(stderr, "Error while decoding frame %d\n", *frame_count); 
  463.         return len; 
  464.     } 
  465.     if (got_frame) { 
  466.         printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count); 
  467.         fflush(stdout); 
  468.  
  469.         /* the picture is allocated by the decoder, no need to free it */ 
  470.         snprintf(buf, sizeof(buf), outfilename, *frame_count); 
  471.         pgm_save(frame->data[0], frame->linesize[0], 
  472.                  avctx->width, avctx->height, buf); 
  473.         (*frame_count)++; 
  474.     } 
  475.     if (pkt->data) { 
  476.         pkt->size -= len; 
  477.         pkt->data += len; 
  478.     } 
  479.     return 0; 
  480.  
  481. static void video_decode_example(const char *outfilename, const char *filename) 
  482.     AVCodec *codec; 
  483.     AVCodecContext *c= NULL; 
  484.     int frame_count; 
  485.     FILE *f; 
  486.     AVFrame *frame; 
  487.     uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; 
  488.     AVPacket avpkt; 
  489.  
  490.     av_init_packet(&avpkt); 
  491.  
  492.     /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */ 
  493.     memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE); 
  494.  
  495.     printf("Decode video file %s to %s\n", filename, outfilename); 
  496.  
  497.     /* find the mpeg1 video decoder */ 
  498.     codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO); 
  499.     if (!codec) { 
  500.         fprintf(stderr, "Codec not found\n"); 
  501.         exit(1); 
  502.     } 
  503.  
  504.     c = avcodec_alloc_context3(codec); 
  505.     if (!c) { 
  506.         fprintf(stderr, "Could not allocate video codec context\n"); 
  507.         exit(1); 
  508.     } 
  509.  
  510.     if(codec->capabilities&CODEC_CAP_TRUNCATED) 
  511.         c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */ 
  512.  
  513.     /* For some codecs, such as msmpeg4 and mpeg4, width and height 
  514.        MUST be initialized there because this information is not 
  515.        available in the bitstream. */ 
  516.  
  517.     /* open it */ 
  518.     if (avcodec_open2(c, codec, NULL) < 0) { 
  519.         fprintf(stderr, "Could not open codec\n"); 
  520.         exit(1); 
  521.     } 
  522.  
  523.     f = fopen(filename, "rb"); 
  524.     if (!f) { 
  525.         fprintf(stderr, "Could not open %s\n", filename); 
  526.         exit(1); 
  527.     } 
  528.  
  529.     frame = avcodec_alloc_frame(); 
  530.     if (!frame) { 
  531.         fprintf(stderr, "Could not allocate video frame\n"); 
  532.         exit(1); 
  533.     } 
  534.  
  535.     frame_count = 0; 
  536.     for(;;) { 
  537.         avpkt.size = fread(inbuf, 1, INBUF_SIZE, f); 
  538.         if (avpkt.size == 0) 
  539.             break
  540.  
  541.         /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio) 
  542.            and this is the only method to use them because you cannot 
  543.            know the compressed data size before analysing it. 
  544.  
  545.            BUT some other codecs (msmpeg4, mpeg4) are inherently frame 
  546.            based, so you must call them with all the data for one 
  547.            frame exactly. You must also initialize 'width' and 
  548.            'height' before initializing them. */ 
  549.  
  550.         /* NOTE2: some codecs allow the raw parameters (frame size, 
  551.            sample rate) to be changed at any frame. We handle this, so 
  552.            you should also take care of it */ 
  553.  
  554.         /* here, we use a stream based decoder (mpeg1video), so we 
  555.            feed decoder and see if it could decode a frame */ 
  556.         avpkt.data = inbuf; 
  557.         while (avpkt.size > 0) 
  558.             if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0) 
  559.                 exit(1); 
  560.     } 
  561.  
  562.     /* some codecs, such as MPEG, transmit the I and P frame with a 
  563.        latency of one frame. You must do the following to have a 
  564.        chance to get the last frame of the video */ 
  565.     avpkt.data = NULL; 
  566.     avpkt.size = 0; 
  567.     decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1); 
  568.  
  569.     fclose(f); 
  570.  
  571.     avcodec_close(c); 
  572.     av_free(c); 
  573.     avcodec_free_frame(&frame); 
  574.     printf("\n"); 
  575.  
  576. int main(int argc, char **argv) 
  577.     const char *output_type; 
  578.  
  579.     /* register all the codecs */ 
  580.     avcodec_register_all(); 
  581.  
  582.     if (argc < 2) { 
  583.         printf("usage: %s output_type\n" 
  584.                "API example program to decode/encode a media stream with libavcodec.\n" 
  585.                "This program generates a synthetic stream and encodes it to a file\n" 
  586.                "named test.h264, test.mp2 or test.mpg depending on output_type.\n" 
  587.                "The encoded stream is then decoded and written to a raw data output.\n" 
  588.                "output_type must be choosen between 'h264', 'mp2', 'mpg'.\n"
  589.                argv[0]); 
  590.         return 1; 
  591.     } 
  592.     output_type = argv[1]; 
  593.  
  594.     if (!strcmp(output_type, "h264")) { 
  595.         video_encode_example("test.h264", AV_CODEC_ID_H264); 
  596.     } else if (!strcmp(output_type, "mp2")) { 
  597.         audio_encode_example("test.mp2"); 
  598.         audio_decode_example("test.sw""test.mp2"); 
  599.     } else if (!strcmp(output_type, "mpg")) { 
  600.         video_encode_example("test.mpg", AV_CODEC_ID_MPEG1VIDEO); 
  601.         video_decode_example("test%02d.pgm""test.mpg"); 
  602.     } else { 
  603.         fprintf(stderr, "Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n"
  604.                 output_type); 
  605.         return 1; 
  606.     } 
  607.  
  608.     return 0; 

 

(ymsdu2004)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201301/16498.html]
本文出处:CSDN博客 作者:ymsdu2004 原文
顶一下
(6)
100%
踩一下
(0)
0%
------分隔线----------------------------
相关文章
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容