WPF演示,抓取flv视频
我们来做个WPF的windows程序,想要抓取FLV视频。
1.新建一个wpf project
2.编辑windows1.xaml
这里有一个textbox,两个textblock,一个gif动画,一个进度条
3.wpf下GIF动画的显示
找了一个GifImageLib.dll
编辑xaml
<my:GifImage Margin="27,92,0,0" Name="imageLoad" RenderTransformOrigin="0.5,0.5" Source="Images\loader.gif" Stretch="Uniform" xmlns:my="clr-namespace:GifImageLib;assembly=GifImageLib" HorizontalAlignment="Left" Width="16" Visibility="Hidden" Height="18" VerticalAlignment="Top"> <my:GifImage.RenderTransform> <TransformGroup> <ScaleTransform ScaleX="1" ScaleY="1" /> <SkewTransform AngleX="0" AngleY="0" /> <RotateTransform Angle="0" /> <TranslateTransform X="0" Y="0" /> </TransformGroup> </my:GifImage.RenderTransform> </my:GifImage>
4.进度条的显示
说道进度条,一定是用多线程了,试验了第一次
//第一次尝试,gif不能动,可以显示
imageLoad.Visibility = Visibility.Visible;
new Thread(() =>
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
Thread.Sleep(5000);
imageLoad.Visibility = Visibility.Hidden;
tbWait.Visibility = Visibility.Hidden;
}));
}).Start();
用的是Dispatcher.BeginInvoke()方法,结果是gif能显示,但不能动,失败
查了msdn,换了种方法,简单明了
_backgroundWorker = new BackgroundWorker();
_backgroundWorker.DoWork+=new DoWorkEventHandler(DoWork);
_backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(FinsihWork);
BeforeWork();
_backgroundWorker.RunWorkerAsync();
_backgroundWorker.Dispose();
不错,就是BackgroundWorker,注意这要引用using System.ComponentModel;
gif能动了。
private void DoWork(object sender, DoWorkEventArgs e)
{
//模拟进度条 for (int i = 0; i < 10; i ++)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
WorkProgress.Value = i*10;
}));
Thread.Sleep(100);
}
try { if(strUrl!=string.Empty)
parseUrl(strUrl);
}
catch { flvUrl="error";
}
}
private void BeforeWork()
{
imageLoad.Visibility = Visibility.Visible;
WorkProgress.Visibility = Visibility.Visible;
WorkProgress.Maximum = 100;
WorkProgress.Minimum = 0;
WorkProgress.Value = 0;
btnGet.IsEnabled = false;
strUrl = textBox1.Text;
}
private void FinsihWork(object sender,RunWorkerCompletedEventArgs e)
{
WorkProgress.Visibility = Visibility.Hidden;
imageLoad.Visibility = Visibility.Hidden;
btnGet.IsEnabled = true;
if (flvUrl != "error")
tbResult.Text = flvUrl;
}
5.抓取flv的算法
这里我们采用httpwatch插件,又免费的 HttpWatch Basic Edition 下载
添加引用
在com里找到httpwatch,ok
算法如下:
private bool parseUrl(string URL)
{
bool bResult = UrlExistsUsingHttpWebRequest(URL);
if (bResult)
{
HttpWatch.ControllerClass ct = new HttpWatch.ControllerClass();
HttpWatch.Plugin plugin = ct.IE.New();
plugin.Log.EnableFilter(false);
plugin.Record();
plugin.GotoURL(URL);
ct.Wait(plugin, -1);
foreach (HttpWatch.Entry entry in plugin.Log.Entries)
{
if (entry.IsRedirect)
parseUrl(entry.URL);
else { if (entry.URL.ToLower().Contains(".flv"))
{
flvUrl = entry.URL;
Dispatcher.BeginInvoke(DispatcherPriority.Normal,new Action(()=>{
tbResult.Text = flvUrl;
}));
}
}
}
plugin.Stop();
plugin.CloseBrowser();
}
return bResult;
}
private bool UrlExistsUsingHttpWebRequest(string url)
{
try { System.Net.WebRequest.Create(url); return true;
}
catch { return false;
}
}
6.显示得到文件的路径,并点击下载
private void tbResult_MouseDown(object sender, MouseButtonEventArgs e)
{
if (flvUrl != "error")
System.Diagnostics.Process.Start(flvUrl);
}
7.好了,看一下效果
后记
抓取算法并不完善,发现56.com的可以下载,土豆网的不是抓不到就是403错误,或者难得又可以下载的。
有了解的可以给我写信联系
完整代码下载,我的个人网站 http://conan77.vicp.net/