Skip to content
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

downloader: download captions #190

Merged
merged 1 commit into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ $ annie -j https://www.bilibili.com/video/av20203945
```console
$ annie -h

Usage of annie:
Usage of ./annie:
-C Download captions
-F string
URLs file
-O string
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var (
PlaylistEnd int
// PlaylistItems Playlist video items to download. Separated by commas like: 1,5,6
PlaylistItems string
// Caption download captions
Caption bool
)

// FakeHeaders fake http headers
Expand Down
43 changes: 34 additions & 9 deletions downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ type VideoData struct {
Formats map[string]FormatData
}

func progressBar(size int64) *pb.ProgressBar {
bar := pb.New64(size).SetUnits(pb.U_BYTES).SetRefreshRate(time.Millisecond * 10)
bar.ShowSpeed = true
bar.ShowFinalTime = true
bar.SetMaxWidth(1000)
return bar
}

func (data *FormatData) calculateTotalSize() {
var size int64
for _, urlData := range data.URLs {
Expand All @@ -54,15 +62,35 @@ func (data *FormatData) calculateTotalSize() {
data.Size = size
}

// urlSave save url file
func (data FormatData) urlSave(
// Caption download danmaku, subtitles, etc
func Caption(url, refer, fileName, ext string) {
if !config.Caption || config.InfoOnly {
return
}
fmt.Println("\nDownloading captions...")
body := request.Get(url, refer, nil)
filePath := utils.FilePath(fileName, ext, false)
file, fileError := os.Create(filePath)
if fileError != nil {
log.Fatal(fileError)
}
defer file.Close()
file.WriteString(body)
}

// Save save url file
func Save(
urlData URLData, refer, fileName string, bar *pb.ProgressBar,
) {
filePath := utils.FilePath(fileName, urlData.Ext, false)
fileSize, exists := utils.FileSize(filePath)
if bar == nil {
bar = progressBar(urlData.Size)
bar.Start()
}
// Skip segment file
// TODO: Live video URLs will not return the size
if fileSize == urlData.Size {
if exists && fileSize == urlData.Size {
fmt.Printf("%s: file already exists, skipping\n", filePath)
bar.Add64(fileSize)
return
Expand Down Expand Up @@ -201,14 +229,11 @@ func (v VideoData) Download(refer string) {
fmt.Printf("%s: file already exists, skipping\n", mergedFilePath)
return
}
bar := pb.New64(data.Size).SetUnits(pb.U_BYTES).SetRefreshRate(time.Millisecond * 10)
bar.ShowSpeed = true
bar.ShowFinalTime = true
bar.SetMaxWidth(1000)
bar := progressBar(data.Size)
bar.Start()
if len(data.URLs) == 1 {
// only one fragment
data.urlSave(data.URLs[0], refer, title, bar)
Save(data.URLs[0], refer, title, bar)
bar.Finish()
return
}
Expand All @@ -223,7 +248,7 @@ func (v VideoData) Download(refer string) {
wgp.Add()
go func(url URLData, refer, fileName string, bar *pb.ProgressBar) {
defer wgp.Done()
data.urlSave(url, refer, fileName, bar)
Save(url, refer, fileName, bar)
}(url, refer, partFileName, bar)

}
Expand Down
7 changes: 6 additions & 1 deletion extractors/bilibili/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,17 @@ func bilibiliDownload(url string, options bilibiliOptions) downloader.VideoData
}
title = tempTitle
}
title = utils.FileName(title)
extractedData := downloader.VideoData{
Site: "哔哩哔哩 bilibili.com",
Title: utils.FileName(title),
Title: title,
Type: "video",
Formats: format,
}
extractedData.Download(url)
downloader.Caption(
fmt.Sprintf("https://comment.bilibili.com/%s.xml", cid),
url, title, "xml",
)
return extractedData
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func init() {
&config.PlaylistItems, "items", "",
"Playlist video items to download. Separated by commas like: 1,5,6",
)
flag.BoolVar(&config.Caption, "C", false, "Download captions")
}

func download(videoURL string) {
Expand Down
4 changes: 2 additions & 2 deletions request/request.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package request

import (
"compress/flate"
"compress/gzip"
"compress/zlib"
"crypto/tls"
"fmt"
"io"
Expand Down Expand Up @@ -133,7 +133,7 @@ func Get(url, refer string, headers map[string]string) string {
case "gzip":
reader, _ = gzip.NewReader(res.Body)
case "deflate":
reader, _ = zlib.NewReader(res.Body)
reader = flate.NewReader(res.Body)
default:
reader = res.Body
}
Expand Down