Here is an example of using FFmpeg library in C#:
- Install the FFmpeg binary:
Download the latest FFmpeg build here: https://ffmpeg.org/download.html
Extract the downloaded file and store it in a location.
- Install the FFmpeg.AutoGen library:
In Visual Studio, right-click on the project and select Manage NuGet Packages.
Search for "FFmpeg.AutoGen" in the Browse tab and install the library.
- Create a C# class to access FFmpeg:
using System;
using System.Runtime.InteropServices;
using FFmpeg.AutoGen;
namespace FFmpegCSharpExample
{
public class FFmpegWrapper
{
private AVFormatContext* formatContext = null;
private AVCodecContext* codecContext = null;
private AVCodec* codec = null;
private AVPacket* packet = null;
private AVFrame* frame = null;
private SwsContext* scalingContext = null;
private byte[] buffer = null;
public FFmpegWrapper()
{
ffmpeg.av_register_all();
ffmpeg.avcodec_register_all();
ffmpeg.avformat_network_init();
formatContext = ffmpeg.avformat_alloc_context();
packet = ffmpeg.av_packet_alloc();
}
public bool OpenFile(string fileName)
{
int result = ffmpeg.avformat_open_input(&formatContext, fileName, null, null);
if (result < 0)
{
return false;
}
result = ffmpeg.avformat_find_stream_info(formatContext, null);
if (result < 0)
{
return false;
}
int streamIndex = -1;
for (int i = 0; i < formatContext->nb_streams; i++)
{
AVStream* stream = formatContext->streams[i];
AVCodecParameters* codecParameters = stream->codecpar;
AVCodec* streamCodec = ffmpeg.avcodec_find_decoder(codecParameters->codec_id);
if (streamCodec != null && codecParameters->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
{
streamIndex = i;
codecContext = ffmpeg.avcodec_alloc_context3(streamCodec);
if (codecContext == null)
{
return false;
}
result = ffmpeg.avcodec_parameters_to_context(codecContext, codecParameters);
if (result < 0)
{
return false;
}
result = ffmpeg.avcodec_open2(codecContext, streamCodec, null);
if (result < 0)
{
return false;
}
codec = streamCodec;
break;
}
}
if (streamIndex < 0)
{
return false;
}
frame = ffmpeg.av_frame_alloc();
if (frame == null)
{
return false;
}
scalingContext = ffmpeg.sws_getContext(codecContext->width, codecContext->height, codecContext->pix_fmt, codecContext->width, codecContext->height, AVPixelFormat.AV_PIX_FMT_BGR24, ffmpeg.SWS_FAST_BILINEAR, null, null, null);
buffer = new byte[ffmpeg.av_image_get_buffer_size(AVPixelFormat.AV_PIX_FMT_BGR24, codecContext->width, codecContext->height, 1)];
return true;
}
public bool ReadFrame(out byte[] frameBuffer, out int width, out int height)
{
frameBuffer = null;
width = 0;
height = 0;
while (ffmpeg.av_read_frame(formatContext, packet) >= 0)
{
if (packet->stream_index == codec->index)
{
ffmpeg.avcodec_send_packet(codecContext, packet);
while (ffmpeg.avcodec_receive_frame(codecContext, frame) >= 0)
{
if (frame->format != AVPixelFormat.AV_PIX_FMT_BGR24)
{
ffmpeg.sws_scale(scalingContext, frame->data, frame->linesize, 0, codecContext->height, frame->data, frame->linesize);
ffmpeg.av_image_fill_arrays(ref buffer, ref width, ref height, buffer.Length, AVPixelFormat.AV_PIX_FMT_BGR24, codecContext->width, codecContext->height, 1);
Marshal.Copy((IntPtr)frame->data[0], buffer, 0, buffer.Length);
frameBuffer = buffer;
return true;
}
}
}
ffmpeg.av_packet_unref(packet);
}
return false;
}
public void CloseFile()
{
ffmpeg.avcodec_free_context(&codecContext);
ffmpeg.avformat_close_input(&formatContext);
ffmpeg.avformat_network_deinit();
ffmpeg.av_frame_free(&frame);
ffmpeg.av_packet_free(&packet);
ffmpeg.sws_freeContext(scalingContext);
}
}
}
- Use the C# class to read frames from a video file:
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace FFmpegCSharpExample
{
class Program
{
static void Main(string[] args)
{
FFmpegWrapper ffmpeg = new FFmpegWrapper();
if (ffmpeg.OpenFile("video.mp4"))
{
Bitmap bitmap = null;
int width = 0, height = 0;
while (ffmpeg.ReadFrame(out byte[] frameBuffer, out width, out height))
{
bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
Marshal.Copy(frameBuffer, 0, data.Scan0, frameBuffer.Length);
bitmap.UnlockBits(data);
// Display the frame
bitmap.Save("frame.jpg");
}
ffmpeg.CloseFile();
}
}
}
}
This example opens a video file using FFmpeg, reads each frame, and converts it to a Bitmap object using the Copy() method in BitmapData and then saving it to a file.