1. demo源码 http://dranger.com/ffmpeg/tutorial01.html (由于版本太低会有诸多问题....)
2.首先建立目录ffmpeg,configure and make ffmpeg 源码。
3.由于tutorial01.c对应的ffmpeg的版本有些旧,有些宏定义和API的名字需要修改
- #include "avcodec.h" ---> #include "libavcodec/avcodec.h"
- #include "avformat.h" ---> #include "libavformat/avformat.h"
- CODEC_TYPE_VIDEO ---> AVMEDIA_TYPE_VIDEO
- if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0) --->
- if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0)
- dump_format(pFormatCtx, 0, argv[1], 0); ---> av_dump_format(pFormatCtx, 0, argv[1], 0);
- avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, packet.data, packet.size); --->
- avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
(1)
出现undefined reference to `img_convert'错误。解决方法:使用下面两个函数代替img_convert(。。。。):
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
// other codes
// Convert the image from its native format to RGB
sws_scale(img_convert_ctx, (
const
uint8_t*
const
*)pFrame->data, pFrame->linesize,
0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
|
在#include <stdio.h>下面加上:static struct SwsContext *img_convert_ctx;
以及 include swscale 库。
(2)对tutorial01.c增加一下内容
- #include "libswscale/swscale.h"
- #include "libswscale/swscale_internal.h"
- int img_convert(AVPicture *dst, int dst_pix_fmt,
- const AVPicture *src, int src_pix_fmt,
- int src_width, int src_height)
- {
- int w;
- int h;
- SwsContext *pSwsCtx;
- w = src_width;
- h = src_height;
- pSwsCtx = sws_getContext(w, h, src_pix_fmt,
- w, h, dst_pix_fmt,
- SWS_BICUBIC, NULL, NULL, NULL);
- sws_scale(pSwsCtx, src->data, src->linesize,
- 0, h, dst->data, dst->linesize);
- //这里释放掉pSwsCtx的内存
- return 0;
- }
gcc -o tutorial01 tutorial01.c -lavutil -lavformat -lavcodec -lz -lavutil -lswscale -lm
4. 编译报错:
- /home/liaowm/workspace/iPlayer/jni/ffmpeg/libavformat/udp.c:657: undefined reference to `pthread_cancel'
- /home/liaowm/workspace/iPlayer/jni/ffmpeg/libavformat/udp.c:658: undefined reference to `pthread_join'
- /usr/local/lib/libavformat.a(udp.o): In function `udp_open':
- /home/liaowm/workspace/iPlayer/jni/ffmpeg/libavformat/udp.c:545: undefined reference to `pthread_create'
- /usr/local/lib/libavcodec.a(pthread.o): In function `frame_thread_free':
- /home/liaowm/workspace/iPlayer/jni/ffmpeg/libavcodec/pthread.c:756: undefined reference to `pthread_join'
- /usr/local/lib/libavcodec.a(pthread.o): In function `thread_free':
- /home/liaowm/workspace/iPlayer/jni/ffmpeg/libavcodec/pthread.c:251: undefined reference to `pthread_join'
- /usr/local/lib/libavcodec.a(pthread.o): In function `frame_thread_init':
- /home/liaowm/workspace/iPlayer/jni/ffmpeg/libavcodec/pthread.c:873: undefined reference to `pthread_create'
- /usr/local/lib/libavcodec.a(pthread.o): In function `thread_init':
- /home/liaowm/workspace/iPlayer/jni/ffmpeg/libavcodec/pthread.c:339: undefined reference to `pthread_create'
5.编译错误undefined reference to `BZ2_bzDecompressInit'等错误,还要加上-lbz2库。
最终: gcc -o tutorial01 tutorial01.c -lavutil -lavformat -lavcodec -lz -lavutil -lswscale -lpthread -lbz2 -lm
编译通过。
6.执行 ./tutorial01 filename。
7.可能会出现“段错误”的问题,调试发现问题出现在format_open_input()函数。(参照源码中format_open_input()函数,可能需要初始化指针。)
8 bingo!