一、环境准备

ffmpeg下载:http://www.ffmpeg.org/download.html

x264下载:http://download.videolan.org/x264/snapshots/

yasm下载:http://yasm.tortall.net/Download.html

二、编译

1、编译yasm。最新的x264,要求yasm1.2以上

./configure --prefix=/usr/local/yasm

make

make install

2、解压x264,进入目录,输入:

./configure --prefix=/usr/local/x264 --enable-shared --enable-static --enable-yasm

make

make install

由于是手动安装的yasm,下面继续的时候,可能会报yasm找不到,需在/etc/profile

export PATH=$PATH:/usr/local/yasm/bin

之后

source  /etc/profile

3、解压ffmpeg,进入目录,

然后安装ffmpeg,ffmpeg有许多依赖包,需要一个一个先安装

apt-get install libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libxext-dev libxfixes-dev

输入:

./configure --prefix=/usr/local/ffmpeg --enable-libmp3lame --enable-libvorbis --enable-gpl --enable-version3 --enable-nonfree --enable-pthreads --enable-libfaac --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libxvid --enable-postproc --enable-ffserver --enable-ffplay --enable-shared --extra-cflags=-I/usr/local/x264/include --extra-ldflags=-L/usr/local/x264/lib
可能会提示缺少库,缺啥装啥

make

make install

三、配置环境变量及库路径

首先是命令的路径,编辑/etc/profile

export PATH=$PATH:/usr/local/ffmpeg/bin:/usr/local/yasm/bin:/usr/local/x264/bin
 
其次是链接库路径,编辑/etc/ld.so.conf
/usr/local/ffmpeg/lib
/usr/local/x264/lib
之后执行 sudo ldconfig
编译器默认搜索路径并不包含这两个目录,虽然这里设置了配置文件,但在编译的时候也会报错,仍然需要
-L/usr/local/ffmpeg/lib -L/usr/local/x264/lib来链接库
为了简化,可以直接将

/usr/local/ffmpeg/lib
/usr/local/x264/lib这两个目录中的.so文件直接考到/usr/local/lib目录,一劳永逸
 
