From 3184158e029ecb8e92778297a3b9520208c582ee Mon Sep 17 00:00:00 2001 From: ar2rworld Date: Mon, 21 Aug 2023 17:20:19 -0400 Subject: [PATCH 1/9] DownloadSections option --- goutubedl.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/goutubedl.go b/goutubedl.go index 1631ba1..6340d1a 100644 --- a/goutubedl.go +++ b/goutubedl.go @@ -207,6 +207,7 @@ type Options struct { Downloader string // --downloader DownloadThumbnail bool DownloadSubtitles bool + DownloadSections string // --download-sections 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 @@ -555,6 +556,10 @@ func (result Result) DownloadWithOptions(ctx context.Context, options DownloadOp if result.Options.Downloader != "" { cmd.Args = append(cmd.Args, "--downloader", result.Options.Downloader) } + + if result.Options.DownloadSections != "" { + cmd.Args = append(cmd.Args, "--download-sections", result.Options.DownloadSections) + } if result.Options.MergeOutputFormat != "" { cmd.Args = append(cmd.Args, From e29655f2bdeda707cb5154783714d883fe033b46 Mon Sep 17 00:00:00 2001 From: ar2rworld Date: Wed, 23 Aug 2023 16:48:24 -0400 Subject: [PATCH 2/9] add TestDownloadSections --- goutubedl_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/goutubedl_test.go b/goutubedl_test.go index b5dbd95..48458f9 100644 --- a/goutubedl_test.go +++ b/goutubedl_test.go @@ -285,6 +285,35 @@ func TestSubtitles(t *testing.T) { } } +func TestDownloadSections(t *testing.T) { + defer leakChecks(t)() + + ydlResult, ydlResultErr := goutubedl.New( + context.Background(), + "https://www.youtube.com/watch?v=OyuL5biOQ94", + goutubedl.Options{ + DownloadSections: "*0:0-0:5", + }) + + + if ydlResult.Options.DownloadSections != "*0:0-0:5" { + t.Errorf("failed to setup --download-sections") + } + if ydlResultErr != nil { + t.Errorf("failed to download: %s", ydlResultErr) + } + dr, err := ydlResult.Download(context.Background(), ydlResult.Info.Formats[0].FormatID) + if err != nil { + t.Fatal(err) + } + downloadBuf := &bytes.Buffer{} + _, err = io.Copy(downloadBuf, dr) + if err != nil { + t.Fatal(err) + } + dr.Close() +} + func TestErrorNotAPlaylist(t *testing.T) { defer leakChecks(t)() From 89ddc385736ce3161f02470115adbeb4da028857 Mon Sep 17 00:00:00 2001 From: ar2rworld Date: Fri, 1 Sep 2023 16:27:39 -0400 Subject: [PATCH 3/9] add ffmpeg to Dockerfile --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index c3ba843..d8041de 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,8 @@ 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 + chmod a+x /usr/local/bin/yt-dlp \ + apt-get install ffmpeg && ffmpeg -version FROM base AS dev From 36319b5e0f7b56b0f31d3c11b1b1f1060bd724bf Mon Sep 17 00:00:00 2001 From: ar2rworld Date: Fri, 1 Sep 2023 16:28:03 -0400 Subject: [PATCH 4/9] add ffmpeg check to test --- goutubedl_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/goutubedl_test.go b/goutubedl_test.go index 48458f9..3dfbd7e 100644 --- a/goutubedl_test.go +++ b/goutubedl_test.go @@ -288,6 +288,12 @@ func TestSubtitles(t *testing.T) { func TestDownloadSections(t *testing.T) { defer leakChecks(t)() + cmd := exec.Command("ffmpeg", "-version") + _, err := cmd.Output() + if err != nil { + t.Errorf("failed to check ffmpeg installed: %s", err) + } + ydlResult, ydlResultErr := goutubedl.New( context.Background(), "https://www.youtube.com/watch?v=OyuL5biOQ94", From 9a0dc509a33ca1793203f876f635f63bf841843b Mon Sep 17 00:00:00 2001 From: ar2rworld Date: Fri, 1 Sep 2023 16:28:30 -0400 Subject: [PATCH 5/9] change Download filter to best --- goutubedl_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/goutubedl_test.go b/goutubedl_test.go index 3dfbd7e..b460db5 100644 --- a/goutubedl_test.go +++ b/goutubedl_test.go @@ -308,7 +308,7 @@ func TestDownloadSections(t *testing.T) { if ydlResultErr != nil { t.Errorf("failed to download: %s", ydlResultErr) } - dr, err := ydlResult.Download(context.Background(), ydlResult.Info.Formats[0].FormatID) + dr, err := ydlResult.Download(context.Background(), "best") if err != nil { t.Fatal(err) } From 2438eebf7ab6d094a59185343f0ceb9284d533c9 Mon Sep 17 00:00:00 2001 From: ar2rworld Date: Tue, 5 Sep 2023 15:47:25 -0400 Subject: [PATCH 6/9] fixing Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index d8041de..41eb6d0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,7 @@ 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 \ + chmod a+x /usr/local/bin/yt-dlp && \ apt-get install ffmpeg && ffmpeg -version FROM base AS dev From d87bdbe81c249b871762b832f48ae085b801399d Mon Sep 17 00:00:00 2001 From: ar2rworld Date: Tue, 5 Sep 2023 16:27:53 -0400 Subject: [PATCH 7/9] add -y --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 41eb6d0..8fe0dbc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ RUN \ 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 && \ - apt-get install ffmpeg && ffmpeg -version + apt-get install -y ffmpeg FROM base AS dev From fc0dec1f1faea24ee65b1e545318f4e1a7fcfe01 Mon Sep 17 00:00:00 2001 From: ar2rworld Date: Tue, 3 Oct 2023 14:58:09 -0400 Subject: [PATCH 8/9] ffprobe testing duration --- goutubedl_test.go | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/goutubedl_test.go b/goutubedl_test.go index b460db5..7c3a156 100644 --- a/goutubedl_test.go +++ b/goutubedl_test.go @@ -7,9 +7,12 @@ import ( "bytes" "context" "encoding/json" + "fmt" "io" + "os" "os/exec" "regexp" + "strconv" "strings" "testing" @@ -288,6 +291,9 @@ func TestSubtitles(t *testing.T) { func TestDownloadSections(t *testing.T) { defer leakChecks(t)() + fileName := "durationTestingFile" + duration := 5 + cmd := exec.Command("ffmpeg", "-version") _, err := cmd.Output() if err != nil { @@ -298,7 +304,7 @@ func TestDownloadSections(t *testing.T) { context.Background(), "https://www.youtube.com/watch?v=OyuL5biOQ94", goutubedl.Options{ - DownloadSections: "*0:0-0:5", + DownloadSections: fmt.Sprintf("*0:0-0:%d", duration), }) @@ -312,11 +318,40 @@ func TestDownloadSections(t *testing.T) { if err != nil { t.Fatal(err) } - downloadBuf := &bytes.Buffer{} - _, err = io.Copy(downloadBuf, dr) + + f, err := os.Create(fileName) if err != nil { t.Fatal(err) } + _, err = io.Copy(f, dr) + if err != nil { + t.Fatal(err) + } + + cmd = exec.Command("ffprobe", "-v", "quiet", "-show_entries", "format=duration", fileName) + stdout, err := cmd.Output() + if err != nil { + t.Fatal(err) + } + + var gotDurationString string + output := string(stdout) + for _, line := range strings.Split(output, "\n") { + if strings.Contains(line, "duration") { + if d, found := strings.CutPrefix(line, "duration="); found { + gotDurationString = d + } + } + } + + gotDuration, err := strconv.ParseFloat(gotDurationString, 32) + if err != nil { + t.Fatal(err) + } + seconds := int(gotDuration) + if seconds != duration { + t.Fatalf("didnot get expected duration of %d, but got %d", duration, seconds) + } dr.Close() } From 12053d10b1fc925f0d25c1d19bd02901ae5c1179 Mon Sep 17 00:00:00 2001 From: ar2rworld Date: Tue, 3 Oct 2023 17:09:40 -0400 Subject: [PATCH 9/9] fix file close --- goutubedl_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/goutubedl_test.go b/goutubedl_test.go index 7c3a156..ce9f3b5 100644 --- a/goutubedl_test.go +++ b/goutubedl_test.go @@ -323,6 +323,7 @@ func TestDownloadSections(t *testing.T) { if err != nil { t.Fatal(err) } + defer f.Close() _, err = io.Copy(f, dr) if err != nil { t.Fatal(err)