Compile and use FFMPEG static Link Library in Linux

Source: Internet
Author: User

Operating System: Linux sles10sp1 i686

1. Download ffmpeg-0.8.14

Http://url.cn/ETZJSB

2. faac Compilation

Download link: http://url.cn/GBUh10

Reference

Http://www.blogjava.net/fly2008/archive/2009/10/14/298233.html

Http://comments.gmane.org/gmane.comp.video.ffmpeg.user/6840

Tar xvfz faac-1.28.tar.gzcd faacfind.-exec dos2unix {}\; // error in Bootstrap bad interpreter chmod 1.28 Bootstrap./Bootstrap./configure make install

After you execute make install, The faac library file will be copied to/usr/local/lib, And the header file will be copied to/usr/local/include.

3. Compile libmp 3lame

Download link: http://url.cn/EdaXLe

./configuremakemake install

After you execute make install, the library file of libmp 3lame will be copied to/usr/local/lib, And the header file will be copied to/usr/local/include.

4. Compile libgsm

Download link: http://url.cn/HVJBXO

Reference link: http://forums.droplinegnome.org/viewtopic.php? F = 8 & t = 4432

make make installcp ./lib/libgsm.a /usr/local/libmkdir /usr/local/include/gsmcp ./inc/gsm.h /usr/loacal/include/gsm

5. Compile libamr

Download link: http://url.cn/EAV8jH

Reference link:

Http://www.feiyan.info/13.html

Http://sourceforge.net/projects/opencore-amr? Source = DLP

./configuremakemake install

After you run make install, the library files of libopencore-amrnb and libopencore-amrwb are copied to/usr/local/lib, And the header files are copied to/usr/local/include.

6. Compile FFMPEG

./configure --disable-yasm --enable-nonfree --enable-libfaac --enable-libgsm --enable-version3 --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwbmake make install

After you run make install, the related library files of FFMPEG are copied to/usr/local/lib, And the header files are copied to/usr/local/include.

7. Test Procedure

: Http://url.cn/E7kfeV

Including audio test files, makefile, audio_convertor.c

Audio_convertor.c file:

/* Audio_convertor.c audio transcoding Program */# 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 (avcodeccont EXT * CC); int main (INT argc, char * argv []) {avformatcontext * in_fctx; avcodeccontext * in_cctx; avcodec * in_codec; const char * in_filename; const char * out_filename; char * decoded_buf; char * output_buf; char * resample_buf; char * before_encoding_buf; int ret = 0; If (argc! = 3) {printf (". /audio_convertor input ouput \ n "); Return-1;} // in_filename = ".. /input. AAC "; // out_filename =" output/aac2mp3.mp3 "; in_filename = argv [1]; out_filename = argv [2]; decoded_buf = (char *) av_malloc (bytes ); output_buf = (char *) av_malloc (avcodec_max_audio_frame_size); resample_buf = (char *) av_malloc (bytes); before_encoding_buf = (char *) av_malloc (Token); avcodec_register_all (); av_register_all (); in_fctx = avformat_alloc_context (); ret = equals (& 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 the 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_id = codec_id; CC-> codec_type = avmedia_type_audio;/* The sample_fmt format of Google Keep recording notes is av_sample_fmt_flt */CC-> sample_fmt = example; // 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 ;}

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.