Skip to content

Commit

Permalink
Merge pull request #12 from dpolakovics/11-switch-to-ffstatic
Browse files Browse the repository at this point in the history
feat: add ffmpeg binaries
  • Loading branch information
dpolakovics authored Nov 8, 2024
2 parents 6f5bccd + 1a2de36 commit 9a35d74
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ffmpeg filter=lfs diff=lfs merge=lfs -text
ffprobe filter=lfs diff=lfs merge=lfs -text
73 changes: 73 additions & 0 deletions internal/ffmpeg/darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//go:build darwin
// +build darwin

package ffmpeg

import (
_ "embed"
"fmt"
"os"
"runtime"
)

//go:embed darwin_amd64/ffmpeg
var ffmpegAmd64 []byte
//go:embed darwin_amd64/ffprobe
var ffprobeAmd64 []byte

//go:embed darwin_arm64/ffmpeg
var ffmpegArm64 []byte
//go:embed darwin_arm64/ffprobe
var ffprobeArm64 []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
ffprobePath string
)

func FFmpegPath() string { return ffmpegPath }

func FFprobePath() string { return ffprobePath }

func init() {
var err error

switch runtime.GOARCH {
case "amd64":
ffmpegPath, err = writeTempExec("ffmpeg_linux_amd64", ffmpegAmd64)
if err != nil {
panic(fmt.Errorf("failed to write ffmpeg_linux_amd64: %v", err))
}
ffprobePath, err = writeTempExec("ffprobe_linux_amd64", ffprobeAmd64)
if err != nil {
panic(fmt.Errorf("failed to write ffprobe_linux_amd64: %v", err))
}
case "arm64":
ffmpegPath, err = writeTempExec("ffmpeg_linux_amd64", ffmpegArm64)
if err != nil {
panic(fmt.Errorf("failed to write ffmpeg_linux_amd64: %v", err))
}
ffprobePath, err = writeTempExec("ffprobe_linux_amd64", ffprobeArm64)
if err != nil {
panic(fmt.Errorf("failed to write ffprobe_linux_amd64: %v", err))
}
default:
panic(fmt.Errorf("Running on an unknown architecture: %s\n", runtime.GOARCH))
}
}
3 changes: 3 additions & 0 deletions internal/ffmpeg/darwin_amd64/ffmpeg
Git LFS file not shown
3 changes: 3 additions & 0 deletions internal/ffmpeg/darwin_amd64/ffprobe
Git LFS file not shown
3 changes: 3 additions & 0 deletions internal/ffmpeg/darwin_arm64/ffmpeg
Git LFS file not shown
3 changes: 3 additions & 0 deletions internal/ffmpeg/darwin_arm64/ffprobe
Git LFS file not shown
52 changes: 52 additions & 0 deletions internal/ffmpeg/linux_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//go:build linux && amd64

package ffmpeg

import (
_ "embed"
"fmt"
"os"
)

//go:embed linux_amd64/ffmpeg
var ffmpeg []byte

//go:embed linux_amd64/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
ffprobePath string
)

func FFmpegPath() string { return ffmpegPath }

func FFprobePath() string { return ffprobePath }

func init() {
var err error
ffmpegPath, err = writeTempExec("ffmpeg_linux_amd64", ffmpeg)
if err != nil {
panic(fmt.Errorf("failed to write ffmpeg_linux_amd64: %v", err))
}
ffprobePath, err = writeTempExec("ffprobe_linux_amd64", ffprobe)
if err != nil {
panic(fmt.Errorf("failed to write ffprobe_linux_amd64: %v", err))
}
}
3 changes: 3 additions & 0 deletions internal/ffmpeg/linux_amd64/ffmpeg
Git LFS file not shown
3 changes: 3 additions & 0 deletions internal/ffmpeg/linux_amd64/ffprobe
Git LFS file not shown
52 changes: 52 additions & 0 deletions internal/ffmpeg/windows_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//go:build windows && amd64

package ffmpeg

import (
_ "embed"
"fmt"
"os"
)

//go:embed windows_amd64/ffmpeg
var ffmpeg []byte

//go:embed windows_amd64/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
ffprobePath string
)

func FFmpegPath() string { return ffmpegPath }

func FFprobePath() string { return ffprobePath }

func init() {
var err error
ffmpegPath, err = writeTempExec("ffmpeg_windows_amd64", ffmpeg)
if err != nil {
panic(fmt.Errorf("failed to write ffmpeg_windows_amd64: %v", err))
}
ffprobePath, err = writeTempExec("ffprobe_windows_amd64", ffprobe)
if err != nil {
panic(fmt.Errorf("failed to write ffprobe_windows_amd64: %v", err))
}
}
3 changes: 3 additions & 0 deletions internal/ffmpeg/windows_amd64/ffmpeg
Git LFS file not shown
3 changes: 3 additions & 0 deletions internal/ffmpeg/windows_amd64/ffprobe
Git LFS file not shown

0 comments on commit 9a35d74

Please sign in to comment.