1 Commits

Author SHA1 Message Date
8408e12a2a Add HTTPChunkSize option (--http-chunk-size)
Switch to _test test package to make sure to test external API.
Also don't ignore download process wait error
2021-06-28 20:09:32 +02:00
5 changed files with 45 additions and 52 deletions

View File

@ -1,22 +1,17 @@
# bump: golang /GOLANG_VERSION=([\d.]+)/ docker:golang|^1
# bump: golang link "Release notes" https://golang.org/doc/devel/release.html
ARG GOLANG_VERSION=1.20.5
# bump: yt-dlp /YT_DLP=([\d.-]+)/ https://github.com/yt-dlp/yt-dlp.git|/^\d/|sort
# bump: yt-dlp link "Release notes" https://github.com/yt-dlp/yt-dlp/releases/tag/$LATEST
ARG YT_DLP=2023.03.04
ARG GOLANG_VERSION=1.16.5
# bump: youtube-dl /YDL_VERSION=([\d.]+)/ https://github.com/ytdl-org/youtube-dl.git|/^\d/|sort
# bump: youtube-dl link "Release notes" https://github.com/ytdl-org/youtube-dl/releases/tag/$LATEST
ARG YDL_VERSION=2021.06.06
FROM golang:$GOLANG_VERSION AS base
ARG YT_DLP
FROM golang:$GOLANG_VERSION
ARG YDL_VERSION
RUN \
apt-get update -q && \
apt-get install -y -q python-is-python3 && \
curl -L https://github.com/yt-dlp/yt-dlp/releases/download/$YT_DLP/yt-dlp -o /usr/local/bin/yt-dlp && \
chmod a+x /usr/local/bin/yt-dlp
curl -L -o /usr/local/bin/youtube-dl https://yt-dl.org/downloads/$YDL_VERSION/youtube-dl && \
chmod a+x /usr/local/bin/youtube-dl
FROM base AS dev
FROM base
WORKDIR /src
COPY go.* *.go ./
COPY cmd cmd

View File

