看了很多ffmpeg mingw的编译的文章,什么msys中使用--toolchain=msvc的编译方法,加什么c99conv.exe和c99wrap.exe文件到D:\MinGW\msys\1.0\bin还是无法解决msvc报c99不兼容的问题。经过一番研究,发现使用mingw msys编译后使用并使用它的静态库就可以解决兼容性问题,取出mingw中的libws2_32.a libiconv.a libgcc.a libmingwex.a libmoldname100.a加入到vs工程即可。
1,ffmpeg官网:
http://ffmpeg.org/
2,MinGW :
http://www.mingw.org/ 下载mingw-get-setup.exe(包含msys),安装后把D:\MinGW\msys\1.0\bin;D:\MinGW\bin加入到环境变量里面。
3,C99头文件inttypes.h和stdint.h :
http://code.google.com/p/msinttypes/downloads/list 下载msinttypes-r26.zip(国内请翻墙下载,或百度搜索别人下载的资源),将此两个头文件复制到: D:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include 目录下( 自行修改为自己的安装目录 );
4,yasm :
http://yasm.tortall.net/, yasm-1.3.0-win32.exe改名为yasm.exe,复制到:d:/MinGW/msys/1.0/bin 目录下;
5
.打开d:MinGW/msys/1.0/msys.bat进入命令行模式,cd到ffmpeg工程目录:
ffmpeg_compile.sh:
#!/bin/sh
MINGW_PAHT=/d/MinGW/lib
INSTALL_PATH=_lib_install
make distclean
#//static lib
./configure --enable-static --prefix=$INSTALL_PATH
#//dynamic lib
#./configure --enable-shared --prefix=$INSTALL_PATH
make -j $NUMBER_OF_PROCESSORS
make install
cd $INSTALL_PATH
cp $MINGW_PAHT/libws2_32.a lib/
cp $MINGW_PAHT/libiconv.a lib/
cp $MINGW_PAHT/libpthread.a lib/
cp $MINGW_PAHT/libmingwex.a lib/
cp $MINGW_PAHT/libmoldname100.a lib/
cp $MINGW_PAHT/gcc/mingw32/5.3.0/libgcc.a lib/
执行完拷贝_lib_install到vs工程就可以正常使用mingw编译的ffmpeg了。
使用时注意添加下面的语句:
#ifdef __cplusplus
extern "C" {
#endif
#define __STDC_CONSTANT_MACROS
#ifdef _STDINT_H
#undef _STDINT_H
#endif
#include <stdint.h>
#ifdef __cplusplus
}
#endif
#pragma comment (lib, "libavcodec.a")
#pragma comment (lib, "libavdevice.a")
#pragma comment (lib, "libavfilter.a")
#pragma comment (lib, "libavformat.a")
#pragma comment (lib, "libavutil.a")
#pragma comment (lib, "libswresample.a")
#pragma comment (lib, "libswscale.a")
//*** mingw lib ***//
#pragma comment (lib, "libws2_32.a")
#pragma comment (lib, "libiconv.a")
#pragma comment (lib, "libgcc.a")
#pragma comment (lib, "libpthread.a")
#pragma comment (lib, "libmingwex.a")
#pragma comment (lib, "libmoldname100.a") // _stricoll
例程验证:ffmpeg_rtsppull.cpp:
#ifdef __cplusplus
extern "C" {
#endif
#define __STDC_CONSTANT_MACROS
#ifdef _STDINT_H
#undef _STDINT_H
#endif
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <libavcodec/avcodec.h>
#include <libavcodec/avfft.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#ifdef __cplusplus
}
#endif
#pragma comment (lib, "libavcodec.a")
#pragma comment (lib, "libavdevice.a")
#pragma comment (lib, "libavfilter.a")
#pragma comment (lib, "libavformat.a")
#pragma comment (lib, "libavutil.a")
#pragma comment (lib, "libswresample.a")
#pragma comment (lib, "libswscale.a")
//*** mingw lib ***//
//#pragma comment (lib, "ws2_32.lib")
#pragma comment (lib, "libws2_32.a")
#pragma comment (lib, "libiconv.a")
#pragma comment (lib, "libgcc.a")
#pragma comment (lib, "libpthread.a")
#pragma comment (lib, "libmingwex.a")
#pragma comment (lib, "libmoldname100.a") // _stricoll
AVFormatContext *i_fmt_ctx;
AVStream *i_video_stream;
AVFormatContext *o_fmt_ctx;
AVStream *o_video_stream;
int main(int argc, char **argv)
{
avcodec_register_all();
av_register_all();
avformat_network_init();
/* should set to NULL so that avformat_open_input() allocate a new one */
i_fmt_ctx = NULL;
const char *filename = argv[2];
if (avformat_open_input(&i_fmt_ctx, argv[1], NULL, NULL)!=0)
{
fprintf(stderr, "could not open input file\n");
return -1;
}
if (avformat_find_stream_info(i_fmt_ctx, NULL)<0)
{
fprintf(stderr, "could not find stream info\n");
return -1;
}
//av_dump_format(i_fmt_ctx, 0, argv[1], 0);
/* find first video stream */
for (unsigned i=0; i<i_fmt_ctx->nb_streams; i++)
{
if (i_fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
i_video_stream = i_fmt_ctx->streams[i];
break;
}
}
if (i_video_stream == NULL)
{
fprintf(stderr, "didn't find any video stream\n");
return -1;
}
avformat_alloc_output_context2(&o_fmt_ctx, NULL, NULL, filename);
/*
* since all input files are supposed to be identical (framerate, dimension, color format, ...)
* we can safely set output codec values from first input file
*/
o_video_stream = avformat_new_stream(o_fmt_ctx, NULL);
{
AVCodecContext *c;
c = o_video_stream->codec;
c->bit_rate = 400000;
c->codec_id = i_video_stream->codec->codec_id;
c->codec_type = i_video_stream->codec->codec_type;
c->time_base.num = i_video_stream->time_base.num;
c->time_base.den = i_video_stream->time_base.den;
fprintf(stderr, "time_base.num = %d time_base.den = %d\n", c->time_base.num, c->time_base.den);
c->width = i_video_stream->codec->width;
c->height = i_video_stream->codec->height;
c->pix_fmt = i_video_stream->codec->pix_fmt;
printf("%d %d %d", c->width, c->height, c->pix_fmt);
c->flags = i_video_stream->codec->flags;
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
c->me_range = i_video_stream->codec->me_range;
c->max_qdiff = i_video_stream->codec->max_qdiff;
c->qmin = i_video_stream->codec->qmin;
c->qmax = i_video_stream->codec->qmax;
c->qcompress = i_video_stream->codec->qcompress;
}
avio_open(&o_fmt_ctx->pb, filename, AVIO_FLAG_WRITE);
avformat_write_header(o_fmt_ctx, NULL);
int last_pts = 0;
int last_dts = 0;
int64_t pts, dts;
while (1)
{
AVPacket i_pkt;
av_init_packet(&i_pkt);
i_pkt.size = 0;
i_pkt.data = NULL;
if (av_read_frame(i_fmt_ctx, &i_pkt) <0 )
break;
/*
* pts and dts should increase monotonically
* pts should be >= dts
*/
i_pkt.flags |= AV_PKT_FLAG_KEY;
pts = i_pkt.pts;
i_pkt.pts += last_pts;
dts = i_pkt.dts;
i_pkt.dts += last_dts;
i_pkt.stream_index = 0;
//printf("%lld %lld\n", i_pkt.pts, i_pkt.dts);
static int num = 1;
printf("frame %d\n", num++);
av_interleaved_write_frame(o_fmt_ctx, &i_pkt);
//av_free_packet(&i_pkt);
//av_init_packet(&i_pkt);
}
last_dts += dts;
last_pts += pts;
avformat_close_input(&i_fmt_ctx);
av_write_trailer(o_fmt_ctx);
avcodec_close(o_fmt_ctx->streams[0]->codec);
av_freep(&o_fmt_ctx->streams[0]->codec);
av_freep(&o_fmt_ctx->streams[0]);
avio_close(o_fmt_ctx->pb);
av_free(o_fmt_ctx);
return 0;
}
如果是mingw中使用要链接这两个库 -lws2_32 -liconv,比如:
gcc
ffmpeg_rtsppull.cpp -o
ffmpeg_rtsppull -I_lib_install/include -I. -L_lib_install/lib -lavformat -lavcodec -lavdevice -lavfilter -lavutil -lswresample -lswscale -lws2_32 -liconv -lpthread