ffmpeg+sdl教程=编写一个简单的播放器

上传人:博****1 文档编号:430027302 上传时间:2023-08-17 格式:DOCX 页数:115 大小:97.65KB
返回 下载 相关 举报
ffmpeg+sdl教程=编写一个简单的播放器_第1页
第1页 / 共115页
ffmpeg+sdl教程=编写一个简单的播放器_第2页
第2页 / 共115页
ffmpeg+sdl教程=编写一个简单的播放器_第3页
第3页 / 共115页
ffmpeg+sdl教程=编写一个简单的播放器_第4页
第4页 / 共115页
ffmpeg+sdl教程=编写一个简单的播放器_第5页
第5页 / 共115页
点击查看更多>>
资源描述

《ffmpeg+sdl教程=编写一个简单的播放器》由会员分享,可在线阅读,更多相关《ffmpeg+sdl教程=编写一个简单的播放器(115页珍藏版)》请在金锄头文库上搜索。

1、ffmpeg+sdl教程-编写一个简单的播放器1 2021-08-15 13:55 2856人阅读 评论(1) 收藏 举报 最近在研究ffmpeg,由于详细介绍ffmpeg的文档资料很少,有人就说了学习ffmpeg的最好方法就是看一些可以正常工作的代码,当你看懂了 ffmpeg.c和ffplay.c两个文件就算入门了,但是这两个文件相当恐怖啊,一个4300行,一个3200行,初学者难免看着难免头晕吃力,可以 先从简单的开始,比方output-example.c和api-example.c,然后再去网上找点稍微复杂点的例子来看 我觉得这个 :/dranger /ffmpeg/tutorial01

2、.html 教程很好,由浅入深,而最终代码也不过一千行多一点,非常适合像我这样的初学者,决定整理一下这个教程,方便以后查看 首先是编译环境的搭建,先下载两个压缩包FFmpeg-full-SDK-3.2.rar和SDL-devel-1.2.14-VC8.zip,再下载一 个工程压缩包output_example_me这个压缩包可以通过关键字“ffmpeg output-example找到。 第一步,编译output_example_me中的工程 解压output_example_me中的工程,我用的是vc2005编译的这个工程,也可用vc6.0来编译,如果编译成功了,运行程序会生成一个自 定义文

3、件名的视频音频文件,这个文件通过简单的算法生成了规那么变换的图像和声音图像是几个有渐变色的斜放色条像左移动,声音很难听,至此ffmpeg 的环境就算搭建好了,可以成功编译 :/dranger /ffmpeg/tutorial01.html的 第一个例子了,要成功编译后面的例子需要SDL的支持。 第二步,为工程导入SDL库,具体步骤可以通过搜索“vc SDL使用找到相关教程,如果导入不成功可以为工程添加依赖项:工程-选项-链接器-输入-附加依赖项:SDL.lib SDLmain.lib avcodec.lib avdevice.lib avfilter.lib avformat.lib avut

4、il.lib swscale.lib 例1的程序功能是把一个视频文件的前5帧导出为ppm格式的文件,ppm格式的文件可以通过ACDSee等软件翻开 下面是例1的完整代码:cpp:collapse:showcolumns + expand sourceview plaincopyprint?1. /tutorial01.c2. /CodebasedonatutorialbyMartinBohme(boehmeinb.uni-luebeckREMOVETHIS.de)3. /TestedonGentoo,CVSversion5/01/07compiledwithGCC4.1.14. 5. /Asm

5、allsampleprogramthatshowshowtouselibavformatandlibavcodecto6. /readvideofromafile.7. /8. /Use9. /10. /gcc-otutorial01tutorial01.c-lavformat-lavcodec-lz11. /12. /tobuild(assuminglibavformatandlibavcodecarecorrectlyinstalled13. /yoursystem).14. /15. /Runusing16. /17. /tutorial01myvideofile.mpg18. /19.

6、 /towritethefirstfiveframesfrommyvideofile.mpgtodiskinPPM20. /format.21. 22. #includelibavformat/avformat.h23. #includelibswscale/swscale.h24. 25. #include26. #include27. 28. voidSaveFrame(AVFrame*pFrame,intwidth,intheight,intiFrame)29. 30. FILE*pFile;31. charszFilename32;32. inty;33. 34. /Openfile3

7、5. sprintf(szFilename,frame%d.ppm,iFrame);36. pFile=fopen(szFilename,wb);37. if(pFile=NULL)38. return;39. 40. /Writeheader41. fprintf(pFile,P6/n%d%d/n255/n,width,height);42. 43. /Writepixeldata44. for(y=0;ydata0+y*pFrame-linesize0,1,width*3,pFile);46. 47. /Closefile48. fclose(pFile);49. 50. 51. intm

8、ain(intargc,char*argv)52. 53. AVFormatContext*pFormatCtx;54. inti,videoStream;55. AVCodecContext*pCodecCtx;56. AVCodec*pCodec;57. AVFrame*pFrame;58. AVFrame*pFrameRGB;59. AVPacketpacket;60. intframeFinished;61. intnumBytes;62. uint8_t*buffer;63. 64. if(argc2)65. 66. printf(Pleaseprovideamoviefile/n)

9、;67. return-1;68. 69. /Registerallformatsandcodecs70. av_register_all();71. 72. /Openvideofile73. if(av_open_input_file(&pFormatCtx,argv1,NULL,0,NULL)!=0)74. return-1;/Couldntopenfile75. 76. /Retrievestreaminformation77. if(av_find_stream_info(pFormatCtx)0)78. return-1;/Couldntfindstreaminformation7

10、9. 80. /Dumpinformationaboutfileontostandarderror81. dump_format(pFormatCtx,0,argv1,0);82. 83. /Findthefirstvideostream84. videoStream=-1;85. for(i=0;inb_streams;i+)86. if(pFormatCtx-streamsi-codec-codec_type=CODEC_TYPE_VIDEO)87. 88. videoStream=i;89. break;90. 91. if(videoStream=-1)92. return-1;/Di

11、dntfindavideostream93. 94. /Getapointertothecodeccontextforthevideostream95. pCodecCtx=pFormatCtx-streamsvideoStream-codec;96. 97. /Findthedecoderforthevideostream98. pCodec=avcodec_find_decoder(pCodecCtx-codec_id);99. if(pCodec=NULL)100. 101. fprintf(stderr,Unsupportedcodec!/n);102. return-1;/Codec

12、notfound103. 104. /Opencodec105. if(avcodec_open(pCodecCtx,pCodec)0)106. return-1;/Couldnotopencodec107. 108. /Allocatevideoframe109. pFrame=avcodec_alloc_frame();110. 111. /AllocateanAVFramestructure112. pFrameRGB=avcodec_alloc_frame();113. if(pFrameRGB=NULL)114. return-1;115. 116. /Determinerequiredbuffersizeandallocatebuffer117

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 商业计划书

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号