3

我想显示我网站上列出的视频的缩略图,我想从视频中(从特定时间)获取单个帧并将它们显示为缩略图。

这可能使用 .Net 吗?(C# 或 VB)

4

1 回答 1

2

是的,这是可能的。您需要使用 DirectShow.NET。我发现很有用。

编辑 :

好吧,自从我使用它以来,图书馆似乎发生了变化……诅咒开源:)

我刚刚翻译成以下代码并进行了测试,它对我来说很好(注意它假设 c:\aaa 中有一个名为 C4.wmv 的 wmv,输出将转到 c:\aaa\out.bmp)

IGraphBuilder graphbuilder = (IGraphBuilder)new FilterGraph();
      ISampleGrabber samplegrabber = (ISampleGrabber) new SampleGrabber();
      graphbuilder.AddFilter((IBaseFilter)samplegrabber, "samplegrabber");

      AMMediaType mt = new AMMediaType();
      mt.majorType = MediaType.Video;
      mt.subType = MediaSubType.RGB24;
      mt.formatType = FormatType.VideoInfo;
      samplegrabber.SetMediaType(mt);

      int hr = graphbuilder.RenderFile("C:\\aaa\\c4.wmv", null);

      IMediaEventEx mediaEvt = (IMediaEventEx)graphbuilder;
      IMediaSeeking mediaSeek = (IMediaSeeking)graphbuilder;
      IMediaControl mediaCtrl = (IMediaControl)graphbuilder;
      IBasicAudio basicAudio = (IBasicAudio)graphbuilder;
      IVideoWindow videoWin = (IVideoWindow)graphbuilder;

      basicAudio.put_Volume(-10000);
      videoWin.put_AutoShow(OABool.False);

      samplegrabber.SetOneShot(true);
      samplegrabber.SetBufferSamples(true);

      long d = 0;
      mediaSeek.GetDuration(out d);
      long numSecs = d / 10000000;

      long secondstocapture = (long)(numSecs * 0.10f);


      DsLong rtStart, rtStop;
      rtStart = new DsLong(secondstocapture * 10000000);
      rtStop = rtStart;
      mediaSeek.SetPositions(rtStart, AMSeekingSeekingFlags.AbsolutePositioning, rtStop, AMSeekingSeekingFlags.AbsolutePositioning);

      mediaCtrl.Run();
      EventCode evcode;
      mediaEvt.WaitForCompletion(-1, out evcode);

      VideoInfoHeader videoheader = new VideoInfoHeader();
      AMMediaType grab = new AMMediaType();
      samplegrabber.GetConnectedMediaType(grab);
      videoheader =
      (VideoInfoHeader)Marshal.PtrToStructure(grab.formatPtr,
      typeof(VideoInfoHeader));


      int width = videoheader.SrcRect.right;
      int height = videoheader.SrcRect.bottom;
      Bitmap b = new Bitmap(width, height, PixelFormat.Format24bppRgb);

      uint bytesPerPixel = (uint)(24 >> 3);
      uint extraBytes = ((uint)width * bytesPerPixel) % 4;
      uint adjustedLineSize = bytesPerPixel * ((uint)width + extraBytes);
      uint sizeOfImageData = (uint)(height) * adjustedLineSize;


      BitmapData bd1 = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
      int bufsize = (int)sizeOfImageData;
      int n = samplegrabber.GetCurrentBuffer(ref bufsize, bd1.Scan0);
      b.UnlockBits(bd1);
      b.RotateFlip(RotateFlipType.RotateNoneFlipY);
      b.Save("C:\\aaa\\out.bmp");
      Marshal.ReleaseComObject(graphbuilder);
      Marshal.ReleaseComObject(samplegrabber);

还要注意 DirectShow 是一个处于边缘的框架...... MS有点建议你去媒体基金会......我是一个老派的 DirectX 程序员,坦率地说,它不再做太多事情了。

于 2010-04-06T11:56:54.493 回答