C#从MP4流中提取位图

时间:2012-10-07 20:10:00

标签: c# dll bitmap stream mp4

最近,我在互联网上搜索了一个很好的C#lib来从MP4视频中提取位图,我找到了类似于这段代码的东西,但没有找到MP4:

        VideoStream stream = aviManager.GetVideoStream(@"C:\Users\User\Desktop\video.mp4");
        //the video.mp4 is usually .avi or .mpeg
        for (int n = 0; n < stream.CountFrames; n++)
        {
            Bitmap bmp = new Bitmap(stream.GetBitmap(Position));
            bmp.Save(@"C:\Users\Nehoray\Desktop\Something.png");
            bmp.Dispose;
        }

任何人都可以给我一个像这样的简单代码的库,仅适用于MP4吗? :)

3 个答案:

答案 0 :(得分:2)

我使用Expression Encoder SDK执行类似的任务。他们declare支持MP4。看一下AudioVideoFile.GetThumbnail方法。

答案 1 :(得分:1)

对于avi,我曾经写过类似的东西:

string filename = "file.avi";
string storagepath = @"temp\";
static int counter = 0;
MediaDetClass memde;

private void button1_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        // Create the subfolder
        System.IO.Directory.CreateDirectory("temp");

    // Some properities
        counter = 0;
        memde = new MediaDetClass();
        Image img;
        memde.Filename = openFileDialog1.FileName;
        memde.CurrentStream = 0;
        int len = (int)memde.StreamLength;
        float percent = 0.002f;//how far do u want for a single step

        for (float i = 0.0f; i < len; i = i + (float)(percent * len))
        {

            counter++;
            string fbitname = storagepath + counter.ToString();
            memde.WriteBitmapBits(i, 850, 480, fbitname + ".bmp");
            //img = Image.FromFile(fbitname + ".bmp");
            //img.Save(fbitname + ".bmp", ImageFormat.Bmp);
            //img.Dispose();
            //System.IO.File.Delete(i + fbitname + ".bmp");
        }
}

使用的库是Interop.DexterLib.dll,我现在不确定你是否可以在C#资源中找到它。如果你愿意,我可以分享链接。


GitHub Repo只需下载RAW文件(有C#文件夹)。

答案 2 :(得分:0)

对于遇到此帖子的其他人,请查看此答案:Get thumbnail image of video file in C#

其中一篇帖子提到名为NReco.VideoConverter的NuGet包。这使您可以从MP4文件中轻松创建缩略图:

var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
ffMpeg.GetVideoThumbnail(pathToVideoFile, pathToNewThumbnail, locationInSeconds);