@ -1,15 +1,12 @@
## goutubedl
Go wrapper for [youtube-dl](https://github.com/ytdl-org/youtube-dl) and [yt-dlp](https://github.com/yt-dlp/yt-dlp), currently tested and
developed using yt-dlp.
API documentation can be found at [godoc.org](https://pkg.go.dev/github.com/wader/goutubedl?tab=doc).
Go wrapper for [youtube-dl](https://github.com/ytdl-org/youtube-dl). API documentation can be found at [godoc.org](https://pkg.go.dev/github.com/wader/goutubedl?tab=doc).
See [yt-dlp documentation](https://github.com/yt-dlp/yt-dlp) for how to
See [youtube-dl documentation](https://github.com/ytdl-org/youtube-dl) for how to
install and what is recommended to install in addition to youtube-dl.
goutubedl default uses `PATH` to find youtube-dl but it can be configured with the `goutubedl.Path`
variable. Default is currently `youtube-dl` for backwards compability. If your using yt-dlp you
probably want to set it to `yt-dlp`.
variable.
Due to the nature and frequent updates of youtube-dl only the latest version
is tested. But it seems to work well with older versions also.

View File

@ -15,17 +15,11 @@ var dumpFlag = flag.Bool("J", false, "Dump JSON")
var typeFlag = flag.String("t", "any", "Type")
func main() {
goutubedl.Path = "yt-dlp"
log.SetFlags(0)
flag.Parse()
optType := goutubedl.TypeFromString[*typeFlag]
result, err := goutubedl.New(
context.Background(),
flag.Arg(0),
goutubedl.Options{Type: optType, DebugLog: log.Default()},
)
optType, _ := goutubedl.TypeFromString[*typeFlag]
result, err := goutubedl.New(context.Background(), flag.Arg(0), goutubedl.Options{Type: optType})
if err != nil {
log.Fatal(err)
}
@ -50,8 +44,6 @@ func main() {
log.Fatal(err)
}
defer f.Close()
if _, err := io.Copy(f, dr); err != nil {
log.Fatal(err)
}
io.Copy(f, dr)
dr.Close()
}

View File

@ -206,9 +206,9 @@ type Options struct {
PlaylistEnd uint // --playlist-end
DownloadThumbnail bool
DownloadSubtitles bool
ProxyUrl string // --proxy URL http://host:port or socks5://host:port
DebugLog Printer
StderrFn func(cmd *exec.Cmd) io.Writer // if not nil, function to get Writer for stderr
HTTPChunkSize uint // --http-chunk-size
HTTPClient *http.Client // Client for download thumbnail and subtitles (nil use http.DefaultClient)
}
@ -259,11 +259,6 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info
"--batch-file", "-",
"-J",
)
if options.ProxyUrl != "" {
cmd.Args = append(cmd.Args, "--proxy", options.ProxyUrl)
}
if options.Type == TypePlaylist {
cmd.Args = append(cmd.Args, "--yes-playlist")
@ -460,9 +455,8 @@ func (result Result) Download(ctx context.Context, filter string) (*DownloadResu
if !result.Info.Direct {
cmd.Args = append(cmd.Args, "-f", filter)
}
if result.Options.ProxyUrl != "" {
cmd.Args = append(cmd.Args, "--proxy", result.Options.ProxyUrl)
if result.Options.HTTPChunkSize != 0 {
cmd.Args = append(cmd.Args, "--http-chunk-size", fmt.Sprintf("%d", result.Options.HTTPChunkSize))
}
cmd.Dir = tempPath
@ -482,14 +476,16 @@ func (result Result) Download(ctx context.Context, filter string) (*DownloadResu
return nil, err
}
var waitErr error
go func() {
cmd.Wait()
waitErr = cmd.Wait()
w.Close()
os.RemoveAll(tempPath)
close(dr.waitCh)
}()
return dr, nil
return dr, waitErr
}
func (dr *DownloadResult) Read(p []byte) (n int, err error) {

View File

@ -18,11 +18,6 @@ import (
"github.com/wader/osleaktest"
)
func init() {
// we're using yt-dlp at the moment
goutubedl.Path = "yt-dlp"
}
const testVideoRawURL = "https://www.youtube.com/watch?v=C0DPdy98e4c"
const playlistRawURL = "https://soundcloud.com/mattheis/sets/kindred-phenomena"
const subtitlesTestVideoRawURL = "https://www.youtube.com/watch?v=QRS8MkLhQmM"
@ -63,15 +58,21 @@ func TestVersion(t *testing.T) {
}
}
func TestDownload(t *testing.T) {
func testDownload(t *testing.T, rawURL string, optionsFn func(options *goutubedl.Options)) {
defer leakChecks(t)()
stderrBuf := &bytes.Buffer{}
r, err := goutubedl.New(context.Background(), testVideoRawURL, goutubedl.Options{
options := goutubedl.Options{
StderrFn: func(cmd *exec.Cmd) io.Writer {
return stderrBuf
},
})
}
if optionsFn != nil {
optionsFn(&options)
}
r, err := goutubedl.New(context.Background(), rawURL, options)
if err != nil {
t.Fatal(err)
}
@ -90,8 +91,8 @@ func TestDownload(t *testing.T) {
t.Errorf("copy n not equal to download buffer: %d!=%d", n, downloadBuf.Len())
}
if n < 10000 {
t.Errorf("should have copied at least 10000 bytes: %d", n)
if n < 29000 {
t.Errorf("should have copied at least 29000 bytes: %d", n)
}
if !strings.Contains(stderrBuf.String(), "Destination") {
@ -99,6 +100,18 @@ func TestDownload(t *testing.T) {
}
}
func TestDownload(t *testing.T) {
defer leakChecks(t)()
testDownload(t, testVideoRawURL, nil)
}
func TestHTTPChunkSize(t *testing.T) {
defer leakChecks(t)()
testDownload(t, testVideoRawURL, func(options *goutubedl.Options) {
options.HTTPChunkSize = 1000000
})
}
func TestParseInfo(t *testing.T) {
for _, c := range []struct {
url string
@ -194,7 +207,7 @@ func TestTestUnsupportedURL(t *testing.T) {
if ydlResultErr == nil {
t.Errorf("expected unsupported url")
}
expectedErrPrefix := "Unsupported URL:"
expectedErrPrefix := "Unsupported URL: https://www.google.com"
if ydlResultErr != nil && !strings.HasPrefix(ydlResultErr.Error(), expectedErrPrefix) {
t.Errorf("expected error prefix %q got %q", expectedErrPrefix, ydlResultErr.Error())