如何使用FFmpeg抽取音视频的视频数据,代码如下:

  1. // FFmpegTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include "AACFormat.h"
  7.  
  8. #define __STDC_CONSTANT_MACROS
  9. #define __STDC_FORMAT_MACROS
  10.  
  11. // For extractVideo
  12. #ifndef AV_WB32
  13. # define AV_WB32(p, val) do { \
  14. uint32_t d = (val); \
  15. ((uint8_t*)(p))[] = (d); \
  16. ((uint8_t*)(p))[] = (d)>>; \
  17. ((uint8_t*)(p))[] = (d)>>; \
  18. ((uint8_t*)(p))[] = (d)>>; \
  19. } while()
  20. #endif
  21.  
  22. // For extractVideo
  23. #ifndef AV_RB16
  24. # define AV_RB16(x) \
  25. ((((const uint8_t*)(x))[] << ) | \
  26. ((const uint8_t*)(x))[])
  27. #endif
  28.  
  29. extern "C"{
  30. #include <stdio.h>
  31. #include "libavcodec/avcodec.h"
  32. #include "libavformat/avformat.h"
  33. #include "libavutil/log.h"
  34. }
  35.  
  36. // For extractVideo
  37. static int alloc_and_copy(AVPacket *out,
  38. const uint8_t *sps_pps, uint32_t sps_pps_size,
  39. const uint8_t *in, uint32_t in_size)
  40. {
  41. uint32_t offset = out->size;
  42. uint8_t nal_header_size = offset ? : ;
  43. int err;
  44.  
  45. err = av_grow_packet(out, sps_pps_size + in_size + nal_header_size);
  46. if (err < )
  47. return err;
  48.  
  49. if (sps_pps)
  50. memcpy(out->data + offset, sps_pps, sps_pps_size);
  51. memcpy(out->data + sps_pps_size + nal_header_size + offset, in, in_size);
  52. if (!offset) {
  53. AV_WB32(out->data + sps_pps_size, );
  54. }
  55. else {
  56. (out->data + offset + sps_pps_size)[] =
  57. (out->data + offset + sps_pps_size)[] = ;
  58. (out->data + offset + sps_pps_size)[] = ;
  59. }
  60.  
  61. return ;
  62. }
  63.  
  64. // For extractVideo
  65. int h264_extradata_to_annexb(const uint8_t *codec_extradata, const int codec_extradata_size, AVPacket *out_extradata, int padding)
  66. {
  67. uint16_t unit_size;
  68. uint64_t total_size = ;
  69. uint8_t *out = NULL, unit_nb, sps_done = ,
  70. sps_seen = , pps_seen = , sps_offset = , pps_offset = ;
  71. const uint8_t *extradata = codec_extradata + ;
  72. static const uint8_t nalu_header[] = { , , , };
  73. int length_size = (*extradata++ & 0x3) + ; // retrieve length coded size, 用于指示表示编码数据长度所需字节数
  74.  
  75. sps_offset = pps_offset = -;
  76.  
  77. /* retrieve sps and pps unit(s) */
  78. unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
  79. if (!unit_nb) {
  80. goto pps;
  81. }
  82. else {
  83. sps_offset = ;
  84. sps_seen = ;
  85. }
  86.  
  87. while (unit_nb--) {
  88. int err;
  89.  
  90. unit_size = AV_RB16(extradata);
  91. total_size += unit_size + ;
  92. if (total_size > INT_MAX - padding) {
  93. av_log(NULL, AV_LOG_ERROR,
  94. "Too big extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");
  95. av_free(out);
  96. return AVERROR(EINVAL);
  97. }
  98. if (extradata + + unit_size > codec_extradata + codec_extradata_size) {
  99. av_log(NULL, AV_LOG_ERROR, "Packet header is not contained in global extradata, "
  100. "corrupted stream or invalid MP4/AVCC bitstream\n");
  101. av_free(out);
  102. return AVERROR(EINVAL);
  103. }
  104. if ((err = av_reallocp(&out, total_size + padding)) < )
  105. return err;
  106. memcpy(out + total_size - unit_size - , nalu_header, );
  107. memcpy(out + total_size - unit_size, extradata + , unit_size);
  108. extradata += + unit_size;
  109. pps:
  110. if (!unit_nb && !sps_done++) {
  111. unit_nb = *extradata++; /* number of pps unit(s) */
  112. if (unit_nb) {
  113. pps_offset = total_size;
  114. pps_seen = ;
  115. }
  116. }
  117. }
  118.  
  119. if (out)
  120. memset(out + total_size, , padding);
  121.  
  122. if (!sps_seen)
  123. av_log(NULL, AV_LOG_WARNING,
  124. "Warning: SPS NALU missing or invalid. "
  125. "The resulting stream may not play.\n");
  126.  
  127. if (!pps_seen)
  128. av_log(NULL, AV_LOG_WARNING,
  129. "Warning: PPS NALU missing or invalid. "
  130. "The resulting stream may not play.\n");
  131.  
  132. out_extradata->data = out;
  133. out_extradata->size = total_size;
  134.  
  135. return length_size;
  136. }
  137.  
  138. // For extractVideo
  139. int h264_mp4toannexb(AVFormatContext *fmt_ctx, AVPacket *in, FILE *dst_fd)
  140. {
  141.  
  142. AVPacket *out = NULL;
  143. AVPacket spspps_pkt;
  144.  
  145. int len;
  146. uint8_t unit_type;
  147. int32_t nal_size;
  148. uint32_t cumul_size = ;
  149. const uint8_t *buf;
  150. const uint8_t *buf_end;
  151. int buf_size;
  152. int ret = , i;
  153.  
  154. out = av_packet_alloc();
  155.  
  156. buf = in->data;
  157. buf_size = in->size;
  158. buf_end = in->data + in->size;
  159.  
  160. do {
  161. ret = AVERROR(EINVAL);
  162. if (buf + /*s->length_size*/ > buf_end)
  163. goto fail;
  164.  
  165. for (nal_size = , i = ; i < /*s->length_size*/; i++)
  166. nal_size = (nal_size << ) | buf[i];
  167.  
  168. buf += ; /*s->length_size;*/
  169. unit_type = *buf & 0x1f;
  170.  
  171. if (nal_size > buf_end - buf || nal_size < )
  172. goto fail;
  173.  
  174. /*
  175. if (unit_type == 7)
  176. s->idr_sps_seen = s->new_idr = 1;
  177. else if (unit_type == 8) {
  178. s->idr_pps_seen = s->new_idr = 1;
  179. */
  180. /* if SPS has not been seen yet, prepend the AVCC one to PPS */
  181. /*
  182. if (!s->idr_sps_seen) {
  183. if (s->sps_offset == -1)
  184. av_log(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n");
  185. else {
  186. if ((ret = alloc_and_copy(out,
  187. ctx->par_out->extradata + s->sps_offset,
  188. s->pps_offset != -1 ? s->pps_offset : ctx->par_out->extradata_size - s->sps_offset,
  189. buf, nal_size)) < 0)
  190. goto fail;
  191. s->idr_sps_seen = 1;
  192. goto next_nal;
  193. }
  194. }
  195. }
  196. */
  197.  
  198. /* if this is a new IDR picture following an IDR picture, reset the idr flag.
  199. * Just check first_mb_in_slice to be 0 as this is the simplest solution.
  200. * This could be checking idr_pic_id instead, but would complexify the parsing. */
  201. /*
  202. if (!s->new_idr && unit_type == 5 && (buf[1] & 0x80))
  203. s->new_idr = 1;
  204.  
  205. */
  206. /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */
  207. if (/*s->new_idr && */unit_type == /*&& !s->idr_sps_seen && !s->idr_pps_seen*/) {
  208.  
  209. h264_extradata_to_annexb(fmt_ctx->streams[in->stream_index]->codec->extradata,
  210. fmt_ctx->streams[in->stream_index]->codec->extradata_size,
  211. &spspps_pkt,
  212. AV_INPUT_BUFFER_PADDING_SIZE);
  213.  
  214. if ((ret = alloc_and_copy(out,
  215. spspps_pkt.data, spspps_pkt.size,
  216. buf, nal_size)) < )
  217. goto fail;
  218. /*s->new_idr = 0;*/
  219. /* if only SPS has been seen, also insert PPS */
  220. }
  221. /*else if (s->new_idr && unit_type == 5 && s->idr_sps_seen && !s->idr_pps_seen) {
  222. if (s->pps_offset == -1) {
  223. av_log(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n");
  224. if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)
  225. goto fail;
  226. } else if ((ret = alloc_and_copy(out,
  227. ctx->par_out->extradata + s->pps_offset, ctx->par_out->extradata_size - s->pps_offset,
  228. buf, nal_size)) < 0)
  229. goto fail;
  230. }*/ else {
  231. if ((ret = alloc_and_copy(out, NULL, , buf, nal_size)) < )
  232. goto fail;
  233. /*
  234. if (!s->new_idr && unit_type == 1) {
  235. s->new_idr = 1;
  236. s->idr_sps_seen = 0;
  237. s->idr_pps_seen = 0;
  238. }
  239. */
  240. }
  241.  
  242. len = fwrite(out->data, , out->size, dst_fd);
  243. if (len != out->size) {
  244. av_log(NULL, AV_LOG_DEBUG, "warning, length of writed data isn't equal pkt.size(%d, %d)\n",
  245. len,
  246. out->size);
  247. }
  248. fflush(dst_fd);
  249.  
  250. next_nal:
  251. buf += nal_size;
  252. cumul_size += nal_size + ;//s->length_size;
  253. } while (cumul_size < buf_size);
  254.  
  255. /*
  256. ret = av_packet_copy_props(out, in);
  257. if (ret < 0)
  258. goto fail;
  259.  
  260. */
  261. fail:
  262. av_packet_free(&out);
  263.  
  264. return ret;
  265. }
  266.  
  267. // 使用FFmpeg从视频中抽取视频
  268. void extractVideo() {
  269.  
  270. // Start Code 特征码
  271. // PSP PPS(从codec->extradata即在数据空间中存放)
  272.  
  273. // 设置日志输出等级
  274. av_log_set_level(AV_LOG_INFO);
  275.  
  276. AVFormatContext *fmt_ctx = NULL;
  277. AVPacket pkt;
  278.  
  279. // 注册所有的格式和编解码器
  280. av_register_all();
  281.  
  282. int ret;
  283. int len;
  284. int video_index = -;
  285.  
  286. // 打开输入文件
  287. ret = avformat_open_input(&fmt_ctx, "111.mp4", NULL, NULL);
  288.  
  289. // 检查打开输入文件是否成功
  290. if (ret < ) {
  291. printf("cant open file,error message = %s", av_err2str(ret));
  292. return;
  293. }
  294.    // 打开输出文件
  295. FILE* dst_fd = fopen("111.H264", "wb"); // w 写入 b 二进制文件
  296.  
  297. // 检查输出文件打开是否成功,如果失败,就输出日志,并关闭输出文件的引用
  298. if (!dst_fd) {
  299. av_log(NULL, AV_LOG_ERROR, "Can't Open Out File!\n");
  300. avformat_close_input(&fmt_ctx);
  301. }
  302.  
  303. av_init_packet(&pkt);
  304. pkt.data = NULL;
  305. pkt.size = ;
  306.  
  307. for (int i = ; i < fmt_ctx->nb_streams; i++) {
  308. if (fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  309. video_index = i;
  310. break;
  311. }
  312. }
  313.  
  314. printf("Video Stream Index = %d", video_index);
  315.  
  316. // 检查发现视频流的结果
  317. if (video_index < ) {
  318. av_log(NULL, AV_LOG_ERROR, "Can't find Best Video Stream!\n");
  319. //printf("Reason = %s", av_err2str(ret));
  320. // 关闭输出文件和输出文件的引用
  321. avformat_close_input(&fmt_ctx);
  322. fclose(dst_fd);
  323. return;
  324. }
  325.  
  326. while (av_read_frame(fmt_ctx, &pkt) >= ) {
  327. if (pkt.stream_index == video_index) {
  328. h264_mp4toannexb(fmt_ctx, &pkt, dst_fd);
  329. }
  330. // 将引用基数减一
  331. av_packet_unref(&pkt);
  332. }
  333.  
  334. // 关闭文件(输入/输出)
  335. avformat_close_input(&fmt_ctx);
  336. if (dst_fd) {
  337. fclose(dst_fd);
  338. }
  339. }
  340.  
  341. int main(int argc, char* argv[]) {
  342. /**使用FFmpeg从视频中抽取视频 **/
  343. extractVideo();
  344. return ;
  345. }

