c# - How do i loop thorugh all double positions to save all frames to hard disk? -
i'm using framegrabber.cs , added dll reference: jockersoft.media.dll , interop.qedit.
now in form1 code in constructor i'm trying retrieve frames in position , save each frame new bitmap file on hard disk can't figure out how it.
i don't know how loop through frames/double[] position wanted see information how many frames there framerate 17.8....
i want list/array of frames , save each frame hard disk , manipulations on frames.
this site got example from. tried there , on source code couldn't figure out how it.
** have trackbar1 in designer wanted able scroll through frames doesn't work good.
this form1 code doesn't work can 1 frame.
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using extractframes.properties; using extractframes; using jockersoft.media; using jockersoft; using interop.qedit; using interop; using system.runtime.interopservices; namespace extractframes { public partial class form1 : form { double x; string sf; double[] streamdouble ; string strvideofile; public form1() { initializecomponent(); sf = @"d:\frames\"; strvideofile = @"d:\my movie.wmv"; (int x = 0; x < 40; x++) { streamdouble = new double[x]; saveframesfromvideo(strvideofile, streamdouble, sf + x.tostring("d6") + ".bmp"); } //getframerate(); } private void form1_load(object sender, eventargs e) { } private void getframerate() { // framerate mediadet md = new mediadet(); md.filename = strvideofile; //md.currentstream = 0; // choose video stream x = md.framerate; double = md.streamlength; } public static void saveframesfromvideo(string videofile, double[] positions, string outputbitmapfiles) { if (videofile == null) throw new argumentnullexception("videofile"); double streamlength; imediadet mediadet = null; try { _ammediatype mediatype; if (openvideostream(videofile, out mediadet, out mediatype)) { streamlength = mediadet.streamlength; size target = getvideosize(mediatype); int iteration = 0; foreach (double position in positions) { iteration++; string outputbitmapfile = string.format(outputbitmapfiles, iteration); mediadet.writebitmapbits(position, target.width, target.height, outputbitmapfile); } return; } } catch (comexception ex) { throw new invalidvideofileexception(); } { if (mediadet != null) marshal.releasecomobject(mediadet); } throw new invalidvideofileexception("no video stream found"); } private static size getvideosize(_ammediatype mediatype) { winstructs.videoinfoheader videoinfo = (winstructs.videoinfoheader)marshal.ptrtostructure(mediatype.pbformat, typeof(winstructs.videoinfoheader)); return new size(videoinfo.bmiheader.biwidth, videoinfo.bmiheader.biheight); } private static size scaletofit(size target, size original) { if (target.height * original.width > target.width * original.height) target.height = target.width * original.height / original.width; else target.width = target.height * original.width / original.height; return target; } private static size scaletofitsmart(size target, size original) { target = scaletofit(target, original); if (target.width > original.width || target.height > original.height) return original; return target; } private static bool openvideostream(string videofile, out imediadet mediadet, out _ammediatype ammediatype) { mediadet = new mediadet(); //loads file mediadet.filename = videofile; //gets # of streams int streamsnumber = mediadet.outputstreams; //finds video stream (int = 0; < streamsnumber; i++) { mediadet.currentstream = i; _ammediatype mediatype = mediadet.streammediatype; if (mediatype.majortype == jockersoft.media.mayortypes.mediatype_video) { //video stream found ammediatype = mediatype; return true; } } //no video stream found marshal.releasecomobject(mediadet); mediadet = null; ammediatype = new _ammediatype(); return false; } } }
the saveframesfromvideo()
method expects double array filled percentage position in file in seconds - since don't know how relates length of video cannot use frame rate alone. can save frame @ each full percentage file:
var streamdouble = new double[1]; (int x = 0; x < 100; x++) { streamdouble[0] = x; saveframesfromvideo(strvideofile, streamdouble, sf + x.tostring("d6") + ".bmp"); }
if need wmv files use asfmojo instead - complete solution this:
asffile asffile = new asffile(@"d:\samples\sample.wmv"); asffileproperties fileproperties = asffile.getasfobject<asffileproperties>(); timespan duration = timespan.fromticks((long)fileproperties.playduration) - timespan.frommilliseconds(fileproperties.preroll); var streamprops = asffile.getasfobjects<asfextendedstreamproperties>() .singleordefault(x => x.streamnumber == asffile.packetconfiguration.asfvideostreamid); double timeperframe = streamprops.avgtimeperframe / (double) 10000000 ; double offset = 0; while (offset < duration.totalseconds) { using (var bitmap = asfimage.fromfile(@"d:\samples\sample.wmv").atoffset(offset)) { if(bitmap!=null) bitmap.save(string.format(@"d:\frames\{0}.jpg", offset.tostring("n")), imageformat.jpeg); } offset += timeperframe; }
Comments
Post a Comment