声明:转载自 blog.fpzeng.com
1.首先写Makefile,由于ffmpeg提供了很多动态库,你首先需要指定gcc编译、链接的参数,这个可以用pkg-config命令来获取。
# use pkg-config for getting CFLAGS and LDLIBS FFMPEG_LIBS= libavformat \ libavcodec \ libavutil CFLAGS += -Wall -O2 -g CFLAGS += $(shell pkg-config --cflags $(FFMPEG_LIBS)) LDLIBS += $(shell pkg-config --libs $(FFMPEG_LIBS)) EXAMPLES= player \ OBJS=$(addsuffix .o,$(EXAMPLES)) # the following examples make explicit use of the math library #main: LDLIBS += -lm %: %.o $(CC) $< $(LDLIBS) -o $@ %.o: %.c $(CC) $< $(CFLAGS) -c -o $@ .phony: all help show clean all: $(OBJS) $(EXAMPLES) help: @echo "Makefile for ffmpeg and SDL Programs version 1.0" @echo "Copyright (C) 2012 Franken <zfpnuc@hotmail.com>" @echo "Usage: make [TARGET]" @echo "TARGETS:" @echo " all (=make) compile and link." @echo " clean clean objects and the executable file." @echo " show show variables (for debug use only)." @echo " help print this message." @echo "" @echo "Report bugs to <zfpnuc@hotmail.com>." show: @echo "CFLAGS :" $(CFLAGS) @echo "LDLIBS :" $(LDLIBS) clean: rm -rf $(EXAMPLES) $(OBJS)
2.然后再写一个最小的函数测试一下
#include "libavcodec/avcodec.h" #include "libavformat/avformat.h" int main(int argc, char *argv[]) { av_register_all(); }
3.使用make 可以编译,make show 可以看装载了哪些信息
4.这是一个带有debug信息的版本
5.更详细的例子(获取视频播放文件时长)
#include "libavcodec/avcodec.h" #include "libavformat/avformat.h" #include "libavutil/avstring.h" void print_error(const char *filename, int err) { char errbuf[128]; const char *errbuf_ptr = errbuf; if (av_strerror(err, errbuf, sizeof(errbuf)) < 0) errbuf_ptr = strerror(AVUNERROR(err)); av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, errbuf_ptr); } void print_duration(int seconds) { av_log(NULL,0,"duration [hh]:[mm]:[ss] %02d:%02d:%02d\n",\ seconds/60/60,seconds/60%60,seconds%60); } int main(int argc, char *argv[]) { int ret=0; AVFormatContext *ic = NULL; char filename[1024]; AVInputFormat *iformat = NULL; AVDictionary *format_opts = NULL; AVDictionaryEntry *t; int err; av_strlcpy(filename, argv[1], sizeof(filename)); av_register_all(); err = avformat_open_input(&ic, filename, iformat, &format_opts); if (err < 0) { print_error(filename, err); ret = -1; goto fail; } print_duration(ic->duration/1000/1000); fail: if (ic) { avformat_close_input(&ic); } return ret; }
备注:
1. http://blog.fpzeng.com/index.php/2012/05/ffplayer-2/
2.http://blog.fpzeng.com/index.php/2012/05/sdl/