上一篇:通过flv的url得到视频截图
但是不能通过一些视频网站连接直接得到图片,而只能通过flv的url得到图片,很鸡肋的一个东西。偶尔发现有很多网站提供在线flv解析.如我要用的这个:http://www.flvcd.com/ ,号称支持66个视频网站的flv地址获取,那就直接用它的了。通过它得到flv的实际地址之后再下载小部分,然后再截图操做。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
protected
void
Button1_Click(
object
sender, EventArgs e)
{
string flvUrl = string .Empty;
string flvName = string .Empty;
string urlStr = TextBox1.Text;
WebClient web = new WebClient();
string reqStr = web.DownloadString( " http://www.flvcd.com/parse.php?flag=&format=&kw= " + Server.UrlEncode(urlStr));
string [] str = reqStr.Split( new string [] { " copyToClipboard(' " }, StringSplitOptions.None);
if (str.Length > 1 )
{
for ( int i = 1 ; i < str.Length; i ++ )
{
int start = str[i].IndexOf( " ' " );
string temp = str[i].Substring( 0 , start);
if (temp.StartsWith( " http " ))
flvUrl = temp;
else
flvName = temp;
}
}
Literal1.Text = " FlvUrl地址为: " + flvUrl + " <br/>flv名称: " + flvName;
string tempFlvName = System.Guid.NewGuid().ToString() + " .flv " ;
SaveTempFlv(flvUrl, tempFlvName, 200 );
Image1.ImageUrl = CatchImg(tempFlvName);
}
{
string flvUrl = string .Empty;
string flvName = string .Empty;
string urlStr = TextBox1.Text;
WebClient web = new WebClient();
string reqStr = web.DownloadString( " http://www.flvcd.com/parse.php?flag=&format=&kw= " + Server.UrlEncode(urlStr));
string [] str = reqStr.Split( new string [] { " copyToClipboard(' " }, StringSplitOptions.None);
if (str.Length > 1 )
{
for ( int i = 1 ; i < str.Length; i ++ )
{
int start = str[i].IndexOf( " ' " );
string temp = str[i].Substring( 0 , start);
if (temp.StartsWith( " http " ))
flvUrl = temp;
else
flvName = temp;
}
}
Literal1.Text = " FlvUrl地址为: " + flvUrl + " <br/>flv名称: " + flvName;
string tempFlvName = System.Guid.NewGuid().ToString() + " .flv " ;
SaveTempFlv(flvUrl, tempFlvName, 200 );
Image1.ImageUrl = CatchImg(tempFlvName);
}
通过webclient下载带有flv地址的页面再提取它的地址。
同时附上上篇代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
private
void
SaveTempFlv(
string
url,
string
fileName,
int
sizeKB)
{
HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(url);
wr.Method = " get " ;
HttpWebResponse rs = (HttpWebResponse)wr.GetResponse();
BinaryReader br = new BinaryReader( rs.GetResponseStream());
FileStream fs = File.Create(Server.MapPath( "" ) + " \\ " + fileName);
int byteread = 1 ;
int tempsize = 0 ;
byte [] bt = new byte [ 1024 ];
while ((byteread = br.Read(bt, 0 , 1024 )) > 0 )
{
tempsize += byteread;
if (tempsize >= sizeKB * 1024 )
break ;
fs.Write(bt, 0 , byteread);
}
br.Close();
fs.Close();
fs.Dispose();
}
public string CatchImg( string vFileName)
{
string ffmpeg = Server.MapPath( "" ) + " \\ " + " ffmpeg.exe " ;
if ( ! System.IO.File.Exists(ffmpeg))
return "" ;
string flv_img = Path.ChangeExtension(vFileName, " .jpg " );
string flv_img_p = Server.MapPath(flv_img);
vFileName = Server.MapPath(vFileName);
string FlvImgSize = " 500x400 " ;
// Response.Write(vFileName);
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.Arguments = " -i " + vFileName + " -y -f image2 -ss 1 -s " + FlvImgSize + " " + flv_img_p;
Response.Write(startInfo.Arguments);
// -ss 后面的是搜索指定时间,1表示1s。200K应该都有1s吧~~
System.Diagnostics.Process.Start(startInfo);
return flv_img;
}
{
HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(url);
wr.Method = " get " ;
HttpWebResponse rs = (HttpWebResponse)wr.GetResponse();
BinaryReader br = new BinaryReader( rs.GetResponseStream());
FileStream fs = File.Create(Server.MapPath( "" ) + " \\ " + fileName);
int byteread = 1 ;
int tempsize = 0 ;
byte [] bt = new byte [ 1024 ];
while ((byteread = br.Read(bt, 0 , 1024 )) > 0 )
{
tempsize += byteread;
if (tempsize >= sizeKB * 1024 )
break ;
fs.Write(bt, 0 , byteread);
}
br.Close();
fs.Close();
fs.Dispose();
}
public string CatchImg( string vFileName)
{
string ffmpeg = Server.MapPath( "" ) + " \\ " + " ffmpeg.exe " ;
if ( ! System.IO.File.Exists(ffmpeg))
return "" ;
string flv_img = Path.ChangeExtension(vFileName, " .jpg " );
string flv_img_p = Server.MapPath(flv_img);
vFileName = Server.MapPath(vFileName);
string FlvImgSize = " 500x400 " ;
// Response.Write(vFileName);
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.Arguments = " -i " + vFileName + " -y -f image2 -ss 1 -s " + FlvImgSize + " " + flv_img_p;
Response.Write(startInfo.Arguments);
// -ss 后面的是搜索指定时间,1表示1s。200K应该都有1s吧~~
System.Diagnostics.Process.Start(startInfo);
return flv_img;
}
效果:测试优酷和土豆,两个站点都能下载到几秒的flv视频,但是没有截取到图片,cmd执行的时候发现ffmpeg不支持这种类型的视频格式。flv视频有哪几种格式呢,我想得换种转化工具才行。因为我下载到的几秒钟视频时可以正常播放的。