提取视频

分离网址获取Bvid

网址https://www.bilibili.com/video/BV1M9XeYJE7V/?spm_id_from=333.1007.tianma.1-1-1.click&vd_source=d5b9a2e757fb051555e941f2124bf792的bvid为
BV1M9XeYJE7V

利用正则表达式
package com.download.bilidown.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BliApi {
    public static String extractBvidFromUrl(String url) {
        //构建正则:10位的BV开头的
        Pattern pattern = Pattern.compile("(?<=video/|BV)[a-zA-Z0-9]{10}");
        Matcher matcher = pattern.matcher(url);

        if (matcher.find()) {
            String bvid = matcher.group();
            return bvid.startsWith("BV") ? bvid : "BV" + bvid;
        }
        return null;
    }

    public static void main(String[] args) {
        String url1 = "https://www.bilibili.com/video/BV1M9XeYJE7V/?spm_id_from=333.1007.tianma.1-1-1.click&vd_source=d5b9a2e757fb051555e941f2124bf792";
        String url2 = "https://b23.tv/BV1GJ411x7h7";

        System.out.println(extractBvidFromUrl(url1));
        System.out.println(extractBvidFromUrl(url2));
    }
}

利用网址路径
import java.net.URI;
import java.net.URISyntaxException;

public class BvidExtractor {
    public static String extractBvidFromUrl(String url) throws URISyntaxException {
        URI uri = new URI(url);
        String path = uri.getPath();
        
        // 从路径中提取BV号
        if (path.contains("/video/BV")) {
            String[] parts = path.split("/");
            for (String part : parts) {
                if (part.startsWith("BV") && part.length() == 12) {
                    return part;
                }
            }
        }
        return null;
    }

    public static void main(String[] args) throws URISyntaxException {
        String url = "https://www.bilibili.com/video/BV1M9XeYJE7V/?spm_id_from=333.1007.tianma.1-1-1.click&vd_source=d5b9a2e757fb051555e941f2124bf792";
        System.out.println(extractBvidFromUrl(url)); // 输出: BV1GJ411x7h7
    }
}

获取Cid

这里我们需要用到一条api链接:https://api.bilibili.com/x/web-interface/view?bvid=
我们将Bvid添加到这条链接末尾进行访问,它会返回一个json文本,其中就包括我们需要的Cid

json格式示例

