ffmpeg入门教程之Windows开发环境搭建

mac2022-06-30  22

文章目录

ffmpeg官网文档About FFmpegffmpegffplayffprobeFFmpeg Libraries for developersffmpeg-APIffmpeg开发环境搭建(Windows)Visual Studio 搭建ffmepg开发环境(Windows)Visual Studio 编写ffmpeg封装格式转换器(无编解码)GitHub:[https://github.com/AnJiaoDe/FFmpegDemoVS](https://github.com/AnJiaoDe/FFmpegDemoVS)Clion 搭建ffmepg开发环境(Windows)Clion 编写ffmpeg封装格式转换器(无编解码)GitHub:[https://github.com/AnJiaoDe/FFmpegDemo](https://github.com/AnJiaoDe/FFmpegDemo)欢迎联系、指正、批评

ffmpeg官网文档

官网链接:http://ffmpeg.org/

About FFmpeg

关于ffmpeg:

ffmpeg是领先的多媒体库,能够解码、编码、转码、封装、解封装、流操作、过滤和播放几乎所有人类和机器创造的东西。它支持最模糊的古老的格式,直到如今高端的多媒体格式。不管它们是由一些标准委员会、社区还是公司设计的,它也是高度可移植的:ffmpeg编译、运行、通过Linux、Macos x、Microsoft Windows、BSD、Solaris等多种构建环境、机器体系结构和配置测试。

它包括以下这些供应用程序使用的库:libavcodec、libavutil、libavFormat、libavfilter、libavDevice、libswscale 和libswresSample。此外,提供了可供终端用户转码和播放的ffmpeg、ffplay和ffprobe 程序。

ffmpeg

转换多媒体文件格式的命令行工具

ffplay

基于SDL和ffmpeg库的简易媒体播放器

ffprobe

简单的多媒体流分析器

FFmpeg Libraries for developers

ffmpeg为开发者提供的库:

libavutil:包含一系列简化编程的函数,如随机数生成、数据结构、数学计算、核心多媒体实用工具等。libavcodec:包含音频/视频编解码器。libavFormat:包含用于多媒体文件格式的封装器和解封装器libavDevice:可以读取多媒体设备的数据,或者输出数据到指定的多媒体设备上。libavfilter:包含媒体过滤器。libswScale:执行高度优化的图像缩放和颜色空间/像素格式转换操作。libswresSample:执行高度优化的音频重采样、重矩阵和采样格式转换操作。

ffmpeg-API

ffmpeg开发环境搭建(Windows)

1.下载相关文件

左边的windows builds是编译好的lib库等文件,可直接使用。右边是ffmpeg源码下载。

选择操作系统

下载dev和shared

dev下载最新的

shared下载最新的

2.dev目录结构

示例程序:

include头文件:

lib文件:

3.shared目录结构

bin目录文件:

Visual Studio 搭建ffmepg开发环境(Windows)

1.创建空项目FFmpegDemo

2.配置ffmpeg的include和lib

在FFmpegDemo的根目录创建文件夹ffmpeg,(此步骤可随意,关键是为了配置include,lib)

将dev下的include文件夹和lib文件夹复制到创建的ffmpeg文件夹下

将shared下的所有dll文件复制到FFmpegDemo/FFmpeg/Demo下

点击项目,再点击属性,点击VC++目录,选择平台,64位还是32位,点击包含目录右边的小三角形,再点击编辑

将ffmpeg的inlcude目录配置进去

将ffmpeg的lib目录配置进去

点击连接器下的输入,附加依赖项,将如下内容添加进去:

avcodec.lib; avformat.lib; avutil.lib; avdevice.lib; avfilter.lib;postproc.lib;swresample.lib; swscale.lib

环境搭建至此就收工了!

Visual Studio 编写ffmpeg封装格式转换器(无编解码)

下面,我们来完成一个格式转换的程序

右击源文件,添加新建项,test.cpp(名称自己定)

在dev的examples目录中随便找一个示例程序(注意,这些都是C文件),比如找一个remuxing.c,多媒体文件格式转换程序

为方便测试,快速体验ffmpeg的乐趣,将红线报错的地方先注释,main()方法参数去掉

在FFmpegDemo下创建文件夹resources用于存放资源文件

原来的

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

改成:

in_filename = "../resources/video.avi"; out_filename = "../resources/video.mp4";

目的是为了将avi格式的视频文件转为mp4格式的视频文件,注意路径千万别写错了。

然后很激动地点击run,注意debug/release,64/32,取决于开始的配置。

结果报错如下:

类型不兼容,强转一下即可; 至于define__STDC_CONSTANT_MACROS,按照提示在文件前面添加如下代码即可:

extern "C" { #ifdef __cplusplus #define __STDC_CONSTANT_MACROS #endif }

注意:示例程序是C文件,我们创建的是CPP文件,最好在文件头引入的地方用extern "C"包起来:

extern "C" { #include <libavutil/timestamp.h> #include <libavformat/avformat.h> }

然后,点击run

发现avi格式的视频转为了mp4格式的视频

注意:如果报错某某 已声明为被否决,是因为有一些函数,属性之类的,在源码中已经被标为废弃,

可以设置SDL检查为否

test.cpp:

/* * Copyright (c) 2013 Stefano Sabatini * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ /** * @file * libavformat/libavcodec demuxing and muxing API example. * * Remux streams from one container format to another. * @example remuxing.c */ extern "C" { #ifdef __cplusplus #define __STDC_CONSTANT_MACROS #endif } extern "C" { #include <libavutil/timestamp.h> #include <libavformat/avformat.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_mallocz_array(stream_mapping_size, sizeof(*stream_mapping)); if (!stream_mapping) { ret = AVERROR(ENOMEM); goto end; } ofmt = 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; }

GitHub:https://github.com/AnJiaoDe/FFmpegDemoVS

视频文件没有上传,可自己pull下来添加文件进行测试。

Clion 搭建ffmepg开发环境(Windows)

小编相当喜欢JetBrains 风格开发工具,相当智能。 Android开发用Android Studio JAVA开发用Intellj idea php开发用… python开发用… 现在就向大家分享Clion 搭建ffmepg开发环境(Windows)。

1.下载安装

clion下载地址:http://www.jetbrains.com/clion/download/#section=windows

2.按提示激活

3.配置环境

如果你已经安装了VS,可以借助VS配置clion, 点击file 的settings下面的Toolchains配置已经安装的VS路径

如果关联VS,环境并未搭建成功,可以安装MinGW 参考https://www.jianshu.com/p/1aa989808e15

Clion 编写ffmpeg封装格式转换器(无编解码)

配置如下: 和VS配置一个意思,不再赘述

点击run,右边的正方形是stop

可以看到视频格式转换成功

GitHub:https://github.com/AnJiaoDe/FFmpegDemo

欢迎联系、指正、批评

Github:https://github.com/AnJiaoDe

:https://blog.csdn.net/confusing_awakening

OpenCV入门教程:https://blog.csdn.net/confusing_awakening/article/details/113372425

ffmpeg入门教程:https://blog.csdn.net/confusing_awakening/article/details/102007792

微信公众号

QQ群

最新回复(0)