使用 libav 转换 mp4-ts

2024-03-09

我正在尝试使用将 mp4 文件转换为 mpegtslibav。该代码可以工作,但输出的文件无法被任何播放器播放。

这里是输入文件 http://yt-dash-mse-test.commondatastorage.googleapis.com/media/motion-20120802-85.mp4(只有视频,没有音频)

这是我到目前为止的代码:

int convert(const char *input, const char *output)
{
AVFormatContext *inputContext = NULL;
AVFormatContext *outputContext = NULL;
AVCodecContext *input_codec_context = NULL;
AVCodecContext *output_codec_context = NULL;
AVCodec *output_codec = NULL;
AVCodec *input_codec = NULL;
AVStream *stream = NULL;
AVIOContext *avioContext = NULL;

av_register_all();
avformat_network_init();
avformat_alloc_output_context2(&outputContext, NULL, "mpegts", output);
avio_open(&avioContext, output, AVIO_FLAG_WRITE);
outputContext->pb = avioContext;
stream = avformat_new_stream(outputContext, output_codec);

avformat_open_input(&inputContext, input, NULL, NULL);

input_codec_context = inputContext->streams[0]->codec;
input_codec = avcodec_find_decoder(inputContext->streams[0]->codec->codec_id);
avcodec_open2(inputContext->streams[0]->codec, input_codec, NULL);
avformat_find_stream_info(inputContext, NULL);
outputContext->oformat = av_guess_format(NULL, output, NULL);
output_codec = input_codec;

output_codec_context = avcodec_alloc_context3(NULL);
avcodec_copy_context(output_codec_context, input_codec_context);

avcodec_open2(output_codec_context, output_codec, NULL);

AVPacket packet;
av_init_packet(&packet);

avformat_write_header(outputContext, NULL);
while (!av_read_frame(inputContext, &packet)) {

    av_write_frame(outputContext, &packet);
}
av_free_packet(&packet);

av_write_trailer(outputContext);
avformat_close_input(&inputContext);
avformat_free_context(inputContext);
avformat_free_context(outputContext);

return 0;
}

任何帮助表示赞赏。


mp4 and mpegts假设 h264 具有不同的比特流格式。

你需要插入h264_mp4toannexb重新格式化您获得的 AVPacket。

分配上下文

    AVBitStreamFilterContext *bsf = av_bitstream_filter_init("h264_mp4toannexb");

在你的解复用循环中

    AVPacket new_pkt = *pkt;
    int ret = av_bitstream_filter_filter(bsfc, avctx, NULL,
                                         &new_pkt.data, &new_pkt.size,
                                         pkt->data, pkt->size,
                                         pkt->flags & AV_PKT_FLAG_KEY);
    if (ret > 0) {
        // non-zero positive, you have new memory allocated, 
        // keep it referenced in the AVBuffer
        av_free_packet(pkt);
        new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size,
                                       av_buffer_default_free, NULL, 0);
        // handle memory error here

    } else if (ret < 0) {
        // handle failure here

    }
    // ret == 0, no problems, nothing else to do
    *pkt = new_pkt;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 libav 转换 mp4-ts 的相关文章

