Advertisement
Terbaddo

libav program - main.cpp

Dec 31st, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <math.h>
  4.  
  5. extern "C" {
  6. #include <libavutil/mathematics.h>
  7. #include <libavutil/opt.h>
  8. #include <libavformat/avformat.h>
  9. #include <libswscale/swscale.h>
  10. #include <libavcodec/avcodec.h>
  11. }
  12. int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags);
  13. int avformat_alloc_output_context2(AVFormatContext **avctx, AVOutputFormat *oformat, const char *format, const char *filename);
  14. int select_sample_rate(AVCodec *codec);
  15. int select_channel_layout(AVCodec *codec);
  16. int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt);
  17. void die(const char *msg) { std::cerr << msg << std::endl; exit(1); }
  18.  
  19. using namespace std;
  20.  
  21. AVFormatContext *inputFormatContext;
  22. AVStream *inputAudioStream;
  23. AVFormatContext *outputFormatContext;
  24. AVStream *outputAudioStream;
  25. int stream_id;
  26. AVCodec *inputAudioCodec;
  27. AVCodecContext *inputAudioCodecContext;
  28. AVCodecContext *outputAudioCodecContext;
  29.  
  30. void openInputFile(const char *filename)
  31. {
  32.     inputFormatContext = NULL;
  33.     if(avformat_open_input(&inputFormatContext, filename, NULL, NULL) != 0) die("could not open input file");
  34.     if(avformat_find_stream_info(inputFormatContext,NULL) < 0) die("could not find stream info");
  35.  
  36.     for(unsigned i=0; i<inputFormatContext->nb_streams; i++)
  37.     {
  38.         if(inputFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  39.         {
  40.             stream_id = i;
  41.             inputAudioStream = inputFormatContext->streams[i];
  42.             break;
  43.         }
  44.     }
  45.     if(inputAudioStream == NULL) die("didn't find any audio stream");
  46.  
  47.     inputAudioCodecContext = inputAudioStream->codec;
  48.     inputAudioCodec = avcodec_find_decoder(inputAudioCodecContext->codec_id);
  49.     if(inputAudioCodec == NULL) die("Unsupported codec");
  50.     if(avcodec_open2(inputAudioCodecContext, inputAudioCodec, NULL)<0) die("Could not open codec");
  51.  
  52.     av_dump_format(inputFormatContext, 0, inputFormatContext->filename, 0);
  53. }
  54.  
  55. void openOutputFile(const char *filename)
  56. {
  57.     outputFormatContext = NULL;
  58.     avformat_alloc_output_context2(&outputFormatContext, NULL, NULL, filename);
  59.  
  60.     outputAudioStream = avformat_new_stream(outputFormatContext, 0);
  61.         //outputAudioStream->codec = inputAudioStream->codec;
  62.     AVCodec *codec = avcodec_find_encoder(CODEC_ID_VORBIS);
  63.     if(!codec) die("Codec not found");
  64.     outputAudioCodecContext = avcodec_alloc_context3(codec);
  65.     if(!outputAudioCodecContext) die("Could not allocate video codec context");
  66.     outputAudioCodecContext->bit_rate = inputAudioStream->codec->bit_rate;
  67.     outputAudioCodecContext->sample_rate = inputAudioStream->codec->sample_rate;
  68.     outputAudioCodecContext->channel_layout = select_channel_layout(codec);
  69.     outputAudioCodecContext->channels = av_get_channel_layout_nb_channels(outputAudioCodecContext->channel_layout);
  70.     outputAudioCodecContext->sample_fmt = AV_SAMPLE_FMT_S16;
  71.     if(!check_sample_fmt(codec, outputAudioCodecContext->sample_fmt)) die("Encoder does not support sample format");
  72.     outputAudioStream->codec = outputAudioCodecContext;
  73.     if(avcodec_open2(outputAudioCodecContext, codec, NULL) < 0) die("Could not open codec");
  74.  
  75.     avio_open(&outputFormatContext->pb, filename, AVIO_FLAG_WRITE);
  76.     av_dump_format(outputFormatContext, 0, outputFormatContext->filename, 1);
  77. }
  78.  
  79. int main(int argc, char **argv)
  80. {
  81.     avcodec_register_all();
  82.     av_register_all();
  83.     openInputFile("test.mp3");
  84.     openOutputFile("out.mp4");
  85.  
  86.     avformat_write_header(outputFormatContext, NULL);
  87.  
  88.     AVPacket inputPacket, outputPacket;
  89.     AVFrame *pFrame;
  90.  
  91.     pFrame = avcodec_alloc_frame();
  92.  
  93.     int rr,xx;
  94.     while(av_read_frame(inputFormatContext, &inputPacket)>=0)
  95.     {
  96.         rr = avcodec_decode_audio4(inputAudioCodecContext, pFrame, &xx, &inputPacket);
  97.         if(rr < 0) {
  98.             char errbuf[128];
  99.             av_strerror(rr, errbuf, 128);
  100.             cout << "audio decode error" << rr << " " << errbuf << endl;
  101.             exit(1);
  102.         }
  103.  
  104.         av_init_packet(&outputPacket);
  105.         rr = avcodec_encode_audio2(outputAudioCodecContext, &outputPacket, pFrame, &xx);
  106.         if(rr < 0) {
  107.             char errbuf[128];
  108.             av_strerror(rr, errbuf, 128);
  109.             cout << "audio encode error" << rr << " " << errbuf << endl;
  110.             exit(1);
  111.         }
  112.         outputPacket.stream_index = outputAudioStream->index;
  113.         outputPacket.flags |= AV_PKT_FLAG_KEY;
  114.  
  115.         rr = av_interleaved_write_frame(outputFormatContext, &outputPacket);
  116.         if (rr && (rr != AVERROR(EINVAL)))
  117.             cout << "audio write error" << hex << rr << endl;
  118.  
  119.         av_free_packet(&outputPacket);
  120.     }
  121.  
  122.     av_write_trailer(outputFormatContext);
  123.  
  124.     avformat_close_input(&inputFormatContext);
  125.     avcodec_close(outputFormatContext->streams[0]->codec);
  126.     av_freep(&outputFormatContext->streams[0]->codec);
  127.     av_freep(&outputFormatContext->streams[0]);
  128.  
  129.     avio_close(outputFormatContext->pb);
  130.     av_free(outputFormatContext);
  131.     cout << "success" << endl;
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement