Skip to content

plugins.bilibili: rewrite and fix plugin #5693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2023

Conversation

bastimeyer
Copy link
Member

Fixes #5692

@hack11112 please check the plugin
https://github.com/streamlink/streamlink/blob/master/CONTRIBUTING.md#pull-request-feedback

As mentioned, no high quality streams available, because they require a login, and I don't have access to an account


$ streamlink --json https://live.bilibili.com/10129169 | jq .metadata
{
  "id": "432478849957793540",
  "author": "卡比同学",
  "category": "CS:GO",
  "title": "B站最纯真的直播间"
}
$ streamlink -l debug https://live.bilibili.com/10129169 best
[cli][debug] OS:         Linux-6.6.2-1-git-x86_64-with-glibc2.38
[cli][debug] Python:     3.12.0
[cli][debug] OpenSSL:    OpenSSL 3.1.4 24 Oct 2023
[cli][debug] Streamlink: 6.4.1+7.gbcbc32a3
[cli][debug] Dependencies:
[cli][debug]  certifi: 2023.11.17
[cli][debug]  isodate: 0.6.1
[cli][debug]  lxml: 4.9.3
[cli][debug]  pycountry: 22.3.5
[cli][debug]  pycryptodome: 3.19.0
[cli][debug]  PySocks: 1.7.1
[cli][debug]  requests: 2.31.0
[cli][debug]  trio: 0.23.1
[cli][debug]  trio-websocket: 0.11.1
[cli][debug]  typing-extensions: 4.8.0
[cli][debug]  urllib3: 2.1.0
[cli][debug]  websocket-client: 1.6.4
[cli][debug] Arguments:
[cli][debug]  url=https://live.bilibili.com/10129169
[cli][debug]  stream=['best']
[cli][debug]  --loglevel=debug
[cli][debug]  --player=mpv
[cli][info] Found matching plugin bilibili for URL https://live.bilibili.com/10129169
[cli][info] Available streams: live_alt, live (worst, best)
[cli][info] Opening stream: live (hls)
[cli][info] Starting player: mpv
[stream.hls][debug] Reloading playlist
[cli][debug] Pre-buffering 8192 bytes
[stream.hls][debug] First Sequence: 60216950; Last Sequence: 60216955
[stream.hls][debug] Start offset: 0; Duration: None; Start Sequence: 60216953; End Sequence: None
[stream.hls][debug] Adding segment 60216953 to queue
[stream.hls][debug] Adding segment 60216954 to queue
[stream.hls][debug] Adding segment 60216955 to queue
[stream.hls][debug] Writing segment 60216953 to output
[stream.hls][debug] Segment initialization 60216953 complete
[cli.output][debug] Opening subprocess: ['/home/basti/.local/bin/mpv', '--force-media-title=https://live.bilibili.com/10129169', '-']
[stream.hls][debug] Writing segment 60216953 to output
[stream.hls][debug] Segment 60216953 complete
[cli][debug] Writing stream to output
[stream.hls][debug] Writing segment 60216954 to output
[stream.hls][debug] Segment 60216954 complete
[stream.hls][debug] Reloading playlist
[stream.hls][debug] Writing segment 60216955 to output
[stream.hls][debug] Segment 60216955 complete
[stream.hls][debug] Adding segment 60216956 to queue
[stream.hls][debug] Adding segment 60216957 to queue
[stream.hls][debug] Writing segment 60216956 to output
[stream.hls][debug] Segment 60216956 complete
[stream.hls][debug] Writing segment 60216957 to output
[stream.hls][debug] Segment 60216957 complete
[stream.hls][debug] Reloading playlist
[stream.hls][debug] Adding segment 60216958 to queue
[stream.hls][debug] Adding segment 60216959 to queue
[stream.hls][debug] Writing segment 60216958 to output
[stream.hls][debug] Segment 60216958 complete
[cli][info] Player closed
[stream.segmented][debug] Closing worker thread
[stream.segmented][debug] Closing writer thread
[cli][info] Stream ended
[cli][info] Closing currently open stream...

@bastimeyer bastimeyer added the plugin issue A Plugin does not work correctly label Nov 28, 2023
Copy link
Member

@gravyboat gravyboat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll mark as approved but let's wait to merge till the reporter confirms this fixes the plugin.

@bastimeyer
Copy link
Member Author

Going to merge this now, since the current plugin implementation on master is completely broken anyway. Then this fix can be included in 6.4.2 which I will publish right after that.

