1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| package main
import ( "fmt" "io/ioutil" "log" "net/http" "net/url" "regexp" "strings" "time"
ffmpeg "github.com/floostack/transcoder/ffmpeg" )
func main() { request, _ := http.NewRequest("GET", "http://91xxx.com/view_video.php?viewkey=123", nil) request.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36") request.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
client := &http.Client{ Timeout: 10 * time.Second, }
resp, err := client.Do(request)
if err != nil { fmt.Println(err) return }
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil { fmt.Println(err) return }
reg := regexp.MustCompile(`<h4 class="login_register_header"[^>]+>([^<]+)</h4>`)
params := reg.FindStringSubmatch(string(body))
if len(params) != 2 { fmt.Println("获取标题失败") return }
title := strings.Replace(params[1], " ", "", -1)
reg = regexp.MustCompile(`strencode2\((.*)\"`)
params = reg.FindStringSubmatch(string(body))
if len(params) != 2 { fmt.Println("获取编码数据失败") return }
source_data, err := url.QueryUnescape(params[1])
if err != nil { fmt.Println(err) return }
reg = regexp.MustCompile(`src='(.*)\' `)
params = reg.FindStringSubmatch(source_data)
if len(params) != 2 { fmt.Println("获取 URL 失败") return }
format := "mp4" overwrite := true
download_path := fmt.Sprintf("./%s.%s", strings.Replace(title, "\n", "", -1), format)
opts := ffmpeg.Options{ OutputFormat: &format, Overwrite: &overwrite, }
ffmpegConf := &ffmpeg.Config{ FfmpegBinPath: "./ffmpeg/ffmpeg.exe", FfprobeBinPath: "./ffmpeg/ffprobe.exe", ProgressEnabled: true, }
progress, err := ffmpeg. New(ffmpegConf). Input(params[1]). Output(download_path). WithOptions(opts). Start(opts)
if err != nil { fmt.Println(err) return }
for msg := range progress { log.Printf("%+v", msg) }
fmt.Println("下载完成") }
|