Tao.FFmpeg video encoding sample (converted from original C to C#.Net)
perhaps may be useful
надеюсь не боян 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Tao.FFmpeg;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
FFmpeg.avcodec_init();
FFmpeg.av_register_all();
// find the mpeg1 video encoder
IntPtr pcodec = FFmpeg.avcodec_find_encoder(FFmpeg.CodecID.CODEC_ID_MPEG1VIDEO);
if (pcodec == IntPtr.Zero)
{
Console.WriteLine("codec not found");
return;
}
IntPtr pcontext = FFmpeg.avcodec_alloc_context();
IntPtr ppicture = FFmpeg.avcodec_alloc_frame();
FFmpeg.AVCodecContext context = (FFmpeg.AVCodecContext)Marshal.PtrToStructure(pcontext, typeof(FFmpeg.AVCodecContext));
// put sample parameters
context.bit_rate = 400000;
// resolution must be a multiple of two
context.width = 352;
context.height = 288;
// frames per second
context.time_base.den = 25;//(AVRational){1,25};
context.time_base.num = 1;
context.gop_size = 10; // emit one intra frame every ten frames
context.max_b_frames = 1;
context.pix_fmt = FFmpeg.PixelFormat.PIX_FMT_YUV420P;
Marshal.StructureToPtr(context, pcontext, false);
// open it
if (FFmpeg.avcodec_open(pcontext, pcodec) < 0)
{
Console.WriteLine("could not open codec");
return;
}
// alloc image and output buffer
int outbuf_size = 100000;
byte[] outbuf = new byte[outbuf_size];
int size = context.width * context.height;
byte[] picture_buf = new byte[(size * 3) / 2]; // size for YUV 420
FFmpeg.AVFrame picture = (FFmpeg.AVFrame)Marshal.PtrToStructure(ppicture, typeof(FFmpeg.AVFrame));
picture.data[0] = Marshal.UnsafeAddrOfPinnedArrayElement(picture_buf, 0);
picture.data[1] = Marshal.UnsafeAddrOfPinnedArrayElement(picture_buf, size); // picture.data[0] + size;
picture.data[2] = Marshal.UnsafeAddrOfPinnedArrayElement(picture_buf, size + size / 4); // picture.data[1] + size / 4;
picture.linesize[0] = context.width;
picture.linesize[1] = context.width / 2;
picture.linesize[2] = context.width / 2;
int data1offset = size;
int data2offset = size + size / 4;
Marshal.StructureToPtr(picture, ppicture, false);
System.IO.FileStream fstream = new System.IO.FileStream(@"D:\TaoMediaPlayer\1.mpg", System.IO.FileMode.Create);
int i, out_size = 0, x, y;
// encode 1 second of video
for (i = 0; i < 250; i++)
{
// prepare a dummy image
// Y
for (y = 0; y < context.height; y++)
{
for (x = 0; x < context.width; x++)
{
picture_buf[y * picture.linesize[0] + x] = (byte)(x + y + i * 3); // picture.data[0][y * picture.linesize[0] + x] = x + y + i * 3;
}
}
// Cb and Cr
for (y = 0; y < context.height / 2; y++)
{
for (x = 0; x < context.width / 2; x++)
{
picture_buf[data1offset + y * picture.linesize[1] + x] = (byte)(128 + y + i * 2); // picture.data[1][y * picture.linesize[1] + x] = 128 + y + i * 2;
picture_buf[data2offset + y * picture.linesize[2] + x] = (byte)(64 + x + i * 5); // picture.data[2][y * picture.linesize[2] + x] = 64 + x + i * 5;
}
}
// encode the image
out_size = FFmpeg.avcodec_encode_video(pcontext, Marshal.UnsafeAddrOfPinnedArrayElement(outbuf, 0), outbuf_size, ppicture);
Console.WriteLine("encoding frame {0:D3} (size={1:D5})", i, out_size);
fstream.Write(outbuf, 0, out_size); // fwrite(outbuf, 1, out_size, f);
}
// get the delayed frames
for (; out_size > 0; i++)
{
out_size = FFmpeg.avcodec_encode_video(pcontext, Marshal.UnsafeAddrOfPinnedArrayElement(outbuf, 0), outbuf_size, IntPtr.Zero);
Console.WriteLine("encoding frame {0:D3} (size={1:D5})", i, out_size);
fstream.Write(outbuf, 0, out_size); // fwrite(outbuf, 1, out_size, f);
}
// add sequence end code to have a real mpeg file
outbuf[0] = 0x00;
outbuf[1] = 0x00;
outbuf[2] = 0x01;
outbuf[3] = 0xb7;
fstream.Write(outbuf, 0, 4); // fwrite(outbuf, 1, 4, f);
fstream.Close();
FFmpeg.avcodec_close(pcontext);
FFmpeg.av_free(pcontext);
FFmpeg.av_free(ppicture);
}
}
}

Hi. I'm trying to use this
Hi.
I'm trying to use this example, but have crash on the following function:
Marshal.StructureToPtr(context, pcontext, false);
The crash is:
Stacktrace:
at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_delegate_to_ftnptr (object) <0x00045>
at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_delegate_to_ftnptr (object) <0xffffffff>
at (wrapper unknown) AVCodecContext.StructureToPtr (object,intptr,bool) <0xffffffff>
at (wrapper runtime-invoke) AVCodecContext.runtime_invoke_void_object_intptr_bool (object,intptr,intptr,intptr) <0xffffffff>
at (wrapper managed-to-native) System.Runtime.InteropServices.Marshal.StructureToPtr (object,intptr,bool) <0x00052>
at (wrapper managed-to-native) System.Runtime.InteropServices.Marshal.StructureToPtr (object,intptr,bool) <0xffffffff>
at ConsoleApplication1.Program.Main (string[]) <0x00168>
at (wrapper runtime-invoke) ConsoleApplication1.Program.runtime_invoke_void_string[] (object,intptr,intptr,intptr) <0xffffffff>
Any idea what happens?