FFMpegCore:.NET 中进行音视频处理解决方案

server/2025/2/27 2:33:58/

简介

FFMpegCore 是一个基于 .NET Standard 的 FFMpeg/FFProbe 封装库,用于轻松将媒体分析和转换功能集成到应用程序中。它支持同步和异步调用。

安装和配置

1. 安装库

Install-Package FFMpegCore

FFMpegCore 库本身不包含 FFmpeg 可执行文件,因此需要手动下载并指定其路径。可以从 FFmpeg 官方网站 下载适合操作系统的版本。

2. 配置 FFmpeg 路径

全局配置
GlobalFFOptions.Configure(new FFOptions 
{ BinaryFolder = "./ffmpeg", TemporaryFilesFolder = "/tmp" 
});
单次运行配置
await FFMpegArguments.FromFileInput(inputPath).OutputToFile(outputPath).ProcessAsynchronously(true, new FFOptions { BinaryFolder = "./bin", TemporaryFilesFolder = "/tmp" });
通过项目文件配置

.csproj 文件中添加以下内容:

<ItemGroup><Resource Include="ffmpeg\ffmpeg.exe" /><Resource Include="ffmpeg\ffplay.exe" /><Resource Include="ffmpeg\ffprobe.exe" />
</ItemGroup>
通过配置文件配置

创建 ffmpeg.config.json 文件:

{"BinaryFolder": "./bin","TemporaryFilesFolder": "/tmp"
}

API 使用

1. 使用 FFProbe 分析媒体文件

var mediaInfo = await FFProbe.AnalyseAsync(inputPath);
// 或同步方式
var mediaInfo = FFProbe.Analyse(inputPath);

2. 使用 FFMpeg 转换媒体文件

FFMpegArguments.FromFileInput(inputPath).OutputToFile(outputPath, false, options => options.WithVideoCodec(VideoCodec.LibX264).WithConstantRateFactor(21).WithAudioCodec(AudioCodec.Aac).WithVariableBitrate(4).WithVideoFilters(filterOptions => filterOptions.Scale(VideoSize.Hd)).WithFastStart()).ProcessSynchronously();

3. 从流中转换或输出到流

await FFMpegArguments.FromPipeInput(new StreamPipeSource(inputStream)).OutputToPipe(new StreamPipeSink(outputStream), options => options.WithVideoCodec("vp9").ForceFormat("webm")).ProcessAsynchronously();

4. 辅助方法

捕获快照
var bitmap = FFMpeg.Snapshot(inputPath, new Size(200, 400), TimeSpan.FromMinutes(1));
// 或保存到磁盘
FFMpeg.Snapshot(inputPath, outputPath, new Size(200, 400), TimeSpan.FromMinutes(1));
捕获 GIF 快照
await FFMpeg.GifSnapshotAsync(inputPath, outputPath, new Size(480, -1), TimeSpan.FromSeconds(10));
合并视频片段
FFMpeg.Join(@"..\joined_video.mp4", @"..\part1.mp4", @"..\part2.mp4", @"..\part3.mp4");
创建子视频
FFMpeg.SubVideo(inputPath, outputPath, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(30));
将图片序列合并为视频
FFMpeg.JoinImageSequence(@"..\joined_video.mp4", frameRate: 1,ImageInfo.FromPath(@"..\1.png"),ImageInfo.FromPath(@"..\2.png"),ImageInfo.FromPath(@"..\3.png"));
音视频文件
FFMpeg.Mute(inputPath, outputPath);
提取音频轨道
FFMpeg.ExtractAudio(inputPath, outputPath);
添加或替换音频轨道
FFMpeg.ReplaceAudio(inputPath, inputAudioPath, outputPath);
将图片与音频文件结合
FFMpeg.PosterWithAudio(inputPath, inputAudioPath, outputPath);

总结

FFMpegCore 提供了一个强大的接口,用于在 .NET 中执行音视频处理任务,包括转码、裁剪、提取音频等操作。通过安装 FFMpegCore 库并配置 FFmpeg 可执行文件的路径,可以在 C# 应用程序中轻松实现各种音视频处理功能。


http://www.ppmy.cn/server/170914.html

相关文章

vue从入门到精通(十六):自定义指令

一、定义语法 (1).局部指令 new Vue({directives:{指令名:配置对象} })或new Vue({directives:{指令名:回调函数} })(2).全局指令 Vue.directive(指令名,配置对象) 或 Vue.directive(指令名,回调函数)二、配置对象中常用的3个回调 (1).bind:指令与元素成功绑定时调用。 (2…

Transformer LLaMA

一、Transformer Transformer&#xff1a;一种基于自注意力机制的神经网络结构&#xff0c;通过并行计算和多层特征抽取&#xff0c;有效解决了长序列依赖问题&#xff0c;实现了在自然语言处理等领域的突破。 Transformer 架构摆脱了RNNs&#xff0c;完全依靠 Attention的优…

LD_PRELOAD 绕过 disable_function 学习

借助这位师傅的文章来学习通过LD_PRELOAD来绕过disable_function的原理 【PHP绕过】LD_PRELOAD bypass disable_functions_phpid绕过-CSDN博客 感谢这位师傅的贡献 介绍 静态链接&#xff1a; &#xff08;1&#xff09;举个情景来帮助理解&#xff1a; 假设你要搬家&#x…

Vue 报错error:0308010C:digital envelope routines::unsupported 解决方案

Vue 报错error:0308010C:digital envelope routines::unsupported 解决方案 拿了一个比较老的项目部署在本地&#xff0c;然后先安装依赖npm install,最后npm run serve,在run serve的时候报错&#xff1a;报错error:0308010C:digital envelope routines::unsupported&#xff…

[算法--前缀和] 一维前缀和

目录 1. 前缀和: 是一种对暴力求解的优化.2. 前缀和? 如何利用前面的计算结果提高效率?3. 如何预处理前缀和数组(如何让处理前缀和数组的复杂度是O(N))?接下来, 我们开启一个新的专题 -> 前缀和, 第一道是模板题, 一维前缀和 1. 前缀和: 是一种对暴力求解的优化. 前缀和…

项目实战--网页五子棋(匹配模块)(5)

上期我们实现了websocket后端的大部分代码&#xff0c;这期我们实现具体的匹配逻辑 1. 定义Mather类 我们新建一个Matcher类用来实现匹配逻辑 Component public class Matcher {//每个匹配队列代表不同的段位,这里约定每一千分为一个段位private ArrayList<Queue<User…

蓝桥杯学习大纲

&#xff08;致酷德与热爱算法、编程的小伙伴们&#xff09; 在查阅了相当多的资料后&#xff0c;发现没有那篇博客、文章很符合我们备战蓝桥杯的学习路径。所以&#xff0c;干脆自己整理一篇&#xff0c;欢迎大家补充&#xff01; 一、题型分布&#xff1a; 题型分布为填空…

VSCode ssh远程连接内网服务器(不能上网的内网环境的Linux服务器)的终极解决方案

VSCode ssh远程连接内网服务器&#xff08;不能上网的内网环境的Linux服务器&#xff09; 离线下载vscode-server并安装: 如果远程端不能联网可以下载包离线安装,下载 vscode-server 的 url 需要和 vscode 客户端版本的 commit-id 对应.通过 vscode 面板的帮助->关于可以获…