I want to use youtube-dl exe file in Windows for downloading the videos to the client's browser with Golang Web App.
I have a page that contains an input for the website url (For example a youtube url) and I want to call youtube.dl exe file with this url in my server with Golang. But I could not download the file to the client's browser directly.
I don't want to download the video itself to my server. I want it to be downloaded to client's browser directly.
I tried many things on the web and here where I am. You can find my code snippet below.
func SearchHandler(w http.ResponseWriter, r *http.Request) {
// - --------------------------------------------------------------------------------------------------------------
// - Retrieve the HTML form parameter of POST method
// - --------------------------------------------------------------------------------------------------------------
url := r.FormValue("entry-domain")
logger.Printf("SearchHandler started to research the IP and MX data from %s domain", url)
fmt.Println("starting download................")
cmd := exec.Command("youtube-dl.exe", "-o", "-", url)
fmt.Println("downloading started...............")
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
// //copy the relevant headers. If you want to preserve the downloaded file name, extract it with go's url parser.
w.Header().Set("Content-Disposition", "attachment; filename=BigBuckBunny.mp4")
w.Header().Set("Content-Type", "application/octet-stream")
//stream the body to the client without fully loading it into memory
reader := bytes.NewReader(out)
//w.Write(out)
io.Copy(w, reader)
fmt.Println("written to file.....................")
return}
I could download a file but it did not work as expected. I could not even open the file.