{
    "code": 0,
    "message": "0",
    "ttl": 1,
    "data": {
        "bvid": "BV1SGXsYxESV",
        "aid": 114205780875470,
        "videos": 1,
        "tid": 122,
        "tid_v2": 2099,
        "tname": "野生技能协会",
        "tname_v2": "电脑",
        "copyright": 1,
        "pic": "http://i2.hdslb.com/bfs/archive/22f605c2deacab8910da7a130dc3e12b63188d6f.jpg",
        "title": "内存和显存的区别",
        "pubdate": 1742643088,
        "ctime": 1742643088,
        "desc": "本期视频将为你介绍,内存和显存的区别",
        "desc_v2": [
            {
                "raw_text": "本期视频将为你介绍,内存和显存的区别",
                "type": 1,
                "biz_id": 0
            }
        ],
        "state": 0,
        "duration": 808,
        "rights": {
            "bp": 0,
            "elec": 0,
            "download": 1,
            "movie": 0,
            "pay": 0,
            "hd5": 0,
            "no_reprint": 1,
            "autoplay": 1,
            "ugc_pay": 0,
            "is_cooperation": 0,
            "ugc_pay_preview": 0,
            "no_background": 0,
            "clean_mode": 0,
            "is_stein_gate": 0,
            "is_360": 0,
            "no_share": 0,
            "arc_pay": 0,
            "free_watch": 0
        },
        "owner": {
            "mid": 38154792,
            "name": "Redknot-乔红",
            "face": "https://i0.hdslb.com/bfs/face/860929c8286960634d8497d5d5e65268c304da0e.jpg"
        },
        "stat": {
            "aid": 114205780875470,
            "view": 376713,
            "danmaku": 1161,
            "reply": 1022,
            "favorite": 11844,
            "coin": 7796,
            "share": 922,
            "now_rank": 0,
            "his_rank": 0,
            "like": 18729,
            "dislike": 0,
            "evaluation": "",
            "vt": 0
        },
        "argue_info": {
            "argue_msg": "",
            "argue_type": 0,
            "argue_link": ""
        },
        "dynamic": "",
        "cid": 29009383036,
        "dimension": {
            "width": 3840,
            "height": 2160,
            "rotate": 0
        },
        "season_id": 1891377,
        "teenage_mode": 0,
        "is_chargeable_season": false,
        "is_story": false,
        "is_upower_exclusive": false,
        "is_upower_play": false,
        "is_upower_preview": false,
        "enable_vt": 0,
        "vt_display": "",
        "is_upower_exclusive_with_qa": false,
        "no_cache": false,
        "pages": [
            {
                "cid": 29009383036,
                "page": 1,
                "from": "vupload",
                "part": "内存和显存的区别",
                "duration": 808,
                "vid": "",
                "weblink": "",
                "dimension": {
                    "width": 3840,
                    "height": 2160,
                    "rotate": 0
                },
                "first_frame": "http://i1.hdslb.com/bfs/storyff/n250322sa18ulds4qbzwbg25l2bie142_firsti.jpg",
                "ctime": 1742643088
            }
        ],
        "subtitle": {
            "allow_submit": false,
            "list": [

            ]
        },
        "ugc_season": {
            "id": 1891377,
            "title": "乔红的半导体世界",
            "cover": "https://archive.biliimg.com/bfs/archive/04e02eb1f9b8171f927cc4bac0d2a4bc8aee2e60.jpg",
            "mid": 38154792,
            "intro": "",
            "sign_state": 0,
            "attribute": 140,
            "sections": [
                {
                    "season_id": 1891377,
                    "id": 2172758,
                    "title": "正片",
                    "type": 1,
                    "episodes": [
                        {
                            "season_id": 1891377,
                            "section_id": 2172758,
                            "id": 101853874,
                            "aid": 113772760931132,
                            "cid": 27706592614,
                            "title": "用最好的动画为你讲解--内存的原理",
                            "attribute": 0,
                            "arc": {
                                "aid": 113772760931132,
                                "videos": 0,
                                "type_id": 0,
                                "type_name": "",
                                "copyright": 0,
                                "pic": "http://i2.hdslb.com/bfs/archive/7bdda2abccd11279793692fb79dd6f94d5a1088f.jpg",
                                "title": "用最好的动画为你讲解--内存的原理",
                                "pubdate": 1736035760,
                                "ctime": 1736035760,
                                "desc": "",
                                "state": 0,
                                "duration": 809,
                                "rights": {
                                    "bp": 0,
                                    "elec": 0,
                                    "download": 0,
                                    "movie": 0,
                                    "pay": 0,
                                    "hd5": 0,
                                    "no_reprint": 0,
                                    "autoplay": 0,
                                    "ugc_pay": 0,
                                    "is_cooperation": 0,
                                    "ugc_pay_preview": 0,
                                    "arc_pay": 0,
                                    "free_watch": 0
                                },
                                "author": {
                                    "mid": 38154792,
                                    "name": "Redknot-乔红",
                                    "face": "https://i0.hdslb.com/bfs/face/860929c8286960634d8497d5d5e65268c304da0e.jpg"
                                },
                                "stat": {
                                    "aid": 113772760931132,
                                    "view": 2768455,
                                    "danmaku": 3568,
                                    "reply": 2964,
                                    "fav": 126052,
                                    "coin": 50438,
                                    "share": 7294,
                                    "now_rank": 0,
                                    "his_rank": 30,
                                    "like": 137177,
                                    "dislike": 0,
                                    "evaluation": "",
                                    "argue_msg": "",
                                    "vt": 9000886,
                                    "vv": 2768455
                                },
                                "dynamic": "",
                                "dimension": {
                                    "width": 0,
                                    "height": 0,
                                    "rotate": 0
                                },
                                "is_chargeable_season": false,
                                "is_blooper": false,
                                "enable_vt": 0,
                                "vt_display": "",
                                "type_id_v2": 0,
                                "type_name_v2": "",
                                "is_lesson_video": 0
                            },
                            "page": {
                                "cid": 27706592614,
                                "page": 1,
                                "from": "vupload",
                                "part": "用最好的动画为你讲解--内存的原理",
                                "duration": 809,
                                "vid": "",
                                "weblink": "",
                                "dimension": {
                                    "width": 3840,
                                    "height": 2160,
                                    "rotate": 0
                                }
                            },
                            "bvid": "BV1Htr8YhELV",
                            "pages": [
                                {
                                    "cid": 27706592614,
                                    "page": 1,
                                    "from": "vupload",
                                    "part": "用最好的动画为你讲解--内存的原理",
                                    "duration": 809,
                                    "vid": "",
                                    "weblink": "",
                                    "dimension": {
                                        "width": 3840,
                                        "height": 2160,
                                        "rotate": 0
                                    }
                                }
                            ]
                        },
                        {
                            "season_id": 1891377,
                            "section_id": 2172758,
                            "id": 44661430,
                            "aid": 578646750,
                            "cid": 1337864921,
                            "title": "来学造芯片吧!MOSFET是什么?",
                            "attribute": 0,
                            "arc": {
                                "aid": 578646750,
                                "videos": 0,
                                "type_id": 0,
                                "type_name": "",
                                "copyright": 0,
                                "pic": "http://i0.hdslb.com/bfs/archive/04e02eb1f9b8171f927cc4bac0d2a4bc8aee2e60.jpg",
                                "title": "来学造芯片吧!MOSFET是什么?",
                                "pubdate": 1700449200,
                                "ctime": 1700449200,
                                "desc": "",
                                "state": 0,
                                "duration": 522,
                                "rights": {
                                    "bp": 0,
                                    "elec": 0,
                                    "download": 0,
                                    "movie": 0,
                                    "pay": 0,
                                    "hd5": 0,
                                    "no_reprint": 0,
                                    "autoplay": 0,
                                    "ugc_pay": 0,
                                    "is_cooperation": 0,
                                    "ugc_pay_preview": 0,
                                    "arc_pay": 0,
                                    "free_watch": 0
                                },
                                "author": {
                                    "mid": 38154792,
                                    "name": "Redknot-乔红",
                                    "face": "https://i0.hdslb.com/bfs/face/860929c8286960634d8497d5d5e65268c304da0e.jpg"
                                },
                                "stat": {
                                    "aid": 578646750,
                                    "view": 170072,
                                    "danmaku": 170,
                                    "reply": 136,
                                    "fav": 4015,
                                    "coin": 3225,
                                    "share": 466,
                                    "now_rank": 0,
                                    "his_rank": 0,
                                    "like": 5193,
                                    "dislike": 0,
                                    "evaluation": "",
                                    "argue_msg": "",
                                    "vt": 619154,
                                    "vv": 170072
                                },
                                "dynamic": "",
                                "dimension": {
                                    "width": 0,
                                    "height": 0,
                                    "rotate": 0
                                },
                                "is_chargeable_season": false,
                                "is_blooper": false,
                                "enable_vt": 0,
                                "vt_display": "",
                                "type_id_v2": 0,
                                "type_name_v2": "",
                                "is_lesson_video": 0
                            },
                            "page": {
                                "cid": 1337864921,
                                "page": 1,
                                "from": "vupload",
                                "part": "什么是金属氧化物半导体场效应晶体管? MOSFET",
                                "duration": 522,
                                "vid": "",
                                "weblink": "",
                                "dimension": {
                                    "width": 3840,
                                    "height": 2160,
                                    "rotate": 0
                                }
                            },
                            "bvid": "BV1Vz4y1c7WJ",
                            "pages": [
                                {
                                    "cid": 1337864921,
                                    "page": 1,
                                    "from": "vupload",
                                    "part": "什么是金属氧化物半导体场效应晶体管? MOSFET",
                                    "duration": 522,
                                    "vid": "",
                                    "weblink": "",
                                    "dimension": {
                                        "width": 3840,
                                        "height": 2160,
                                        "rotate": 0
                                    }
                                }
                            ]
                        },
                        {
                            "season_id": 1891377,
                            "section_id": 2172758,
                            "id": 45100963,
                            "aid": 704080046,
                            "cid": 1281967587,
                            "title": "人类最伟大的发明--PN结",
                            "attribute": 0,
                            "arc": {
                                "aid": 704080046,
                                "videos": 0,
                                "type_id": 0,
                                "type_name": "",
                                "copyright": 0,
                                "pic": "http://i2.hdslb.com/bfs/archive/4a467dd25c821623327144416825e638333d10ae.jpg",
                                "title": "人类最伟大的发明--PN结",
                                "pubdate": 1695889800,
                                "ctime": 1695889800,
                                "desc": "",
                                "state": 0,
                                "duration": 599,
                                "rights": {
                                    "bp": 0,
                                    "elec": 0,
                                    "download": 0,
                                    "movie": 0,
                                    "pay": 0,
                                    "hd5": 0,
                                    "no_reprint": 0,
                                    "autoplay": 0,
                                    "ugc_pay": 0,
                                    "is_cooperation": 0,
                                    "ugc_pay_preview": 0,
                                    "arc_pay": 0,
                                    "free_watch": 0
                                },
                                "author": {
                                    "mid": 38154792,
                                    "name": "Redknot-乔红",
                                    "face": "https://i0.hdslb.com/bfs/face/860929c8286960634d8497d5d5e65268c304da0e.jpg"
                                },
                                "stat": {
                                    "aid": 704080046,
                                    "view": 533446,
                                    "danmaku": 770,
                                    "reply": 733,
                                    "fav": 15675,
                                    "coin": 5956,
                                    "share": 2243,
                                    "now_rank": 0,
                                    "his_rank": 0,
                                    "like": 15260,
                                    "dislike": 0,
                                    "evaluation": "",
                                    "argue_msg": "",
                                    "vt": 1452258,
                                    "vv": 533446
                                },
                                "dynamic": "",
                                "dimension": {
                                    "width": 0,
                                    "height": 0,
                                    "rotate": 0
                                },
                                "is_chargeable_season": false,
                                "is_blooper": false,
                                "enable_vt": 0,
                                "vt_display": "",
                                "type_id_v2": 0,
                                "type_name_v2": "",
                                "is_lesson_video": 0
                            },
                            "page": {
                                "cid": 1281967587,
                                "page": 1,
                                "from": "vupload",
                                "part": "人类最伟大的发明--PN结",
                                "duration": 599,
                                "vid": "",
                                "weblink": "",
                                "dimension": {
                                    "width": 3840,
                                    "height": 2160,
                                    "rotate": 0
                                }
                            },
                            "bvid": "BV1bm4y1V7Ek",
                            "pages": [
                                {
                                    "cid": 1281967587,
                                    "page": 1,
                                    "from": "vupload",
                                    "part": "人类最伟大的发明--PN结",
                                    "duration": 599,
                                    "vid": "",
                                    "weblink": "",
                                    "dimension": {
                                        "width": 3840,
                                        "height": 2160,
                                        "rotate": 0
                                    }
                                }
                            ]
                        },
                        {
                            "season_id": 1891377,
                            "section_id": 2172758,
                            "id": 45100964,
                            "aid": 532678437,
                            "cid": 1245944692,
                            "title": "学计算机的都应该看--电子管的原理",
                            "attribute": 0,
                            "arc": {
                                "aid": 532678437,
                                "videos": 0,
                                "type_id": 0,
                                "type_name": "",
                                "copyright": 0,
                                "pic": "http://i2.hdslb.com/bfs/archive/422fdd08709470754ec5e3a208d82a32b95fd569.jpg",
                                "title": "学计算机的都应该看--电子管的原理",
                                "pubdate": 1692950400,
                                "ctime": 1692950400,
                                "desc": "",
                                "state": 0,
                                "duration": 472,
                                "rights": {
                                    "bp": 0,
                                    "elec": 0,
                                    "download": 0,
                                    "movie": 0,
                                    "pay": 0,
                                    "hd5": 0,
                                    "no_reprint": 0,
                                    "autoplay": 0,
                                    "ugc_pay": 0,
                                    "is_cooperation": 0,
                                    "ugc_pay_preview": 0,
                                    "arc_pay": 0,
                                    "free_watch": 0
                                },
                                "author": {
                                    "mid": 38154792,
                                    "name": "Redknot-乔红",
                                    "face": "https://i0.hdslb.com/bfs/face/860929c8286960634d8497d5d5e65268c304da0e.jpg"
                                },
                                "stat": {
                                    "aid": 532678437,
                                    "view": 208946,
                                    "danmaku": 515,
                                    "reply": 420,
                                    "fav": 8099,
                                    "coin": 3597,
                                    "share": 639,
                                    "now_rank": 0,
                                    "his_rank": 0,
                                    "like": 9025,
                                    "dislike": 0,
                                    "evaluation": "",
                                    "argue_msg": "",
                                    "vt": 699527,
                                    "vv": 208946
                                },
                                "dynamic": "",
                                "dimension": {
                                    "width": 0,
                                    "height": 0,
                                    "rotate": 0
                                },
                                "is_chargeable_season": false,
                                "is_blooper": false,
                                "enable_vt": 0,
                                "vt_display": "",
                                "type_id_v2": 0,
                                "type_name_v2": "",
                                "is_lesson_video": 0
                            },
                            "page": {
                                "cid": 1245944692,
                                "page": 1,
                                "from": "vupload",
                                "part": "学计算机的都应该看--电子管的原理",
                                "duration": 472,
                                "vid": "",
                                "weblink": "",
                                "dimension": {
                                    "width": 3840,
                                    "height": 2160,
                                    "rotate": 0
                                }
                            },
                            "bvid": "BV1Pu411N74Q",
                            "pages": [
                                {
                                    "cid": 1245944692,
                                    "page": 1,
                                    "from": "vupload",
                                    "part": "学计算机的都应该看--电子管的原理",
                                    "duration": 472,
                                    "vid": "",
                                    "weblink": "",
                                    "dimension": {
                                        "width": 3840,
                                        "height": 2160,
                                        "rotate": 0
                                    }
                                }
                            ]
                        },
                        {
                            "season_id": 1891377,
                            "section_id": 2172758,
                            "id": 115546799,
                            "aid": 114205780875470,
                            "cid": 29009383036,
                            "title": "内存和显存的区别",
                            "attribute": 0,
                            "arc": {
                                "aid": 114205780875470,
                                "videos": 0,
                                "type_id": 0,
                                "type_name": "",
                                "copyright": 0,
                                "pic": "http://i2.hdslb.com/bfs/archive/22f605c2deacab8910da7a130dc3e12b63188d6f.jpg",
                                "title": "内存和显存的区别",
                                "pubdate": 1742643088,
                                "ctime": 1742643088,
                                "desc": "",
                                "state": 0,
                                "duration": 808,
                                "rights": {
                                    "bp": 0,
                                    "elec": 0,
                                    "download": 0,
                                    "movie": 0,
                                    "pay": 0,
                                    "hd5": 0,
                                    "no_reprint": 0,
                                    "autoplay": 0,
                                    "ugc_pay": 0,
                                    "is_cooperation": 0,
                                    "ugc_pay_preview": 0,
                                    "arc_pay": 0,
                                    "free_watch": 0
                                },
                                "author": {
                                    "mid": 38154792,
                                    "name": "Redknot-乔红",
                                    "face": "https://i0.hdslb.com/bfs/face/860929c8286960634d8497d5d5e65268c304da0e.jpg"
                                },
                                "stat": {
                                    "aid": 114205780875470,
                                    "view": 376713,
                                    "danmaku": 1161,
                                    "reply": 1022,
                                    "fav": 11844,
                                    "coin": 7796,
                                    "share": 922,
                                    "now_rank": 0,
                                    "his_rank": 0,
                                    "like": 18729,
                                    "dislike": 0,
                                    "evaluation": "",
                                    "argue_msg": "",
                                    "vt": 1618966,
                                    "vv": 376713
                                },
                                "dynamic": "",
                                "dimension": {
                                    "width": 0,
                                    "height": 0,
                                    "rotate": 0
                                },
                                "is_chargeable_season": false,
                                "is_blooper": false,
                                "enable_vt": 0,
                                "vt_display": "",
                                "type_id_v2": 0,
                                "type_name_v2": "",
                                "is_lesson_video": 0
                            },
                            "page": {
                                "cid": 29009383036,
                                "page": 1,
                                "from": "vupload",
                                "part": "内存和显存的区别",
                                "duration": 808,
                                "vid": "",
                                "weblink": "",
                                "dimension": {
                                    "width": 3840,
                                    "height": 2160,
                                    "rotate": 0
                                }
                            },
                            "bvid": "BV1SGXsYxESV",
                            "pages": [
                                {
                                    "cid": 29009383036,
                                    "page": 1,
                                    "from": "vupload",
                                    "part": "内存和显存的区别",
                                    "duration": 808,
                                    "vid": "",
                                    "weblink": "",
                                    "dimension": {
                                        "width": 3840,
                                        "height": 2160,
                                        "rotate": 0
                                    }
                                }
                            ]
                        }
                    ]
                }
            ],
            "stat": {
                "season_id": 1891377,
                "view": 4057631,
                "danmaku": 6184,
                "reply": 5275,
                "fav": 165685,
                "coin": 71012,
                "share": 11564,
                "now_rank": 0,
                "his_rank": 0,
                "like": 185384,
                "vt": 0,
                "vv": 0
            },
            "ep_count": 5,
            "season_type": 1,
            "is_pay_season": false,
            "enable_vt": 0
        },
        "is_season_display": true,
        "user_garb": {
            "url_image_ani_cut": ""
        },
        "honor_reply": {
            "honor": [
                {
                    "aid": 114205780875470,
                    "type": 7,
                    "desc": "热门收录",
                    "weekly_recommend_num": 0
                }
            ]
        },
        "like_icon": "",
        "need_jump_bv": false,
        "disable_show_up_info": false,
        "is_story_play": 1,
        "is_view_self": false
    }
}

