※VisualStudio不使用の環境であるため、VisualStudioを使用する場合は参考サイト等を参照ください。
やったこと
画像を生成し、AForge.NETを使って動画(.avi ファイル)を作成しました。
※音声はAForge.NETでは結合できないようなので、AviUtlなどのツールを使って別途合成が必要です。
動的リンクの注意点
exeと同じフォルダ階層に dll (今回はAForge.dll と AForge.Video.dll と AForge.Video.VFW.dll) をおいてください。
そうしないと実行時に下記のエラーがでます。
ハンドルされていない例外: System.IO.FileNotFoundException: ファイルまたはアセンブリ 'AForge.Video.VFW, Version=2.2.4.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx'、またはその依存関係の 1 つが読み込めませんでした。指定されたファイルが見つかりません。
ソースコード
using System;
using System.Drawing;
class AviEnc
{
static readonly int FrameRate = 15; // 1秒あたりのフレーム数を指定する
static readonly int MovieWidth = 800;
static readonly int MovieHeight = 600;
[STAThread]
static void Main(string[] args)
{
var writer = new AForge.Video.VFW.AVIWriter();
writer.Codec = "MSVC";
writer.FrameRate = FrameRate;
writer.Open("out.avi", MovieWidth, MovieHeight);
var font = new Font("Arial", 72.0f);
var bmp = new Bitmap(MovieWidth, MovieHeight);
var g = Graphics.FromImage(bmp);
// 60秒分のフレームを作成
for ( int i=0; i<60*FrameRate; i++ ) {
g.FillRectangle(Brushes.Black, 0, 0, MovieWidth, MovieHeight);
g.DrawString(i.ToString(), font, Brushes.White, 10, 100);
writer.AddFrame(bmp); // フレームを追加
}
g.Dispose();
writer.Close();
}
}
cscでのコンパイル方法
csc /r:AForge.Video.VFW.dll AviEnc.cs
Aviファイルに関する注意点
動画再生ソフトによってはAviファイルをうまく再生できないケースがあるため、AviUtlなどのツールを使って別のフォーマット(mp4など)への変換が必要となる場合があります。