从【抓包分析】到【代码实战】,实现下载某破站视频(附源码)

这篇具有很好参考价值的文章主要介绍了从【抓包分析】到【代码实战】,实现下载某破站视频(附源码)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、前言

前两天,我的邻居找到我,问我某破站的视频能否帮她下载成mp4格式?

网上应该有很多的下载工具,但是如果直接让她网上找,那么无法彰显我程序员大神的威武形象。因此我回复她,程序员大神是无敌的,只要在浏览器上能看到的东西,都能用程序拿到。只要在浏览器上用手能操作的东西,都能用程序操作。只要......

我发现我的邻居,已经悄然成为了我的产品经理,这些年着实给我提了不少产品思路哈哈哈。

二、需求分析

其实要做的功能,非常简单。从某破站上打开一个视频,从浏览器地址栏拿到这个视频的地址,然后粘贴到我开发的程序中,程序自动将相应的视频下载下来变成mp4格式。

干脆,我把程序放到我的云服务器上,这样不但邻居可以使用,世界各地的美女帅哥都能使用。如果用的人多了,我给他变成收费模式,下载一个视频收1分钱,一天如果有1万个人下载,不就能收100元吗?一个月30天,那就是3万,一年365天,那就是365*3万=1095万,艾玛这是要发大财呀。

你看,我不止是程序员大神,还是数学大神。其实数学十分简单,只不过剩下的九十分很难。

三、抓包分析

1、拿到视频文件真实地址

我们进入某破站,随便打开一个视频,咱们就用浏览器自带的网络监控工具抓包。

好家伙,这一大堆请求,一直在不停地刷,放个图大家感受一下:

从【抓包分析】到【代码实战】,实现下载某破站视频(附源码)

不过凭借程序员大神多年的经验,直觉告诉我,咱们重点关注这俩请求:

从【抓包分析】到【代码实战】,实现下载某破站视频(附源码)

看一下这俩请求的应答内容,这一看就是我们要的视频二进制内容嘛:

从【抓包分析】到【代码实战】,实现下载某破站视频(附源码)

等等!1267024297-1-100024.m4s和1267024297-1-30232.m4s这些数字是从哪里来的呢?看起来像是视频的ID号之类的,但是浏览器链接栏中也没看到类似的号呢?

那接下来咱先看看第一个请求吧,看这里面能否找到啥蛛丝马迹。

这第一个请求就是我点击视频链接后发出的,这和浏览器地址栏的地址是一致的:

从【抓包分析】到【代码实战】,实现下载某破站视频(附源码)

再看一下这个请求的应答是啥内容:

从【抓包分析】到【代码实战】,实现下载某破站视频(附源码)

应答就是一段标准的html嘛。看看这里面有没有1267024297-1-100024.m4s和1267024297-1-30232.m4s相关的内容呢?搜一下,果然找到了:

从【抓包分析】到【代码实战】,实现下载某破站视频(附源码)

看起来就是一段json,下面我把这段json贴出来,内容太多,我稍微删减了一些,只留下关键信息:

