本文共 4221 字,大约阅读时间需要 14 分钟。
最近在学习FFMPEG,看了很多的例子都是在Windows下的,这里提供一个linux下的一个简单程序,由雷神的工程修改而来。
- #include <stdio.h>
- #define __STDC_CONSTANT_MACROS
- #include "avformat.h"
- #include "avcodec.h"
- #include "swscale.h"
-
- int main(int argc, char* argv[])
- {
- AVFormatContext *pFormatCtx;
- int i, videoindex;
- AVCodecContext *pCodecCtx;
- AVCodec *pCodec;
- AVFrame *pFrame,*pFrameYUV;
- uint8_t *out_buffer;
- AVPacket *packet;
- int y_size;
- int ret, got_picture;
- struct SwsContext *img_convert_ctx;
-
- char filepath[]="../testfile/school.flv";
-
- int frame_cnt;
-
- av_register_all();
- avformat_network_init();
- pFormatCtx = avformat_alloc_context();
-
- if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){
- printf("Couldn't open input stream.\n");
- return -1;
- }
- if(avformat_find_stream_info(pFormatCtx,NULL)<0){
- printf("Couldn't find stream information.\n");
- return -1;
- }
- videoindex=-1;
- for(i=0; i<pFormatCtx->nb_streams; i++)
- if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
- videoindex=i;
- break;
- }
- if(videoindex==-1){
- printf("Didn't find a video stream.\n");
- return -1;
- }
-
- pCodecCtx=pFormatCtx->streams[videoindex]->codec;
- pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
- if(pCodec==NULL){
- printf("Codec not found.\n");
- return -1;
- }
- if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
- printf("Could not open codec.\n");
- return -1;
- }
-
-
- printf("AVFormatContext AVInputFormat name = %s \n",pFormatCtx->iformat->name);
- printf("Number of elements in AVFormatContext.streams = %d \n",pFormatCtx->nb_streams);
- printf("Duration of the stream, in AV_TIME_BASE fractional = %d \n",pFormatCtx->duration);
- printf("Total stream bitrate in %d bit/s \n",pFormatCtx->bit_rate);
-
- printf("picture width = %d \n",pCodecCtx->width);
- printf("picture height = %d \n",pCodecCtx->height);
-
-
-
- pFrame=av_frame_alloc();
- pFrameYUV=av_frame_alloc();
- out_buffer=(uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
- avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
- packet=(AVPacket *)av_malloc(sizeof(AVPacket));
-
- printf("--------------- File Information ----------------\n");
- av_dump_format(pFormatCtx,0,filepath,0);
- printf("-------------------------------------------------\n");
-
- img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
- pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
-
- frame_cnt=0;
-
-
- while(av_read_frame(pFormatCtx, packet)>=0){
- if(packet->stream_index==videoindex){
-
-
- ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
- if(ret < 0){
- printf("Decode Error.\n");
- return -1;
- }
- if(got_picture){
- sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
- pFrameYUV->data, pFrameYUV->linesize);
-
- frame_cnt++;
-
- }
- }
- av_free_packet(packet);
- }
-
-
- sws_freeContext(img_convert_ctx);
-
- av_frame_free(&pFrameYUV);
- av_frame_free(&pFrame);
- avcodec_close(pCodecCtx);
- avformat_close_input(&pFormatCtx);
-
- return 0;
- }
Makefile 文件如下,头文件和库文件的目录会因为安装目录的不同而不同。 - OUT_APP = test
- INCLUDE_PATH = /usr/local/include/
- INCLUDE = -I$(INCLUDE_PATH)libavutil/ -I$(INCLUDE_PATH)libavdevice/ \
- -I$(INCLUDE_PATH)libavcodec/ -I$(INCLUDE_PATH)libswresample \
- -I$(INCLUDE_PATH)libavfilter/ -I$(INCLUDE_PATH)libavformat \
- -I$(INCLUDE_PATH)libswscale/
-
- FFMPEG_LIBS = -lavformat -lavutil -lavdevice -lavcodec -lswresample -lavfilter -lswscale
- SDL_LIBS =
- LIBS = $(FFMPEG_LIBS)$(SDL_LIBS)
-
- COMPILE_OPTS = $(INCLUDE)
- C = c
- OBJ = o
- C_COMPILER = cc
- C_FLAGS = $(COMPILE_OPTS) $(CPPFLAGS) $(CFLAGS)
-
- LINK = cc -o
- LINK_OPTS = -lz -lm -lpthread
- LINK_OBJ = echo_information.o
-
- .$(C).$(OBJ):
- $(C_COMPILER) -c $(C_FLAGS) $<
-
-
- $(OUT_APP): $(LINK_OBJ)
- $(LINK)$@ $(LINK_OBJ) $(LIBS) $(LINK_OPTS)
-
- clean:
- -rm -rf *.$(OBJ) $(OUT_APP) core *.core *~ include/*~
转载地址:http://svdxi.baihongyu.com/