1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C# - AForge.NETを使って動画を生成する(画像のみ)

Last updated at Posted at 2021-05-16

※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 つが読み込めませんでした。指定されたファイルが見つかりません。

ソースコード

AviEnc.cs

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など)への変換が必要となる場合があります。

参考サイト

参考サイト(AviUtl関連)

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?