Skip to content

Commit

Permalink
fix(extractors/bilibili): Change the parsing method for some videos
Browse files Browse the repository at this point in the history
  • Loading branch information
Half9000 committed Jul 24, 2020
1 parent 03c160f commit e463c20
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 39 deletions.
2 changes: 1 addition & 1 deletion downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ func (downloader *Downloader) Download(data *types.Data) error {
}

fmt.Printf("Merging video parts into %s\n", mergedFilePath)
if stream.Ext != "mp4" || data.Site == "YouTube youtube.com" || data.Site == "哔哩哔哩 bilibili.com" {
if stream.Ext != "mp4" || stream.NeedMux == true {
return utils.MergeFilesWithSameExtension(parts, mergedFilePath)
}
return utils.MergeToMP4(parts, mergedFilePath, title)
Expand Down
97 changes: 60 additions & 37 deletions extractors/bilibili/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,40 @@ func genAPI(aid, cid, quality int, bangumi bool, cookie string) (string, error)
}

func genParts(dashData *dashInfo, quality int, referer string) ([]*types.Part, error) {
parts := make([]*types.Part, 2)
checked := false
for _, stream := range dashData.Streams.Video {
if stream.ID == quality {
s, err := request.Size(stream.BaseURL, referer)
if err != nil {
return nil, err
}
parts[0] = &types.Part{
URL: stream.BaseURL,
Size: s,
Ext: "mp4",
parts := make([]*types.Part, 1)
if dashData.Streams.Audio == nil {
url := dashData.DURL[0].URL
_, ext, err := utils.GetNameAndExt(url)
if err != nil {
return nil, err
}
parts[0] = &types.Part{
URL: url,
Size: dashData.DURL[0].Size,
Ext: ext,
}

} else {

checked := false
for _, stream := range dashData.Streams.Video {
if stream.ID == quality {
s, err := request.Size(stream.BaseURL, referer)
if err != nil {
return nil, err
}
parts[0] = &types.Part{
URL: stream.BaseURL,
Size: s,
Ext: "mp4",
}
checked = true
break
}
checked = true
break
}
}
if !checked {
return nil, nil
if !checked {
return nil, nil
}
}
return parts, nil
}
Expand Down Expand Up @@ -308,25 +323,28 @@ func bilibiliDownload(options bilibiliOptions, extractOption types.Options) *typ
dashData = data.Data
}

// Get audio part
var audioID int
audios := map[int]string{}
bandwidth := 0
for _, stream := range dashData.Streams.Audio {
if stream.Bandwidth > bandwidth {
audioID = stream.ID
}
audios[stream.ID] = stream.BaseURL
bandwidth = stream.Bandwidth
}
s, err := request.Size(audios[audioID], referer)
if err != nil {
return types.EmptyData(options.url, err)
}
audioPart := &types.Part{
URL: audios[audioID],
Size: s,
Ext: "m4a",
var audioPart *types.Part
if dashData.Streams.Audio != nil {
// Get audio part
var audioID int
audios := map[int]string{}
bandwidth := 0
for _, stream := range dashData.Streams.Audio {
if stream.Bandwidth > bandwidth {
audioID = stream.ID
}
audios[stream.ID] = stream.BaseURL
bandwidth = stream.Bandwidth
}
s, err := request.Size(audios[audioID], referer)
if err != nil {
return types.EmptyData(options.url, err)
}
audioPart = &types.Part{
URL: audios[audioID],
Size: s,
Ext: "m4a",
}
}

streams := make(map[string]*types.Stream, len(dashData.Quality))
Expand Down Expand Up @@ -360,16 +378,21 @@ func bilibiliDownload(options bilibiliOptions, extractOption types.Options) *typ
if err != nil {
return types.EmptyData(options.url, err)
}
parts[1] = audioPart
var size int64
for _, part := range parts {
size += part.Size
}
if audioPart != nil {
parts = append(parts, audioPart)
}
streams[strconv.Itoa(q)] = &types.Stream{
Parts: parts,
Size: size,
Quality: qualityString[q],
}
if audioPart != nil {
streams[strconv.Itoa(q)].NeedMux = true
}
}

// get the title
Expand Down
2 changes: 1 addition & 1 deletion extractors/bilibili/bilibili_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestBilibili(t *testing.T) {
args: test.Args{
URL: "https://www.bilibili.com/video/av41301960",
Title: "【英雄联盟】2019赛季CG 《觉醒》",
Size: 65774670,
Size: 62266048,
Quality: "高清 1080P",
},
playlist: false,
Expand Down
6 changes: 6 additions & 0 deletions extractors/bilibili/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@ type dashStreams struct {
Audio []dashStream `json:"audio"`
}

type dURL struct {
Size int64 `json:"size"`
URL string `json:"url"`
}

type dashInfo struct {
CurQuality int `json:"quality"`
Description []string `json:"accept_description"`
Quality []int `json:"accept_quality"`
Streams dashStreams `json:"dash"`
DURL []dURL `json:"durl"`
}

type dash struct {
Expand Down
2 changes: 2 additions & 0 deletions extractors/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Stream struct {
Size int64 `json:"size"`
// the file extension after video parts merged
Ext string `json:"ext"`
// if the parts need mux
NeedMux bool
}

// DataType indicates the type of extracted data, eg: video or image.
Expand Down
1 change: 1 addition & 0 deletions extractors/youtube/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func genStream(videoFormat *streamFormat, videoInfo *ytdl.VideoInfo) (*types.Str
ID: strconv.Itoa(videoFormat.Itag),
Parts: []*types.Part{video},
Quality: quality,
NeedMux: true,
}, nil
}

Expand Down

0 comments on commit e463c20

Please sign in to comment.