官网下载最新的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("