kmdiy 发表于 2020-11-15 20:12

爬虫-哔哩哔哩-视频下载

一个哔哩哔哩视频下载,因为要下载一个4k画质的视频奈何要开会员和下载客户端才能下载。
哔哩哔哩的视频有点特殊,他是由视频+音频两个文件组成的,所以把这两个文件下载下来后需要将视频和音频合并。
合并用的ffmpeg的插件,但是不知道为什么有时候会合并失败,不过不重要,把音频和视频下载出来后网上有很多第三方的合并软件可以合成。
使用方式:首先选择文件保存的地址,然后输入视频网页地址,点击确定,然后选择画质一般选4K吧。
然后就等他下载,不得不说哔哩哔哩的网络带宽挺大的,下载十多秒,下载完成后,按照提示点击合并按钮进行合并。
合并完成后你的文件夹有三个文件(音频、视频、合成视频)。
有时候可能会合并失败,多试几次,或者网上找其他的合成工具进行合并
本来想放成品的,但是51限制文件大小3M,就懒得用其他方式上传了 因为没啥技术含量,我把关键代码贴出来就基本上能做出来(贴出的代码就是获取下载链接的,里面包括视频链接和音频链接,然后下载下来用ffmpeg合并)。
PS:不知道为什么最近感觉身体被掏空了。
代码:
private void button2_Click(object sender, EventArgs e)
      {
            label2.Visible = true;
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem
            {
                URL = textBox1.Text.Trim(),
                Host = "www.bilibili.com",
                UserAgent = userAgent
            };
            var result = http.GetHtml(item);
            string html = result.Html;
            string gl = HttpHelper.GetBetweenHtml(html, "window.__playinfo__=", "</script><script>window.__INITIAL_STATE__");
            title = HttpHelper.GetBetweenHtml(html, "<title data-vue-meta=\"true\">", "</title>");
            root = (Root)HttpHelper.JsonToObject<Root>(gl);
            if (root.code == 0)
            {

                RadioButton[] radioButton = new RadioButton;
                //控件上边缘与容器上边缘的距离
                int top = 6;
                //记录循环,控件左上角相对于容器左上角的坐标
                int i = 0;
                int n = 0;
                GroupBox gb = new GroupBox();
                panel1.AutoScroll = true;
                for (n = 0; n < root.data.accept_description.Count; n++)
                {
                  radioButton = new RadioButton();
                  button3.Visible = true;
                  if (n % 3 == 0 && n != 0)
                  {
                        top += 30;
                        i = 0;
                  }
                  if (n == 0)
                  {
                        radioButton.Checked = true;
                  }
                  radioButton.AutoSize = true;
                  radioButton.Top = top;
                  //控件左上角相对于容器左上角的坐标,以及每个控件之间的距离
                  radioButton.Location = new Point(i * 150 + 2, top);
                  // MessageBox.Show(name.ToString());
                  radioButton.Text = root.data.accept_description;
                  radioButton.Visible = true;
                  radioButton.Name = "radioButton" + n;
                  radioButton.Tag = n;
                  this.panel1.Controls.Add(radioButton);
                  i++;
                }
            };
      }
          //点击下载按钮的代码
            foreach (var item in panel1.Controls)
            {
                if (item is RadioButton)
                {
                  RadioButton radio = item as RadioButton;
                  if (radio.Checked)
                  {
                        var tag = int.Parse(radio.Tag.ToString());
                        string videourl = root.data.dash.video.ToList().baseUrl;
                        string audiourl = root.data.dash.audio.ToList().baseUrl;
                        //DownloadAudio(audiourl);
                        //DownloadVideo(videourl);

                        Thread thread = new Thread(new ParameterizedThreadStart(DownloadVideo));
                        thread.Start(videourl);
                        Thread thread2 = new Thread(new ParameterizedThreadStart(DownloadAudio));
                        thread2.Start(audiourl);

                  }

                }
//合成代码
private void button4_Click(object sender, EventArgs e)
      {
            this.Invoke((EventHandler)delegate
            {
                textBox2.Text += DateTime.Now.ToString() + "合并中 请稍后...\r\n";
            });
            string path = textBox3.Text + guid ;
            string avi = textBox3.Text +"合并视频";
            string arguments = "-i " + path + ".avi-i " + path + ".aac-vcodec copy -acodec copy " + avi + ".avi";
            //arguments = "-i " + path + ".avi-i " + path + ".aac-c:v copy -c:a aac -strict experimental " + avi + ".avi";
            Process p = new Process();
            p.StartInfo.FileName = "ffmpeg.exe";
            p.StartInfo.Arguments = arguments;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //p.StartInfo.RedirectStandardError = true;
            p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序
            p.Start();//启动线程
            timer1.Enabled = true;
      }


zyw35100 发表于 2020-11-15 20:12


感谢分享,谢谢提供分享

gg1900 发表于 2020-11-15 20:37


不错不错~~~~~

haihai 发表于 2020-11-15 20:38

6666

yqxts 发表于 2020-11-15 21:16

谢谢大佬分享

笑东风丶 发表于 2020-11-15 21:20

感谢大佬分享,大牛因有你而更精彩。

cooklove 发表于 2020-11-15 21:51

6666666666

qinfen777 发表于 2020-11-15 22:06


不错不错,,,

360001987 发表于 2020-11-15 22:22

谢谢@Thanks!

快鱼仔 发表于 2020-11-15 22:35

谢谢楼主分享,先收藏了。
页: [1] 2
查看完整版本: 爬虫-哔哩哔哩-视频下载