從【抓包分析】到【程式碼實戰】,實現下載某破站視訊(附原始碼)

2023-10-23 12:01:12

一、前言

前兩天,我的鄰居找到我,問我某破站的視訊能否幫她下載成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,如果給我幹崩了,我可拿你沒辦法。學習交流使用,真的不要亂來哦。