FFmpeg开发实战(五):FFmpeg 抽取音视频的视频数据的更多相关文章

  1. [原创].NET 分布式架构开发实战五 Framework改进篇

    原文:[原创].NET 分布式架构开发实战五 Framework改进篇 .NET 分布式架构开发实战五 Framework改进篇 前言:本来打算这篇文章来写DAL的重构的,现在计划有点改变.之前的文章 ...

  2. FFmpeg开发实战(四):FFmpeg 抽取音视频的音频数据

    如何使用FFmpeg抽取音视频的音频数据,代码如下: void adts_header(char *szAdtsHeader, int dataLen); // 使用FFmpeg从视频中抽取音频 vo ...

  3. FFmpeg开发实战(一):FFmpeg 打印日志

    在Visual Studio 开发(二):VS 2017配置FFmpeg开发环境一文中,我们配置好了FFmpeg的开发环境,下面我们开始边实战,边学习FFmpeg. 首先,我们要学习的就是FFmpeg ...

  4. FFmpeg开发实战(三):FFmpeg 打印音视频Meta信息

    在之前使用FFmpeg命令行的时候,我们经常看到FFmpeg命令行在输出音视频文件的会打印一下文件的Meta信息,类似如图: 那么我们如何通过代码的方式输出这些Meta信息呢? FFmpeg提供了一个 ...

  5. FFmpeg开发实战(六):使用 FFmpeg 将YUV数据编码为视频文件

    本文中实现的一个小功能是把一个YUV原始视频数据(时间序列图像)经过h264编码为视频码流,然后在使用mp4封装格式封装. 编码&封装的流程图如下: 使用ffmpeg编码流程: 1.首先使用a ...

  6. FFmpeg开发实战(二):FFmpeg 文件操作

    FFmpeg 提供了丰富的API供我们使用,下面我们来讲述一下文件操作相关的API: FFmpeg 删除文件:avpriv_io_delete() FFmpeg 重命名文件:avpriv_io_mov ...

  7. JavaScript使用DeviceOne开发实战(四)仿优酷视频应用

    开发之前需要考虑系统的差异性,比如ios手机没有回退键,所以在开发时一定要考虑二级界面需要有回退键,否则ios的手机就会陷入到这个页面出不去了.安卓系统有回退键,针对这个情况需要要求用户在3秒钟之内连 ...

  8. Android NDK开发篇(五):Java与原生代码通信(数据操作)

    尽管说使用NDK能够提高Android程序的运行效率,可是调用起来还是略微有点麻烦.NDK能够直接使用Java的原生数据类型,而引用类型,由于Java的引用类型的实如今NDK被屏蔽了,所以在NDK使用 ...

  9. [原创].NET 业务框架开发实战之十 第一阶段总结,深入浅出,水到渠成(后篇)

    原文:[原创].NET 业务框架开发实战之十 第一阶段总结,深入浅出,水到渠成(后篇) .NET 业务框架开发实战之十 第一阶段总结,深入浅出,水到渠成(后篇) 前言:接着上篇来. 系列文章链接: [ ...

随机推荐

  1. 一丶HTML介绍

    import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('local ...

  2. thymleaf th:text="|第${user.courseSort}课|" 对于不知道的真的是解渴了

    简单描述:最近再做一个课程管理,列表显示第几课,但是后台传递过来的只是数字0~9,意味着,我得自己拼了ヾ(◍°∇°◍)ノ゙  我最烦的就是这种拼,各种难调,果真和我想的一样,4.5遍了还没出来. 我的 ...

  3. ID3、C4.5、CART决策树介绍

    决策树是一类常见的机器学习方法,它可以实现分类和回归任务.决策树同时也是随机森林的基本组成部分,后者是现今最强大的机器学习算法之一. 1. 简单了解决策树 举个例子,我们要对”这是好瓜吗?”这样的问题 ...

  4. unity3d优化-代码篇(不定期更新)

    1.Update 大多数情况是需要在update中处理很多逻辑的,然而unity3d底层是c/c++编写,逻辑层是c#,通过monobehaviour挂载于对象中,实现一些unity3d接口的重载. ...

  5. (译)(function (window, document, undefined) {})(window, document); 真正的意思

    由于非常感兴趣, 我查询了很多关于IIFE (immediately-invoked function expression)的东西, 如下: (function (window, document, ...

  6. python第十五天

    什么是模块? 一系列功能的集合 定义模块? 创建一个py文件就是一个模块,该py文件名就是模块名 怎么使用模块? 在要是用的模块文件中通过import 模块名 来导入模块 模块的四种方式? 1.编译执 ...

  7. 放球游戏B

    题目描述 校园里在上活动课,Red和Blue两位小朋友在玩一种游戏,他俩在一排N个格子里,自左到右地轮流放小球,每个格子只能放一个小球.第一个人只能放1个球,之后的人最多可以放前一个人的两倍数目的球, ...

  8. java写word转pdf

    https://blog.csdn.net/csdnFlyFun/article/details/79523262

  9. Linux命令学习总结之rmdir命令的相关资料可以参考下

    这篇文章主要介绍了Linux命令学习总结之rmdir命令的相关资料,需要的朋友可以参考下(http://www.nanke0834.com) 命令简介: rmdir命令用用来删除空目录,如果目录非空, ...

  10. Imcash:一边大裁员,一边大扩招,你能否成为区块链人才中的7%?

    农历春节后,互联网创业圈并不太平. 最早,滴滴被曝裁员,占比约为全员的15%,涉及员工约2000人.CEO程维在全员会议上称公司要做好过冬准备.此后,京东接棒,其裁员对象上升至副总裁级别高管,比例占到 ...