Using the FFmpeg transcoding PCM to AAC format

Source: Internet
Author: User

Preparatory work

To the official website to download the source code to compile their own library, or directly from the official website to download the library has been written, because this example is the window platform development, and the official website has a compiled library, so directly download the required library files can be compiled. : You need to download two compressed packages, dev version and shared version, where dev version is the header and Lib library files, and Gkfx contains the DLL files required to run.

After the download is completed, we need to introduce these header files, static libraries, and dynamic link libraries into our project, but under Windows, the header file will be used in addition to the ffmpeg provided, but also to use a further three files: inttypes.h,stdint.h,_ Mingw.h, these three files are placed in the root directory of the FFmpeg header file.


Project Engineering code Example

C + + code needs to add this!! Otherwise, the extern "C" {#include "libavcodec\avcodec.h" #include "libavformat\avformat.h" #include "libswscale\swscale.h" of the function cannot be found Linked to the FFmpeg library, I put all that might be needed to write here #pragma comment (lib, "Ws2_32.lib") #pragma comment (lib, "Avcodec.lib") #pragma comment (l IB, "Avdevice.lib") #pragma comment (lib, "Avfilter.lib") #pragma comment (lib, "Avformat.lib") #pragma comment (lib, " Avutil.lib ") #pragma comment (lib," Swresample.lib ") #pragma comment (lib," Swscale.lib ")};int main (int argc, char* argv[] ) {avformatcontext* pformatctx; Avoutputformat* FMT; avstream* Audio_st; avcodeccontext* Pcodecctx; avcodec* pcodec;uint8_t* Frame_buf; avframe* frame;int size; FILE *filein = fopen ("BEYOND.PCM", "RB"), char* fileout = "BEYOND.AAC";//Initialize register ffmpeg for use Av_register_all ();// Decode file Format avformat_alloc_output_context2 (&pformatctx, NULL, NULL, fileout); FMT = pformatctx->oformat;//Note the output path if ( Avio_open (&AMP;PFORMATCTX-&GT;PB, Fileout, Avio_flag_read_write) < 0) {printf ("Output File open failed! \ n "); return-1;} Audio_st = Avformat_new_strEAM (pformatctx, 0), if (Audio_st = = NULL) {printf ("New stream failed!!! \ n "); return-1;} Set transcoding information pcodecctx = audio_st->codec;pcodecctx->codec_id = Fmt->audio_codec;pcodecctx->codec_type = AVMEDIA_TYPE_AUDIO;PCODECCTX-&GT;SAMPLE_FMT = av_sample_fmt_s16;pcodecctx->sample_rate= 44100;// Audio sample Rate Pcodecctx->channel_layout=av_ch_layout_stereo;pcodecctx->channels = Av_get_channel_layout_nb_ Channels (pcodecctx->channel_layout);p codecctx->bit_rate = 64000;//Audio bit rate//debug output format information Av_dump_format ( Pformatctx, 0, Fileout, 1);p codec = Avcodec_find_encoder (pcodecctx->codec_id), if (!pcodec) {printf ("The encoder required to enter the file cannot be found!") \ n "); return-1;} if (Avcodec_open2 (Pcodecctx, Pcodec,null) < 0) {printf ("Open encoder failed!\n"); return-1;} frame = Avcodec_alloc_frame (); frame->nb_samples= pcodecctx->frame_size;frame->format= pCodecCtx-> Sample_fmt;size = Av_samples_get_buffer_size (NULL, pcodecctx->channels,pcodecctx->frame_size,pcodecctx-> SAMPLE_FMT, 1); frame_buf = (uint8_t *) av_malloc (size); Avcodec_fill_audIo_frame (frame, pcodecctx->channels, pcodecctx->sample_fmt, (const uint8_t*) frame_buf, size, 1);// Fill the output header file information Avformat_write_header (pformatctx,null); Avpacket Pkt;av_new_packet (&pkt,size); for (int i=0;; i++) {//Read into PCMIF (Fread (frame_buf, 1, size, Filein) < 0) {print F ("File read error! \ n "); return-1;} else if (feof (Filein)) {break;}  Frame->data[0] = frame_buf; Sample signal Frame->pts=i*100;int got_frame=0;//encoded INT ret = Avcodec_encode_audio2 (Pcodecctx, &pkt,frame, &got_ frame); if (Ret < 0) {printf ("Coding Error! \ n "); return-1;} if (got_frame==1) {pkt.stream_index = Audio_st->index;ret = Av_write_frame (Pformatctx, &AMP;PKT); Av_free_packet ( &AMP;PKT);}} Write file Tail av_write_trailer (PFORMATCTX);//cleanup if (audio_st) {avcodec_close (AUDIO_ST-&GT;CODEC); Av_free (frame); Av_free ( FRAME_BUF);} Avio_close (PFORMATCTX-&GT;PB), Avformat_free_context (Pformatctx), fclose (Filein);p rintf ("transcoding Complete!!! \ n "); return 0;}

Note

Because FFmpeg will be fine to modify the interface, so different versions of FFmpeg may compile but, anyway, January 23, 2015 17:29:31 can be compiled through.

ReferenceThe simplest ffmpeg-based audio encoder (PCM encoded as AAC)

Using the FFmpeg transcoding PCM to AAC format

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.