这里我们用alibaba的fastjson来解析json

    public String getCidFromBvid(String bvid){
        try {
            // 1. 构建请求URL
            String apiUrl = BliConstant.GET_CVI_JSON_URL + bvid;

            // 2. 创建HTTP连接
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");

            // 3. 获取响应
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // 4. 使用AliJson解析JSON获取cid
                JSONObject jsonResponse = JSON.parseObject(response.toString());
                if (jsonResponse.getIntValue("code") == 0) {
                    JSONObject data = jsonResponse.getJSONObject("data");
                    return String.valueOf(data.getIntValue("cid"));
                } else {
                    System.err.println("API返回错误: " + jsonResponse.getString("message"));
                }
            } else {
                System.err.println("HTTP请求失败,状态码: " + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws URISyntaxException {
        BliApi bliApi=new BliApiImpl();
        System.out.println(bliApi.getCidFromBvid(bliApi.getBvidFromUrl("https://www.bilibili.com/video/BV1M9XeYJE7V/?spm_id_from=333.1007.tianma.1-1-1.click&vd_source=d5b9a2e757fb051555e941f2124bf792")));
    }

获取视频原始链接

获取到Bvid和Cid后,我们就可以通过另一条api链接来获取视频原始链接:https://api.bilibili.com/x/player/playurl?otype=json&fnver=0&fnval=2&player=1&qn=64&bvid=Bvid&cid=Cid
将这条链接中的Bvid和Cid换成我们刚刚获取到的,在网页中请求一下,即可获得一个json文本

获取视频播放信息
    public JSONObject getVideoPlayInfo(String bvid, String cid) {
        try {
            // 1. 构建请求URL
            String apiUrl = String.format(BliConstant.PLAY_URL_API, bvid, cid);

            // 2. 创建HTTP连接
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");
            connection.setRequestProperty("Referer", "https://www.bilibili.com");

            // 3. 获取响应
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // 4. 使用AliJson解析JSON
                return JSON.parseObject(response.toString());
            } else {
                System.err.println("HTTP请求失败,状态码: " + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

获取了之后的JSON消息格式

{
    "code": 0,
    "message": "0",
    "ttl": 1,
    "data": {
        "from": "local",
        "result": "suee",
        "message": "",
        "quality": 64,
        "format": "mp4720",
        "timelength": 807104,
        "accept_format": "mp4720,mp4",
        "accept_description": [
            "高清 720P",
            "流畅 360P"
        ],
        "accept_quality": [
            64,
            16
        ],
        "video_codecid": 7,
        "seek_param": "start",
        "seek_type": "offset",
        "durl": [
            {
                "order": 1,
                "length": 807104,
                "size": 37524023,
                "ahead": "",
                "vhead": "",
                "url": "https://upos-sz-estgcos.bilivideo.com/upgcxcode/36/30/29009383036/29009383036-1-192.mp4?e=ig8euxZM2rNcNbRVhwdVhwdlhWdVhwdVhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&platform=pc&os=upos&nbs=1&uipk=5&trid=d985f7a3732f4df182bc7946a7350c0u&mid=451550645&deadline=1743420955&oi=3402238452&tag=&gen=playurlv3&og=cos&upsig=35aad0361ff2c682740e462f46e8d58b&uparams=e,platform,os,nbs,uipk,trid,mid,deadline,oi,tag,gen,og&bvc=vod&nettype=0&bw=371985&buvid=6944EE31-6678-EFC2-7D72-25792B306F6958390infoc&build=0&dl=0&f=u_0_0&agrr=1&orderid=0,3",
                "backup_url": [
                    "https://upos-sz-mirrorcos.bilivideo.com/upgcxcode/36/30/29009383036/29009383036-1-192.mp4?e=ig8euxZM2rNcNbRVhwdVhwdlhWdVhwdVhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&deadline=1743420955&oi=3402238452&og=cos&nbs=1&uipk=5&trid=d985f7a3732f4df182bc7946a7350c0u&mid=451550645&platform=pc&tag=&gen=playurlv3&os=cosbv&upsig=192b4aa5f878def1b31bf28705aeeeb4&uparams=e,deadline,oi,og,nbs,uipk,trid,mid,platform,tag,gen,os&bvc=vod&nettype=0&bw=371985&agrr=1&buvid=6944EE31-6678-EFC2-7D72-25792B306F6958390infoc&build=0&dl=0&f=u_0_0&orderid=1,3",
                    "https://upos-sz-mirrorcos.bilivideo.com/upgcxcode/36/30/29009383036/29009383036-1-192.mp4?e=ig8euxZM2rNcNbRVhwdVhwdlhWdVhwdVhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&tag=&os=cosbv&platform=pc&trid=d985f7a3732f4df182bc7946a7350c0u&deadline=1743420955&uipk=5&gen=playurlv3&og=cos&mid=451550645&oi=3402238452&nbs=1&upsig=3a02782277f5effcce88e13fdab0ca6d&uparams=e,tag,os,platform,trid,deadline,uipk,gen,og,mid,oi,nbs&bvc=vod&nettype=0&bw=371985&agrr=1&buvid=6944EE31-6678-EFC2-7D72-25792B306F6958390infoc&build=0&dl=0&f=u_0_0&orderid=2,3"
                ]
            }
        ],
        "support_formats": [
            {
                "quality": 64,
                "format": "mp4720",
                "new_description": "720P 高清",
                "display_desc": "720P",
                "superscript": "",
                "codecs": null
            },
            {
                "quality": 16,
                "format": "mp4",
                "new_description": "360P 流畅",
                "display_desc": "360P",
                "superscript": "",
                "codecs": null
            }
        ],
        "high_format": null,
        "last_play_time": 0,
        "last_play_cid": 0,
        "view_info": null,
        "play_conf": {
            "is_new_description": false
        }
    }
}
获取视频原始链接列表
    public List<String> getVideoFromUrls(JSONObject playInfo) {
        List<String> urls = new ArrayList<>();

        if (playInfo != null && playInfo.getIntValue("code") == 0) {
            JSONObject data = playInfo.getJSONObject("data");

            // 处理两种可能的URL结构
            if (data.containsKey("dash")) {
                // DASH格式流
                JSONArray dashVideo = data.getJSONObject("dash").getJSONArray("video");
                for (int i = 0; i < dashVideo.size(); i++) {
                    JSONObject video = dashVideo.getJSONObject(i);
                    urls.add(video.getString("baseUrl"));
                }
            } else if (data.containsKey("durl")) {
                // 普通格式流
                JSONArray durl = data.getJSONArray("durl");
                for (int i = 0; i < durl.size(); i++) {
                    JSONObject video = durl.getJSONObject(i);
                    urls.add(video.getString("url"));

                    // 添加备用URL
                    JSONArray backupUrls = video.getJSONArray("backup_url");
                    if (backupUrls != null) {
                        for (int j = 0; j < backupUrls.size(); j++) {
                            urls.add(backupUrls.getString(j));
                        }
                    }
                }
            }
        }

        return urls;
    }
获取音频原始链接列表
    public String getAudioFromUrl(JSONObject playInfo) {
        if (playInfo != null && playInfo.getIntValue("code") == 0) {
            JSONObject data = playInfo.getJSONObject("data");

            // 处理DASH格式的音频
            if (data.containsKey("dash")) {
                JSONArray dashAudio = data.getJSONObject("dash").getJSONArray("audio");
                if (dashAudio != null && dashAudio.size() > 0) {
                    return dashAudio.getJSONObject(0).getString("baseUrl");
                }
            }
            //TODO 支持其他格式的音频
        }
        return null;
    }

下载

有了对应的视频原始链接,即可通过文件传输来下载视频了