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

fix(client): Randomly slow youtube download speed #229

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 45 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"log"
"net/http"
"strconv"
)

// Client offers methods to download video metadata and video streams.
Expand Down Expand Up @@ -38,7 +39,7 @@ func (c *Client) GetVideoContext(ctx context.Context, url string) (*Video, error
}

func (c *Client) videoFromID(ctx context.Context, id string) (*Video, error) {
body, err := c.videoDataByInnertube(ctx, id, Web)
body, err := c.videoDataByInnertube(ctx, id, Android)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -115,8 +116,8 @@ type innertubeClient struct {
type ClientType string

const (
Web ClientType = "WEB"
EmbeddedClient ClientType = "WEB_EMBEDDED_PLAYER"
Android ClientType = "ANDROID"
EmbeddedClient ClientType = "ANDROID_EMBEDDED_PLAYER"
)

func (c *Client) videoDataByInnertube(ctx context.Context, id string, clientType ClientType) ([]byte, error) {
Expand Down Expand Up @@ -157,13 +158,14 @@ func (c *Client) videoDataByInnertube(ctx context.Context, id string, clientType
}

var innertubeClientInfo = map[ClientType]map[string]string{
// might add ANDROID and other in future, but i don't see reason yet
Web: {
"version": "2.20210617.01.00",
// kanged from
// https://github.com/yt-dlp/yt-dlp/blob/11aa91a12f95821500fa064402a3e2c046b072fb/yt_dlp/extractor/youtube.py#L120-L140
Android: {
"version": "16.20",
"key": "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8",
},
EmbeddedClient: {
"version": "1.19700101",
"version": "16.20",
// seems like same key works for both clients
"key": "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8",
},
Expand All @@ -172,8 +174,8 @@ var innertubeClientInfo = map[ClientType]map[string]string{
func prepareInnertubeData(videoID string, sts string, clientType ClientType) (innertubeRequest, string) {
cInfo, ok := innertubeClientInfo[clientType]
if !ok {
// if provided clientType not exist - use Web as fallback option
clientType = Web
// if provided clientType not exist - use Android as fallback option
clientType = Android
cInfo = innertubeClientInfo[clientType]
}

Expand Down Expand Up @@ -251,7 +253,20 @@ func (c *Client) GetStreamContext(ctx context.Context, video *Video, format *For

go c.download(req, w, format)

return r, format.ContentLength, nil
contentLength, err := strconv.ParseInt(format.ContentLength, 10, 64)
if err != nil {
return nil, 0, err
}

if contentLength == 0 {
contentLength, err := c.httpGetContentLength(ctx, video, format, url)
format.ContentLength = fmt.Sprint(contentLength)
if err != nil {
return nil, 0, err
}
}
format.ContentLength = fmt.Sprint(contentLength)
return r, contentLength, nil
}

func (c *Client) download(req *http.Request, w *io.PipeWriter, format *Format) {
Expand All @@ -276,8 +291,14 @@ func (c *Client) download(req *http.Request, w *io.PipeWriter, format *Format) {
}

defer w.Close()

contentLength, err := strconv.ParseInt(format.ContentLength, 10, 64)
if err != nil {
return
}

//nolint:revive,errcheck
if format.ContentLength == 0 {
if contentLength == 0 {
resp, err := c.httpDo(req)
if err != nil {
w.CloseWithError(err)
Expand All @@ -292,7 +313,7 @@ func (c *Client) download(req *http.Request, w *io.PipeWriter, format *Format) {

//nolint:revive,errcheck
// load all the chunks
for pos := int64(0); pos < format.ContentLength; {
for pos := int64(0); pos < contentLength; {
written, err := loadChunk(pos)
if err != nil {
w.CloseWithError(err)
Expand Down Expand Up @@ -349,6 +370,11 @@ func (c *Client) httpDo(req *http.Request) (*http.Response, error) {

// httpGet does a HTTP GET request, checks the response to be a 200 OK and returns it
func (c *Client) httpGet(ctx context.Context, url string) (*http.Response, error) {
return c.httpDoWithoutBody(ctx, http.MethodGet, url)
}

// httpGet does a HTTP request, checks the response to be a 200 OK and returns it
func (c *Client) httpDoWithoutBody(ctx context.Context, method, url string) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -376,3 +402,10 @@ func (c *Client) httpGetBodyBytes(ctx context.Context, url string) ([]byte, erro

return io.ReadAll(resp.Body)
}

// credit @aykxt
func (c *Client) httpGetContentLength(ctx context.Context, video *Video, format *Format, url string) (int64, error) {
resp, err := c.httpDoWithoutBody(ctx, http.MethodHead, url)
resp.Body.Close()
return resp.ContentLength, err
}
8 changes: 4 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func TestGetVideoWithoutManifestURL(t *testing.T) {
require.NotNil(video)

assert.NotEmpty(video.Thumbnails)
assert.Greater(len(video.Thumbnails), 0)
assert.NotEmpty(video.Thumbnails[0].URL)
assert.Greater(len(video.Thumbnails.Thumbnails), 0)
assert.NotEmpty(video.Thumbnails.Thumbnails[0].URL)
assert.Empty(video.HLSManifestURL)
assert.Empty(video.DASHManifestURL)

Expand All @@ -108,8 +108,8 @@ func TestGetVideoWithManifestURL(t *testing.T) {
require.NotNil(video)

assert.NotEmpty(video.Thumbnails)
assert.Greater(len(video.Thumbnails), 0)
assert.NotEmpty(video.Thumbnails[0].URL)
assert.Greater(len(video.Thumbnails.Thumbnails), 0)
assert.NotEmpty(video.Thumbnails.Thumbnails[0].URL)
assert.NotEmpty(video.HLSManifestURL)
assert.NotEmpty(video.DASHManifestURL)
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/youtubedr/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ var infoCmd = &cobra.Command{
bitrate = format.Bitrate
}

size := format.ContentLength
if size == 0 {
contentLength, err := strconv.ParseInt(format.ContentLength, 10, 64)
if err != nil {
return
}

if contentLength == 0 {
// Some formats don't have this information
size = int64(float64(bitrate) * video.Duration.Seconds() / 8)
contentLength = int64(float64(bitrate) * video.Duration.Seconds() / 8)
}

videoInfo.Formats = append(videoInfo.Formats, VideoFormat{
Expand All @@ -70,7 +74,7 @@ var infoCmd = &cobra.Command{
VideoQuality: format.QualityLabel,
AudioQuality: strings.ToLower(strings.TrimPrefix(format.AudioQuality, "AUDIO_QUALITY_")),
AudioChannels: format.AudioChannels,
Size: size,
Size: contentLength,
Bitrate: bitrate,
MimeType: format.MimeType,
})
Expand Down
2 changes: 1 addition & 1 deletion itag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ func TestYoutube_GetItagInfo(t *testing.T) {
url := "https://www.youtube.com/watch?v=rFejpH_tAHM"
video, err := client.GetVideo(url)
require.NoError(err)
require.Len(video.Formats, 24)
require.Len(video.Formats, 26)
}
Loading