四、在eclipse下搭建一个ffmpeg工程
1.首先建立一个空c工程
2.设置包含路径
3.设置链接目录及库
具体包括:ffmpeg的所有库:avcodec、avdevice、avfilter、avformat、avutil、swresample、swscale还有四个必须的库:pthread、m,x264和mp3lame。其中pthread是Linux系统进程库,m是数学库、x264是H264编码库,mp3lame是mp3的编码库
4.main.c
  1. /**
  2. * @file
  3. * libavcodec API use example.
  4. *
  5. * @example decoding_encoding.c
  6. * Note that libavcodec only handles codecs (mpeg, mpeg4, etc...),
  7. * not file formats (avi, vob, mp4, mov, mkv, mxf, flv, mpegts, mpegps, etc...). See library 'libavformat' for the
  8. * format handling
  9. */
  10.  
  11. #include <math.h>
  12.  
  13. #include <libavutil/opt.h>
  14. #include <libavcodec/avcodec.h>
  15. #include <libavutil/channel_layout.h>
  16. #include <libavutil/common.h>
  17. #include <libavutil/imgutils.h>
  18. #include <libavutil/mathematics.h>
  19. #include <libavutil/samplefmt.h>
  20.  
  21. #define INBUF_SIZE 4096
  22. #define AUDIO_INBUF_SIZE 20480
  23. #define AUDIO_REFILL_THRESH 4096
  24.  
  25. /* check that a given sample format is supported by the encoder */
  26. static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)
  27. {
  28. const enum AVSampleFormat *p = codec->sample_fmts;
  29.  
  30. while (*p != AV_SAMPLE_FMT_NONE) {
  31. if (*p == sample_fmt)
  32. return ;
  33. p++;
  34. }
  35. return ;
  36. }
  37.  
  38. /* just pick the highest supported samplerate */
  39. static int select_sample_rate(AVCodec *codec)
  40. {
  41. const int *p;
  42. int best_samplerate = ;
  43.  
  44. if (!codec->supported_samplerates)
  45. return ;
  46.  
  47. p = codec->supported_samplerates;
  48. while (*p) {
  49. best_samplerate = FFMAX(*p, best_samplerate);
  50. p++;
  51. }
  52. return best_samplerate;
  53. }
  54.  
  55. /* select layout with the highest channel count */
  56. static int select_channel_layout(AVCodec *codec)
  57. {
  58. const uint64_t *p;
  59. uint64_t best_ch_layout = ;
  60. int best_nb_channels = ;
  61.  
  62. if (!codec->channel_layouts)
  63. return AV_CH_LAYOUT_STEREO;
  64.  
  65. p = codec->channel_layouts;
  66. while (*p) {
  67. int nb_channels = av_get_channel_layout_nb_channels(*p);
  68.  
  69. if (nb_channels > best_nb_channels) {
  70. best_ch_layout = *p;
  71. best_nb_channels = nb_channels;
  72. }
  73. p++;
  74. }
  75. return best_ch_layout;
  76. }
  77.  
  78. /*
  79. * Audio encoding example
  80. */
  81. static void audio_encode_example(const char *filename)
  82. {
  83. AVCodec *codec;
  84. AVCodecContext *c= NULL;
  85. AVFrame *frame;
  86. AVPacket pkt;
  87. int i, j, k, ret, got_output;
  88. int buffer_size;
  89. FILE *f;
  90. uint16_t *samples;
  91. float t, tincr;
  92.  
  93. printf("Encode audio file %s\n", filename);
  94.  
  95. /* find the MP2 encoder */
  96. codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
  97. if (!codec) {
  98. fprintf(stderr, "Codec not found\n");
  99. exit();
  100. }
  101.  
  102. c = avcodec_alloc_context3(codec);
  103. if (!c) {
  104. fprintf(stderr, "Could not allocate audio codec context\n");
  105. exit();
  106. }
  107.  
  108. /* put sample parameters */
  109. c->bit_rate = ;
  110.  
  111. /* check that the encoder supports s16 pcm input */
  112. c->sample_fmt = AV_SAMPLE_FMT_S16;
  113. if (!check_sample_fmt(codec, c->sample_fmt)) {
  114. fprintf(stderr, "Encoder does not support sample format %s",
  115. av_get_sample_fmt_name(c->sample_fmt));
  116. exit();
  117. }
  118.  
  119. /* select other audio parameters supported by the encoder */
  120. c->sample_rate = select_sample_rate(codec);
  121. c->channel_layout = select_channel_layout(codec);
  122. c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
  123.  
  124. /* open it */
  125. if (avcodec_open2(c, codec, NULL) < ) {
  126. fprintf(stderr, "Could not open codec\n");
  127. exit();
  128. }
  129.  
  130. f = fopen(filename, "wb");
  131. if (!f) {
  132. fprintf(stderr, "Could not open %s\n", filename);
  133. exit();
  134. }
  135.  
  136. /* frame containing input raw audio */
  137. frame = av_frame_alloc();
  138. if (!frame) {
  139. fprintf(stderr, "Could not allocate audio frame\n");
  140. exit();
  141. }
  142.  
  143. frame->nb_samples = c->frame_size;
  144. frame->format = c->sample_fmt;
  145. frame->channel_layout = c->channel_layout;
  146.  
  147. /* the codec gives us the frame size, in samples,
  148. * we calculate the size of the samples buffer in bytes */
  149. buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,
  150. c->sample_fmt, );
  151. if (buffer_size < ) {
  152. fprintf(stderr, "Could not get sample buffer size\n");
  153. exit();
  154. }
  155. samples = av_malloc(buffer_size);
  156. if (!samples) {
  157. fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",
  158. buffer_size);
  159. exit();
  160. }
  161. /* setup the data pointers in the AVFrame */
  162. ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
  163. (const uint8_t*)samples, buffer_size, );
  164. if (ret < ) {
  165. fprintf(stderr, "Could not setup audio frame\n");
  166. exit();
  167. }
  168.  
  169. /* encode a single tone sound */
  170. t = ;
  171. tincr = * M_PI * 440.0 / c->sample_rate;
  172. for (i = ; i < ; i++) {
  173. av_init_packet(&pkt);
  174. pkt.data = NULL; // packet data will be allocated by the encoder
  175. pkt.size = ;
  176.  
  177. for (j = ; j < c->frame_size; j++) {
  178. samples[*j] = (int)(sin(t) * );
  179.  
  180. for (k = ; k < c->channels; k++)
  181. samples[*j + k] = samples[*j];
  182. t += tincr;
  183. }
  184. /* encode the samples */
  185. ret = avcodec_encode_audio2(c, &pkt, frame, &got_output);
  186. if (ret < ) {
  187. fprintf(stderr, "Error encoding audio frame\n");
  188. exit();
  189. }
  190. if (got_output) {
  191. fwrite(pkt.data, , pkt.size, f);
  192. av_free_packet(&pkt);
  193. }
  194. }
  195.  
  196. /* get the delayed frames */
  197. for (got_output = ; got_output; i++) {
  198. ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output);
  199. if (ret < ) {
  200. fprintf(stderr, "Error encoding frame\n");
  201. exit();
  202. }
  203.  
  204. if (got_output) {
  205. fwrite(pkt.data, , pkt.size, f);
  206. av_free_packet(&pkt);
  207. }
  208. }
  209. fclose(f);
  210.  
  211. av_freep(&samples);
  212. av_frame_free(&frame);
  213. avcodec_close(c);
  214. av_free(c);
  215. }
  216.  
  217. /*
  218. * Audio decoding.
  219. */
  220. static void audio_decode_example(const char *outfilename, const char *filename)
  221. {
  222. AVCodec *codec;
  223. AVCodecContext *c= NULL;
  224. int len;
  225. FILE *f, *outfile;
  226. uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
  227. AVPacket avpkt;
  228. AVFrame *decoded_frame = NULL;
  229.  
  230. av_init_packet(&avpkt);
  231.  
  232. printf("Decode audio file %s to %s\n", filename, outfilename);
  233.  
  234. /* find the mpeg audio decoder */
  235. codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
  236. if (!codec) {
  237. fprintf(stderr, "Codec not found\n");
  238. exit();
  239. }
  240.  
  241. c = avcodec_alloc_context3(codec);
  242. if (!c) {
  243. fprintf(stderr, "Could not allocate audio codec context\n");
  244. exit();
  245. }
  246.  
  247. /* open it */
  248. if (avcodec_open2(c, codec, NULL) < ) {
  249. fprintf(stderr, "Could not open codec\n");
  250. exit();
  251. }
  252.  
  253. f = fopen(filename, "rb");
  254. if (!f) {
  255. fprintf(stderr, "Could not open %s\n", filename);
  256. exit();
  257. }
  258. outfile = fopen(outfilename, "wb");
  259. if (!outfile) {
  260. av_free(c);
  261. exit();
  262. }
  263.  
  264. /* decode until eof */
  265. avpkt.data = inbuf;
  266. avpkt.size = fread(inbuf, , AUDIO_INBUF_SIZE, f);
  267.  
  268. while (avpkt.size > ) {
  269. int got_frame = ;
  270.  
  271. if (!decoded_frame) {
  272. if (!(decoded_frame = av_frame_alloc())) {
  273. fprintf(stderr, "Could not allocate audio frame\n");
  274. exit();
  275. }
  276. }
  277.  
  278. len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
  279. if (len < ) {
  280. fprintf(stderr, "Error while decoding\n");
  281. exit();
  282. }
  283. if (got_frame) {
  284. /* if a frame has been decoded, output it */
  285. int data_size = av_samples_get_buffer_size(NULL, c->channels,
  286. decoded_frame->nb_samples,
  287. c->sample_fmt, );
  288. if (data_size < ) {
  289. /* This should not occur, checking just for paranoia */
  290. fprintf(stderr, "Failed to calculate data size\n");
  291. exit();
  292. }
  293. fwrite(decoded_frame->data[], , data_size, outfile);
  294. }
  295. avpkt.size -= len;
  296. avpkt.data += len;
  297. avpkt.dts =
  298. avpkt.pts = AV_NOPTS_VALUE;
  299. if (avpkt.size < AUDIO_REFILL_THRESH) {
  300. /* Refill the input buffer, to avoid trying to decode
  301. * incomplete frames. Instead of this, one could also use
  302. * a parser, or use a proper container format through
  303. * libavformat. */
  304. memmove(inbuf, avpkt.data, avpkt.size);
  305. avpkt.data = inbuf;
  306. len = fread(avpkt.data + avpkt.size, ,
  307. AUDIO_INBUF_SIZE - avpkt.size, f);
  308. if (len > )
  309. avpkt.size += len;
  310. }
  311. }
  312.  
  313. fclose(outfile);
  314. fclose(f);
  315.  
  316. avcodec_close(c);
  317. av_free(c);
  318. av_frame_free(&decoded_frame);
  319. }
  320.  
  321. /*
  322. * Video encoding example
  323. */
  324. static void video_encode_example(const char *filename, int codec_id)
  325. {
  326. AVCodec *codec;
  327. AVCodecContext *c= NULL;
  328. int i, ret, x, y, got_output;
  329. FILE *f;
  330. AVFrame *frame;
  331. AVPacket pkt;
  332. uint8_t endcode[] = { , , , 0xb7 };
  333.  
  334. printf("Encode video file %s\n", filename);
  335.  
  336. /* find the mpeg1 video encoder */
  337. codec = avcodec_find_encoder(codec_id);
  338. if (!codec) {
  339. fprintf(stderr, "Codec not found\n");
  340. exit();
  341. }
  342.  
  343. c = avcodec_alloc_context3(codec);
  344. if (!c) {
  345. fprintf(stderr, "Could not allocate video codec context\n");
  346. exit();
  347. }
  348.  
  349. /* put sample parameters */
  350. c->bit_rate = ;
  351. /* resolution must be a multiple of two */
  352. c->width = ;
  353. c->height = ;
  354. /* frames per second */
  355. c->time_base = (AVRational){,};
  356. /* emit one intra frame every ten frames
  357. * check frame pict_type before passing frame
  358. * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
  359. * then gop_size is ignored and the output of encoder
  360. * will always be I frame irrespective to gop_size
  361. */
  362. c->gop_size = ;
  363. c->max_b_frames = ;
  364. c->pix_fmt = AV_PIX_FMT_YUV420P;
  365.  
  366. if (codec_id == AV_CODEC_ID_H264)
  367. av_opt_set(c->priv_data, "preset", "slow", );
  368.  
  369. /* open it */
  370. if (avcodec_open2(c, codec, NULL) < ) {
  371. fprintf(stderr, "Could not open codec\n");
  372. exit();
  373. }
  374.  
  375. f = fopen(filename, "wb");
  376. if (!f) {
  377. fprintf(stderr, "Could not open %s\n", filename);
  378. exit();
  379. }
  380.  
  381. frame = av_frame_alloc();
  382. if (!frame) {
  383. fprintf(stderr, "Could not allocate video frame\n");
  384. exit();
  385. }
  386. frame->format = c->pix_fmt;
  387. frame->width = c->width;
  388. frame->height = c->height;
  389.  
  390. /* the image can be allocated by any means and av_image_alloc() is
  391. * just the most convenient way if av_malloc() is to be used */
  392. ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height,
  393. c->pix_fmt, );
  394. if (ret < ) {
  395. fprintf(stderr, "Could not allocate raw picture buffer\n");
  396. exit();
  397. }
  398.  
  399. /* encode 1 second of video */
  400. for (i = ; i < ; i++) {
  401. av_init_packet(&pkt);
  402. pkt.data = NULL; // packet data will be allocated by the encoder
  403. pkt.size = ;
  404.  
  405. fflush(stdout);
  406. /* prepare a dummy image */
  407. /* Y */
  408. for (y = ; y < c->height; y++) {
  409. for (x = ; x < c->width; x++) {
  410. frame->data[][y * frame->linesize[] + x] = x + y + i * ;
  411. }
  412. }
  413.  
  414. /* Cb and Cr */
  415. for (y = ; y < c->height/; y++) {
  416. for (x = ; x < c->width/; x++) {
  417. frame->data[][y * frame->linesize[] + x] = + y + i * ;
  418. frame->data[][y * frame->linesize[] + x] = + x + i * ;
  419. }
  420. }
  421.  
  422. frame->pts = i;
  423.  
  424. /* encode the image */
  425. ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
  426. if (ret < ) {
  427. fprintf(stderr, "Error encoding frame\n");
  428. exit();
  429. }
  430.  
  431. if (got_output) {
  432. printf("Write frame %3d (size=%5d)\n", i, pkt.size);
  433. fwrite(pkt.data, , pkt.size, f);
  434. av_free_packet(&pkt);
  435. }
  436. }
  437.  
  438. /* get the delayed frames */
  439. for (got_output = ; got_output; i++) {
  440. fflush(stdout);
  441.  
  442. ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
  443. if (ret < ) {
  444. fprintf(stderr, "Error encoding frame\n");
  445. exit();
  446. }
  447.  
  448. if (got_output) {
  449. printf("Write frame %3d (size=%5d)\n", i, pkt.size);
  450. fwrite(pkt.data, , pkt.size, f);
  451. av_free_packet(&pkt);
  452. }
  453. }
  454.  
  455. /* add sequence end code to have a real mpeg file */
  456. fwrite(endcode, , sizeof(endcode), f);
  457. fclose(f);
  458.  
  459. avcodec_close(c);
  460. av_free(c);
  461. av_freep(&frame->data[]);
  462. av_frame_free(&frame);
  463. printf("\n");
  464. }
  465.  
  466. /*
  467. * Video decoding example
  468. */
  469.  
  470. static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
  471. char *filename)
  472. {
  473. FILE *f;
  474. int i;
  475.  
  476. f = fopen(filename,"w");
  477. fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, );
  478. for (i = ; i < ysize; i++)
  479. fwrite(buf + i * wrap, , xsize, f);
  480. fclose(f);
  481. }
  482.  
  483. static int decode_write_frame(const char *outfilename, AVCodecContext *avctx,
  484. AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
  485. {
  486. int len, got_frame;
  487. char buf[];
  488.  
  489. len = avcodec_decode_video2(avctx, frame, &got_frame, pkt);
  490. if (len < ) {
  491. fprintf(stderr, "Error while decoding frame %d\n", *frame_count);
  492. return len;
  493. }
  494. if (got_frame) {
  495. printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count);
  496. fflush(stdout);
  497.  
  498. /* the picture is allocated by the decoder, no need to free it */
  499. snprintf(buf, sizeof(buf), outfilename, *frame_count);
  500. pgm_save(frame->data[], frame->linesize[],
  501. avctx->width, avctx->height, buf);
  502. (*frame_count)++;
  503. }
  504. if (pkt->data) {
  505. pkt->size -= len;
  506. pkt->data += len;
  507. }
  508. return ;
  509. }
  510.  
  511. static void video_decode_example(const char *outfilename, const char *filename)
  512. {
  513. AVCodec *codec;
  514. AVCodecContext *c= NULL;
  515. int frame_count;
  516. FILE *f;
  517. AVFrame *frame;
  518. uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
  519. AVPacket avpkt;
  520.  
  521. av_init_packet(&avpkt);
  522.  
  523. /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
  524. memset(inbuf + INBUF_SIZE, , FF_INPUT_BUFFER_PADDING_SIZE);
  525.  
  526. printf("Decode video file %s to %s\n", filename, outfilename);
  527.  
  528. /* find the mpeg1 video decoder */
  529. codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
  530. if (!codec) {
  531. fprintf(stderr, "Codec not found\n");
  532. exit();
  533. }
  534.  
  535. c = avcodec_alloc_context3(codec);
  536. if (!c) {
  537. fprintf(stderr, "Could not allocate video codec context\n");
  538. exit();
  539. }
  540.  
  541. if(codec->capabilities&CODEC_CAP_TRUNCATED)
  542. c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */
  543.  
  544. /* For some codecs, such as msmpeg4 and mpeg4, width and height
  545. MUST be initialized there because this information is not
  546. available in the bitstream. */
  547.  
  548. /* open it */
  549. if (avcodec_open2(c, codec, NULL) < ) {
  550. fprintf(stderr, "Could not open codec\n");
  551. exit();
  552. }
  553.  
  554. f = fopen(filename, "rb");
  555. if (!f) {
  556. fprintf(stderr, "Could not open %s\n", filename);
  557. exit();
  558. }
  559.  
  560. frame = av_frame_alloc();
  561. if (!frame) {
  562. fprintf(stderr, "Could not allocate video frame\n");
  563. exit();
  564. }
  565.  
  566. frame_count = ;
  567. for (;;) {
  568. avpkt.size = fread(inbuf, , INBUF_SIZE, f);
  569. if (avpkt.size == )
  570. break;
  571.  
  572. /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
  573. and this is the only method to use them because you cannot
  574. know the compressed data size before analysing it.
  575.  
  576. BUT some other codecs (msmpeg4, mpeg4) are inherently frame
  577. based, so you must call them with all the data for one
  578. frame exactly. You must also initialize 'width' and
  579. 'height' before initializing them. */
  580.  
  581. /* NOTE2: some codecs allow the raw parameters (frame size,
  582. sample rate) to be changed at any frame. We handle this, so
  583. you should also take care of it */
  584.  
  585. /* here, we use a stream based decoder (mpeg1video), so we
  586. feed decoder and see if it could decode a frame */
  587. avpkt.data = inbuf;
  588. while (avpkt.size > )
  589. if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, ) < )
  590. exit();
  591. }
  592.  
  593. /* some codecs, such as MPEG, transmit the I and P frame with a
  594. latency of one frame. You must do the following to have a
  595. chance to get the last frame of the video */
  596. avpkt.data = NULL;
  597. avpkt.size = ;
  598. decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, );
  599.  
  600. fclose(f);
  601.  
  602. avcodec_close(c);
  603. av_free(c);
  604. av_frame_free(&frame);
  605. printf("\n");
  606. }
  607.  
  608. int main(int argc, char **argv)
  609. {
  610. const char *output_type;
  611.  
  612. /* register all the codecs */
  613. avcodec_register_all();
  614.  
  615. if (argc < ) {
  616. printf("usage: %s output_type\n"
  617. "API example program to decode/encode a media stream with libavcodec.\n"
  618. "This program generates a synthetic stream and encodes it to a file\n"
  619. "named test.h264, test.mp2 or test.mpg depending on output_type.\n"
  620. "The encoded stream is then decoded and written to a raw data output.\n"
  621. "output_type must be chosen between 'h264', 'mp2', 'mpg'.\n",
  622. argv[]);
  623. return ;
  624. }
  625. output_type = argv[];
  626. // video_encode_example("test.h264", AV_CODEC_ID_H264);
  627.  
  628. if (!strcmp(output_type, "h264")) {
  629. video_encode_example("1080P.h264", AV_CODEC_ID_H264);
  630. } else if (!strcmp(output_type, "mp2")) {
  631. audio_encode_example("test.mp2");
  632. audio_decode_example("test.sw", "test.mp2");
  633. } else if (!strcmp(output_type, "mpg")) {
  634. video_encode_example("test.mpg", AV_CODEC_ID_MPEG1VIDEO);
  635. video_decode_example("test%02d.pgm", "test.mpg");
  636. } else {
  637. fprintf(stderr, "Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n",
  638. output_type);
  639. return ;
  640. }
  641.  
  642. return ;
  643. }

test.h264

链接:http://pan.baidu.com/s/1o8pRflS 密码:x6a8
 
 
编译之后,命令行进入Debug目录,同时将test.h264考进次目录,执行./app h264
成功的标志,会生成一个1080.h264的文件,可以使用ffplay 播放
 
参考:
 
 

下载SDL2 SDL2_image(依赖libpng1.5)
 libpng1.5
 
编译安装
libpng1.5
./configure 全部默认即可
make 
make install
SDL2 SDL2_image 
./configure --prefix=/usr/local/sdl2
 
注意:SDL2_image 需要进行下面这个步骤,SDL2 跳过
vim Makefile
搜索命令行/png查找
将libpng相关的数字改成15(系统本身可能自带libpng12,或者其他,编译程序的时候不会报错,
执行的时候会报错,
ibpng warning: Application was compiled with png.h from libpng-1.4.3
libpng warning: Application  is  running with png.c from libpng-1.2.44
libpng error: Incompatible libpng version in application and library
sdl2image configure 的时候有问题,需要include和lib一直,统一改成一个版本)
 
make -j4
sudo make install
配置/etc/profile /etc/ld.so.conf 和ffmpeg方法类似
 
eclipse建立工程
  1. /*
  2. * main.c
  3. *
  4. * Created on: Sep 16, 2016
  5. * Author: tla001
  6. */
  7. #include<stdio.h>
  8. #include <SDL2/SDL.h>
  9. #include <SDL2/SDL_image.h>
  10. void example00() ;
  11. int main()
  12. {
  13. SDL_Window* window =NULL;
  14. SDL_Renderer* render=NULL;
  15. SDL_Texture *texture=NULL;
  16. SDL_Rect src,dst;
  17. int width,height;
  18. SDL_Init(SDL_INIT_EVERYTHING);
  19. window=SDL_CreateWindow("hello",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,,,SDL_WINDOW_SHOWN);
  20. render=SDL_CreateRenderer(window,-,SDL_RENDERER_ACCELERATED |SDL_RENDERER_PRESENTVSYNC);
  21. texture=IMG_LoadTexture(render,"./lufi.bmp");
  22. //SDL_UpdateTexture(texture);
  23. if(texture==NULL){
  24. printf("err");
  25. exit();
  26. }
  27. SDL_QueryTexture(texture,NULL,NULL,&width,&height);
  28. printf("w=%d h=%d\n",width,height);
  29. src.x=src.y=;
  30. src.w=width;
  31. src.h=height;
  32. dst.x=;
  33. dst.y=;
  34. dst.w=width/;
  35. dst.h=height/;
  36. SDL_SetRenderDrawColor(render,,,,);
  37. SDL_RenderClear(render);
  38. SDL_RenderCopy(render,texture,NULL,&src);
  39. SDL_RenderPresent(render);
  40.  
  41. SDL_Delay();
  42. SDL_DestroyWindow(window);
  43. SDL_DestroyRenderer(render);
  44. SDL_Quit();
  45. // SDL_Window *pw = SDL_CreateWindow("hello1",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,640,480,SDL_WINDOW_SHOWN);
  46. // SDL_Renderer *pr = SDL_CreateRenderer(pw, -1, 0);
  47. // SDL_Surface *ps = IMG_Load("/home/tla001/Desktop/lufi.png");
  48. // if(ps==NULL){
  49. // exit(-1);
  50. // }
  51. // SDL_Texture *pt = SDL_CreateTextureFromSurface(pr, ps);
  52. // SDL_RenderClear(pr);
  53. // SDL_Rect r;
  54. // r.x = 0;
  55. // r.y = 0;
  56. // r.w = 1000;
  57. // r.h = 1000;
  58. // SDL_RenderCopy(pr, pt, NULL, &r);
  59. // SDL_RenderPresent(pr);
  60. // //SDL_Flip(pw);
  61. // SDL_Delay(3000);
  62. // SDL_Quit();
  63. //example00() ;
  64. return ;
  65. }
  66. void example00()
  67. {
  68. SDL_Window *pWindow = NULL;
  69. SDL_Renderer*pRenderer = NULL;
  70.  
  71. // 1. initialize SDL
  72. if (SDL_Init(SDL_INIT_EVERYTHING) < )
  73. {
  74. printf ("SDL initialize fail:%s\n", SDL_GetError());
  75. return;
  76. }
  77.  
  78. // 2. create window
  79. pWindow = SDL_CreateWindow("example00:Setting up SDL",
  80. SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  81. , ,
  82. SDL_WINDOW_SHOWN);
  83. if (NULL == pWindow)
  84. {
  85. printf ("Create window fail:%s\n", SDL_GetError());
  86. }
  87.  
  88. // 3. create renderer
  89. pRenderer = SDL_CreateRenderer(pWindow, -, );
  90.  
  91. // 4. clear the window to green
  92. SDL_SetRenderDrawColor(pRenderer,,,,);
  93. SDL_RenderClear(pRenderer);
  94.  
  95. // 5. show the window
  96. SDL_RenderPresent(pRenderer);
  97.  
  98. SDL_Delay(); // for display
  99.  
  100. // 6. exit
  101. SDL_Quit();
  102. }

