c#批量下载短视频

需求:哈哈,不多说了,就是想快速搞下来。

动图演示

方案:最开始我准备查资料自己解析地址,结果发现有一些难度,我主要的目的也并不是一定得自己解析地址,如果有其他手段拿到地址,也是可以接受了,偶然发现了EEAPI这个网站,开放了短视频地址解析接口,虽然收费,但完全在可接受范围之内,所以最终采用这个付费方案,也算支持同行,设身处地,我自己也想哪天别人掏钱购买我的服务啊,哈哈,好吧,开始。。。

在该网站注册购买后,申请个人api接口,得到如下专属api

发送请求后,返回json,拿到真实视频地址,就可以下载了。

关键代码:

1、解析剪切板口令,获取地址list

private List<string> GetUrlList(string str)
        {
            List<string> list = new List<string>();
            string stra;
            int n;
            while (true)
            {
                stra = CxUitls.GetStr(str, "https", " ");
                n=str.IndexOf("https")+stra.Length;
                if (stra.Length > 0)
                {
                    list.Add(stra);
                    str = str.Substring(n, str.Length - n);
                }
                else break;
            }            
            return list;
        }        
//字符串搜索
        //begin开始,stopchar结束
        public static string GetStr(string str, string findstr, string stopchar)
        {
            int n = str.IndexOf(findstr);
            if (n < 0) return "";
            string result = "";
            string t;
            for (int i = n; i < str.Length; i++)
            {
                t = str.Substring(i, 1);
                if (t != stopchar)
                    result += t;
                else
                    return result;
            }
            return "";
        }

2,轮询短视频地址表,下载视频

for (int i = 0; i < UrlList.Count; i++)
            {
                textbox.Text = textbox.Text + "\r\n   下载第"+(i+1)+"个:"+UrlList[i];
                Application.DoEvents();
                result = DownVideo(UrlList[i]);
                textbox.Text = textbox.Text + "\r\n" + (result ? "   下载成功...." : "   下载失败....");
                Application.DoEvents();
                if (progressBar2.Visible)
                {
                    progressBar2.Value = i + 1;
                    Application.DoEvents();
                }                
            }  

3,下载视频downvideo函数

private bool DownVideo(string url)
        {
            string str = HttpUitls.Get(api + url);
            Dictionary<string, object> json = JsonConvert.DeserializeObject<Dictionary<string, object>>(str);
            if (Convert.ToInt16(json["code"]) == 200)
            {
                //Console.WriteLine(json["data"]); 
                Dictionary<object, object> jsona = JsonConvert.DeserializeObject<Dictionary<object, object>>(json["data"].ToString());
                //Console.WriteLine(jsona["down"]);
                str = jsona["down"].ToString();
                if (str.Length > 0)
                {
                    string title = jsona["title"].ToString();
                    if (title.Length > 0)
                    {
                        return HttpUitls.DownloadFile(str, title + ".mp4", progressBar1);
                    }
                }
            }
            return false;
        }

4、downvideo函数中调用HttpWebRequest,获取短视频解析参数

public static string Get(string Url)
        {            
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Proxy = null;
            request.KeepAlive = false;
            request.Method = "GET";
            request.ContentType = "application/json; charset=UTF-8";
            request.AutomaticDecompression = DecompressionMethods.GZip;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            myResponseStream.Close();

            if (response != null)
            {
                response.Close();
            }
            if (request != null)
            {
                request.Abort();
            }

            return retString;
        }

5,downvideo函数中下载视频函数

/// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="URL">下载文件地址</param>
        /// <param name="Filename">下载后的存放地址</param>
        /// <param name="Prog">用于显示的进度条</param>
        public static bool DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog)
        {
            try
            {                
                System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
                System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                long totalBytes = myrp.ContentLength;
 
                if (prog != null)
                {
                    prog.Maximum = (int)totalBytes;
                }
 
                System.IO.Stream st = myrp.GetResponseStream();
                System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                long totalDownloadedByte = 0;
                byte[] by = new byte[1024];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    System.Windows.Forms.Application.DoEvents();
                    so.Write(by, 0, osize);
 
                    if (prog != null)
                    {
                        prog.Value = (int)totalDownloadedByte;
                    }
                    if (prog.Value > 1095000)
                    {
                        string aa= ""; 
                    }
                    osize = st.Read(by, 0, (int)by.Length);                    
                }
                so.Close();
                st.Close();
                return true;
            }
            catch (System.Exception)
            {
                throw;                
            }
            return false;
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="URL">下载文件地址</param>
        /// <param name="Filename">下载后的存放地址</param>
        public static void DownloadFile(string URL, string filename)
        {
            DownloadFile(URL, filename, null);
        }    

6,界面

先把口令复制到剪切板,再点击【粘贴口令->解析->下载】即可完全自动解析和视频下载

 

源码地址:https://download.csdn.net/download/xchenbb/87614373

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xchenbb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值