@bastimeyer bastimeyer merged commit 6ede2c0 into streamlink:master Nov 28, 2023
@bastimeyer bastimeyer deleted the plugins/bilibili/rewrite branch November 28, 2023 19:38
@ghost
Copy link

ghost commented Dec 3, 2023

现在要合并它,因为 master 上当前的插件实现无论如何都完全被破坏了。 然后这个修复可以包含在 6.4.2 中,我将在之后立即发布。
It seems that the latest version still cannot solve this problem. What is obtained is still low -quality

@ghost
Copy link

ghost commented Dec 3, 2023

Going to merge this now, since the current plugin implementation on master is completely broken anyway. Then this fix can be included in 6.4.2 which I will publish right after that.

// Package liveurls
// @Time:2023/02/10 01:03
// @File:bilibili.go
// @SoftWare:Goland
// @Author:feiyang
// @Contact:TG@feiyangdigital

package liveurls

import (
	"encoding/json"
	"fmt"
	"github.com/tidwall/gjson"
	"io"
	"net/http"
)

type BiliBili struct {
	Rid      string
	Line     string
	Quality  string
	Platform string
}

func (b *BiliBili) GetRealRoomID() any {
	var firstmap = make(map[string]any)
	var realroomid string
	apiurl := "https://api.live.bilibili.com/room/v1/Room/room_init?id=" + b.Rid
	client := &http.Client{}
	r, _ := http.NewRequest("GET", apiurl, nil)
	r.Header.Add("user-agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 16_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Mobile/15E148 Safari/604.1")
	resp, _ := client.Do(r)
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	json.Unmarshal(body, &firstmap)
	if firstmap["msg"] == "直播间不存在" {
		return nil
	}
	if newmap, ok := firstmap["data"].(map[string]any); ok {
		if newmap["live_status"] != float64(1) {
			return nil
		} else {
			if flt, ok := newmap["room_id"].(float64); ok {
				realroomid = fmt.Sprintf("%v", int(flt))
			}
		}

	}
	return realroomid
}

func (b *BiliBili) GetPlayUrl() any {
	var roomid string
	var realurl string
	if str, ok := b.GetRealRoomID().(string); ok {
		roomid = str
	} else {
		return nil
	}
	client := &http.Client{}
	params := map[string]string{
		"room_id":  roomid,
		"protocol": "0,1",
		"format":   "0,1,2",
		"codec":    "0,1",
		"qn":       b.Quality,
		"platform": b.Platform,
		"ptype":    "8",
	}
	r, _ := http.NewRequest("GET", "https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo", nil)
	q := r.URL.Query()
	for k, v := range params {
		q.Add(k, v)
	}
	r.URL.RawQuery = q.Encode()
	resp, _ := client.Do(r)
	defer resp.Body.Close()
	body, _ := io.ReadAll(resp.Body)
	var json = string(body)
	value := gjson.Get(json, "data.playurl_info.playurl.stream")
	value.ForEach(func(key, value gjson.Result) bool {
		newvalue := gjson.Get(value.String(), "format.0.format_name")
		if newvalue.String() == "ts" {
			nnvalue := gjson.Get(value.String(), "format.#")
			valuelast := fmt.Sprintf("%v", nnvalue.Int()-1)
			codeclen := gjson.Get(value.String(), "format."+valuelast+".codec.#")
			codeclast := fmt.Sprintf("%v", codeclen.Int()-1)
			base_url := gjson.Get(value.String(), "format."+valuelast+".codec."+codeclast+".base_url")
			url_info := gjson.Get(value.String(), "format."+valuelast+".codec."+codeclast+".url_info")
			url_info.ForEach(func(key, value gjson.Result) bool {
				keyval := fmt.Sprintf("%v", key)
				switch b.Line {
				case "first":
					if keyval == "0" {
						host := gjson.Get(value.String(), "host")
						extra := gjson.Get(value.String(), "extra")
						realurl = fmt.Sprintf("%v%v%v", host, base_url, extra)
					}

				case "second":
					if keyval == "1" {
						host := gjson.Get(value.String(), "host")
						extra := gjson.Get(value.String(), "extra")
						realurl = fmt.Sprintf("%v%v%v", host, base_url, extra)
					}
				case "third":
					if keyval == "2" {
						host := gjson.Get(value.String(), "host")
						extra := gjson.Get(value.String(), "extra")
						realurl = fmt.Sprintf("%v%v%v", host, base_url, extra)
					}
				}

				return true
			})
		}
		return true
	})
	return realurl
}

This Go language can be written to 720p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin issue A Plugin does not work correctly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

plugins.bilibili: Some live streams cannot be obtained
2 participants