在 C# 中从图像制作视频的工作方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/539257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:20:16  来源:igfitidea点击:

Working way to make video from images in C#

c#videoffmpeg

提问by Jim Mischel

Does anybody have a known reliable way to create a video from a series of image files? Before you mod me down for not searching for the answer before posting the question, and before you fire off a simple message like "use FFMPEG," read the rest of this message.

有没有人知道从一系列图像文件创建视频的可靠方法?在您因为在发布问题之前没有搜索答案而让我失望之前,在您发出“使用 FFMPEG”之类的简单消息之前,请阅读此消息的其余部分。

I'm trying to create a video, it doesn't matter too much what format as long as it's widely supported, from a series of images (.jpg, .bmp, etc.). My platform is Windows Server 2008, 64-bit. If I can make the video from within my C# program, that's great, but I'm not averse to writing a series of image files to a directory and then firing off an external program to make a video from those images.

我正在尝试从一系列图像(.jpg、.bmp 等)创建视频,只要它得到广泛支持,什么格式都无所谓。我的平台是 Windows Server 2008,64 位。如果我可以从我的 C# 程序中制作视频,那就太好了,但我并不反对将一系列图像文件写入目录,然后启动外部程序以从这些图像制作视频。

The only constraints are: it must work on my Windows Server 2008 system, and be scriptable. That is, no GUI programs that require operator intervention.

唯一的限制是:它必须在我的 Windows Server 2008 系统上运行,并且可以编写脚本。也就是说,没有需要操作员干预的 GUI 程序。

I found a number of similar questions on StackOverflow, and have tried several of the solutions, all with varying degrees of frustration and none with anything like success.

我在 StackOverflow 上发现了许多类似的问题,并尝试了几种解决方案,但都遇到了不同程度的挫败感,但都没有成功。

FFMPEG looks like a great program. Maybe it is, on Linux. The two Windows builds I downloaded are broken. Given this command line:

FFMPEG 看起来是一个很棒的程序。也许是,在 Linux 上。我下载的两个 Windows 版本都坏了。鉴于此命令行:

  ffmpeg -r 1 -f image2 -i jpeg\*.jpg video.avi

One of the builds reads the images and then crashes due to data execution prevention. The other reads the first file and then spits out an error message that says "cannot find suitable codec for file jpeg/image2.jpg". Helpful, that. In any case, FFMPEG looks like a non-starter under Windows.

其中一个构建读取图像,然后由于数据执行预防而崩溃。另一个读取第一个文件,然后吐出一条错误消息,指出“找不到文件 jpeg/image2.jpg 的合适编解码器”。有用,那个。在任何情况下,FFMPEG 在 Windows 下看起来都不是入门者。

One answer to a previous posting recommended Splicer. It looks like pretty good code. I compiled the samples and tried to run, but got some cryptic error message about a file not found. It looks like a COM class isn't registered. I suppose I need to install something (DirectShow, maybe, although I thought that was already installed?). Depending on what's required, I might have a difficult time justifying its installation on a server. ("What? Why do you need that on a server?")

以前发布的一个答案推荐了Splicer。看起来很不错的代码。我编译了示例并尝试运行,但收到​​了一些关于找不到文件的神秘错误消息。看起来 COM 类未注册。我想我需要安装一些东西(DirectShow,也许,虽然我认为已经安装了?)。根据需要什么,我可能很难证明它在服务器上的安装是合理的。(“什么?为什么在服务器上需要它?”)

Another answer suggested the AviFilelibrary from Code Project. That looks simple enough: a wrapper around the Windows AviFile subsystem. Except that the AVI files the package creates appear to have all of the frames, but only the first frame shows when I play the AVI in Windows Media Player. Well, that and if you try to create a compressed video, the program throws an exception.

另一个答案建议使用Code Project 中的AviFile库。这看起来很简单:Windows AviFile 子系统的包装器。除了包创建的 AVI 文件似乎包含所有帧,但当我在 Windows Media Player 中播放 AVI 时只显示第一帧。好吧,如果您尝试创建压缩视频,程序会抛出异常。

So, I'm left wondering if there is a good, reliable way to do what I want: on a Windows system, create an AVI or other common video file format from a series of images, either through a .NET API or using an external program. Any help?

所以,我想知道是否有一种好的、可靠的方法来做我想做的事:在 Windows 系统上,从一系列图像创建 AVI 或其他常见的视频文件格式,通过 .NET API 或使用外部程序。有什么帮助吗?

采纳答案by Jim Mischel

After working with it a while and taking a look at x264 and VideoLan, I went back to Splicer. It turns out that the cryptic error message was due to an error in my code.

在使用它一段时间并查看了 x264 和 VideoLan 之后,我又回到了 Splicer。事实证明,神秘的错误消息是由于我的代码中的错误造成的。

It looks like Splicer will do what I want: programmatically create videos from a series of images.

看起来 Splicer 会做我想要的:以编程方式从一系列图像创建视频。

Thanks to all who responded.

感谢所有回复的人。

回答by U62

The handling of video on Windows (encoding and playback) is a real quagmire. And .NET isn't going to help you at all - you're always going to be either PInvoking/COM-interoping native APIs or using a .NET wrapper that someone else has built to do the same.

Windows 上的视频处理(编码和播放)是一个真正的泥潭。并且 .NET 根本不会帮助您 - 您将始终要么使用 PInvoking/COM 互操作本机 API,要么使用其他人为执行相同操作而构建的 .NET 包装器。

Anyway, I can't say I've used it, but a good bet seems to be the x264 library that is part of the videolan project. That gives you a self-contained C library that you can PInvoke and you don't have to worry about installing and DirectShow garbage on your server. It'll generate H.264 format video which is playable in Flash as I understand it.

无论如何,我不能说我已经使用过它,但是作为videolan 项目一部分x264 库似乎是一个不错的选择。这为您提供了一个独立的 C 库,您可以对其进行 PInvoke,而且您不必担心在您的服务器上安装和 DirectShow 垃圾。它将生成 H.264 格式的视频,据我所知,它可以在 Flash 中播放。

回答by Chris Holmes

You can use VideoLan and I'd recommend it.

您可以使用 VideoLan,我会推荐它。

I've had direct experience in a C# application with VideoLan doing these two things:

我在使用 VideoLan 的 C# 应用程序中有过直接经验,可以做以下两件事:

  1. Embedding a VLC viewer in my C# application (there are 3-4 C# "wrappers" for the VLC veiwer).
  2. Using vlc.exe in a separate Process by sending it command-line arguments.
  1. 在我的 C# 应用程序中嵌入 VLC 查看器(VLC veiwer 有 3-4 个 C#“包装器”)。
  2. 通过发送命令行参数在单独的进程中使用 vlc.exe。

The fact that VideoLan has a command-line interface is a great thing. And VLC supports a command-line option that disables any visual element; so the VLC GUI doesn't pop up and neither does a command-line window. Thus, in a C# application you can farm out the video-related work to the VLC client. C# has the Process class which can manage your vlc.exe instances for you. It ends up being a pretty neat solution.

VideoLan 具有命令行界面这一事实是一件了不起的事情。并且 VLC 支持禁用任何视觉元素的命令行选项;所以 VLC GUI 不会弹出,命令行窗口也不会弹出。因此,在 C# 应用程序中,您可以将与视频相关的工作外包给 VLC 客户端。C# 有 Process 类,它可以为你管理你的 vlc.exe 实例。它最终成为一个非常简洁的解决方案。

回答by Tometzky

I've used mplayer's mencoder to create AVI (with codecs msmpeg4/mp3, readable by default with Windows Media Player) from TGA files on linux like this:

我已经使用mplayer的 mencoder 从 Linux 上的 TGA 文件创建 AVI(使用编解码器 msmpeg4/mp3,默认情况下可使用 Windows Media Player 读取),如下所示:

mencoder mf://*.tga -mf fps=25:type=tga \
  -audiofile /tmp/test.mp3 -oac copy \
  -of lavf -ovc lavc \          
  -lavcopts vcodec=msmpeg4v2:mbd=2:mv0:trell:cbp:last_pred=3:vbitrate=3000 \
  -o /tmp/test.avi

I know mplayer does have a Windows version.

我知道 mplayer 确实有 Windows 版本。

回答by 1-800-RUGS

Instead of just going through all the hassle, you could coppy all the images that you want to be included in your video to the clipboard and have it imported into Windows Live Movie Maker (dynamically, of course!) and have it compile for you!

您可以将要包含在视频中的所有图像复制到剪贴板,然后将其导入 Windows Live Movie Maker(当然是动态的!)并为您编译!

All of this CAN be done through code, but this way, you don't have to create it yourself. As you said that the platform was Windows, and ALL Windows comps have Windows Live Movie Maker installed, so it shouldn't be a problem for anyone using your application unless

所有这些都可以通过代码来完成,但这样一来,您就不必自己创建了。正如您所说的平台是 Windows,并且所有 Windows 组合都安装了 Windows Live Movie Maker,因此对于使用您的应用程序的任何人来说,这应该不是问题,除非

a) They uninstalled Windows Live Movie Maker

a) 他们卸载了 Windows Live Movie Maker

b) They are using a computer running on a Windows Version more than 8 or 9 years old...

b) 他们使用的计算机运行的 Windows 版本已超过 8 或 9 年...