查看代码
 <script>
    window.__playinfo__ = {
        "code": 0,
        "message": "0",
        "ttl": 1,
        "data": {
            "from": "local",
            "result": "suee",
            "message": "",
            "quality": 64,
            "format": "flv720",
            "timelength": 312490,
            "accept_format": "hdflv2,flv,flv720,flv480,flv360",
            "accept_description": ["高清 1080P+", "高清 1080P", "高清 720P", "清晰 480P", "流畅 360P"],
            "accept_quality": [112, 80, 64, 32, 16],
            "video_codecid": 7,
            "seek_param": "start",
            "seek_type": "offset",
            "dash": {
                "duration": 313,
                "minBufferTime": 1.5,
                "min_buffer_time": 1.5,
                "video": [{
                    "id": 80,
                    "baseUrl": "https://xy182x117x194x4xy.xxxxxxxxxx.cn:8082/v1/resource/1267024297-1-100113.m4s?agrr=0&build=0&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&bvc=vod&bw=107051&deadline=1695874583&e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M%3D&f=u_0_0&gen=playurlv2&logo=A0000400&mcdnid=11000413&mid=1716139964&nbs=1&nettype=0&oi=3550958494&orderid=0%2C3&os=mcdn&platform=pc&sign=20f269&traceid=trDNtOvronEayd_0_e_N&uipk=5&uparams=e%2Cuipk%2Cnbs%2Cdeadline%2Cgen%2Cos%2Coi%2Ctrid%2Cmid%2Cplatform&upsig=b33a062d8cc1d08690ad8f7d727e5a1f",
                    "base_url": "https://xy182x117x194x4xy.xxxxxxxxxx.cn:8082/v1/resource/1267024297-1-100113.m4s?agrr=0&build=0&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&bvc=vod&bw=107051&deadline=1695874583&e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M%3D&f=u_0_0&gen=playurlv2&logo=A0000400&mcdnid=11000413&mid=1716139964&nbs=1&nettype=0&oi=3550958494&orderid=0%2C3&os=mcdn&platform=pc&sign=20f269&traceid=trDNtOvronEayd_0_e_N&uipk=5&uparams=e%2Cuipk%2Cnbs%2Cdeadline%2Cgen%2Cos%2Coi%2Ctrid%2Cmid%2Cplatform&upsig=b33a062d8cc1d08690ad8f7d727e5a1f",
                    "backupUrl": ["https://xy112x111x47x2xy.xxxxxxxxxx.cn:4483/upgcxcode/07/13/1267024297/1267024297-1-100113.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=mcdn&oi=3550958494&trid=00001a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=b33a062d8cc1d08690ad8f7d727e5a1f&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&mcdnid=11000413&bvc=vod&nettype=0&orderid=0,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=107051&logo=A0000400", "https://upos-sz-mirrorali.xxxxxxxxxx.com/upgcxcode/07/13/1267024297/1267024297-1-100113.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=alibv&oi=3550958494&trid=1a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=7b626b942dab4437433e276e1dfd6c63&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=1,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=107051&logo=40000000"],
                    "backup_url": ["https://xy112x111x47x2xy.xxxxxxxxxx.cn:4483/upgcxcode/07/13/1267024297/1267024297-1-100113.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=mcdn&oi=3550958494&trid=00001a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=b33a062d8cc1d08690ad8f7d727e5a1f&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&mcdnid=11000413&bvc=vod&nettype=0&orderid=0,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=107051&logo=A0000400", "https://upos-sz-mirrorali.xxxxxxxxxx.com/upgcxcode/07/13/1267024297/1267024297-1-100113.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=alibv&oi=3550958494&trid=1a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=7b626b942dab4437433e276e1dfd6c63&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=1,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=107051&logo=40000000"],
                    "bandwidth": 855502,
                    "mimeType": "video/mp4",
                    "mime_type": "video/mp4",
                    "codecs": "hev1.1.6.L150.90",
                    "width": 1920,
                    "height": 1080,
                    "frameRate": "23.810",
                    "frame_rate": "23.810",
                    "sar": "1:1",
                    "startWithSap": 1,
                    "start_with_sap": 1,
                    "SegmentBase": {
                        "Initialization": "0-1159",
                        "indexRange": "1160-1935"
                    },
                    "segment_base": {
                        "initialization": "0-1159",
                        "index_range": "1160-1935"
                    },
                    "codecid": 12
                }, {
                    "id": 80,
                    "baseUrl": "https://xy49x71x163x109xy.xxxxxxxxxx.cn:8082/v1/resource/1267024297-1-100050.m4s?agrr=0&build=0&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&bvc=vod&bw=279453&deadline=1695874583&e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M%3D&f=u_0_0&gen=playurlv2&logo=A0000400&mcdnid=11000413&mid=1716139964&nbs=1&nettype=0&oi=3550958494&orderid=0%2C3&os=mcdn&platform=pc&sign=9e67ce&traceid=trFvCerZmkKQtz_0_e_N&uipk=5&uparams=e%2Cuipk%2Cnbs%2Cdeadline%2Cgen%2Cos%2Coi%2Ctrid%2Cmid%2Cplatform&upsig=3aac9a201552c1a8f1d4de37510b4a21",
                    "base_url": "https://xy49x71x163x109xy.mcdn.xxxxxxxxxx.cn:8082/v1/resource/1267024297-1-100050.m4s?agrr=0&build=0&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&bvc=vod&bw=279453&deadline=1695874583&e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M%3D&f=u_0_0&gen=playurlv2&logo=A0000400&mcdnid=11000413&mid=1716139964&nbs=1&nettype=0&oi=3550958494&orderid=0%2C3&os=mcdn&platform=pc&sign=9e67ce&traceid=trFvCerZmkKQtz_0_e_N&uipk=5&uparams=e%2Cuipk%2Cnbs%2Cdeadline%2Cgen%2Cos%2Coi%2Ctrid%2Cmid%2Cplatform&upsig=3aac9a201552c1a8f1d4de37510b4a21",
                    "backupUrl": ["https://xy112x111x47x2xy.mcdn.xxxxxxxxxx.cn:4483/upgcxcode/07/13/1267024297/1267024297-1-100050.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=mcdn&oi=3550958494&trid=00001a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=3aac9a201552c1a8f1d4de37510b4a21&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&mcdnid=11000413&bvc=vod&nettype=0&orderid=0,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=279453&logo=A0000400", "https://upos-sz-mirrorali.xxxxxxxxxx.com/upgcxcode/07/13/1267024297/1267024297-1-100050.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=alibv&oi=3550958494&trid=1a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=3dc776f9ddfeab28ff0105c7ec4875a4&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=1,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=279453&logo=40000000"],
                    "backup_url": ["https://xy112x111x47x2xy.mcdn.xxxxxxxxxx.cn:4483/upgcxcode/07/13/1267024297/1267024297-1-100050.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=mcdn&oi=3550958494&trid=00001a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=3aac9a201552c1a8f1d4de37510b4a21&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&mcdnid=11000413&bvc=vod&nettype=0&orderid=0,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=279453&logo=A0000400", "https://upos-sz-mirrorali.xxxxxxxxxx.com/upgcxcode/07/13/1267024297/1267024297-1-100050.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=alibv&oi=3550958494&trid=1a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=3dc776f9ddfeab28ff0105c7ec4875a4&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=1,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=279453&logo=40000000"],
                    "bandwidth": 2233245,
                    "mimeType": "video/mp4",
                    "mime_type": "video/mp4",
                    "codecs": "avc1.640032",
                    "width": 1920,
                    "height": 1080,
                    "frameRate": "24.390",
                    "frame_rate": "24.390",
                    "sar": "1:1",
                    "startWithSap": 1,
                    "start_with_sap": 1,
                    "SegmentBase": {
                        "Initialization": "0-996",
                        "indexRange": "997-1772"
                    },
                    "segment_base": {
                        "initialization": "0-996",
                        "index_range": "997-1772"
                    },
                    "codecid": 7
                }],
                "audio": [{
                    "id": 30280,
                    "baseUrl": "https://xy27x188x11x241xy.mcdn.xxxxxxxxxx.cn:8082/v1/resource/1267024297-1-30280.m4s?agrr=0&build=0&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&bvc=vod&bw=14271&deadline=1695874583&e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M%3D&f=u_0_0&gen=playurlv2&logo=A0000400&mcdnid=11000413&mid=1716139964&nbs=1&nettype=0&oi=3550958494&orderid=0%2C3&os=mcdn&platform=pc&sign=252dc5&traceid=trPZFhEozQrraC_0_e_N&uipk=5&uparams=e%2Cuipk%2Cnbs%2Cdeadline%2Cgen%2Cos%2Coi%2Ctrid%2Cmid%2Cplatform&upsig=ce4b9ea5064ef1e6074c45e253b02cbb",
                    "base_url": "https://xy27x188x11x241xy.mcdn.xxxxxxxxxx.cn:8082/v1/resource/1267024297-1-30280.m4s?agrr=0&build=0&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&bvc=vod&bw=14271&deadline=1695874583&e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M%3D&f=u_0_0&gen=playurlv2&logo=A0000400&mcdnid=11000413&mid=1716139964&nbs=1&nettype=0&oi=3550958494&orderid=0%2C3&os=mcdn&platform=pc&sign=252dc5&traceid=trPZFhEozQrraC_0_e_N&uipk=5&uparams=e%2Cuipk%2Cnbs%2Cdeadline%2Cgen%2Cos%2Coi%2Ctrid%2Cmid%2Cplatform&upsig=ce4b9ea5064ef1e6074c45e253b02cbb",
                    "backupUrl": ["https://xy112x111x47x2xy.mcdn.xxxxxxxxxx.cn:4483/upgcxcode/07/13/1267024297/1267024297-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=mcdn&oi=3550958494&trid=00001a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=ce4b9ea5064ef1e6074c45e253b02cbb&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&mcdnid=11000413&bvc=vod&nettype=0&orderid=0,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=14271&logo=A0000400", "https://upos-sz-mirrorali.xxxxxxxxxx.com/upgcxcode/07/13/1267024297/1267024297-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=alibv&oi=3550958494&trid=1a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=16af80196302d63ec9b163a091cd175c&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=1,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=14271&logo=40000000"],
                    "backup_url": ["https://xy112x111x47x2xy.mcdn.xxxxxxxxxx.cn:4483/upgcxcode/07/13/1267024297/1267024297-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=mcdn&oi=3550958494&trid=00001a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=ce4b9ea5064ef1e6074c45e253b02cbb&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&mcdnid=11000413&bvc=vod&nettype=0&orderid=0,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=14271&logo=A0000400", "https://upos-sz-mirrorali.xxxxxxxxxx.com/upgcxcode/07/13/1267024297/1267024297-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=alibv&oi=3550958494&trid=1a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=16af80196302d63ec9b163a091cd175c&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=1,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=14271&logo=40000000"],
                    "bandwidth": 114023,
                    "mimeType": "audio/mp4",
                    "mime_type": "audio/mp4",
                    "codecs": "mp4a.40.2",
                    "width": 0,
                    "height": 0,
                    "frameRate": "",
                    "frame_rate": "",
                    "sar": "",
                    "startWithSap": 0,
                    "start_with_sap": 0,
                    "SegmentBase": {
                        "Initialization": "0-933",
                        "indexRange": "934-1721"
                    },
                    "segment_base": {
                        "initialization": "0-933",
                        "index_range": "934-1721"
                    },
                    "codecid": 0
                }, {
                    "id": 30216,
                    "baseUrl": "https://xy106x115x132x228xy.mcdn.xxxxxxxxxx.cn:8082/v1/resource/1267024297-1-30216.m4s?agrr=0&build=0&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&bvc=vod&bw=4091&deadline=1695874583&e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M%3D&f=u_0_0&gen=playurlv2&logo=A0000400&mcdnid=11000413&mid=1716139964&nbs=1&nettype=0&oi=3550958494&orderid=0%2C3&os=mcdn&platform=pc&sign=cda2a5&traceid=trpwNglAmwaBCr_0_e_N&uipk=5&uparams=e%2Cuipk%2Cnbs%2Cdeadline%2Cgen%2Cos%2Coi%2Ctrid%2Cmid%2Cplatform&upsig=0d2abfa51f30dcea5d37c226758fcd36",
                    "base_url": "https://xy106x115x132x228xy.mcdn.xxxxxxxxxx.cn:8082/v1/resource/1267024297-1-30216.m4s?agrr=0&build=0&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&bvc=vod&bw=4091&deadline=1695874583&e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M%3D&f=u_0_0&gen=playurlv2&logo=A0000400&mcdnid=11000413&mid=1716139964&nbs=1&nettype=0&oi=3550958494&orderid=0%2C3&os=mcdn&platform=pc&sign=cda2a5&traceid=trpwNglAmwaBCr_0_e_N&uipk=5&uparams=e%2Cuipk%2Cnbs%2Cdeadline%2Cgen%2Cos%2Coi%2Ctrid%2Cmid%2Cplatform&upsig=0d2abfa51f30dcea5d37c226758fcd36",
                    "backupUrl": ["https://xy112x111x47x2xy.mcdn.xxxxxxxxxx.cn:4483/upgcxcode/07/13/1267024297/1267024297-1-30216.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=mcdn&oi=3550958494&trid=00001a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=0d2abfa51f30dcea5d37c226758fcd36&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&mcdnid=11000413&bvc=vod&nettype=0&orderid=0,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=4091&logo=A0000400", "https://upos-sz-mirrorcoso1.xxxxxxxxxx.com/upgcxcode/07/13/1267024297/1267024297-1-30216.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=coso1bv&oi=3550958494&trid=1a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=3863d2a8bb127e48ab30bbc0147a2810&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=1,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=4091&logo=40000000"],
                    "backup_url": ["https://xy112x111x47x2xy.mcdn.xxxxxxxxxx.cn:4483/upgcxcode/07/13/1267024297/1267024297-1-30216.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=mcdn&oi=3550958494&trid=00001a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=0d2abfa51f30dcea5d37c226758fcd36&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&mcdnid=11000413&bvc=vod&nettype=0&orderid=0,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=4091&logo=A0000400", "https://upos-sz-mirrorcoso1.xxxxxxxxxx.com/upgcxcode/07/13/1267024297/1267024297-1-30216.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=coso1bv&oi=3550958494&trid=1a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=3863d2a8bb127e48ab30bbc0147a2810&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=1,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=4091&logo=40000000"],
                    "bandwidth": 32682,
                    "mimeType": "audio/mp4",
                    "mime_type": "audio/mp4",
                    "codecs": "mp4a.40.5",
                    "width": 0,
                    "height": 0,
                    "frameRate": "",
                    "frame_rate": "",
                    "sar": "",
                    "startWithSap": 0,
                    "start_with_sap": 0,
                    "SegmentBase": {
                        "Initialization": "0-943",
                        "indexRange": "944-1731"
                    },
                    "segment_base": {
                        "initialization": "0-943",
                        "index_range": "944-1731"
                    },
                    "codecid": 0
                }],
                "dolby": {
                    "type": 0,
                    "audio": null
                },
                "flac": null
            },
            "support_formats": [{
                "quality": 112,
                "format": "hdflv2",
                "new_description": "1080P 高码率",
                "display_desc": "1080P",
                "superscript": "高码率",
                "codecs": ["av01.0.08M.08.0.110.01.01.01.0", "avc1.640032", "hev1.1.6.L150.90"]
            }, {
                "quality": 80,
                "format": "flv",
                "new_description": "1080P 高清",
                "display_desc": "1080P",
                "superscript": "",
                "codecs": ["av01.0.08M.08.0.110.01.01.01.0", "avc1.640032", "hev1.1.6.L150.90"]
            }, {
                "quality": 64,
                "format": "flv720",
                "new_description": "720P 高清",
                "display_desc": "720P",
                "superscript": "",
                "codecs": ["av01.0.08M.08.0.110.01.01.01.0", "avc1.640028", "hev1.1.6.L120.90"]
            }, {
                "quality": 32,
                "format": "flv480",
                "new_description": "480P 清晰",
                "display_desc": "480P",
                "superscript": "",
                "codecs": ["av01.0.08M.08.0.110.01.01.01.0", "avc1.64001F", "hev1.1.6.L120.90"]
            }, {
                "quality": 16,
                "format": "flv360",
                "new_description": "360P 流畅",
                "display_desc": "360P",
                "superscript": "",
                "codecs": ["av01.0.08M.08.0.110.01.01.01.0", "avc1.64001E", "hev1.1.6.L120.90"]
            }],
            "high_format": null,
            "volume": {
                "measured_i": -18.5,
                "measured_lra": 8.9,
                "measured_tp": 1.2,
                "measured_threshold": -28.8,
                "target_offset": 0.4,
                "target_i": -14,
                "target_tp": -1
            },
            "last_play_time": 14000,
            "last_play_cid": 1267024297
        },
        "session": "5b95542e60887c5e597810671aa5001a"
    }
