This commit is contained in:
panqianyu
2022-04-19 13:34:14 +08:00
parent 98b8cfd0bd
commit 6dfcdfdc80
4 changed files with 138 additions and 85 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"fmt"
"html/template"
"io"
"net/http"
"os"
@@ -13,93 +14,81 @@ var kinds = []string{
"ffmpeg",
"ffprobe",
}
var systems = []string{
"darwin",
"freebsd",
"win32",
"linux",
}
var arches = []string{
"arm64",
"ia32",
"arm",
"x64",
var builds = []struct {
System string
Arch string
}{
{System: "darwin", Arch: "amd64"},
{System: "darwin", Arch: "arm64"},
{System: "windows", Arch: "amd64"},
{System: "windows", Arch: "386"},
{System: "linux", Arch: "amd64"},
{System: "linux", Arch: "arm64"},
{System: "linux", Arch: "arm"},
{System: "linux", Arch: "386"},
{System: "freebsd", Arch: "amd64"},
}
const pkg = "ffstatic"
const tag = "b4.4.0-rc.11"
var tpls = template.Must(template.ParseGlob("templates/*.tpl"))
func genGoFile(system, arch string) error {
for _, t := range tpls.Templates() {
targetFile := fmt.Sprintf("%s-%s/%s", system, arch, strings.Trim(t.Name(), ".tpl"))
os.Remove(targetFile)
file, err := os.OpenFile(targetFile, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("open file %s: %v", t.Name(), err)
}
if err := t.Execute(file, map[string]interface{}{
"system": system,
"arch": arch,
}); err != nil {
return fmt.Errorf("execute template %s: %v", t.Name(), err)
}
}
return nil
}
func download(kind, system, arch string) error {
fileName := fmt.Sprintf("%s_%s_%s", kind, system, arch)
constraint := []string{}
targetFile := system + "-" + arch + "/" + kind
os.Remove(targetFile)
switch arch {
case "ia32":
constraint = []string{"386"}
case "x64":
constraint = []string{"amd64"}
default:
constraint = append(constraint, arch)
case "386":
arch = "ia32"
case "amd64":
arch = "x64"
}
switch system {
case "win32":
constraint = append(constraint, "windows")
default:
constraint = append(constraint, system)
case "windows":
system = "win32"
}
{
downloadURL := fmt.Sprintf("https://github.com/descriptinc/ffmpeg-ffprobe-static/releases/download/%s/%s-%s-%s", tag, kind, system, arch)
resp, err := http.Get(downloadURL)
if err != nil {
return fmt.Errorf("failed to download %s: %v", downloadURL, err)
}
defer resp.Body.Close()
downloadURL := fmt.Sprintf("https://github.com/descriptinc/ffmpeg-ffprobe-static/releases/download/%s/%s-%s-%s", tag, kind, system, arch)
if resp.StatusCode == http.StatusNotFound {
return nil
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
if _, err := io.Copy(file, resp.Body); err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
resp, err := http.Get(downloadURL)
if err != nil {
return fmt.Errorf("failed to download %s: %v", downloadURL, err)
}
{
file, err := os.OpenFile(fileName+".go", os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
defer resp.Body.Close()
if _, err := fmt.Fprintf(file, `//go:generate go run ./generate
//go:build %s
// +build %s
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
package %s
file, err := os.OpenFile(targetFile, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
import (
_ "embed"
)
//go:embed %s
var %sBinary []byte
`,
strings.Join(constraint, " && "),
strings.Join(constraint, ","),
pkg,
fileName,
kind,
); err != nil {
return fmt.Errorf("failed to write file: %v", err)
}
if _, err := io.Copy(file, resp.Body); err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}
@@ -107,19 +96,21 @@ var %sBinary []byte
func main() {
var g sync.WaitGroup
for _, kind := range kinds {
for _, system := range systems {
for _, arch := range arches {
kind, system, arch := kind, system, arch
g.Add(1)
go func() {
defer g.Done()
err := download(kind, system, arch)
if err != nil {
fmt.Printf("Fail to download %s-%s-%s: %s\n", kind, system, arch, err.Error())
}
}()
}
for _, build := range builds {
system, arch := build.System, build.Arch
for _, kind := range kinds {
kind, system, arch := kind, system, arch
g.Add(1)
go func() {
defer g.Done()
err := download(kind, system, arch)
if err != nil {
fmt.Printf("Fail to download %s-%s-%s: %s\n", kind, system, arch, err.Error())
}
}()
}
if err := genGoFile(system, arch); err != nil {
panic(fmt.Errorf("failed to generate go file: %w", err))
}
}
g.Wait()

9
templates/README.md.tpl Normal file
View File

@@ -0,0 +1,9 @@
# ffstatic-{{.system}}-{{.arch}}
Check https://github.com/go-ffstatic/ffstatic for more information.
## Installation
```
go get -u github.com/go-ffstatic/{{.system}}-{{.arch}}
```

50
templates/ffstatic.go.tpl Normal file
View File

@@ -0,0 +1,50 @@
//generated by github.com/go-ffstatic/ffstatic
//go:build {{.system}} && {{.arch}}
// +build {{.system}},{{.arch}}
package ffstatic_{{.system}}_{{.arch}}
import (
_ "embed"
"fmt"
"os"
)
//go:embed ffmpeg
var ffmpeg []byte
//go:embed ffprobe
var ffprobe []byte
func writeTempExec(pattern string, binary []byte) (string, error) {
f, err := os.CreateTemp("", pattern)
if err != nil {
return "", fmt.Errorf("failed to create temp file: %v", err)
}
defer f.Close()
_, err = f.Write(binary)
if err != nil {
return "", fmt.Errorf("fail to write executable: %v", err)
}
if err := f.Chmod(os.ModePerm); err != nil {
return "", fmt.Errorf("fail to chmod: %v", err)
}
return f.Name(), nil
}
var (
FfmpegPath string
FfprobeEPath string
)
func init() {
var err error
FfmpegPath, err = writeTempExec("ffmpeg_{{.system}}_{{.arch}}", ffmpeg)
if err != nil {
panic(fmt.Errorf("failed to write ffmpeg_{{.system}}_{{.arch}}: %v", err))
}
FfprobePath, err = writeTempExec("ffprobe_{{.system}}_{{.arch}}", ffprobe)
if err != nil {
panic(fmt.Errorf("failed to write ffprobe_{{.system}}_{{.arch}}: %v", err))
}
}

3
templates/go.mod.tpl Normal file
View File

@@ -0,0 +1,3 @@
module github.com/go-ffstatic/{{.system}}-{{.arch}}
go 1.16