public
Created

ThumbCreator

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Diagnostics;
using MediaBrowser.Library.Interop.DirectShowLib;
using MediaBrowser.Library.Logging;
 
namespace FrameGrabProvider
{
class ThumbCreator {
 
static readonly Guid videoType = new
System.Guid("73646976-0000-0010-8000-00AA00389B71");
 
 
public static bool CreateThumb(string videoFilename, string thumbFilename, double positionPercent)
{
Logger.ReportInfo("Creating thumb for " + videoFilename);
bool rval = false;
IMediaDet m = new MediaDet() as IMediaDet;
m.put_Filename(videoFilename);
 
int streamCount;
m.get_OutputStreams(out streamCount);
 
AMMediaType media_type = new AMMediaType();
 
for (int i = 0; i < streamCount; i++)
{
m.get_StreamMediaType(media_type);
 
VideoInfoHeader vih = (VideoInfoHeader)Marshal.PtrToStructure(media_type.formatPtr, typeof(VideoInfoHeader));
 
if (vih == null)
{
continue;
}
 
double pos;
m.get_StreamLength(out pos);
pos = (int)(pos * positionPercent);
 
int width = vih.BmiHeader.Width;
int height = vih.BmiHeader.Height;
 
if (height < 10 || width < 10)
{
continue;
}
 
string tempfile = Path.GetTempFileName() + ".bmp";
 
m.WriteBitmapBits(pos, width, height, tempfile);
 
if (File.Exists(tempfile))
{
using (var bitmap = new Bitmap(tempfile))
{
bitmap.Save(thumbFilename, ImageFormat.Png);
}
 
File.Delete(tempfile);
rval = true;
}
 
break;
}
 
Marshal.ReleaseComObject(m);
return rval;
 
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.