Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a video from a directory of images in C#?

I have a directory of bitmaps that are all of the same dimension. I would like to convert these bitmaps into a video file. I don't care if the video file (codec) is wmv or avi. My only requirement is that I specify the frame rate. This does not need to be cross platform, Windows (Vista and XP) only. I have read a few things about using the Windows Media SDK or DirectShow, but none of them are that explicit about providing code samples.

Could anyone provide some insight, or some valuable resources that might help me to do this in C#?

like image 643
JP Richardson Avatar asked Oct 30 '08 19:10

JP Richardson


2 Answers

At the risk of being voted down, I'll offer a possible alternative option-- a buffered Bitmap animation.

double framesPerSecond;
Bitmap[] imagesToDisplay;     // add the desired bitmaps to this array
Timer playbackTimer;

int currentImageIndex;
PictureBox displayArea;

(...)

currentImageIndex = 0;
playbackTimer.Interval = 1000 / framesPerSecond;
playbackTimer.AutoReset = true;
playbackTimer.Elapsed += new ElapsedEventHandler(playbackNextFrame);
playbackTimer.Start();

(...)

void playbackNextFrame(object sender, ElapsedEventArgs e)
{
    if (currentImageIndex + 1 >= imagesToDisplay.Length)
    {
            playbackTimer.Stop();

            return;
    }

    displayArea.Image = imagesToDisplay[currentImageIndex++];
}

An approach such as this works well if the viewing user has access to the images, enough resources to keep the images in memory, doesn't want to wait for a video to encode, and there may exist a need for different playback speeds.

...just throwing it out there.

like image 118
crftr Avatar answered Sep 19 '22 05:09

crftr


You can use Splicer to do this.

Please see example 3 at http://www.codeplex.com/splicer/Wiki/View.aspx?title=News%20Feeds&referringTitle=Home

Edit:

using (ITimeline timeline = new DefaultTimeline(25))
{
    IGroup group = timeline.AddVideoGroup(32, 160, 100);

    ITrack videoTrack = group.AddTrack();
    IClip clip1 = videoTrack.AddImage("image1.jpg", 0, 2);
    IClip clip2 = videoTrack.AddImage("image2.jpg", 0, 2);
    IClip clip3 = videoTrack.AddImage("image3.jpg", 0, 2);
    IClip clip4 = videoTrack.AddImage("image4.jpg", 0, 2);

    double halfDuration = 0.5;

    group.AddTransition(clip2.Offset - halfDuration, halfDuration, StandardTransitions.CreateFade(), true);
    group.AddTransition(clip2.Offset, halfDuration, StandardTransitions.CreateFade(), false);

    group.AddTransition(clip3.Offset - halfDuration, halfDuration, StandardTransitions.CreateFade(), true);
    group.AddTransition(clip3.Offset, halfDuration, StandardTransitions.CreateFade(), false);

    group.AddTransition(clip4.Offset - halfDuration, halfDuration, StandardTransitions.CreateFade(), true);
    group.AddTransition(clip4.Offset, halfDuration, StandardTransitions.CreateFade(), false);

    ITrack audioTrack = timeline.AddAudioGroup().AddTrack();

    IClip audio =
        audioTrack.AddAudio("soundtrack.wav", 0, videoTrack.Duration);

    audioTrack.AddEffect(0, audio.Duration,
                        StandardEffects.CreateAudioEnvelope(1.0, 1.0, 1.0, audio.Duration));

    using (
        WindowsMediaRenderer renderer =
            new WindowsMediaRenderer(timeline, "output.wmv", WindowsMediaProfiles.HighQualityVideo))
    {
        renderer.Render();
    }
}
like image 21
loraderon Avatar answered Sep 18 '22 05:09

loraderon