Use FFMPEG for audio conversion in Windows

Source: Internet
Author: User

Development Environment: Windows 7

FFmpeg versions: 0.8

Dynamic Link Library in FFMPEG windows:

Http://url.cn/IJJwo3

For the FFMPEG version, it should be noted that many interfaces of the old version have been deprecated in the new version, and my program is in just for the FFmpeg-0.8 version.

Supported audio formats:
Currently, the test program supports the conversion of AMR, WAV (codecid: gsm_ms), WAV (PCM), 3GP, 3GPP, AAC, and MP3 formats.

I have previously written an AAC to WAV program, which can be downloaded directly. For the first time I used FFMPEG kids shoes in windows, refer to the vs2008 project file provided in this Article, configure the FFMPEG library file and header file. Here I will not upload the vs2008 project and directly attach CPP.

Convert AAC to WAV through FFMPEG

Audio test file:
Http://url.cn/GXOAx5

Extern "C" {# include "libavformat/avformat. H "# include" libavcodec/avcodec. H "# include" libavutil/FIFO. H "};# include <stdio. h>/* in a single channel, each sample occupies bytes = 2bytes */static int g_in_bytes = 2; static int g_out_bytes = 2; avcodeccontext * output_decode_init (avcodeccontext * in_cctx, avformatcontext * out_fctx, int codec_id); int output_decode_open (avcodeccontext * CC); int Ma In () {avformatcontext * in_fctx; avcodeccontext * in_cctx; avcodec * in_codec; const char * in_filename; const char * out_filename; char * decoded_buf; char * output_buf; char * callback; char * before_encoding_buf; int ret = 0; in_filename = "H: \ filesample \ input. AAC "; out_filename =" output/aac2mp3.mp3 "; decoded_buf = (char *) av_malloc (avcodec_max_audio_frame_size); output_buf = (char *) av_malloc (avcodec_max _ Audio_frame_size); resample_buf = (char *) av_malloc (bytes); bytes (); av_register_all (); in_fctx = avformat_alloc_context (); ret = av_open_input_file (& in_fctx, in_filename, null, 0, null); If (Ret! = 0) {printf ("Open input audio file [% s] fail \ n", in_filename); Return-1;} ret = av_find_stream_info (in_fctx ); if (Ret <0) {printf ("find stream in audio file [% s] fail \ n", in_filename); Return-1;} dump_format (in_fctx, 0, in_filename, 0); // here we assume that if a file contains multiple audio streams, // only transcode the first audio stream, and ignore int I for the video stream; int ast_index =-1; for (I = 0; I <(INT) in_fctx-> nb_streams; ++ I) {If (in_fctx-> streams [I]-> codec-> codec_type = avmed Ia_type_audio) {ast_index = I; break ;}}if (ast_index =-1) {printf ("there is not any audio stream in file [% s] \ n ", in_filename); Return 0;} else {printf ("find audio stream in file [% s] \ n", in_filename );} in_cctx = in_fctx-> streams [ast_index]-> codec; // in_cctx-> codec_id = codec_id_gsm; in_codec = avcodec_find_decoder (in_cctx-> codec_id); If (! In_codec) {printf ("find decoder for codec_id [% d] fail, file [% s] \ n", in_cctx-> codec_id, in_filename); Return-1 ;} ret = avcodec_open (in_cctx, in_codec); If (Ret> = 0) {printf ("Open codec [name: % s] for stream [idx: % d] of file [% s] \ n ", in_codec-> name, ast_index, in_filename);} // The output part initializes avoutputformat * out_fmt; avformatcontext * out_fctx; avcodeccontext * out_cctx = NULL; out_fmt = av_guess_format (null, out_filenam E, null); If (! Out_fmt) {printf ("cocould not deduce output format from File Extension: Using MPEG-3. \ n"); out_fmt = av_guess_format ("MP3", null, null);} If (! Out_fmt) {fprintf (stderr, "cocould not find suitable output format \ n"); exit (1) ;}out_fctx = avformat_alloc_context (); If (! Out_fctx) {fprintf (stderr, "avformat_alloc_context fail \ n"); exit (1) ;}out_fctx-> oformat = out_fmt; out_cctx = convert (in_cctx, out_fctx, out_fmt-> audio_codec); If (! Out_cctx) {fprintf (stderr, "output_codec_init fail \ n"); exit (1) ;}/ * set the output parameters (must be done even if no parameters ). */If (av_set_parameters (out_fctx, null) <0) {fprintf (stderr, "invalid output format parameters \ n"); exit (1);} dump_format (out_fctx, 0, out_filename, 1); output_decode_open (out_cctx);/* Open the output file */If (! (Out_fmt-> flags & avfmt_nofile) {If (url_fopen (& out_fctx-> Pb, out_filename, url_wronly) <0) {fprintf (stderr, "cocould not open '% s' \ n", out_filename); exit (1) ;}}/* write the stream header, if any */If (av_write_header (out_fctx) <0) {fprintf (stderr, "cocould not write header for output file \ n"); Return-1 ;}int decoded_size; avpacket in_packet; avpacket out_packet; resamplecontext * rs_ctx = NULL;/* Reference link: http :/ /Hi.baidu.com/wg_wang/item/34396781d20b4b1ec%270btwo-point decoding: (1) read data from the input file by frame, decode the data according to the output file requirements, and write data to the output file by frame. Here, because sample_rate and channels may be different, audio data needs to be re-sampled. (2) because different encoding types have different requirements for a frame of audio data, you may need to save the input data until the output encoding is sufficient, or, one frame of input data may need to be output multiple times. In this way, you must initialize the re-sampling and libavutil-provided FIFO (libavutils/FIFO. h Declaration) to temporarily Save the data. For example, the frame size of AAC is 1024, and the frame size of MP3 is 1152. If this FIFO is not used, the generated MP3 file is faulty. * // you can specify rs_ctx = av_audio_resample_init (out_cctx-> channels, in_cctx-> channels, out_cctx-> sample_rate, in_cctx-> sample_rate, out_cctx-> sample_fmt, in_cctx-> sample_fmt, 16, 10, 0, 0.8); av1_obuffer * iofifo; iofifo = av_1_o_alloc (Limit * 2 ); av_init_packet (& in_packet); av_init_packet (& out_packet); While (av_read_frame (in_fctx, & in_packet)> = 0) {While (in_packet). Size> 0) {int used_size; decoded_size = bytes; used_size = avcodec_decode_audio3 (in_cctx, (int16_t *) decoded_buf, & decoded_size, & in_packet); If (used_size <0) {printf ("avcodec_decode_audio3 fail \ n"); exit (1);} int BS, frame_bytes; BS = audio_resample (rs_ctx, (short *) resample_buf, (short *) decoded_buf, decoded_size/(in_cctx-> channels * g_in_bytes); ret = av_1_o_generic_write (iofi Fo, (uint8_t *) resample_buf, BS * out_cctx-> channels * g_out_bytes, null); // ret = av_1_o_generic_write (iofifo, (uint8_t *) decoded_buf, in_cctx-> frame_size * in_cctx-> channels * g_out_bytes, null); frame_bytes = out_cctx-> frame_size * g_out_bytes * out_cctx-> channels; while (iofifo)> = frame_bytes) {ret = av_1_o_generic_read (iofifo, before_encoding_buf, frame_bytes, null); out_packet.size = avcod Outputs (out_cctx, (uint8_t *) output_buf, frame_bytes, (short *) bytes); outputs = (uint8_t *) output_buf; av_write_frame (out_fctx, & out_packet );} in_packet.size-= used_size; in_packet.data + = used_size;}/* write the trailer, if any */av_write_trailer (out_fctx); If (! (Out_fmt-> flags & avfmt_nofile) {/* close the output file */url_fclose (out_fctx-> Pb);} int output_decode_open (avcodeccontext * CC) {avcodec * codec; /* Find the Audio Encoder */codec = avcodec_find_encoder (CC-> codec_id); If (! Codec) {fprintf (stderr, "codec not found \ n"); exit (1);}/* Open It */If (avcodec_open (CC, codec) <0) {fprintf (stderr, "cocould not open audio codec \ n"); exit (1);} return 0;} avcodeccontext * output_decode_init (avcodeccontext * in_cctx, avformatcontext * out_fctx, int codec_id) {avcodeccontext * cc; avstream * st; ST = av_new_stream (out_fctx, 0); If (! St) {fprintf (stderr, "cocould not alloc stream \ n"); exit (1) ;}cc = ST-> codec; CC-> codec_id = (codecid) codec_id; CC-> codec_type = avmedia_type_audio;/* The sample_fmt format of the Google Keep recording notes is av_sample_fmt_flt */CC-> sample_fmt = av_sample_fmt_s16; // upload // CC> sample_fmt = in_cctx-> sample_fmt; If (in_cctx-> sample_fmt = av_sample_fmt_flt) {g_in_bytes = 4 ;} if (CC-> sample_fmt = av_sample_fmt_s16) {g_out_bytes = 2;} If (codec_id = codec_id_amr_nb) {CC-> channels = 1; CC-> bit_rate = 12200 ;} else {CC-> channels = in_cctx-> channels; CC-> bit_rate = in_cctx-> bit_rate;} // CC-> sample_rate = in_cctx-> sample_rate; // CC-> sample_rate = 44100; CC-> sample_rate = 8000; return cc ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.