Advertisement
Guest User

ffmpeg transcode

a guest
Jan 23rd, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.53 KB | None | 0 0
  1. /*
  2.  * File:   newmain.c
  3.  * Author: antonello
  4.  *
  5.  * Created on 23 gennaio 2013, 11.24
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11.  
  12. #include <libavutil/samplefmt.h>
  13. #include <libavutil/timestamp.h>
  14. #include <libavformat/avformat.h>
  15. #include <libavcodec/old_codec_ids.h>
  16.  
  17. static AVCodecContext *get_encoder(int sampleRate, int channels, int audioBitrate)
  18. {
  19.     AVCodecContext  *audioCodec;
  20.     AVCodec *codec;
  21.  
  22.    
  23.  
  24.     //Set up audio encoder
  25.     codec = avcodec_find_encoder(CODEC_ID_AAC);
  26.     if (codec == NULL)
  27.     {
  28.         printf("avcodec_find_encoder: ERROR\n");
  29.         return NULL;
  30.     }
  31.     audioCodec = avcodec_alloc_context();
  32.     audioCodec->bit_rate = audioBitrate;
  33.     audioCodec->sample_fmt = AV_SAMPLE_FMT_S16P;
  34.     audioCodec->sample_rate = sampleRate;
  35.     audioCodec->channels = channels;
  36.     audioCodec->profile = FF_PROFILE_AAC_MAIN;
  37.     audioCodec->channel_layout=AV_CH_LAYOUT_MONO;
  38.     //audioCodec->time_base = (AVRational){1, sampleRate};
  39.     audioCodec->time_base.num  = 1;
  40.     audioCodec->time_base.den  = sampleRate;
  41.    
  42.     audioCodec->codec_type = AVMEDIA_TYPE_AUDIO;
  43.     if (avcodec_open(audioCodec, codec) < 0)
  44.     {
  45.         printf("avcodec_open: ERROR\n");
  46.         return NULL;
  47.     }
  48.  
  49.     return audioCodec;
  50. }
  51.  
  52.  
  53. int main(int argc, char** argv) {
  54.   AVFormatContext *aFormatCtx_decoder = NULL;
  55.   AVFormatContext *aFormatCtx_encoder = NULL;
  56.   int             i, audioStream;
  57.   AVPacket        packet_decoder;
  58.   AVPacket        packet_encoder;
  59.   int             got_frame=0;
  60.   int             complete_decode=0;
  61.   int             len;
  62.   AVFrame         *decoded_frame = NULL;
  63.   AVCodecContext  *aCodec_decoderCtx = NULL;
  64.   AVCodec         *aCodec_decoder = NULL;
  65.   FILE            *outfile;
  66.   //reding input file
  67.   avcodec_register_all();
  68.  
  69.    //register all codecs
  70.     av_register_all();
  71.    
  72.  //open file
  73.     if(avformat_open_input(&aFormatCtx_decoder, "sample.aac", NULL, NULL)!=0){
  74.         fprintf(stderr, "Could not open source file \n");
  75.         return -1; // Couldn't open file
  76.     }
  77.  
  78.   // Retrieve stream information
  79.   if(avformat_find_stream_info(aFormatCtx_decoder, NULL)<0){
  80.       fprintf(stderr, "Couldn't find stream information \n");
  81.       return -1; // Couldn't find stream information
  82.   }
  83.  
  84.   // Dump information about file onto standard error
  85.   //av_dump_format(aFormatCtx_decode, 0, argv[1], 0);
  86.  
  87.   // Find the first audio stream
  88.   audioStream=-1;
  89.  
  90.   for(i=0; i<aFormatCtx_decoder->nb_streams; i++) {
  91.     if(aFormatCtx_decoder->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
  92.        audioStream < 0) {
  93.       audioStream=i;
  94.     }
  95.   }
  96.   if(audioStream==-1){
  97.       fprintf(stderr, "File haven't sudio stream \n");
  98.       return -1;
  99.   }
  100.  
  101.   //get audio codec contex
  102.   aCodec_decoderCtx=aFormatCtx_decoder->streams[audioStream]->codec;
  103.   //get audio codec
  104.   aCodec_decoder = avcodec_find_decoder(aCodec_decoderCtx->codec_id);
  105.   aCodec_decoder->sample_fmts=AV_SAMPLE_FMT_S16P;
  106.   if(!aCodec_decoder) {
  107.     fprintf(stderr, "Unsupported codec!\n");
  108.     return -1;//Unsupported codec!
  109.   }
  110.   //open codec
  111.   // Open codec
  112.   if(avcodec_open2(aCodec_decoderCtx, aCodec_decoder, NULL)<0)
  113.     return -1; // Could not open codec
  114.   // allocate audio frame
  115.   decoded_frame = avcodec_alloc_frame();
  116.   if (!decoded_frame) {
  117.     fprintf(stderr, "Could not allocate audio frame\n");
  118.     return -1;//Could not allocate audio frame
  119.     }
  120.   aCodec_decoderCtx->bit_rate=12000;
  121.   aFormatCtx_encoder=get_encoder(8000,1,12000);
  122.   av_init_packet(&packet_encoder);
  123.  
  124.   printf("param %d %d %d",aCodec_decoderCtx->sample_fmt,aCodec_decoderCtx->channels,aCodec_decoderCtx->bit_rate);
  125.  
  126.   outfile = fopen("out.aac", "wb");
  127.     if (!outfile) {
  128.         printf(stderr, "Could not open outfile \n");
  129.         return -1;//Could not open outfile
  130.     }
  131.   while(av_read_frame(aFormatCtx_decoder, &packet_decoder)>=0) {
  132.      // decode frame
  133.      len = avcodec_decode_audio4(aCodec_decoderCtx, decoded_frame, &got_frame, &packet_decoder);
  134.         if (len < 0) {
  135.             fprintf(stderr, "Error while decoding\n");
  136.             return -1;
  137.             }
  138.    
  139.         if (got_frame){
  140.           avcodec_encode_audio2(aFormatCtx_encoder,&packet_encoder,decoded_frame,&complete_decode);
  141.          
  142.           if(complete_decode){
  143.              printf("Write frame  (size=%5d)\n", packet_encoder.size);
  144.               fwrite(packet_encoder.data, 1, packet_encoder.size, outfile);
  145.               av_free_packet(&packet_encoder);
  146.           }
  147.         }
  148.        
  149.    
  150.      
  151.     }
  152.   fclose(outfile);
  153.     return (EXIT_SUCCESS);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement