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

只愿长相守 提交于 2019-11-28 05:31:32

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.

loraderon

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();
    }
}

You can use the AVI* from avifil32 library, there is an example here (not tried):
http://www.adp-gmbh.ch/csharp/mandelbrot/index.html

This might be of interest for you:
http://bytescout.com/swfslideshowscout_example_c_sharp.html
(make flash slideshow from JPG images using C#)

Osama Al-Maadeed

An ideal technology to achieve what you want is DirectShow Editing Services. However, if this is a one-off project then I wouldn't bother - the learning curve can be quite steep.

There's not much in the way of DES sample code available, although there's plenty of general DirectShow samples both within and outside MSDN. For your purposes I'd recommend starting here for a basic explanation of using still images as a video source.

I have not tried it, but Windows Movie Maker has an API, and XML file format you can use.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!