To download a video, use goutubedl.New to create a result object, then call .Download on that object. The .Download method requires a context and a format string (e.g., "best"). The returned downloadResult implements io.ReadCloser, which you should close after use.
Example usage:
package main
import (
"context"
"io"
"log"
"os"
"github.com/wader/goutubedl"
)
func main() {
// Initialize the downloader
result, err := goutubedl.New(context.Background(), "https://www.youtube.com/watch?v=jgVhBThJdXc", goutubedl.Options{})
if err != nil {
log.Fatal(err)
}
// Download the video with the specified format (e.g., "best")
downloadResult, err := result.Download(context.Background(), "best")
if err != nil {
log.Fatal(err)
}
defer downloadResult.Close()
// Save the output to a file
f, err := os.Create("output")
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Stream the download to the file
io.Copy(f, downloadResult)
}
package main
import (
"context"
"io"
"log"
"os"
"github.com/wader/goutubedl"
)
func main() {
result, err := goutubedl.New(context.Background(), "https://www.youtube.com/watch?v=jgVhBThJdXc", goutubedl.Options{})
if err != nil {
log.Fatal(err)
}
downloadResult, err := result.Download(context.Background(), "best")
if err != nil {
log.Fatal(err)
}
defer downloadResult.Close()
f, err := os.Create("output")
if err != nil {
log.Fatal(err)
}
defer f.Close()
io.Copy(f, downloadResult)
}