Linux下编译带x264的ffmpeg的配置方法,包含SDL2的更多相关文章

  1. 在Linux下编译带调试功能的Bochs

    在Linux下使用Bochs参考: http://wangcong.org/articles/bochs.html http://kinglaw05.blog.163.com/blog/static/ ...

  2. linux下编译ffmpeg 引入外部库x264

    Found no assembler Minimum version is nasm-2.13 If you really want to compile without asm, configure ...

  3. Linux 下编译Android-VLC开源播放器详解(附源码下载)

    这两天需要做音视频播放相关的东西,所以重新找了目前android下的解码库.Android自带的解码库支持不全,因此很多第三方播放器都是自带解码器,绝大部分都是使用FFMpeg作为解码库.我11年的时 ...

  4. linux下编译gcc6.2.0

    linux下编译gcc6.2.0 在archlinx的下gcc已经更新到6.2.1了,win10的WSL下还是gcc4.8.官方源没有比较新的版本,于是自己编译使用. GCC6的几个新特性 GCC 6 ...

  5. linux下编译qt5.6.0静态库——configure配置

    linux下编译qt5.6.0静态库 linux下编译qt5.6.0静态库 configure生成makefile 安装选项 Configure选项 第三方库: 附加选项: QNX/Blackberr ...

  6. 【原创】Linux下编译链接中常见问题总结

    前言 一直以来对Linux下编译链接产生的问题没有好好重视起来,出现问题就度娘一下,很多时候的确是在搜索帮助下解决了BUG,但由于对原因不求甚解,没有细细研究,结果总是在遇到在BUG时弄得手忙脚乱得. ...

  7. linux下编译qt5.6.0静态库——configure配置(超详细,有每一个模块的说明)(乌合之众)

    linux下编译qt5.6.0静态库 linux下编译qt5.6.0静态库 configure生成makefile 安装选项 Configure选项 第三方库: 附加选项: QNX/Blackberr ...

  8. linux下编译原理分析

    linux下编译hello.c 程序,使用gcc hello.c,然后./a.out就能够执行:在这个简单的命令后面隐藏了很多复杂的过程,这个过程包含了以下的步骤: ================= ...

  9. [转]linux下编译boost.python

    转自:http://blog.csdn.net/gong_xucheng/article/details/25045407 linux下编译boost.python 最近项目使用c++操作python ...