</script>

所以,我们第一步的思路就有了:请求视频地址,然后将应答中的这段json拿出来,再从json中将视频文件真实地址拿到。

2、下载视频文件

我们看上面的json,data.dash.video路径下面的值,就是我们要下载的视频的真实地址,但我们看到这是个Array,也就是说有多个视频地址,我们下载哪一个呢?我测试了一下,把所有视频都下载下来,视频内容都是一致的,只不过文件大小、视频长宽、码率之类的不同,我估计对应的是"高清 1080P+", "高清 1080P", "高清 720P", "清晰 480P", "流畅 360P"之类的。我们就简单处理吧,默认下载第一个视频就行了。

下面咱看看第一个视频的具体信息:

查看代码
 {
    "id": 80,
    "baseUrl": "https://xy182x117x194x4xy.mcdn.xxxxxxxxxx.cn:8082/v1/resource/1267024297-1-100113.m4s?agrr=0&build=0&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&bvc=vod&bw=107051&deadline=1695874583&e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M%3D&f=u_0_0&gen=playurlv2&logo=A0000400&mcdnid=11000413&mid=1716139964&nbs=1&nettype=0&oi=3550958494&orderid=0%2C3&os=mcdn&platform=pc&sign=20f269&traceid=trDNtOvronEayd_0_e_N&uipk=5&uparams=e%2Cuipk%2Cnbs%2Cdeadline%2Cgen%2Cos%2Coi%2Ctrid%2Cmid%2Cplatform&upsig=b33a062d8cc1d08690ad8f7d727e5a1f",
    "base_url": "https://xy182x117x194x4xy.mcdn.xxxxxxxxxx.cn:8082/v1/resource/1267024297-1-100113.m4s?agrr=0&build=0&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&bvc=vod&bw=107051&deadline=1695874583&e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M%3D&f=u_0_0&gen=playurlv2&logo=A0000400&mcdnid=11000413&mid=1716139964&nbs=1&nettype=0&oi=3550958494&orderid=0%2C3&os=mcdn&platform=pc&sign=20f269&traceid=trDNtOvronEayd_0_e_N&uipk=5&uparams=e%2Cuipk%2Cnbs%2Cdeadline%2Cgen%2Cos%2Coi%2Ctrid%2Cmid%2Cplatform&upsig=b33a062d8cc1d08690ad8f7d727e5a1f",
    "backupUrl": ["https://xy112x111x47x2xy.mcdn.xxxxxxxxxx.cn:4483/upgcxcode/07/13/1267024297/1267024297-1-100113.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=mcdn&oi=3550958494&trid=00001a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=b33a062d8cc1d08690ad8f7d727e5a1f&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&mcdnid=11000413&bvc=vod&nettype=0&orderid=0,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=107051&logo=A0000400", "https://upos-sz-mirrorali.xxxxxxxxxx.com/upgcxcode/07/13/1267024297/1267024297-1-100113.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=alibv&oi=3550958494&trid=1a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=7b626b942dab4437433e276e1dfd6c63&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=1,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=107051&logo=40000000"],
    "backup_url": ["https://xy112x111x47x2xy.mcdn.xxxxxxxxxx.cn:4483/upgcxcode/07/13/1267024297/1267024297-1-100113.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=mcdn&oi=3550958494&trid=00001a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=b33a062d8cc1d08690ad8f7d727e5a1f&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&mcdnid=11000413&bvc=vod&nettype=0&orderid=0,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=107051&logo=A0000400", "https://upos-sz-mirrorali.xxxxxxxxxx.com/upgcxcode/07/13/1267024297/1267024297-1-100113.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1695874583&gen=playurlv2&os=alibv&oi=3550958494&trid=1a6a4eb478e4473c92669eaa7931691bu&mid=1716139964&platform=pc&upsig=7b626b942dab4437433e276e1dfd6c63&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&bvc=vod&nettype=0&orderid=1,3&buvid=423719E5-C1F3-8E99-9949-C4F0FD92681D98687infoc&build=0&f=u_0_0&agrr=0&bw=107051&logo=40000000"],
    "bandwidth": 855502,
    "mimeType": "video/mp4",
    "mime_type": "video/mp4",
    "codecs": "hev1.1.6.L150.90",
    "width": 1920,
    "height": 1080,
    "frameRate": "23.810",
    "frame_rate": "23.810",
    "sar": "1:1",
    "startWithSap": 1,
    "start_with_sap": 1,
    "SegmentBase": {
        "Initialization": "0-1570",
        "indexRange": "1571-8935"
    },
    "segment_base": {
        "initialization": "0-1570",
        "index_range": "1571-8935"
    },
    "codecid": 12
}

我们看到有好几个url,实际上用第一个baseUrl即可。

现在我们拿到了视频真实地址,接下来该下载视频了,现在我们需要再回头分析视频下载请求。

先看请求头:

从【抓包分析】到【代码实战】,实现下载某破站视频(附源码)

我们构造请求的时候可以把上面这些头都设置上,但是经过我的验证,实际上我们只需要设置下面这几个头即可:

Origin:就设置破站的域名即可

Referer:设置这个视频在浏览器地址栏中的地址即可

User-Agent:设置这个固定值即可

Range:上面截图设置的是bytes=0-1570,这个是咋回事?

观察一下上面的json,看到了吧,就设置为这个值就可以了。

从【抓包分析】到【代码实战】,实现下载某破站视频(附源码)

是不是万事大吉了呢?根据以上思路,我构造了个请求试了下,果然还有问题,为啥?显然是Range:bytes=0-1570的问题。

不过,在这个请求的应答头里面,可以找到答案:

从【抓包分析】到【代码实战】,实现下载某破站视频(附源码)

很显然,这个值就是视频的完整大小,所以,咱们设置为Range:bytes=0-3098152果然,下载下来了完整视频。

所以,我们的逻辑应该是,先设置Range:bytes=0-1570,请求一次,从这次请求的应答头中找到Content-Range,拿到视频的总大小3098152。再请求一次,设置Range:bytes=0-3098152,这次的应答,便是完整的视频文件了。

现在总该万事大吉了吧?打开视频检验一下。还是有点不对劲,只有人像,没有声音。

3、下载声音文件

再回看前面的完整json,视频文件信息是从data.dash.video路径下面找到的,我们看到还有一个data.dash.audio路径,显然,这是声音文件。

所以说破站是视频、音频分离的。

接下来我们还要把音频文件下载下来,下载过程跟上面视频文件是一致的,这里不再啰嗦了。

四、程序实现

代码基于SpringBoot,开发一个Web程序,部署到云服务器,供用户下载视频。

1、拿到完整json

根据前面的分析,我们首先请求视频在浏览器地址栏中的地址,拿到html。然后从html中拿到json,最后从json中拿到视频信息和音频信息,代码如下:

logger.info("开始解析视频地址:{}",url);
String html = restTemplate.getForObject(url,String.class);
String regex = "(?<=<script>window.__playinfo__=).*?(?=</script>)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(html);
if (matcher.find()) {
    String jsonStr = matcher.group();
    JSON json = JSONUtil.parse(jsonStr);
    JSONArray videoList = (JSONArray)json.getByPath("data.dash.video");
    JSONArray audioList = (JSONArray)json.getByPath("data.dash.audio");
}

2、下载视频文件

for (Object video:videoList){
    JSONObject map = (JSONObject)video;
    String videoUrl = map.get("baseUrl").toString();
    String segmentInit = map.getByPath("SegmentBase.Initialization").toString();
    RequestCallback requestCallback = new RequestCallback() {
        @Override
        public void doWithRequest(ClientHttpRequest clientHttpRequest) throws IOException {
            clientHttpRequest.getHeaders().add("Referer",url);
            clientHttpRequest.getHeaders().add("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36");
            clientHttpRequest.getHeaders().add("Range","bytes="+segmentInit);
        }
    };
    ResponseExtractor responseExtractor = new ResponseExtractor<String>() {
        @Override
        public String extractData(ClientHttpResponse clientHttpResponse) throws IOException {
            return clientHttpResponse.getHeaders().get("Content-Range").get(0).split("/")[1];
        }
    };
    Object videoSize = restTemplate.execute(videoUrl, HttpMethod.GET,requestCallback,responseExtractor);
    logger.info("视频地址:{}",videoUrl);
    logger.info("视频大小:{}",videoSize);
    RequestCallback videoRequestCallback = new RequestCallback() {
        @Override
        public void doWithRequest(ClientHttpRequest clientHttpRequest) throws IOException {
            clientHttpRequest.getHeaders().add("Referer",url);
            clientHttpRequest.getHeaders().add("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36");
            clientHttpRequest.getHeaders().add("Range","bytes=0-"+videoSize);
        }
    };
    String fileName = StringUtils.substringBefore(videoUrl,".m4s");
    fileName = StringUtils.substringAfterLast(fileName,"/");
    final String finalFileName = fileName +".mp4";
    ResponseExtractor videoResponseExtractor = new ResponseExtractor<Boolean>() {
        @Override
        public Boolean extractData(ClientHttpResponse clientHttpResponse) throws IOException {
            OutputStream output = null;
            try {
                output = new FileOutputStream(dir+ finalFileName);
                logger.info("开始下载视频文件:{}",finalFileName);
                IOUtils.copy(clientHttpResponse.getBody(),output);
                logger.info("视频文件下载完成:{}",finalFileName);
                return Boolean.TRUE;
            }catch (Exception e){
                e.printStackTrace();
                return Boolean.FALSE;
            }finally {
                if (output != null){
                    output.close();
                }
            }
        }
    };
    Object result = restTemplate.execute(videoUrl, HttpMethod.GET,videoRequestCallback,videoResponseExtractor);
    if ((Boolean)result){
        videoFile = finalFileName;
        break;
    }
}

