SkillAgentSearch skills...

Sdcb.FFmpeg

FFmpeg basic .NET API generated by CppSharp

Install / Use

/learn @sdcb/Sdcb.FFmpeg
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Sdcb.FFmpeg main

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 of LoadLibrary
  • Deleted common prefix or enums, for example it emited AVCodecID.H264 instead of AVCodecID.AV_CODEC_ID_H264
  • Combined same prefix macros into a enum, for example AV_DICT_READ.MatchCase instead of AV_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 *.dll binaries using bfg
  • Auto download FFmpeg binaries from known existing sources

NuGet Packages

  • FFmpeg 7.0: | Package | Link | | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | | Sdcb.FFmpeg | NuGet | | Sdcb.FFmpeg.runtime.windows-x64 | NuGet |

  • FFmpeg 6.1: | Package | Link | | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | | Sdcb.FFmpeg | NuGet | | Sdcb.FFmpeg.runtime.windows-x64 | NuGet |

  • FFmpeg 4.4.3: | Package | Link | | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | | Sdcb.FFmpeg | NuGet | | Sdcb.FFmpeg.runtime.windows-x64 | NuGet |

  • FFmpeg 5.1.2: | Package | Link | | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | | Sdcb.FFmpeg | NuGet | | Sdcb.FFmpeg.runtime.windows-x64 | NuGet |

  • FFmpeg 6.0: | Package | Link | | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | | Sdcb.FFmpeg | NuGet | | Sdcb.FFmpeg.runtime.windows-x64 | NuGet |

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:
    Install NuGet package:

    • 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.FFmpeg will link to specific ffmpeg native dynamic libraries automatically according your environment variable.

  • Linux:
    Use your package manager of choice, in Ubuntu 22.04 & ffmpeg 4.4.2 specificly, you can write following commands:

    apt update
    apt install software-properties-common
    add-apt-repository ppa:savoury1/ffmpeg4 -y
    apt update
    apt install ffmpeg -y
    

    For 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

View on GitHub
GitHub Stars397
CategoryDevelopment
Updated1mo ago
Forks61

Languages

C#

Security Score

95/100

Audited on Feb 22, 2026

No findings