随机推荐

  1. matplotlib画图保存

    import numpy as np import matplotlib.pyplot as plt xData = np.arange(0, 10, 1) yData1 = xData.__pow_ ...

  2. OC 解决NSArray、NSDictionary直接打印中文出现乱码的问题

    在iOS开发中,经常需要查看数组中得元素是否是自己想要的,但是苹果并没有对直接打印数组中得中文作处理,直接打印就会出现一堆很讨厌的东西,解决其实很简单,就是需要通过为NSArray添加分类,重写 - ...

  3. poj1502 spfa最短路

    //Accepted 320 KB 16 ms //有n个顶点,边权用A表示 //给出下三角矩阵,求从一号顶点出发到各点的最短路的最大值 #include <cstdio> #includ ...

  4. Java与数据库之间时间的处理

    Java与数据库之间时间的处理 在数据库中建表: DROP TABLE IF EXISTS `times`; CREATE TABLE `times` ( `id` int(11) NOT NULL ...

  5. 开发经验之状态机思想,分别使用了swift,OC,C,PHP语言实现

    这里设计一个简单的练习,使用状态机思想实现,分别使用了swift,OC,C,PHP语言实现 题目:1到10000遍历,开始-打印奇数-遇到7的倍数开始打印偶数--遇到10的倍数打印奇数   //部分结 ...

  6. Qt控件篇 ---- QTableView/QTableWidget

    记录 //按字母排序 item->setText("2"); //按数值排序item->setData(Qt::DisplayRole, 2);

  7. linux,python 常用的处理log的命令

    一般的log文件都是需要过滤 ps:管道符| 管道符前面的输出值 grep 过滤查找 将是error的log过滤显示 grep '221.2.100.138'  web.access.log   gr ...

  8. js窗口边缘滑入滑出效果-初级代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. YHMMR003 农户基本信息的维护程序

    *********************************************************************** * Title : * * Application : ...

  10. Opencv结构与内容

    一.Opencv的结构分类: cxcore.cv.ML(Machine Learning).HighGUI.cvcam.cvaux 二.常见结构的内容与算法: 1.cxcore库(基本结构和算法.XM ...