Come creare miniature da WMV il video di
-
24-09-2019 - |
Domanda
Voglio visualizzare le miniature per i video elencati sul mio sito, voglio prendere un singolo fotogramma da un video (da un momento particolare) e di visualizzarli come miniature.
E 'questo possibile utilizzando Net? (C # o VB)
Soluzione
Sì che è possibile. È necessario utilizzare DirectShow.NET. Ho trovato questo utile.
EDIT:
OK sembra che la biblioteca è cambiato da quando l'ho usato ... maledire open source:)
Ho appena tradotto al seguente codice e testato e funziona bene per me (si noti che esso presuppone c'è un wmv in c: \ aaa chiamato C4.wmv e l'uscita andrà a 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);
Anche essere consapevoli del fatto che DirectShow è somethign di un quadro in un limbo ... MS sorta di consiglio di andare a Media Foundation ... io sono una vecchia scuola DirectX programmatore che francamente non fa molto con più.