本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10506662.html

FFmpeg封装格式处理相关内容分为如下几篇文章:

[1]. FFmpeg封装格式处理-简介

[2]. FFmpeg封装格式处理-解复用例程

[3]. FFmpeg封装格式处理-复用例程

[4]. FFmpeg封装格式处理-转封装例程

5. 转封装例程

转封装是将一种封装格式转换为另一种封装格式,不涉及编解码操作,转换速度非常快。

5.1 源码

源码修改自 FFmpeg 4.1 自带的例程 remuxing.c。代码非常简短:

  1. #include <libavutil/timestamp.h>
  2. #include <libavformat/avformat.h>
  3. int main(int argc, char **argv)
  4. {
  5. AVOutputFormat *ofmt = NULL;
  6. AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
  7. AVPacket pkt;
  8. const char *in_filename, *out_filename;
  9. int ret, i;
  10. int stream_index = 0;
  11. int *stream_mapping = NULL;
  12. int stream_mapping_size = 0;
  13. if (argc < 3) {
  14. printf("usage: %s input output\n"
  15. "API example program to remux a media file with libavformat and libavcodec.\n"
  16. "The output format is guessed according to the file extension.\n"
  17. "\n", argv[0]);
  18. return 1;
  19. }
  20. in_filename = argv[1];
  21. out_filename = argv[2];
  22. // 1. 打开输入
  23. // 1.1 读取文件头,获取封装格式相关信息
  24. if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
  25. printf("Could not open input file '%s'", in_filename);
  26. goto end;
  27. }
  28. // 1.2 解码一段数据,获取流相关信息
  29. if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
  30. printf("Failed to retrieve input stream information");
  31. goto end;
  32. }
  33. av_dump_format(ifmt_ctx, 0, in_filename, 0);
  34. // 2. 打开输出
  35. // 2.1 分配输出ctx
  36. avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
  37. if (!ofmt_ctx) {
  38. printf("Could not create output context\n");
  39. ret = AVERROR_UNKNOWN;
  40. goto end;
  41. }
  42. stream_mapping_size = ifmt_ctx->nb_streams;
  43. stream_mapping = av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
  44. if (!stream_mapping) {
  45. ret = AVERROR(ENOMEM);
  46. goto end;
  47. }
  48. ofmt = ofmt_ctx->oformat;
  49. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  50. AVStream *out_stream;
  51. AVStream *in_stream = ifmt_ctx->streams[i];
  52. AVCodecParameters *in_codecpar = in_stream->codecpar;
  53. if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
  54. in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
  55. in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  56. stream_mapping[i] = -1;
  57. continue;
  58. }
  59. stream_mapping[i] = stream_index++;
  60. // 2.2 将一个新流(out_stream)添加到输出文件(ofmt_ctx)
  61. out_stream = avformat_new_stream(ofmt_ctx, NULL);
  62. if (!out_stream) {
  63. printf("Failed allocating output stream\n");
  64. ret = AVERROR_UNKNOWN;
  65. goto end;
  66. }
  67. // 2.3 将当前输入流中的参数拷贝到输出流中
  68. ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
  69. if (ret < 0) {
  70. printf("Failed to copy codec parameters\n");
  71. goto end;
  72. }
  73. out_stream->codecpar->codec_tag = 0;
  74. }
  75. av_dump_format(ofmt_ctx, 0, out_filename, 1);
  76. if (!(ofmt->flags & AVFMT_NOFILE)) { // TODO: 研究AVFMT_NOFILE标志
  77. // 2.4 创建并初始化一个AVIOContext,用以访问URL(out_filename)指定的资源
  78. ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
  79. if (ret < 0) {
  80. printf("Could not open output file '%s'", out_filename);
  81. goto end;
  82. }
  83. }
  84. // 3. 数据处理
  85. // 3.1 写输出文件头
  86. ret = avformat_write_header(ofmt_ctx, NULL);
  87. if (ret < 0) {
  88. printf("Error occurred when opening output file\n");
  89. goto end;
  90. }
  91. while (1) {
  92. AVStream *in_stream, *out_stream;
  93. // 3.2 从输出流读取一个packet
  94. ret = av_read_frame(ifmt_ctx, &pkt);
  95. if (ret < 0)
  96. break;
  97. in_stream = ifmt_ctx->streams[pkt.stream_index];
  98. if (pkt.stream_index >= stream_mapping_size ||
  99. stream_mapping[pkt.stream_index] < 0) {
  100. av_packet_unref(&pkt);
  101. continue;
  102. }
  103. pkt.stream_index = stream_mapping[pkt.stream_index];
  104. out_stream = ofmt_ctx->streams[pkt.stream_index];
  105. /* copy packet */
  106. // 3.3 更新packet中的pts和dts
  107. // 关于AVStream.time_base的说明:
  108. // 输入:输入流中含有time_base,在avformat_find_stream_info()中可取到每个流中的time_base
  109. // 输出:avformat_write_header()会根据输出的封装格式确定每个流的time_base并写入文件中
  110. // AVPacket.pts和AVPacket.dts的单位是AVStream.time_base,不同的封装格式其AVStream.time_base不同
  111. // 所以输出文件中,每个packet需要根据输出封装格式重新计算pts和dts
  112. av_packet_rescale_ts(&pkt, in_stream->time_base, out_stream->time_base);
  113. pkt.pos = -1;
  114. // 3.4 将packet写入输出
  115. ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
  116. if (ret < 0) {
  117. printf("Error muxing packet\n");
  118. break;
  119. }
  120. av_packet_unref(&pkt);
  121. }
  122. // 3.5 写输出文件尾
  123. av_write_trailer(ofmt_ctx);
  124. end:
  125. avformat_close_input(&ifmt_ctx);
  126. /* close output */
  127. if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
  128. avio_closep(&ofmt_ctx->pb);
  129. avformat_free_context(ofmt_ctx);
  130. av_freep(&stream_mapping);
  131. if (ret < 0 && ret != AVERROR_EOF) {
  132. printf("Error occurred: %s\n", av_err2str(ret));
  133. return 1;
  134. }
  135. return 0;
  136. }

5.2 编译

源文件为remuxing.c,在SHELL中执行如下编译命令:

  1. gcc -o remuxing remuxing.c -lavformat -lavcodec -lavutil -g

生成可执行文件remuxing

5.3 验证

测试文件下载:tnshih.flv



先看一下测试用资源文件的格式:

  1. think@opensuse> ffprobe tnliny.flv
  2. ffprobe version 4.1 Copyright (c) 2007-2018 the FFmpeg developers
  3. Input #0, flv, from 'tnliny.flv':
  4. Metadata:
  5. encoder : Lavf58.20.100
  6. Duration: 00:02:26.54, start: 0.000000, bitrate: 446 kb/s
  7. Stream #0:0: Video: h264 (High), yuv420p(progressive), 800x450, 25 fps, 25 tbr, 1k tbn, 50 tbc
  8. Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp

运行如下命令进行测试:

  1. ./remuxing tnliny.flv tnliny.ts

使用ffprobe检测输出文件正常。使用ffplay播放输出文件正常,播放效果和原始的测试文件一致。

FFmpeg封装格式处理4-转封装例程的更多相关文章

  1. FFmpeg封装格式处理

    本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10506636.html FFmpeg封装格式处理相关内容分为如下几篇文章: [1]. F ...

  2. 音视频处理之FFmpeg封装格式20180510

    一.FFMPEG的封装格式转换器(无编解码) 1.封装格式转换 所谓的封装格式转换,就是在AVI,FLV,MKV,MP4这些格式之间转换(对应.avi,.flv,.mkv,.mp4文件). 需要注意的 ...

  3. FFmpeg封装格式处理3-复用例程

    本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10506653.html FFmpeg封装格式处理相关内容分为如下几篇文章: [1]. F ...

  4. FFmpeg封装格式处理2-解复用例程

    本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10506642.html FFmpeg封装格式处理相关内容分为如下几篇文章: [1]. F ...

  5. 最简单的基于FFMPEG的封装格式转换器(无编解码)

    本文介绍一个基于FFMPEG的封装格式转换器.所谓的封装格式转换,就是在AVI,FLV,MKV,MP4这些格式之间转换(相应.avi,.flv,.mkv,.mp4文件).须要注意的是,本程序并不进行视 ...

  6. 最简单的基于FFmpeg的封装格式处理:视音频复用器(muxer)

    ===================================================== 最简单的基于FFmpeg的封装格式处理系列文章列表: 最简单的基于FFmpeg的封装格式处理 ...

  7. 最简单的基于FFmpeg的封装格式处理:视音频分离器(demuxer)

    ===================================================== 最简单的基于FFmpeg的封装格式处理系列文章列表: 最简单的基于FFmpeg的封装格式处理 ...

  8. 最简单的基于FFmpeg的封装格式处理:视音频分离器简化版(demuxer-simple)

    ===================================================== 最简单的基于FFmpeg的封装格式处理系列文章列表: 最简单的基于FFmpeg的封装格式处理 ...

  9. 如何查看ffmpeg支持的编码器和封装格式

    查看支持的编码器(也就是-vcodec后面可以接的参数):ffmpeg -codecs 查看支持的封装格式(也就是-f后面可以接的参数):ffmpeg -formats 查看支持的滤镜(也就是-vf后 ...

随机推荐

  1. jQuery的事件,动画效果等

    一.事件 click(function(){}) 点击事件 hover(function(){})   悬浮事件,这是jQuery封装的,js没有不能绑定事件 focus(function(){}) ...

  2. (PMP)第12章-----项目采购管理

    B D 12.1 规划采购管理 输入 工具与技术 输出 1.项目章程 2.商业文件 (商业文件, 效益管理计划) 3.项目管理计划 (范围,质量,资源管理计划, 范围基准) 4.项目文件 (里程碑清单 ...

  3. 第一节20181109 《Linux就该这么学》

    在网上发现了刘老师的linux课程,关注了一段时间感觉很好就在10月1日活动日报了第19期的班,希望自己能坚持下来学好linux.

  4. js实现粒子特效,particles.js的使用

    今天偶然看到了一个比较炫酷的js网页.是粒子特效的,就试着用了用.一下是步骤,方便以后查看使用. 1.在网站下载源码https://github.com/VincentGarreau/particle ...

  5. js-function复制变量值和传递参数

    <title>function复制变量值</title></head><body> <script> var a={ num:10 } // ...

  6. Hive记录-Beeline常用操作命令

    Beeline和其他工具有一些不同,执行查询都是正常的SQL输入,但是如果是一些管理的命令, 比如进行连接,中断,退出,执行Beeline命令需要带上"!",不需要终止符.常用命令 ...

  7. 拷问传统企业CIO:微服务化值得吗?

    所谓数字化转型升级,就是以数字技术优化传统资源,企业需要谨慎地选择合适的技术逐步完成自己的数字化战略.以推出轻舟微服务平台的网易云为代表,云计算公司正在微服务领域发力,促进企业数字化创新.那么,微服务 ...

  8. 【BZOJ3676】 [Apio2014]回文串(SAM,manacher)

    传送门 BZOJ 洛谷 Solution 考虑我们每找到一个回文串就更新一次答案,跑个SAM,这样子复杂度是爆炸的. 接下来的就是优化: 我们可以倍增跳直到跳不了,最后的siz就是出现次数. 没了?没 ...

  9. 还原是不可能还原的,这辈子都不可能还原(手动笑cry)

    不好意思,我又把原厂避震换回border的绞牙了. 这套台湾绞牙已经陪伴了我第三个年头了,本次主要是调节了桶身高度,让车身升高了一下,现在是前面3指松将近4指.后面2指(以前是前面2指半.后面1指松2 ...

  10. Ngon 是啥

    https://www.gamefromscratch.com/post/2011/07/11/So-whats-an-ngon-anyways.aspx 在 blender 里面 Add 一个 Cy ...