Last active
July 20, 2022 06:20
-
-
Save satorunooshie/c59c0cc3824cee383dac5b383d83d2ea to your computer and use it in GitHub Desktop.
video synthesis sample code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package service | |
| import ( | |
| "bytes" | |
| "context" | |
| "fmt" | |
| "io" | |
| "os/exec" | |
| ) | |
| var defaultArgs = []string{"-hide_banner", "-loglevel", "error", "-y"} | |
| // NOTE: その他の関数は省略してあります. | |
| func Synthesis(ctx context.Context, alpha io.Reader, in, out string) error { | |
| args := append(defaultArgs, "-i", in, "-i", "pipe:", "-filter_complex", "overlay=x=0:y=0", "-preset", "veryfast", "-movflags", "faststart", "-crf", "28", "-an", "-r", "15", out) | |
| if _, err := runCmd(ctx, "ffmpeg", alpha, args...); err != nil { | |
| return err | |
| } | |
| return nil | |
| } | |
| // runCmd executes any command and returns standard output. | |
| // stdin is optional. | |
| func runCmd(ctx context.Context, name string, stdin io.Reader, args ...string) (*bytes.Buffer, error) { | |
| cmd := exec.Command(name, args...) | |
| if stdin != nil { | |
| cmd.Stdin = stdin | |
| } | |
| var ( | |
| stdout bytes.Buffer | |
| stderr bytes.Buffer | |
| ) | |
| cmd.Stdout, cmd.Stderr = &stdout, &stderr | |
| if err := cmd.Run(); err != nil { | |
| return nil, fmt.Errorf("err: %v stderr: %s", err, stderr.String()) | |
| } | |
| return &stdout, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment