Can now encode both mp4 and vp9 with most of the options I need.

master
Zed A. Shaw 1 month ago
parent 1b07c4be54
commit 2c90827345
  1. 1
      .gitignore
  2. 22
      config.json
  3. 4
      config/settings.go
  4. 35
      main.go

1
.gitignore vendored

@ -29,3 +29,4 @@ coverage/*
public public
*.mp4 *.mp4
ffmpeg* ffmpeg*
renders

@ -1,7 +1,6 @@
[ [
{ {
"Format": "mp4", "Format": "mp4",
"Scale": "1280:720",
"VideoBitrate": 900, "VideoBitrate": 900,
"AudioBitrate": 192, "AudioBitrate": 192,
"Speed": "veryfast", "Speed": "veryfast",
@ -9,22 +8,29 @@
"CRF": 20, "CRF": 20,
"FPS": 30, "FPS": 30,
"Tune": "animation", "Tune": "animation",
"Input": "test*.mp4", "Input": "*.mp4",
"OutDir": "renders", "OutDir": "renders",
"Passes": 3 "Passes": 3,
"Extras": [
"-movflags", "faststart",
"-tune", "animation"
]
}, },
{ {
"Format": "mp4", "Format": "webm",
"Scale": "1920:1080", "VideoBitrate": 600,
"VideoBitrate": 1200, "VideoCodec": "libvpx-vp9",
"AudioBitrate": 192, "AudioBitrate": 192,
"AudioCodec": "libopus",
"Speed": "veryfast", "Speed": "veryfast",
"CleanFilename": false, "CleanFilename": false,
"CRF": 20, "CRF": 20,
"FPS": 30, "FPS": 30,
"Tune": "animation", "Tune": "animation",
"Input": "test*.mp4", "Input": "*.mp4",
"OutDir": "renders", "OutDir": "renders",
"Passes": 2 "Passes": 2,
"Extras": [
]
} }
] ]

@ -8,9 +8,12 @@ import (
) )
type VideoOpts struct { type VideoOpts struct {
Format string
Scale string Scale string
VideoBitrate int VideoBitrate int
VideoCodec string
AudioBitrate int AudioBitrate int
AudioCodec string
Speed string Speed string
CleanFilename bool CleanFilename bool
CRF int CRF int
@ -21,6 +24,7 @@ type VideoOpts struct {
Test int Test int
TestStart int TestStart int
Passes int Passes int
Extras []string
} }
type Settings struct { type Settings struct {

@ -12,7 +12,7 @@ import (
"github.com/modfy/fluent-ffmpeg" "github.com/modfy/fluent-ffmpeg"
) )
func ModFile(fname string, scale string) string { func ModFile(fname string, encoding config.VideoOpts) string {
cleaned := filepath.Clean(fname) cleaned := filepath.Clean(fname)
dir, file := filepath.Split(cleaned) dir, file := filepath.Split(cleaned)
ext := filepath.Ext(file) ext := filepath.Ext(file)
@ -20,8 +20,8 @@ func ModFile(fname string, scale string) string {
base, found := strings.CutSuffix(file, ext) base, found := strings.CutSuffix(file, ext)
if !found { panic("no extension found?!") } if !found { panic("no extension found?!") }
dim := strings.Replace(scale, ":", ".", 1) dim := strings.Replace(encoding.Scale, ":", ".", 1)
renamed := fmt.Sprint(base, ".", dim, ext) renamed := fmt.Sprint(base, ".", dim, ".", encoding.Format)
return filepath.Join(dir, renamed) return filepath.Join(dir, renamed)
} }
@ -29,40 +29,43 @@ func ModFile(fname string, scale string) string {
func Run(encoding config.VideoOpts, pass int, pid int, input string, output string) { func Run(encoding config.VideoOpts, pass int, pid int, input string, output string) {
encode := fluentffmpeg.NewCommand("") encode := fluentffmpeg.NewCommand("")
mp4_encoding := []string{ extras := []string{
"-vf", fmt.Sprintf("scale=%s:flags=lanczos", encoding.Scale),
"-aspect", encoding.Scale,
"-pix_fmt", "yuv420p", "-pix_fmt", "yuv420p",
"-tune", encoding.Tune,
"-movflags", "faststart",
"-pass", fmt.Sprint(pass), "-pass", fmt.Sprint(pass),
"-passlogfile", fmt.Sprintf("ffmpeg2pass-%x.log", pid), "-passlogfile", fmt.Sprintf("ffmpeg2pass-%x.log", pid),
"-preset", encoding.Speed, "-preset", encoding.Speed,
} }
if encoding.Scale != "" {
"-vf", fmt.Sprintf("scale=%s:flags=lanczos", encoding.Scale),
"-aspect", encoding.Scale,
}
extras = append(extras, encoding.Extras...)
if pass != encoding.Passes { if pass != encoding.Passes {
mp4_encoding = append(mp4_encoding, "-an") extras = append(extras, "-an")
} else { } else {
encode.AudioCodec("aac") encode.AudioCodec(encoding.AudioCodec)
mp4_encoding = append(mp4_encoding, extras = append(extras,
"-b:a", fmt.Sprint(encoding.AudioBitrate * 1024)) "-b:a", fmt.Sprint(encoding.AudioBitrate * 1024))
} }
if encoding.Test > 0 { if encoding.Test > 0 {
encode.InputOptions( encode.InputOptions(
"-ss", fmt.Sprintf("00:%d", encoding.TestStart)) "-ss", fmt.Sprintf("00:%d", encoding.TestStart))
mp4_encoding = append(mp4_encoding, "-t", fmt.Sprint(encoding.Test)) extras = append(extras, "-t", fmt.Sprint(encoding.Test))
} }
encode.VideoCodec("libx264"). encode.VideoCodec(encoding.VideoCodec).
VideoBitRate(encoding.VideoBitrate * 1024). VideoBitRate(encoding.VideoBitrate * 1024).
FrameRate(encoding.FPS). FrameRate(encoding.FPS).
ConstantRateFactor(encoding.CRF) ConstantRateFactor(encoding.CRF)
encode.OutputOptions(mp4_encoding...) encode.OutputOptions(extras...)
cmd := encode.InputPath(input). cmd := encode.InputPath(input).
OutputFormat("mp4"). OutputFormat(encoding.Format).
OutputPath(output). OutputPath(output).
Build() Build()
@ -95,7 +98,7 @@ func RenderToDir(encoding config.VideoOpts) {
for _, path := range matches { for _, path := range matches {
base := filepath.Base(path) base := filepath.Base(path)
target := filepath.Join(encoding.OutDir, base) target := filepath.Join(encoding.OutDir, base)
target = ModFile(target, encoding.Scale) target = ModFile(target, encoding)
_, err := os.Stat(target) _, err := os.Stat(target)

Loading…
Cancel
Save