Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include <math.h>
- extern "C" {
- #include <libavutil/mathematics.h>
- #include <libavutil/opt.h>
- #include <libavformat/avformat.h>
- #include <libswscale/swscale.h>
- #include <libavcodec/avcodec.h>
- }
- int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags);
- int avformat_alloc_output_context2(AVFormatContext **avctx, AVOutputFormat *oformat, const char *format, const char *filename);
- int select_sample_rate(AVCodec *codec);
- int select_channel_layout(AVCodec *codec);
- int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt);
- void die(const char *msg) { std::cerr << msg << std::endl; exit(1); }
- using namespace std;
- AVFormatContext *inputFormatContext;
- AVStream *inputAudioStream;
- AVFormatContext *outputFormatContext;
- AVStream *outputAudioStream;
- int stream_id;
- AVCodec *inputAudioCodec;
- AVCodecContext *inputAudioCodecContext;
- AVCodecContext *outputAudioCodecContext;
- void openInputFile(const char *filename)
- {
- inputFormatContext = NULL;
- if(avformat_open_input(&inputFormatContext, filename, NULL, NULL) != 0) die("could not open input file");
- if(avformat_find_stream_info(inputFormatContext,NULL) < 0) die("could not find stream info");
- for(unsigned i=0; i<inputFormatContext->nb_streams; i++)
- {
- if(inputFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
- {
- stream_id = i;
- inputAudioStream = inputFormatContext->streams[i];
- break;
- }
- }
- if(inputAudioStream == NULL) die("didn't find any audio stream");
- inputAudioCodecContext = inputAudioStream->codec;
- inputAudioCodec = avcodec_find_decoder(inputAudioCodecContext->codec_id);
- if(inputAudioCodec == NULL) die("Unsupported codec");
- if(avcodec_open2(inputAudioCodecContext, inputAudioCodec, NULL)<0) die("Could not open codec");
- av_dump_format(inputFormatContext, 0, inputFormatContext->filename, 0);
- }
- void openOutputFile(const char *filename)
- {
- outputFormatContext = NULL;
- avformat_alloc_output_context2(&outputFormatContext, NULL, NULL, filename);
- outputAudioStream = avformat_new_stream(outputFormatContext, 0);
- //outputAudioStream->codec = inputAudioStream->codec;
- AVCodec *codec = avcodec_find_encoder(CODEC_ID_VORBIS);
- if(!codec) die("Codec not found");
- outputAudioCodecContext = avcodec_alloc_context3(codec);
- if(!outputAudioCodecContext) die("Could not allocate video codec context");
- outputAudioCodecContext->bit_rate = inputAudioStream->codec->bit_rate;
- outputAudioCodecContext->sample_rate = inputAudioStream->codec->sample_rate;
- outputAudioCodecContext->channel_layout = select_channel_layout(codec);
- outputAudioCodecContext->channels = av_get_channel_layout_nb_channels(outputAudioCodecContext->channel_layout);
- outputAudioCodecContext->sample_fmt = AV_SAMPLE_FMT_S16;
- if(!check_sample_fmt(codec, outputAudioCodecContext->sample_fmt)) die("Encoder does not support sample format");
- outputAudioStream->codec = outputAudioCodecContext;
- if(avcodec_open2(outputAudioCodecContext, codec, NULL) < 0) die("Could not open codec");
- avio_open(&outputFormatContext->pb, filename, AVIO_FLAG_WRITE);
- av_dump_format(outputFormatContext, 0, outputFormatContext->filename, 1);
- }
- int main(int argc, char **argv)
- {
- avcodec_register_all();
- av_register_all();
- openInputFile("test.mp3");
- openOutputFile("out.mp4");
- avformat_write_header(outputFormatContext, NULL);
- AVPacket inputPacket, outputPacket;
- AVFrame *pFrame;
- pFrame = avcodec_alloc_frame();
- int rr,xx;
- while(av_read_frame(inputFormatContext, &inputPacket)>=0)
- {
- rr = avcodec_decode_audio4(inputAudioCodecContext, pFrame, &xx, &inputPacket);
- if(rr < 0) {
- char errbuf[128];
- av_strerror(rr, errbuf, 128);
- cout << "audio decode error" << rr << " " << errbuf << endl;
- exit(1);
- }
- av_init_packet(&outputPacket);
- rr = avcodec_encode_audio2(outputAudioCodecContext, &outputPacket, pFrame, &xx);
- if(rr < 0) {
- char errbuf[128];
- av_strerror(rr, errbuf, 128);
- cout << "audio encode error" << rr << " " << errbuf << endl;
- exit(1);
- }
- outputPacket.stream_index = outputAudioStream->index;
- outputPacket.flags |= AV_PKT_FLAG_KEY;
- rr = av_interleaved_write_frame(outputFormatContext, &outputPacket);
- if (rr && (rr != AVERROR(EINVAL)))
- cout << "audio write error" << hex << rr << endl;
- av_free_packet(&outputPacket);
- }
- av_write_trailer(outputFormatContext);
- avformat_close_input(&inputFormatContext);
- avcodec_close(outputFormatContext->streams[0]->codec);
- av_freep(&outputFormatContext->streams[0]->codec);
- av_freep(&outputFormatContext->streams[0]);
- avio_close(outputFormatContext->pb);
- av_free(outputFormatContext);
- cout << "success" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement