ffmpeg入门之Windows开发之二(视频转码)

添加ffmpeg windows编译安装及入门指南-CSDN博客 的头文件和依赖库如下:

main 函数如下:

extern "C"
{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS

#endif

}
extern "C" {

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

}

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()
{
    AVOutputFormat* ofmt = NULL;
    AVFormatContext* ifmt_ctx = NULL, * ofmt_ctx = NULL;
    AVPacket pkt;
    const char* in_filename, * out_filename;
    int ret;
    unsigned int  i;
    int stream_index = 0;
    int* stream_mapping = NULL;
    int stream_mapping_size = 0;

    /*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 = "../resources/video.avi";
    out_filename = "../resources/video.mp4";

    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;
    }

    stream_mapping_size = ifmt_ctx->nb_streams;
    stream_mapping = (int*)av_malloc_array(stream_mapping_size, sizeof(*stream_mapping));
    if (!stream_mapping) {
        ret = AVERROR(ENOMEM);
        goto end;
    }

    ofmt = (AVOutputFormat * )ofmt_ctx->oformat;

    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        AVStream* out_stream;
        AVStream* in_stream = ifmt_ctx->streams[i];
        AVCodecParameters* in_codecpar = in_stream->codecpar;

        if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
            in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
            in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
            stream_mapping[i] = -1;
            continue;
        }

        stream_mapping[i] = stream_index++;

        out_stream = avformat_new_stream(ofmt_ctx, NULL);
        if (!out_stream) {
            fprintf(stderr, "Failed allocating output stream\n");
            ret = AVERROR_UNKNOWN;
            goto end;
        }

        ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
        if (ret < 0) {
            fprintf(stderr, "Failed to copy codec parameters\n");
            goto end;
        }
        out_stream->codecpar->codec_tag = 0;
    }
    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];
        if (pkt.stream_index >= stream_mapping_size ||
            stream_mapping[pkt.stream_index] < 0) {
            av_packet_unref(&pkt);
            continue;
        }

        pkt.stream_index = stream_mapping[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, AVRounding(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
        pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AVRounding(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);

    av_freep(&stream_mapping);

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

最后video.avi的格式转换为output.avi文件。

源码下载:

链接:https://pan.baidu.com/s/1ygX8kTARSfyrEhlZMULY4A 
提取码:0vg7 

文章参考:

ffmpeg入门教程之Windows开发环境搭建_windows pkg-config --cflags -- libavformat libavco-CSDN博客 缺的库下载参考 :

ffmpeg5.0+h264+h265 windows下编译方法_ffmpeg 5.0 win10 dll 编译-CSDN博客

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/286362.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

概率论复习

第一章&#xff1a;随机概率及其概率 A和B相容就是 AB 空集 全概率公式与贝叶斯公式&#xff1a; 伯努利求概率&#xff1a; 第二章&#xff1a;一维随机变量及其分布&#xff1a; 离散型随机变量求分布律&#xff1a; 利用常规离散性分布求概率&#xff1a; 连续性随机变量…

一份来自政治学研究者的大模型“实用指南”

大模型&#xff0c;对一个社会科学的研究者意味着什么&#xff1f; 如果抛开一些为了追新打快蹭热点而进行的类似大模型万物的“交叉科学”与“跨界研究”&#xff0c;只是将大模型视为一个“强大的工具”&#xff0c;那么这种“智能工具”如何赋能社会科学研究呢&#xff1f;…

轻空间气膜体育馆打造绿色运动空间

近年来&#xff0c;我国为全面实施全民健身战略&#xff0c;坚持“发展群众体育&#xff0c;服务健康中国”的理念&#xff0c;积极推动群众参与各类体育活动。为了满足全民健身和全面小康的深度融合发展需求&#xff0c;我国正在不断拓展公共体育设施的建设与开放。气膜体育馆…

光储充综合新能源储能系统研究笔记

1. 微电网与储能 1.1. 储能 1.1.1. 概述 储能是指电力储能&#xff0c;属于一种电网供需平衡技术。电力储能的形式通常是通过将电能转化为动能、(水)势能、化学能等形式进行储存&#xff0c;在需要时再转化回电能。尽管电力储能技术并非新兴技术&#xff0c;但从产业角度来看…

服务器解析漏洞是什么?攻击检测及修复

服务器解析漏洞&#xff08;Server-side Include Vulnerability&#xff0c;SSI漏洞&#xff09;是一种安全漏洞&#xff0c;通常出现在支持服务器端包含&#xff08;SSI&#xff09;功能的Web服务器上。SSI是一种在Web页面中嵌入动态内容的技术&#xff0c;允许开发人员将外部…

Amazon CodeWhisperer 体验

文章作者&#xff1a;jiangbei 1. CodeWhisperer 安装 1.1 先安装 IDEA&#xff0c;如下图&#xff0c;IDEA2022 安装为例&#xff1a; 亚马逊云科技开发者社区为开发者们提供全球的开发技术资源。这里有技术文档、开发案例、技术专栏、培训视频、活动与竞赛等。帮助中国开发者…

漏洞复现-TurboMail viewfile 文件读取漏洞(附漏洞检测脚本)

免责声明 文章中涉及的漏洞均已修复&#xff0c;敏感信息均已做打码处理&#xff0c;文章仅做经验分享用途&#xff0c;切勿当真&#xff0c;未授权的攻击属于非法行为&#xff01;文章中敏感信息均已做多层打马处理。传播、利用本文章所提供的信息而造成的任何直接或者间接的…

目标检测——YOLO算法解读(通俗易懂版)

论文&#xff1a;You Only Look Once: Unified, Real-Time Object Detection 作者&#xff1a;Joseph Redmon, Santosh Divvala, Ross Girshick, Ali Farhadi 链接&#xff1a;https://arxiv.org/abs/1506.02640 代码&#xff1a;http://pjreddie.com/yolo/ yolo系列检测算法开…

C#经常用的加密解密算法

1. 引言 在软件开发中&#xff0c;数据的安全性和保密性非常重要。为了保护数据免受未经授权的访问和泄露&#xff0c;我们经常需要对敏感数据进行加密和解密。在C#中&#xff0c;有许多常用的加密解密方法可供选择。本文将详细介绍C#中经常使用的加密和解密方法。 目录 1. 引…

QT设置鼠标样式 QWidget::setCusor()

1、使用Qt内置鼠标样式 例如手型&#xff1a; button->setCursor(Qt::PointingHandCursor); 其他类型&#xff1a; 2.自定义鼠标样式3种方式&#xff1a; 2.1.使用函数生成鼠标样式的图片 2.2使用画图工具生成鼠标样式的图片 2.3使用XPM生成鼠标样式 这三种方式参考&…

「吞噬星空」黑龙飞船售128亿,罗峰晋级恒星买地球,大危机降至

Hello,小伙伴们&#xff0c;我是拾荒君。 《吞噬星空》的国漫第98集已经更新&#xff0c;如同众多的粉丝一样&#xff0c;拾荒君也迫不及待地观看了这一集。在这一集中&#xff0c;布罗临死前向诺岚山家族透露了地球的坐标&#xff0c;这一举动无疑将地球暴露在了危险之中。现…

[笔记] linux 4.19 版本 Kbuild 编译流程解析

目录 写在前面与一些说明linux 编译工程框架 KbuildTop-Makefile 文件 linux 编译命令make helpdistclean 目标defconfig 目标build 变量与 $(build)dir 赋值使用 obj 变量实现包含目标模块下的 makefiledefconfig 规则展开defconfig 的生成命令解析 make 默认目标生成 image.g…