3、下载音频文件

与下载视频文件逻辑一致,不再贴出。

4、视频音频合并

上面的步骤把视频和音频文件都下载下来了,我们需要将这俩合并成一个文件。

百度搜索一个叫ffmpeg的东西,这是个武功高强的音视频处理工具,具体有多高强呢,我估计有三四层楼那么高。

使用这个工具合并音频视频,正常是在命令行中这么用:

ffmpeg -i 视频文件名.mp4 -i 音频文件名.mp3 -c:v copy -c:a copy 输出文件名.mp4

集成到java代码中,其实就是执行上面的命令即可:

List<String> commands = new ArrayList<>();
commands.add(ffmpegPath);
commands.add("-i");
commands.add(dir+videoFile);
commands.add("-i");
commands.add(dir+audioFile);
commands.add("-c:v");
commands.add("copy");
commands.add("-c:a");
commands.add("copy");
commands.add(dir+"final-file.mp4");
logger.info("开始合成视频音频");
ProcessBuilder builder = new ProcessBuilder();
builder.command(commands);
try {
    builder.inheritIO().start().waitFor();
    logger.info("视频合成完成");
} catch (InterruptedException | IOException e) {
    logger.info("视频合成失败:{}", ExceptionUtils.getStackTrace(e));
}

5、文件下载

从用户使用的角度来看,整个流程是这样:

1)输入视频地址

2)后台将对应的视频音频下载并合并成最终mp4文件,保存在磁盘

3)返回保存在磁盘上的mp4文件名,并提示用户是否要下载该视频

4)用户确定后,将3中返回的文件名回传给后台,后台找到文件磁盘保存地址,并下载

下面是下载的相应代码:

logger.info("下载视频文件:{}",file);
if (StringUtils.isEmpty(file)){
    return;
}
String[] arr = file.split("_");
if (arr.length != 2){
    return;
}
String filePath = baseDir+File.separator+arr[0]+File.separator+arr[1];
if (!FileUtil.exist(filePath)){
    return;
}
HttpFile.downloadFile(arr[1],filePath,response);
FileUtil.del(baseDir+File.separator+arr[0]);

五、部署到云服务器

我手上本来就有一台腾讯云服务器,直接拿来用即可。作为一个程序员,云服务器现在应该是标配了,学生可以用来学习,菜鸟可以用来练手,老鸟玩点有趣的东西偶尔赚点小钱。你如果想买一台云服务器来玩儿,下面是直达腾讯云优惠专区的链接:

https://cloud.tencent.com/act/cps/redirect?redirect=5186&cps_key=814b8b5d55ef58acc94a1b6bf43d5a2b&from=console

