记录下centos下 ffmpeg项目编译参数

官网下载最新的ffmpeg源文件,直接./confiure --prefix=path_to_install && make && make install ,期间没有什么问题。(但是在最后想生成动态库的时候有问题,暂时不去解决了,静态的先凑合用吧)

但是在写了一个测试文件去测试的时候,总是链接错误,不是缺少这个库就是av***未定义的引用。少的库直接加-l就行了,但是av***里面未定义引用我就郁闷了,最后发现可能是ffmpeg库的引用顺序不对造成的。

记录下ffmpeg库引用顺序,反正这个顺序在我这里是可以用的。最后附带下测试代码

#! /bin/bash
BIN_PATH=/home/mico/bin
export LD_LIBRARY_PATH=$BIN_PATH/lib:/usr/local/lib64:/usr/lib64

gcc test_ffmpeg.c -L $BIN_PATH/lib -L /usr/local/lib -L /usr/lib64 -I $BIN_PATH/include -lavfilter -lavformat -lavdevice -lavcodec -lswscale -lavutil -lswresample -llzma -lz -lbz2 -pthread -lm -g -o test_ffmpeg.out
    
echo "well done"
  1 /*
  2  * Copyright (c) 2010 Nicolas George
  3  * Copyright (c) 2011 Stefano Sabatini
  4  *
  5  * Permission is hereby granted, free of charge, to any person obtaining a copy
  6  * of this software and associated documentation files (the "Software"), to deal
  7  * in the Software without restriction, including without limitation the rights
  8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9  * copies of the Software, and to permit persons to whom the Software is
 10  * furnished to do so, subject to the following conditions:
 11  *
 12  * The above copyright notice and this permission notice shall be included in
 13  * all copies or substantial portions of the Software.
 14  *
 15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21  * THE SOFTWARE.
 22  */
 23 /**
 24  * @file
 25  * API example for decoding and filtering
 26  * @example filtering_video.c
 27  */
 28 #define _XOPEN_SOURCE 600 /* for usleep */
 29 #include <unistd.h>
 30 #include <stdio.h>
 31 #include <stdlib.h>
 32 #include <libavcodec/avcodec.h>
 33 #include <libavformat/avformat.h>
 34 #include <libavfilter/buffersink.h>
 35 #include <libavfilter/buffersrc.h>
 36 #include <libavutil/opt.h>
 37 #define ENABLE_DISPLAY 0
 38 #define ENABLE_YUV 1
 39 const char *filter_descr = "scale=78:24,transpose=cclock";
 40 /* other way:
 41    scale=78:24 [scl]; [scl] transpose=cclock // assumes "[in]" and "[out]" to be input output pads respectively
 42  */
 43 static AVFormatContext *fmt_ctx;
 44 static AVCodecContext *dec_ctx;
 45 AVFilterContext *buffersink_ctx;
 46 AVFilterContext *buffersrc_ctx;
 47 AVFilterGraph *filter_graph;
 48 static int video_stream_index = -1;
 49 static int64_t last_pts = AV_NOPTS_VALUE;
 50 
 51 #if ENABLE_YUV
 52     FILE *fp_yuv;
 53 #endif
 54 
 55 static int open_input_file(const char *filename)
 56 {    
 57     int ret;
 58     AVCodec *dec;
 59     
 60 #if ENABLE_YUV
 61     fp_yuv=fopen("output.yuv","wb+");
 62 #endif
 63 
 64     if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
 65         av_log(NULL, AV_LOG_ERROR, "Cannot open input file
");
 66         return ret;
 67     }
 68     if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
 69         av_log(NULL, AV_LOG_ERROR, "Cannot find stream information
");
 70         return ret;
 71     }
 72     /* select the video stream */
 73     ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
 74     if (ret < 0) {
 75         av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file
");
 76         return ret;
 77     }
 78     video_stream_index = ret;
 79     /* create decoding context */
 80     dec_ctx = avcodec_alloc_context3(dec);
 81     if (!dec_ctx)
 82         return AVERROR(ENOMEM);
 83     avcodec_parameters_to_context(dec_ctx, fmt_ctx->streams[video_stream_index]->codecpar);
 84     /* init the video decoder */
 85     if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
 86         av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder
");
 87         return ret;
 88     }
 89     return 0;
 90 }
 91 
 92 
 93 static int init_filters(const char *filters_descr)
 94 {
 95     char args[512];
 96     int ret = 0;
 97     const AVFilter *buffersrc  = avfilter_get_by_name("buffer");
 98     const AVFilter *buffersink = avfilter_get_by_name("buffersink");
 99     AVFilterInOut *outputs = avfilter_inout_alloc();
100     AVFilterInOut *inputs  = avfilter_inout_alloc();
101     AVRational time_base = fmt_ctx->streams[video_stream_index]->time_base;
102     enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
103     filter_graph = avfilter_graph_alloc();
104     if (!outputs || !inputs || !filter_graph) {
105         ret = AVERROR(ENOMEM);
106         goto end;
107     }
108     /* buffer video source: the decoded frames from the decoder will be inserted here. */
109     snprintf(args, sizeof(args),
110             "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
111             dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
112             time_base.num, time_base.den,
113             dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);
114     ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
115                                        args, NULL, filter_graph);
116     if (ret < 0) {
117         av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source
");
118         goto end;
119     }
120     /* buffer video sink: to terminate the filter chain. */
121     ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
122                                        NULL, NULL, filter_graph);
123     if (ret < 0) {
124         av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink
");
125         goto end;
126     }
127     ret = av_opt_set_int_list(buffersink_ctx, "pix_fmts", pix_fmts,
128                               AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
129     if (ret < 0) {
130         av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format
");
131         goto end;
132     }
133     /*
134      * Set the endpoints for the filter graph. The filter_graph will
135      * be linked to the graph described by filters_descr.
136      */
137     /*
138      * The buffer source output must be connected to the input pad of
139      * the first filter described by filters_descr; since the first
140      * filter input label is not specified, it is set to "in" by
141      * default.
142      */
143     outputs->name       = av_strdup("in");
144     outputs->filter_ctx = buffersrc_ctx;
145     outputs->pad_idx    = 0;
146     outputs->next       = NULL;
147     /*
148      * The buffer sink input must be connected to the output pad of
149      * the last filter described by filters_descr; since the last
150      * filter output label is not specified, it is set to "out" by
151      * default.
152      */
153     inputs->name       = av_strdup("out");
154     inputs->filter_ctx = buffersink_ctx;
155     inputs->pad_idx    = 0;
156     inputs->next       = NULL;
157     if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr,
158                                     &inputs, &outputs, NULL)) < 0)
159         goto end;
160     if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
161         goto end;
162 end:
163     avfilter_inout_free(&inputs);
164     avfilter_inout_free(&outputs);
165     return ret;
166 }
167 static void display_frame(const AVFrame *frame, AVRational time_base)
168 {
169     int x, y;
170     uint8_t *p0, *p;
171     int64_t delay;
172     if (frame->pts != AV_NOPTS_VALUE) {
173         if (last_pts != AV_NOPTS_VALUE) {
174             /* sleep roughly the right amount of time;
175              * usleep is in microseconds, just like AV_TIME_BASE. */
176             delay = av_rescale_q(frame->pts - last_pts,
177                                  time_base, AV_TIME_BASE_Q);
178             if (delay > 0 && delay < 1000000)
179                 usleep(delay);
180         }
181         last_pts = frame->pts;
182     }
183     /* Trivial ASCII grayscale display. */
184     p0 = frame->data[0];
185     puts("33c");
186     for (y = 0; y < frame->height; y++) {
187         p = p0;
188         for (x = 0; x < frame->width; x++)
189             putchar(" .-+#"[*(p++) / 52]);
190         putchar('
');
191         p0 += frame->linesize[0];
192     }
193     fflush(stdout);
194 }
195 
196 
197 #if ENABLE_YUV
198 static void save_yuv(const AVFrame *pFrame_out) 
199 {    
200     int i=0;
201     if (pFrame_out->format==AV_PIX_FMT_YUV420P) {
202         //Y, U, V
203         for(i=0;i<pFrame_out->height;i++){
204             fwrite(pFrame_out->data[0]+pFrame_out->linesize[0]*i,1,pFrame_out->width,fp_yuv);
205         }
206         for(i=0;i<pFrame_out->height/2;i++){
207             fwrite(pFrame_out->data[1]+pFrame_out->linesize[1]*i,1,pFrame_out->width/2,fp_yuv);
208         }
209         for(i=0;i<pFrame_out->height/2;i++){
210             fwrite(pFrame_out->data[2]+pFrame_out->linesize[2]*i,1,pFrame_out->width/2,fp_yuv);
211         }
212     }    
213 }
214 #endif
215 
216 
217 int main(int argc, char **argv)
218 {
219     int ret;
220     AVPacket packet;
221     AVFrame *frame;
222     AVFrame *filt_frame;
223     if (argc != 2) {
224         fprintf(stderr, "Usage: %s file
", argv[0]);
225         exit(1);
226     }
227     frame = av_frame_alloc();
228     filt_frame = av_frame_alloc();
229     if (!frame || !filt_frame) {
230         perror("Could not allocate frame");
231         exit(1);
232     }
233     if ((ret = open_input_file(argv[1])) < 0)
234         goto end;
235     if ((ret = init_filters(filter_descr)) < 0)
236         goto end;
237     /* read all packets */
238     while (1) {
239         if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
240             break;
241         if (packet.stream_index == video_stream_index) {
242             ret = avcodec_send_packet(dec_ctx, &packet);
243             if (ret < 0) {
244                 av_log(NULL, AV_LOG_ERROR, "Error while sending a packet to the decoder
");
245                 break;
246             }
247             while (ret >= 0) {
248                 ret = avcodec_receive_frame(dec_ctx, frame);
249                 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
250                     break;
251                 } else if (ret < 0) {
252                     av_log(NULL, AV_LOG_ERROR, "Error while receiving a frame from the decoder
");
253                     goto end;
254                 }
255                 frame->pts = frame->best_effort_timestamp;
256                 /* push the decoded frame into the filtergraph */
257                 if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 0) {
258                     av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph
");
259                     break;
260                 }
261                 /* pull filtered frames from the filtergraph */
262                 while (1) {
263                     ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
264                     if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
265                         break;
266                     if (ret < 0)
267                         goto end;
268 #if ENABLE_DISPLAY
269                     display_frame(filt_frame, buffersink_ctx->inputs[0]->time_base);
270 #endif
271 #if ENABLE_YUV
272                     save_yuv(filt_frame);
273 #endif
274                     av_frame_unref(filt_frame);
275                 }
276                 av_frame_unref(frame);
277             }
278         }
279         av_packet_unref(&packet);
280     }
281 end:
282 #if ENABLE_YUV
283     fclose(fp_yuv);
284 #endif
285     avfilter_graph_free(&filter_graph);
286     avcodec_free_context(&dec_ctx);
287     avformat_close_input(&fmt_ctx);
288     av_frame_free(&frame);
289     av_frame_free(&filt_frame);
290     if (ret < 0 && ret != AVERROR_EOF) {
291         fprintf(stderr, "Error occurred: %s
", av_err2str(ret));
292         exit(1);
293     }
294     exit(0);
295 }
原文地址:https://www.cnblogs.com/micoblog/p/12629706.html