Sdcb.FFmpeg
FFmpeg basic .NET API generated by CppSharp
Install / Use
/learn @sdcb/Sdcb.FFmpegREADME
Sdcb.FFmpeg 
FFmpeg auto generated unsafe bindings for C#/.NET, forked from https://github.com/Ruslan-B/FFmpeg.AutoGen.
Compared to original version, For low-level APIs, Sdcb.FFmpeg optimized for:
- Using standard
[DllImport]instead ofLoadLibrary - Deleted common prefix or enums, for example it emited
AVCodecID.H264instead ofAVCodecID.AV_CODEC_ID_H264 - Combined same prefix macros into a enum, for example
AV_DICT_READ.MatchCaseinstead ofAV_DICT_MATCH_CASE - Other optimization and fixs...
Sdcb.FFmpeg also provided some high level APIs:
- wrapper of class-like APIs like
FormatContext/CodecContext/MediaDictionary - helper of wrapping existing APIs like
FramesExtensions.ApplyFilters - some source generators like
VideoFrameGenerator.Yuv420pSequence
For code generations, Sdcb.FFmpeg have benifits from:
- Minimized repository size, removed all ffmpeg
*.dllbinaries usingbfg - Auto download FFmpeg binaries from known existing sources
NuGet Packages
-
FFmpeg 7.0: | Package | Link | | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | | Sdcb.FFmpeg |
| | Sdcb.FFmpeg.runtime.windows-x64 |
|
-
FFmpeg 6.1: | Package | Link | | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | | Sdcb.FFmpeg |
| | Sdcb.FFmpeg.runtime.windows-x64 |
|
-
FFmpeg 4.4.3: | Package | Link | | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | | Sdcb.FFmpeg |
| | Sdcb.FFmpeg.runtime.windows-x64 |
|
-
FFmpeg 5.1.2: | Package | Link | | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | | Sdcb.FFmpeg |
| | Sdcb.FFmpeg.runtime.windows-x64 |
|
-
FFmpeg 6.0: | Package | Link | | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | | Sdcb.FFmpeg |
| | Sdcb.FFmpeg.runtime.windows-x64 |
|
Install
Install the Native asset nuget package:
- https://www.nuget.org/packages/Sdcb.FFmpeg
Note:
- some API isn't stable enough and subjected to change at any time, please try install the specific version and keep a eye on latest updates.
You also need to install FFmpeg binaries native assets or related nuget packages:
-
Windows:
InstallNuGetpackage:- For FFmpeg 4.4.3: https://www.nuget.org/packages/Sdcb.FFmpeg.runtime.windows-x64/4.4.3
- For FFmpeg 5.1.2: https://www.nuget.org/packages/Sdcb.FFmpeg.runtime.windows-x64/5.1.2
Note: these packages is under published under GPL license, you can also download/compile your own native assets,
Sdcb.FFmpegwill link to specificffmpegnative dynamic libraries automatically according your environment variable. -
Linux:
Use your package manager of choice, inUbuntu 22.04&ffmpeg 4.4.2specificly, you can write following commands:apt update apt install software-properties-common add-apt-repository ppa:savoury1/ffmpeg4 -y apt update apt install ffmpeg -yFor
ffmpeg 5.x, you can write following commands:apt update apt install software-properties-common add-apt-repository ppa:savoury1/ffmpeg4 -y add-apt-repository ppa:savoury1/ffmpeg5 -y apt update apt install ffmpeg -y -
Mac OS X:
Install ffmpeg via Homebrew:brew install ffmpeg
For the more sophisticated operations please refer to offical ffmpeg Documentation expecially API section of it.
Tutorial and examples
You can refer to Examples.cs for multiple tutorial and examples.
Featured examples
Example 1: Generate a video from code:
<details>// this example is based on Sdcb.FFmpeg 5.1.2
FFmpegLogger.LogWriter = (level, msg) => Console.Write(msg);
using FormatContext fc = FormatContext.AllocOutput(formatName: "mp4");
fc.VideoCodec = Codec.CommonEncoders.Libx264;
MediaStream vstream = fc.NewStream(fc.VideoCodec);
using CodecContext vcodec = new CodecContext(fc.VideoCodec)
{
Width = 800,
Height = 600,
TimeBase = new AVRational(1, 30),
PixelFormat = AVPixelFormat.Yuv420p,
Flags = AV_CODEC_FLAG.GlobalHeader,
};
vcodec.Open(fc.VideoCodec);
vstream.Codecpar!.CopyFrom(vcodec);
vstream.TimeBase = vcodec.TimeBase;
string outputPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "muxing.mp4");
fc.DumpFormat(streamIndex: 0, outputPath, isOutput: true);
using IOContext io = IOContext.OpenWrite(outputPath);
fc.Pb = io;
fc.WriteHeader();
VideoFrameGenerator.Yuv420pSequence(vcodec.Width, vcodec.Height, 600)
.ConvertFrames(vcodec)
.EncodeAllFrames(fc, null, vcodec)
.WriteAll(fc);
fc.WriteTrailer();
</details>
Example 2: Decode and remuxing mp4:
<details>// this example is based on Sdcb.FFmpeg 4.4.2
void A7r3VideoToWechat(string mp4Path)
{
using FormatContext inFc = FormatContext.OpenInputUrl(mp4Path);
inFc.LoadStreamInfo();
// prepare input stream/codec
MediaStream inAudioStream = inFc.GetAudioStream();
using CodecContext audioDecoder = new(Codec.FindDecoderById(inAudioStream.Codecpar!.CodecId));
audioDecoder.FillParameters(inAudioStream.Codecpar);
audioDecoder.Open();
audioDecoder.ChannelLayout = (ulong)ffmpeg.av_get_default_channel_layout(audioDecoder.Channels);
MediaStream inVideoStream = inFc.GetVideoStream();
using CodecContext videoDecoder = new(Codec.FindDecoderByName("h264_qsv"));
videoDecoder.FillParameters(inVideoStream.Codecpar!);
videoDecoder.Open();
// dest file
string destFile = Path.Combine(Path.GetDirectoryName(mp4Path)!, Path.GetFileNameWithoutExtension(mp4Path) + "_wechat.mp4");
using FormatContext outFc = FormatContext.AllocOutput(fileName: destFile);
// dest encoder and streams
outFc.AudioCodec = Codec.CommonEncoders.AAC;
MediaStream outAudioStream = outFc.NewStream(outFc.AudioCodec);
using CodecContext audioEncoder = new(outFc.AudioCodec)
{
Channels = 1,
SampleFormat = outFc.AudioCodec.Value.NegociateSampleFormat(AVSampleFormat.Fltp),
SampleRate = outFc.AudioCodec.Value.NegociateSampleRates(48000),
BitRate = 48000
};
audioEncoder.ChannelLayout = (ulong)ffmpeg.av_get_default_channel_layout(audioEncoder.Channels);
audioEncoder.TimeBase = new AVRational(1, audioEncoder.SampleRate);
audioEncoder.Open(outFc.AudioCodec);
outAudioStream.Codecpar!.CopyFrom(audioEncoder);
outFc.VideoCodec = Codec.FindEncoderByName("libx264");
MediaStream outVideoStream = outFc.NewStream(outFc.VideoCodec);
using VideoFilterContext vfilter = VideoFilterContext.Create(inVideoStream, "scale=1024:-1");
using CodecContext videoEncoder = new(outFc.VideoCodec)
{
Flags = AV_CODEC_FLAG.GlobalHeader,
ThreadCount = Environment.ProcessorCount,
ThreadType = ffmpeg.FF_THREAD_FRAME,
};
vfilter.ConfigureEncoder(videoEncoder);
var dict = new MediaDictionary
{
["crf"] = "30",
["preset"] = "veryslow"
};
videoEncoder.Open(outFc.VideoCodec, dict);
dict.Dump();
outVideoStream.Codecpar!.CopyFrom(videoEncoder);
outVideoStream.TimeBase = videoEncoder.TimeBase;
// begin write
using IOContext io = IOContext.OpenWrite(destFile);
outFc.Pb = io;
outFc.WriteHeader();
MediaThreadQueue<Fram
Related Skills
node-connect
339.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
339.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.8kCommit, push, and open a PR
