ffmpeg remuxing.c笔记

#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>

// av_ts2str/av_ts2timestr 给定时间值返回字符串,后面可以指定timebase
static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt, const char *tag)
{
    AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;

    printf("%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
           tag,
           av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
           av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
           av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
           pkt->stream_index);
}

int main(int argc, char **argv)
{
    AVOutputFormat *ofmt = NULL;
    AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
    AVPacket pkt;
    const char *in_filename, *out_filename;
    int ret, i;

    if (argc < 3) {
        printf("usage: %s input output\n"
               "API example program to remux a media file with libavformat and libavcodec.\n"
               "The output format is guessed according to the file extension.\n"
               "\n", argv[0]);
        return 1;
    }

    in_filename  = argv[1];
    out_filename = argv[2];

    av_register_all();
    //输入文件
    if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
        fprintf(stderr, "Could not open input file '%s'", in_filename);
        goto end;
    }
    //寻找流
    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
        fprintf(stderr, "Failed to retrieve input stream information");
        goto end;
    }

    av_dump_format(ifmt_ctx, 0, in_filename, 0);

    //创建输出
    avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
    if (!ofmt_ctx) {
        fprintf(stderr, "Could not create output context\n");
        ret = AVERROR_UNKNOWN;
        goto end;
    }

    ofmt = ofmt_ctx->oformat;
    //遍历流
    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        AVStream *in_stream = ifmt_ctx->streams[i];
        //新建stream
        AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
        if (!out_stream) {
            fprintf(stderr, "Failed allocating output stream\n");
            ret = AVERROR_UNKNOWN;
            goto end;
        }

        //copy codec的信息
        ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
        if (ret < 0) {
            fprintf(stderr, "Failed to copy context from input to output stream codec context\n");
            goto end;
        }
        out_stream->codec->codec_tag = 0;
        if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
            out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    }
    av_dump_format(ofmt_ctx, 0, out_filename, 1);
    //打开文件
    if (!(ofmt->flags & AVFMT_NOFILE)) {
        ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
        if (ret < 0) {
            fprintf(stderr, "Could not open output file '%s'", out_filename);
            goto end;
        }
    }
    //写入头信息
    ret = avformat_write_header(ofmt_ctx, NULL);
    if (ret < 0) {
        fprintf(stderr, "Error occurred when opening output file\n");
        goto end;
    }

    while (1) {
        AVStream *in_stream, *out_stream;

        ret = av_read_frame(ifmt_ctx, &pkt);
        if (ret < 0)
            break;

        in_stream  = ifmt_ctx->streams[pkt.stream_index];
        out_stream = ofmt_ctx->streams[pkt.stream_index];

        log_packet(ifmt_ctx, &pkt, "in");

        /* copy packet */
        pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
        pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
        pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
        pkt.pos = -1;
        log_packet(ofmt_ctx, &pkt, "out");
        //写入一帧数据
        ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
        if (ret < 0) {
            fprintf(stderr, "Error muxing packet\n");
            break;
        }
        av_packet_unref(&pkt);
    }
    //写入尾信息
    av_write_trailer(ofmt_ctx);
end:

    avformat_close_input(&ifmt_ctx);

    /* close output */
    if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
        avio_closep(&ofmt_ctx->pb);
    avformat_free_context(ofmt_ctx);

    if (ret < 0 && ret != AVERROR_EOF) {
        fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
        return 1;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 要下载opencv_ffmpeg341.dll文件,可以按照以下步骤操作: 1. 打开浏览器并进入OpenCV官方网站。 2. 在网站的搜索栏中输入“opencv_ffmpeg341.dll”。 3. 点击搜索按钮,浏览器会显示与该文件相关的搜索结果。 4. 选择信任的网站或官方来源,以确保下载的文件安全可靠。 5. 点击下载按钮,将文件保存到计算机的合适位置。 6. 下载完成后,查找存储位置并解压缩文件(如果是压缩文件)。 7. 将解压缩得到的opencv_ffmpeg341.dll文件移动到OpenCV项目的安装目录下。 8. 确保将文件放置在与你的编译器或IDE相匹配的文件夹中。 9. 重新启动你的计算机。 10. 在你的代码中引入该库,并确认路径和文件名拼写正确。 完成上述步骤后,你就可以成功下载并使用opencv_ffmpeg341.dll文件了。请注意,确保从可信任的来源下载文件以避免安全风险。 ### 回答2: 对于下载opencv_ffmpeg341.dll,您可以按照以下步骤进行操作。 第一步,打开您的浏览器,并搜索“opencv_ffmpeg341.dll下载”。 第二步,根据搜索结果,选择合适的下载来源。您可以选择官方网站、第三方软件下载网站或其他途径。 第三步,进入您选择的下载来源网站,并找到相应的下载链接。 第四步,点击下载链接,开始下载opencv_ffmpeg341.dll文件。 第五步,等待下载完成。下载时间取决于您的网络连接速度和文件大小。 第六步,下载完成后,将下载的opencv_ffmpeg341.dll文件保存到您的计算机上。您可以选择将其保存到特定的文件夹中,以便将来使用。 需要注意的是,下载任何文件都需要注意安全性。确保您从可信的来源下载文件,并在下载和使用过程中注意防范潜在的病毒和恶意软件。另外,根据您使用的操作系统和应用程序版本,请确保下载的opencv_ffmpeg341.dll文件与您的系统兼容。 希望以上回答能对您有所帮助!如果您有任何其他问题,请随时提问。 ### 回答3: 要下载opencv_ffmpeg341.dll,可以按照以下步骤进行操作: 1. 首先,打开你的互联网浏览器(如谷歌浏览器、火狐浏览器等)。 2. 在搜索引擎中输入"opencv_ffmpeg341.dll下载",然后按下回车键。 3. 会出现很多搜索结果,你可以选择一个可靠的网站进行下载。一般来说,官方网站或知名软件下载网站是比较可信的选择。 4. 点击下载链接,开始下载opencv_ffmpeg341.dll文件。如果有提示选择下载路径,请选择一个你希望保存文件的位置。 5. 下载完成后,你将会得到一个名为"opencv_ffmpeg341.dll"的文件。请确保保存的文件名和扩展名完全一致,以确保正确安装。 6. 按照你的项目需要,将下载的opencv_ffmpeg341.dll文件复制到你的项目文件夹中,或者系统的相关目录。 7. 根据你使用的操作系统,可能需要将该文件放置在系统文件夹(如Windows系统下的System32文件夹)或特定的软件(如OpenCV)的安装目录中。 8. 完成上述步骤后,重新启动你的应用程序,系统将会在需要时自动加载并使用opencv_ffmpeg341.dll文件。 请注意,确保从可信赖的来源下载文件以避免安全问题,并且根据你的操作系统和软件版本正确选择和安装opencv_ffmpeg341.dll文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值