小编典典

在C#中获取视频文件的缩略图

c#

我想显示我网站上列出的视频的缩略图,我想从视频中提取一个帧(在特定时间)并将其显示为缩略图。

我已经尝试过http://ramcrishna.blogspot.com/2008/09/playing-videos-like-youtube-
and.html,但无法正常工作。

使用.NET C#可以吗?


阅读 1139

收藏
2020-05-19

共1个答案

小编典典

您可以以编程方式执行FFmpeg以生成缩略图文件。然后打开图像文件以根据需要使用它。

这是一些示例代码:

public static Bitmap GetThumbnail(string video, string thumbnail)
{
    var cmd = "ffmpeg  -itsoffset -1  -i " + '"' + video + '"' + " -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x240 " + '"' + thumbnail + '"';

    var startInfo = new ProcessStartInfo
    {
        WindowStyle = ProcessWindowStyle.Hidden,
        FileName = "cmd.exe",
        Arguments = "/C " + cmd
    };

    var process = new Process
    {
        StartInfo = startInfo
    };

    process.Start();
    process.WaitForExit(5000);

    return LoadImage(thumbnail);
}

static Bitmap LoadImage(string path)
{
    var ms = new MemoryStream(File.ReadAllBytes(path));
    return (Bitmap)Image.FromStream(ms);
}
2020-05-19