随机推荐

  • 没有“提醒”的通知

    我正在尝试创建一个具有高优先级的通知 聊天应用程序 但我的客户要求它不会有 抬头 视图 我尝试过创建一个空布局RemoteViews设置为Notification headsUpContentView但还是什么都没有 这是我尝试过的 Int
  • 背景附件:修复了 translateX(0) 的断裂问题

    我有一个具有固定背景的滚动元素 my element background url foo png no repeat right bottom background attachment fixed 效果很好 通常情况下 但是 如果我对其
  • asp.net mvc 4通过按钮从控制器调用方法

    在我的控制器中 我有类 AccountController 并且在我有这个方法 HttpPost ValidateAntiForgeryToken public ActionResult LogOff WebSecurity Logout
  • Elasticsearch with Tire:具有多个单词的edgeNgram

    假设我有 5 部影片 无太阳 Sansa 这也是吗 索尔 古德 唯一的幸存者 我想实现一个具有以下预期行为的自动完成搜索字段 Sans gt 无太阳 珊莎 无忧无虑 gt 无太阳 所以 gt 这也是 索尔 古德 唯一幸存者 也是 gt 这也
  • ASP.NET MVC 路由和经典 ASP 主页(VBScript,不是 Web 表单)可以很好地协同工作吗?

    我们网站的根目录中有一个经典的 VBScript default asp 页面 这是一个沉重的遗留页面 现在无法选择对其进行转换 我们还有一个新的 ASP NET MVC 2 应用程序 我们希望将其托管在同一站点上 实际上 一切都配合得很好
  • 是否可以重命名带有依赖项的 Maven jar?

    我目前正在使用 jar with dependencies 程序集来创建这样的 jar 不过我的jar名字有点长 由于 AS400 上的 RPG 程序正在使用此 jar 因此我想缩短它以使这些开发人员的生活更轻松一些 但是 除了手动之外 我
  • Golang Gin“c.Param未定义(类型*gin.Context没有字段或方法Param)”

    我尝试使用 Golang 框架 Gin https github com gin gonic gin https github com gin gonic gin 我从官方github复制了示例代码 就像这样 package main im
  • 将命名元组的值从字符串转换为整数

    我正在创建一个脚本来将 csv 文件从其列标题读取到一组命名元组中 然后 我将使用这些命名元组来提取满足特定条件的数据行 我已经计算出输入 如下所示 但在将数据输出到另一个文件之前过滤数据时遇到问题 import csv from coll
  • Rails 多态依赖::destroy 无法正常工作

    假设我有三个 Active Record 模型 class Tissue has many boogers as boogerable dependent destroy end class Finger has many boogers
  • GIF 图像在 .net MAUI 应用程序中不起作用

    我想在我的应用程序中显示一些 gif 动画图像 我用来使用 Xamarin Forms 执行此操作的库是 Xamarin FFImageLoading Svg Forms 现在我们正在将应用程序迁移到 net MAUI 我看到 MAUI 文
  • Amazon AWS EC2 端口:连接被拒绝

    我刚刚在一个全新的 AWS 账户上创建了一个 EC2 实例 位于安全组后面 并在上面加载了一些软件 我正在机器上的端口 4567 当前 上运行 Sinatra 并已在我的安全组中向全世界开放该端口 此外 我可以 ssh 进入 EC2 实例
  • C 编程和 TDD

    测试驱动开发仅限于面向对象吗 或者将它与过程语言结合使用是否可能 有用 接下来的几周我必须开始一个更大的 C 项目 我正在考虑如何开发 TDD 是一个design范例 因此不依赖于任何特定的编程范例 简而言之 在编写代码之前先为代码编写测试
  • 如何在引导选项卡中添加关闭图标?

    我想在引导选项卡中添加一个关闭图标 然后我可以通过单击该图标关闭选项卡 我在下面尝试 但 X 显示的不是与选项卡标题在同一行 close font size 20px font weight bold line height 18px co
  • Spark表如何创建索引?

    我知道 Spark Sql 与 Hive 几乎相同 现在我已经创建了一个表 当我执行 Spark sql 查询来创建表索引时 它总是给我这个错误 SQL 语句中的错误 AnalysisException 创建索引语句中的输入 期望 AS 接
  • 指定的输入源无效

    使用 remix IDE 构建智能合约时 通过以下导入收到无效的输入源指定错误 import https github com aave flashloan box blob Remix contracts aave FlashLoanRe
  • 以极高的精度计算小数?

    出于好奇 我正在编写一个小型 JavaScript 程序来估计数字的值e 由系列 1 n 给出从零到无穷大 问题是 由于 IEEE 754 标准 无论我评估多少个术语 我的答案都会四舍五入到小数点后 16 位 有没有一种方法可以在值达到不考
  • 使用 java 创建快速/可靠的基准测试?

    我正在尝试使用 java 创建基准测试 目前我有以下简单的方法 public static long runTest int times long start System nanoTime String str str for int i
  • 线程环境中的 Clojure 全局变量行为

    鉴于这按我的预期工作 do println resolve a nil def a a println resolve a user a 我想了解为什么这不会 future println resolve b user b shouldn
  • 球互相弹开

    我正在编写这个脚本 其中画布中有 x 个弹跳球 在本例中为 20 个球 我的问题是 如何让它们在撞击时相互弹开 以及在撞击黄色矩形时弹开 var mycanvas document getElementById mycanvas var c
  • 使用 libav 转换 mp4-ts

    我正在尝试使用将 mp4 文件转换为 mpegtslibav 该代码可以工作 但输出的文件无法被任何播放器播放 这里是输入文件 http yt dash mse test commondatastorage googleapis com m