1、打包

maven命令随便打个包吧:mvn clean package,或者在你的IDE上双击一下某个按钮。

2、上传

连上你的服务器,把jar包扔上去。这里推荐一个工具:FinalShell,集shell和ftp于一体,非常方便。

3、启动

端口默认配置的30016,可以根据需要进行修改。通过以下命令就可以愉快的启动服务了:

nohup java -jar xxxxxxxx.jar >/dev/null 2>&1 &

4、安装ffmpeg

上面提到,破站是视频、音频分离的,所以程序中需要调用ffmpeg这个工具将视频和音频文件进行合并。ffmpeg工具的安装方法可自行搜索,这不是本文重点。

5、验证一下

目前,可以通过http://服务器公网ip:30016 的方式在全世界每个角落进行访问了。当然,你也可以申请一个域名。

我把这个程序部署到了腾讯云上,试试效果:http://106.53.17.139:30016/

源码请猛点(0积分):https://download.csdn.net/download/u012071890/88403445

或者到这里获取:https://github.com/shenmejianghu/bili-downloader

编译完jar包赶紧扔你的服务器上,开启你的“装13”加“年入千万”之旅吧。

注:本代码基于破站鬼畜视频模块抓包分析,不一定适用于所有模块,可自行分析扩展代码,原理相通。

六、总结

整个开发过程结束了,这其中最重要的环节是抓包分析的过程,如果这个过程搞定了,剩下的编码工作量其实很小。

最后,恳请大家不要乱来,千万别给我上Jmeter,如果给我干崩了,我可拿你没办法。学习交流使用,真的不要乱来哦。文章来源地址https://www.toymoban.com/news/detail-711475.html

到了这里,关于从【抓包分析】到【代码实战】,实现下载某破站视频(附源码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包