c#批量下载短视频

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

动图演示

c#批量下载短视频_第1张图片

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

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

c#批量下载短视频_第2张图片

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

关键代码:

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

private List GetUrlList(string str)
        {
            List list = new List();
            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 json = JsonConvert.DeserializeObject>(str);
            if (Convert.ToInt16(json["code"]) == 200)
            {
                //Console.WriteLine(json["data"]); 
                Dictionary jsona = JsonConvert.DeserializeObject>(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函数中下载视频函数

/// 
        /// 下载文件
        /// 
        /// 下载文件地址
        /// 下载后的存放地址
        /// 用于显示的进度条
        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;
        }
        /// 
        /// 下载文件
        /// 
        /// 下载文件地址
        /// 下载后的存放地址
        public static void DownloadFile(string URL, string filename)
        {
            DownloadFile(URL, filename, null);
        }    

6,界面

c#批量下载短视频_第3张图片

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

 c#批量下载短视频_第4张图片

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

你可能感兴趣的:(【c#,音视频】,c#,音视频)