简体   繁体   English

如何在C#中的图像目录中创建视频?

[英]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. 我不在乎视频文件(编解码器)是wmv还是avi。 My only requirement is that I specify the frame rate. 我唯一的要求是我指定帧率。 This does not need to be cross platform, Windows (Vista and XP) only. 这不需要跨平台,仅限Windows(Vista和XP)。 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. 我已经阅读了一些关于使用Windows Media SDK或DirectShow的内容,但它们都没有明确提供代码示例。

Could anyone provide some insight, or some valuable resources that might help me to do this in C#? 任何人都可以提供一些见解,或者一些有价值的资源可以帮助我在C#中做到这一点吗?

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. ......把它扔出去

You can use Splicer to do this. 您可以使用Splicer执行此操作。

Please see example 3 at http://www.codeplex.com/splicer/Wiki/View.aspx?title=News%20Feeds&referringTitle=Home 请参阅http://www.codeplex.com/splicer/Wiki/View.aspx?title=News%20Feeds&referringTitle=Home上的示例3

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): 您可以使用avifil32库中的AVI *,这里有一个例子(未尝试过):
http://www.adp-gmbh.ch/csharp/mandelbrot/index.html http://www.adp-gmbh.ch/csharp/mandelbrot/index.html

This might be of interest for you: 这可能对您有意义:
http://bytescout.com/swfslideshowscout_example_c_sharp.html http://bytescout.com/swfslideshowscout_example_c_sharp.html
(make flash slideshow from JPG images using C#) (使用C#从JPG图像制作Flash幻灯片)

An ideal technology to achieve what you want is DirectShow Editing Services . DirectShow编辑服务是实现您想要的理想技术。 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. 虽然在MSDN内外有很多通用的DirectShow样本,但DES样本代码的可用性并不多。 For your purposes I'd recommend starting here for a basic explanation of using still images as a video source. 为了您的目的,我建议从这里开始以获取使用静止图像作为视频源的基本说明。

我没有尝试过,但Windows Movie Maker有一个API,你可以使用XML文件格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM