Skip to content

Instantly share code, notes, and snippets.

@blinkinglight
Forked from pheuter/serve.go
Created February 23, 2020 14:38
Show Gist options
  • Save blinkinglight/45211db2568a1a41a705e5e6d9e058c9 to your computer and use it in GitHub Desktop.
Save blinkinglight/45211db2568a1a41a705e5e6d9e058c9 to your computer and use it in GitHub Desktop.
go http streaming
package main
import (
"fmt"
"io"
"io/ioutil"
"strings"
"exec"
"http"
)
func handler(w http.ResponseWriter, r *http.Request) {
data := make(http.Values)
data.Set("code",r.FormValue("code"))
data.Set("ip",r.RemoteAddr)
resp, _ := http.PostForm("http://mppp.it/exists", data)
code, _ := ioutil.ReadAll(resp.Body)
ipresp, _ := http.PostForm("http://mppp.it/ip", data)
ip, _ := ioutil.ReadAll(ipresp.Body)
if (strings.TrimSpace(string(code)) == "true" || strings.TrimSpace(string(ip)) == "true") {
w.Header().Set("Content-Type","application/octet-stream")
w.Header().Set("Content-Disposition",fmt.Sprintf("attachment; filename=%s.mp3",r.FormValue("name")))
url, _ := exec.Command("youtube-dl", "-g", "--cookies", "cookie", r.FormValue("url")).Output()
cmd := exec.Command("mencoder", "-msglevel", "all=-1", "-cookies", "-cookies-file", "cookie", strings.TrimSpace(string(url)), "-of", "rawaudio", "-ss", r.FormValue("stime"), "-endpos", r.FormValue("etime"), "-oac", "mp3lame", "-lameopts", "cbr:mode=2:br=192", "-mc", "0", "-noskip", "-ofps", "24000/1001", "-ovc", "copy", "-o", "-")
reader, _ := cmd.StdoutPipe()
cmd.Start()
io.Copy(w,reader)
cmd.Wait()
cmd.Process.Kill()
http.PostForm("http://mppp.it/convert",make(map[string][]string))
//exec.Command("kill","-9",string(cmd.Process.Pid)).Run()
} else {
fmt.Fprintf(w, "Either your daily limit of 3 converts was reached, or your code expired!")
}
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":4567", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment