我正在使用FrameGrabber.cs并添加了它的dll作为参考:JockerSoft.Media.dll和Interop.qedit。
现在在构造函数中的Form1代码中,我正在尝试检索其位置中的所有帧,并将每个帧保存到硬盘上的新位图文件中,但我无法弄清楚如何执行此操作。
我不知道如何循环遍历所有帧/双[]位置我也希望看到有关帧数的信息我可以获得17.8的帧速率....
我想获取所有帧的列表/数组,然后将每个帧保存到硬盘,然后对帧进行另一次操作。
This是我从中获得示例的网站。试图查看那里和源代码,但无法弄清楚如何做到这一点。
**我在设计师中也有一个trackBar1,我希望能够滚动浏览所有不合适的帧。
这是我的Form1代码,它不能正常工作我只能得到一帧。
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";
for (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()
{
// Get framerate
MediaDet md = new MediaDet();
md.Filename = strVideoFile;
//md.CurrentStream = 0; // choose the video stream
x = md.FrameRate;
double i = 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();
}
finally
{
if (mediaDet != null)
Marshal.ReleaseComObject(mediaDet);
}
throw new InvalidVideoFileException("No video stream was 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 a video stream
for (int i = 0; i < 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;
}
}
}
答案 0 :(得分:1)
SaveFramesFromVideo()
方法需要在数秒内填充文件中百分比位置的双数组 - 因为您不知道这与您无法使用的视频长度有何关系单独的帧速率。但是,您可以将每个完整百分比的帧保存到文件中:
var streamDouble = new double[1];
for (int x = 0; x < 100; x++)
{
streamDouble[0] = x;
SaveFramesFromVideo(strVideoFile, streamDouble, sf + x.ToString("D6") + ".bmp");
}
如果您只需要WMV文件,那么您也可以使用AsfMojo - 完整的解决方案将如下所示:
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;
}