FFMPEG学习资料少之又少,在此推荐雷神的博客:

http://blog.csdn.net/leixiaohua1020

在这里,我们把打印视频里的相关信息作为学习FFMPEG的 Hello World程序。

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. extern "C"
  5. {
  6. #include "libavformat/avformat.h"
  7. #include "libavutil/dict.h"
  8. };
  9.  
  10. #pragma comment(lib, "avformat.lib")
  11. #pragma comment(lib, "avutil.lib")
  12. #pragma comment(lib, "avcodec.lib")
  13.  
  14. int main()
  15. {
  16. AVFormatContext *pFormatCtx = NULL;
  17. AVCodecContext *pCodecCtx = NULL;
  18. AVCodec *pCodec;
  19. AVDictionaryEntry *dict = NULL;
  20. AVInputFormat *pInputFormat = NULL;
  21.  
  22. int iHour, iMinute, iSecond, iTotalSeconds;//HH:MM:SS
  23. int videoIndex, audioIndex;
  24.  
  25. char *fileName = "bad.mp4";
  26. //char *fileName = "Titanic.ts";
  27.  
  28. av_register_all();//注册所有组件
  29.  
  30. if (avformat_open_input(&pFormatCtx, fileName, NULL, NULL) != 0)//打开输入视频文件
  31. {
  32. printf("Couldn't open input stream.\n");
  33. return -1;
  34. }
  35.  
  36. if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
  37. {
  38. printf("Couldn't find stream information.\n");
  39. return -1;
  40. }
  41.  
  42. videoIndex = -1;
  43. for (int i = 0; i < pFormatCtx->nb_streams; i++)
  44. {
  45. if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)//查找音频
  46. {
  47. videoIndex = i;
  48. break;
  49. }
  50. }
  51. if (videoIndex == -1)
  52. {
  53. printf("Couldn't find a video stream.\n");
  54. return -1;
  55. }
  56.  
  57. pCodecCtx = pFormatCtx->streams[videoIndex]->codec; //指向AVCodecContext的指针
  58. pCodec = avcodec_find_decoder(pCodecCtx->codec_id); //指向AVCodec的指针.查找解码器
  59. if (pCodec == NULL)
  60. {
  61. printf("Codec not found.\n");
  62. return -1;
  63. }
  64. //打开解码器
  65. if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
  66. {
  67. printf("Could not open codec.\n");
  68. return -1;
  69. }
  70.  
  71. audioIndex = -1;
  72. for (int i = 0; i < pFormatCtx->nb_streams; i++)
  73. {
  74. if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  75. {
  76. audioIndex = i;
  77. break;
  78. }
  79. }
  80. if (audioIndex == -1)
  81. {
  82. printf("Couldn't find a audio stream.\n");
  83. return -1;
  84. }
  85.  
  86. //打印结构体信息
  87.  
  88. puts("AVFormatContext信息:");
  89. puts("---------------------------------------------");
  90. printf("文件名:%s\n", pFormatCtx->filename);
  91. iTotalSeconds = (int)pFormatCtx->duration / 1000000;
  92. iHour = iTotalSeconds / 3600;//小时
  93. iMinute = iTotalSeconds % 3600 / 60;//分钟
  94. iSecond = iTotalSeconds % 60;//秒
  95. printf("持续时间:%02d:%02d:%02d\n", iHour, iMinute, iSecond);
  96. printf("平均混合码率:%d kb/s\n", pFormatCtx->bit_rate / 1000);
  97. printf("视音频个数:%d\n", pFormatCtx->nb_streams);
  98. puts("---------------------------------------------");
  99.  
  100. puts("FFMPEG支持的所有的输入文件格式:");
  101. puts("---------------------------------------------");
  102. pInputFormat = pFormatCtx->iformat;
  103. while (pInputFormat)
  104. {
  105. printf("%s ", pInputFormat->name);
  106. pInputFormat = pInputFormat->next;
  107. }
  108. puts("\n---------------------------------------------");
  109.  
  110. puts("AVInputFormat信息:");
  111. puts("---------------------------------------------");
  112. printf("封装格式名称:%s\n", pFormatCtx->iformat->name);
  113. printf("封装格式长名称:%s\n", pFormatCtx->iformat->long_name);
  114. printf("封装格式扩展名:%s\n", pFormatCtx->iformat->extensions);
  115. printf("封装格式ID:%d\n", pFormatCtx->iformat->raw_codec_id);
  116. puts("---------------------------------------------");
  117.  
  118. puts("AVStream信息:");
  119. puts("---------------------------------------------");
  120. printf("视频流标识符:%d\n", pFormatCtx->streams[videoIndex]->index);
  121. printf("音频流标识符:%d\n", pFormatCtx->streams[audioIndex]->index);
  122. printf("视频流长度:%d微秒\n", pFormatCtx->streams[videoIndex]->duration);
  123. printf("音频流长度:%d微秒\n", pFormatCtx->streams[audioIndex]->duration);
  124. puts("---------------------------------------------");
  125.  
  126. puts("AVCodecContext信息:");
  127. puts("---------------------------------------------");
  128. printf("视频码率:%d kb/s\n", pCodecCtx->bit_rate / 1000);
  129. printf("视频大小:%d * %d\n", pCodecCtx->width, pCodecCtx->height);
  130. puts("---------------------------------------------");
  131.  
  132. puts("AVCodec信息:");
  133. puts("---------------------------------------------");
  134. printf("视频编码格式:%s\n", pCodec->name);
  135. printf("视频编码详细格式:%s\n", pCodec->long_name);
  136. puts("---------------------------------------------");
  137.  
  138. printf("视频时长:%d微秒\n", pFormatCtx->streams[videoIndex]->duration);
  139. printf("音频时长:%d微秒\n", pFormatCtx->streams[audioIndex]->duration);
  140. printf("音频采样率:%d\n", pFormatCtx->streams[audioIndex]->codec->sample_rate);
  141. printf("音频信道数目:%d\n", pFormatCtx->streams[audioIndex]->codec->channels);
  142.  
  143. puts("AVFormatContext元数据:");
  144. puts("---------------------------------------------");
  145. while (dict = av_dict_get(pFormatCtx->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
  146. {
  147. printf("[%s] = %s\n", dict->key, dict->value);
  148. }
  149. puts("---------------------------------------------");
  150.  
  151. puts("AVStream视频元数据:");
  152. puts("---------------------------------------------");
  153. dict = NULL;
  154. while (dict = av_dict_get(pFormatCtx->streams[videoIndex]->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
  155. {
  156. printf("[%s] = %s\n", dict->key, dict->value);
  157. }
  158. puts("---------------------------------------------");
  159.  
  160. puts("AVStream音频元数据:");
  161. puts("---------------------------------------------");
  162. dict = NULL;
  163. while (dict = av_dict_get(pFormatCtx->streams[audioIndex]->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
  164. {
  165. printf("[%s] = %s\n", dict->key, dict->value);
  166. }
  167. puts("---------------------------------------------");
  168.  
  169. av_dump_format(pFormatCtx, -1, fileName, 0);
  170.  
  171. printf("\n\n编译信息:\n%s\n\n", avcodec_configuration());
  172.  
  173. avcodec_close(pCodecCtx);
  174. avformat_close_input(&pFormatCtx);
  175. return 0;
  176. }
  177.  
  178. /*
  179. *打印FFmpeg支持的解码器
  180. int main(void)
  181. {
  182. char *info = (char *)malloc(40000);
  183. memset(info, 0, 40000);
  184.  
  185. av_register_all();
  186.  
  187. AVCodec *c_temp = av_codec_next(NULL);
  188.  
  189. while (c_temp != NULL)
  190. {
  191. if (c_temp->decode != NULL)
  192. {
  193. strcat(info, "[Decode]");
  194. }
  195. else
  196. {
  197. strcat(info, "[Encode]");
  198. }
  199. switch (c_temp->type)
  200. {
  201. case AVMEDIA_TYPE_VIDEO:
  202. strcat(info, "[Video]");
  203. break;
  204. case AVMEDIA_TYPE_AUDIO:
  205. strcat(info, "[Audeo]");
  206. break;
  207. default:
  208. strcat(info, "[Other]");
  209. break;
  210. }
  211. sprintf(info, "%s %10s\n", info, c_temp->long_name);
  212. c_temp = c_temp->next;
  213. }
  214. puts(info);
  215. free(info);
  216.  
  217. return 0;
  218. }
  219. */
  220.  
  221. /*
  222. avcodec:编解码(最重要的库)
  223. avformat:封装格式处理的库
  224. avfilter:滤镜特效处理的库
  225. avdevice:各种设备的输入输出
  226. avutil:工具库(大部分库都依赖这个库)
  227. postproc:后加工
  228. swresample:音频采样数据格式转换
  229. swscale:视频像素数据格式转换
  230. */
  1.  

VS2013环境下error

error C4996: 'AVStream::codec': 被声明为已否决

解决:

输出:

  1. AVFormatContext信息:
  2. ---------------------------------------------
  3. 文件名:bad.mp4
  4. 持续时间:00:03:39
  5. 平均混合码率:803 kb/s
  6. 视音频个数:2
  7. ---------------------------------------------
  8. FFMPEG支持的所有的输入文件格式:
  9. ---------------------------------------------
  10. mov,mp4,m4a,3gp,3g2,mj2 mp3 mpc mpc8 mpeg mpegts mpegtsraw mpegvideo mpjpeg mpl2
  11. mpsub msf msnwctcp mtv musx mv mvi mxf mxg nc nistsphere nsv nut nuv ogg oma pa
  12. f alaw mulaw f64be f64le f32be f32le s32be s32le s24be s24le s16be s16le s8 u32b
  13. e u32le u24be u24le u16be u16le u8 pjs pmp pva pvf qcp r3d rawvideo realtext red
  14. spark rl2 rm roq rpl rsd rso rtp rtsp sami sap sbg sdp sdr2 film_cpk shn siff sl
  15. n smk smjpeg smush sol sox spdif srt psxstr stl subviewer1 subviewer sup svag sw
  16. f tak tedcaptions thp 3dostr tiertexseq tmv truehd tta txd tty v210 v210x vag vc
  17. 1 vc1test vivo vmd vobsub voc vpk vplayer vqf w64 wav wc3movie webm_dash_manifes
  18. t webvtt wsaud wsvqa wtv wve wv xa xbin xmv xvag xwma yop yuv4mpegpipe bmp_pipe
  19. dds_pipe dpx_pipe exr_pipe j2k_pipe jpeg_pipe jpegls_pipe pcx_pipe pictor_pipe p
  20. ng_pipe qdraw_pipe sgi_pipe sunrast_pipe tiff_pipe webp_pipe libgme libmodplug
  21. ---------------------------------------------
  22. AVInputFormat信息:
  23. ---------------------------------------------
  24. 封装格式名称:mov,mp4,m4a,3gp,3g2,mj2
  25. 封装格式长名称:QuickTime / MOV
  26. 封装格式扩展名:mov,mp4,m4a,3gp,3g2,mj2
  27. 封装格式ID0
  28. ---------------------------------------------
  29. AVStream信息:
  30. ---------------------------------------------
  31. 视频流标识符:0
  32. 音频流标识符:1
  33. 视频流长度:3362816微秒
  34. 音频流长度:9660425微秒
  35. ---------------------------------------------
  36. AVCodecContext信息:
  37. ---------------------------------------------
  38. 视频码率:669 kb/s
  39. 视频大小:1440 * 1080
  40. ---------------------------------------------
  41. AVCodec信息:
  42. ---------------------------------------------
  43. 视频编码格式:h264
  44. 视频编码详细格式:H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
  45. ---------------------------------------------
  46. 视频时长:3362816微秒
  47. 音频时长:9660425微秒
  48. 音频采样率:44100
  49. 音频信道数目:2
  50. AVFormatContext元数据:
  51. ---------------------------------------------
  52. [major_brand] = isom
  53. [minor_version] = 512
  54. [compatible_brands] = isomiso2avc1mp41
  55. [encoder] = Lavf57.34.100
  56. [title] = BadApple
  57. [comment] = FFMPEG TEST
  58. ---------------------------------------------
  59. AVStream视频元数据:
  60. ---------------------------------------------
  61. [language] = und
  62. [handler_name] = VideoHandler
  63. ---------------------------------------------
  64. AVStream音频元数据:
  65. ---------------------------------------------
  66. [language] = und
  67. [handler_name] = SoundHandler
  68. ---------------------------------------------
  69. Input #-1, mov,mp4,m4a,3gp,3g2,mj2, from 'bad.mp4':
  70. Metadata:
  71. major_brand : isom
  72. minor_version : 512
  73. compatible_brands: isomiso2avc1mp41
  74. encoder : Lavf57.34.100
  75. title : BadApple
  76. comment : FFMPEG TEST
  77. Duration: 00:03:39.06, start: 0.000000, bitrate: 803 kb/s
  78. Stream #-1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1440x108
  79. 0 [SAR 1:1 DAR 4:3], 669 kb/s, 15 fps, 15 tbr, 15360 tbn (default)
  80. Metadata:
  81. handler_name : VideoHandler
  82. Stream #-1:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fl
  83. tp, 130 kb/s (default)
  84. Metadata:
  85. handler_name : SoundHandler
  86.  
  87. 编译信息:
  88. --disable-static --enable-shared --enable-gpl --enable-version3 --disable-w32thr
  89. eads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enab
  90. le-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --e
  91. nable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libi
  92. lbc --enable-libmodplug --enable-libmfx --enable-libmp3lame --enable-libopencore
  93. -amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable
  94. -librtmp --enable-libschroedinger --enable-libsnappy --enable-libsoxr --enable-l
  95. ibspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libv
  96. o-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwe
  97. bp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-
  98. libzimg --enable-lzma --enable-decklink --enable-zlib
  99.  
  100. 请按任意键继续. . .

FFMPEG入门第一个程序。

FFMPEG学习----打印视频信息的更多相关文章

  1. FFMPEG学习----解码视频

    基础概念 我们平时看到的视频文件有许多格式,比如 avi, mkv, rmvb, mov, mp4等等,这些被称为容器(Container), 不同的容器格式规定了其中音视频数据的组织方式(也包括其他 ...

  2. FFMPEG学习----分离视频里的H.264与YUV数据

    #include <stdio.h> extern "C" { #include "libavcodec/avcodec.h" #include & ...

  3. 使用JavaCV实现读取视频信息及自动截取封面图

    概述 最近在对之前写的一个 Spring Boot 的视频网站项目做功能完善,需要利用 FFmpeg 实现读取视频信息和自动截图的功能,查阅资料后发现网上这部分的内容非常少,于是就有了这篇文章. 视频 ...

  4. 【转】学习FFmpeg API – 解码视频

    ffmpeg是编解码的利器,用了很久,以前看过dranger 的教程,非常精彩,受益颇多,是学习ffmpeg api很好的材料.可惜的是其针对的ffmpeg版本已经比较老了,而ffmpeg的更新又很快 ...

  5. .net core Docker 容器添加ffmpeg 获取视频信息和截图

    最近在处理上传视频,需要获取视频信息和截图,这里就需要用到ffmpeg; 由于我的项目是在docker compose中运行调试,所以ffmpeg也需要在docker中能调用: 网上找到的方法在Doc ...

  6. 学习FFmpeg API – 解码视频

    本文转载 视频播放过程 首先简单介绍以下视频文件的相关知识.我们平时看到的视频文件有许多格式,比如 avi, mkv, rmvb, mov, mp4等等,这些被称为容器(Container), 不同的 ...

  7. FFmpeg Android 学习(一):Android 如何调用 FFMPEG 编辑音视频

    一.概述 在Android开发中,我们对一些音视频的处理比较无力,特别是编辑音视频这部分.而且在Android上对视频编辑方面,几乎没有任何API做支持,MediaCodec(硬编码)也没有做支持.那 ...

  8. Hadoop源码学习笔记(2) ——进入main函数打印包信息

    Hadoop源码学习笔记(2) ——进入main函数打印包信息 找到了main函数,也建立了快速启动的方法,然后我们就进去看一看. 进入NameNode和DataNode的主函数后,发现形式差不多: ...

  9. python学习(五)--打印错误信息

    from urllib import request #打印错误信息 except Exceptionlist = [ "http://www.baidu11.com/", &qu ...

随机推荐

  1. $Noip2018/Luogu5022$ 旅行

    $Luogu$ $Description$ 一个$n$个点,$m$条边的图.$m=n-1$或$m=n$.任意选取一点作为起始点,可以去往一个没去过的点,或者回到第一次到达这个点时来自的点.要求遍历整个 ...

  2. 为什么TCP建立连接协议是三次握手,而关闭连接却是四次握手呢?

    看到了一道面试题:"为什么TCP建立连接协议是三次握手,而关闭连接却是四次握手呢?为什么不能用两次握手进行连接?",想想最近也到金三银四了,所以就查阅了相关资料,整理出来了这篇文章 ...

  3. Docker日常常用命令汇总

    一.使用docker镜像/容器 (1)创建容器,且进入命令台 docker run --name 容器名 -i -t ubuntu /bin/bash (2)查看/容器 docker ps #查看正在 ...

  4. Win10系统下应用窗口任务栏居中效果

    实现步骤: 在资源管理器中新建文件夹,一定要保证文件夹内无任何文件 任务栏上鼠标右键,移动到工具栏上,选择新建工具栏 选择新建的空文件夹 空文件夹出现在任务栏后,鼠标可以拖动工具栏前的两条竖线(图片上 ...

  5. LEFT函数使用

    详解:LEFT函数用于从一个文本字符串的第一个字符开始返回指定个数的字符 1.提取A2单元格从左往右2位字符 2.函数使用用途: LEFT函数用于从一个文本字符串的第一个字符开始返回指定个数的字符 语 ...

  6. Dockerfile + Nginx.conf文件记录(用于前端项目部署)

    Dockerfile + Nginx.conf文件记录(用于前端项目部署) 本教程依据个人理解并经过实际验证为正确,特此记录下来,权当笔记. 注:基于linux操作系统(敏感信息都进行了处理),默认服 ...

  7. 【tf.keras】AdamW: Adam with Weight decay

    论文 Decoupled Weight Decay Regularization 中提到,Adam 在使用时,L2 与 weight decay 并不等价,并提出了 AdamW,在神经网络需要正则项时 ...

  8. 【转】HTML5+WebGL:构建 3D 网页新世界

    今年下半年, HTML5 和 WebGL 变成极热门词语,3D 网页来势汹汹.主流的浏览器 Google Chrome 以及 Mozilla Firefox 均致力于 HTML5+WebGL 的 3D ...

  9. 自定义实现的ArrayList以及自定义实现的Iterator迭代器

    ArrayList的底层是长度可动态变化的数组,其适用于查找多,修改少的情况,原因是数组的元素的增加删除元素会涉及大量元素的移动,效率比较低,ArrayList中的元素可以重复,与插入时的顺序相同,可 ...

  10. Springboot2.1.1下的自定义拦截器而静态资源不能访问的问题

    1.项目结构 2.自定义拦截器 public class LoginHandlerlnterceptor implements HandlerInterceptor { //目标方法执行之前 @Ove ...