Skip to content

Commit

Permalink
release: added interfaces for every struct type. This enables mocking…
Browse files Browse the repository at this point in the history
… for ffmpeg-go library components
  • Loading branch information
Paxx committed Feb 21, 2023
1 parent 72217a5 commit 24d351f
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 30 deletions.
2 changes: 1 addition & 1 deletion ffmpeg/audio_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/Paxx-RnD/go-ffmpeg/arguments"
)

func (f *Ffmpeg) Flanger(delay float64) *Ffmpeg {
func (f *Ffmpeg) Flanger(delay float64) IFfmpeg {
if delay < 0 {
delay = 0
} else if delay > 30 {
Expand Down
4 changes: 2 additions & 2 deletions ffmpeg/bitrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"fmt"
)

func (f *Ffmpeg) BitrateVideo(bitrate int) *Ffmpeg {
func (f *Ffmpeg) BitrateVideo(bitrate int) IFfmpeg {
f.arguments.Options.Append("-b:v", fmt.Sprintf("%d", bitrate))
return f
}

func (f *Ffmpeg) BitrateAudio(bitrate int) *Ffmpeg {
func (f *Ffmpeg) BitrateAudio(bitrate int) IFfmpeg {
f.arguments.Options.Append("-b:a", fmt.Sprintf("%d", bitrate))
return f
}
6 changes: 5 additions & 1 deletion ffmpeg/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import (
"strings"
)

type IBuilder interface {
Build() []string
}

type Builder Ffmpeg

func (f *Ffmpeg) Output(path string) *Builder {
func (f *Ffmpeg) Output(path string) IBuilder {
f.arguments.Output.Append(path)
return (*Builder)(f)
}
Expand Down
49 changes: 34 additions & 15 deletions ffmpeg/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,70 @@ import (
"github.com/Paxx-RnD/go-helper/helpers/boolean_helper"
)

type IChain interface {
Format(input string, format pixel_formats.PixelFormat, output string) IChain
Pad(input string, width int, height int, x int, y int, output string) IChain
Volume(input string, volume float64, output string) IChain
Fps(input string, fps float64, output string) IChain
Scale(input string, width float64, height float64, output string) IChain
Reverse(input string, output string) IChain
Trim(input string, start float64, end float64, output string) IChain
Split(input string, outputs ...string) IChain
ATrim(input string, start float64, end float64, output string) IChain
AlphaExtract(input string, output string) IChain
AlphaMerge(input string, mask string, shortest bool, output string) IChain
Concat(inputs []string, videoEnable bool, audioEnable bool, output string) IChain
Overlay(under string, over string, shortest bool, output string) IChain
ACrossFade(input1 string, input2 string, duration float64, output string) IChain
ADelay(input string, delay float64, output string) IChain
Map(output string) IMap
}

type Chain Ffmpeg

func (fg *Chain) Format(input string, format pixel_formats.PixelFormat, output string) *Chain {
func (fg *Chain) Format(input string, format pixel_formats.PixelFormat, output string) IChain {
chain := fmt.Sprintf("[%s]format=%s[%s]", input, format, output)
fg.arguments.FilterGraph.FilterChain = append(fg.arguments.FilterGraph.FilterChain, chain)
return fg
}

func (fg *Chain) Pad(input string, width int, height int, x int, y int, output string) *Chain {
func (fg *Chain) Pad(input string, width int, height int, x int, y int, output string) IChain {
chain := fmt.Sprintf("[%s]pad=width=%d:height=%d:x=%d:y=%d[%s]", input, width, height, x, y, output)
fg.arguments.FilterGraph.FilterChain = append(fg.arguments.FilterGraph.FilterChain, chain)
return fg
}

func (fg *Chain) Volume(input string, volume float64, output string) *Chain {
func (fg *Chain) Volume(input string, volume float64, output string) IChain {
chain := fmt.Sprintf("[%s]volume=volume=%f[%s]", input, volume, output)
fg.arguments.FilterGraph.FilterChain = append(fg.arguments.FilterGraph.FilterChain, chain)
return fg
}

func (fg *Chain) Fps(input string, fps float64, output string) *Chain {
func (fg *Chain) Fps(input string, fps float64, output string) IChain {
chain := fmt.Sprintf("[%s]fps=fps=%f[%s]", input, fps, output)
fg.arguments.FilterGraph.FilterChain = append(fg.arguments.FilterGraph.FilterChain, chain)
return fg
}

func (fg *Chain) Scale(input string, width float64, height float64, output string) *Chain {
func (fg *Chain) Scale(input string, width float64, height float64, output string) IChain {
chain := fmt.Sprintf("[%s]scale=width=%f:height=%f[%s]", input, width, height, output)
fg.arguments.FilterGraph.FilterChain = append(fg.arguments.FilterGraph.FilterChain, chain)
return fg
}

func (fg *Chain) Reverse(input string, output string) *Chain {
func (fg *Chain) Reverse(input string, output string) IChain {
chain := fmt.Sprintf("[%s]reverse[%s]", input, output)
fg.arguments.FilterGraph.FilterChain = append(fg.arguments.FilterGraph.FilterChain, chain)
return fg
}

func (fg *Chain) Trim(input string, start float64, end float64, output string) *Chain {
func (fg *Chain) Trim(input string, start float64, end float64, output string) IChain {
chain := fmt.Sprintf("[%s]trim=start=%f:end=%f,setpts=PTS-STARTPTS[%s]", input, start, end, output)
fg.arguments.FilterGraph.FilterChain = append(fg.arguments.FilterGraph.FilterChain, chain)
return fg
}

func (fg *Chain) Split(input string, outputs ...string) *Chain {
func (fg *Chain) Split(input string, outputs ...string) IChain {
chain := fmt.Sprintf("[%s]split=n=%d", input, len(outputs))
for _, output := range outputs {
chain += fmt.Sprintf("[%s]", output)
Expand All @@ -59,26 +78,26 @@ func (fg *Chain) Split(input string, outputs ...string) *Chain {
return fg
}

func (fg *Chain) ATrim(input string, start float64, end float64, output string) *Chain {
func (fg *Chain) ATrim(input string, start float64, end float64, output string) IChain {
chain := fmt.Sprintf("[%s]atrim=start=%f:end=%f,asetpts=PTS-STARTPTS[%s]", input, start, end, output)
fg.arguments.FilterGraph.FilterChain = append(fg.arguments.FilterGraph.FilterChain, chain)
return fg
}

func (fg *Chain) AlphaExtract(input string, output string) *Chain {
func (fg *Chain) AlphaExtract(input string, output string) IChain {
chain := fmt.Sprintf("[%s]alphaextract[%s]", input, output)
fg.arguments.FilterGraph.FilterChain = append(fg.arguments.FilterGraph.FilterChain, chain)
return fg
}

func (fg *Chain) AlphaMerge(input string, mask string, shortest bool, output string) *Chain {
func (fg *Chain) AlphaMerge(input string, mask string, shortest bool, output string) IChain {
short := boolean_helper.ToInt(shortest)
chain := fmt.Sprintf("[%s][%s]alphamerge=shortest=%d[%s]", input, mask, short, output)
fg.arguments.FilterGraph.FilterChain = append(fg.arguments.FilterGraph.FilterChain, chain)
return fg
}

func (fg *Chain) Concat(inputs []string, videoEnable bool, audioEnable bool, output string) *Chain {
func (fg *Chain) Concat(inputs []string, videoEnable bool, audioEnable bool, output string) IChain {
toConcat := make([]string, len(inputs))
for i, input := range inputs {
toConcat[i] = fmt.Sprintf("[%s]", input)
Expand All @@ -92,20 +111,20 @@ func (fg *Chain) Concat(inputs []string, videoEnable bool, audioEnable bool, out
return fg
}

func (fg *Chain) Overlay(under string, over string, shortest bool, output string) *Chain {
func (fg *Chain) Overlay(under string, over string, shortest bool, output string) IChain {
short := boolean_helper.ToInt(shortest)
chain := fmt.Sprintf("[%s][%s]overlay=shortest=%d[%s]", under, over, short, output)
fg.arguments.FilterGraph.FilterChain = append(fg.arguments.FilterGraph.FilterChain, chain)
return fg
}

func (fg *Chain) ACrossFade(input1 string, input2 string, duration float64, output string) *Chain {
func (fg *Chain) ACrossFade(input1 string, input2 string, duration float64, output string) IChain {
chain := fmt.Sprintf("[%s][%s]acrossfade=d=%f[%s]", input1, input2, duration, output)
fg.arguments.FilterGraph.FilterChain = append(fg.arguments.FilterGraph.FilterChain, chain)
return fg
}

func (fg *Chain) ADelay(input string, delay float64, output string) *Chain {
func (fg *Chain) ADelay(input string, delay float64, output string) IChain {
chain := fmt.Sprintf("[%s]adelay=%f[%s]", input, delay, output)
fg.arguments.FilterGraph.FilterChain = append(fg.arguments.FilterGraph.FilterChain, chain)
return fg
Expand Down
4 changes: 2 additions & 2 deletions ffmpeg/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"github.com/Paxx-RnD/go-ffmpeg/constants/codec_video"
)

func (f *Ffmpeg) CodecVideo(codec codec_video.CodecVideo) *Ffmpeg {
func (f *Ffmpeg) CodecVideo(codec codec_video.CodecVideo) IFfmpeg {
f.arguments.Options.Append("-c:v", string(codec))
return f
}

func (f *Ffmpeg) CodecAudio(codec codec_audio.CodecAudio) *Ffmpeg {
func (f *Ffmpeg) CodecAudio(codec codec_audio.CodecAudio) IFfmpeg {
f.arguments.Options.Append("-c:a", string(codec))
return f
}
21 changes: 19 additions & 2 deletions ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@ package ffmpeg
import (
"github.com/Paxx-RnD/go-ffmpeg/arguments"
"github.com/Paxx-RnD/go-ffmpeg/configuration"
"github.com/Paxx-RnD/go-ffmpeg/constants/codec_audio"
"github.com/Paxx-RnD/go-ffmpeg/constants/codec_video"
)

type IFfmpeg interface {
BitrateVideo(bitrate int) IFfmpeg
BitrateAudio(bitrate int) IFfmpeg
Flanger(delay float64) IFfmpeg
CodecVideo(codec codec_video.CodecVideo) IFfmpeg
CodecAudio(codec codec_audio.CodecAudio) IFfmpeg
Scale(width int, height int) IFfmpeg
Fps(fps float64) IFfmpeg
FilterGraph() IChain
Input(path string) IFfmpeg
Inputs(paths ...string) IFfmpeg
Output(path string) IBuilder
Run(args []string) error
}

type Ffmpeg struct {
arguments Arguments
Configuration *configuration.Configuration
Expand All @@ -20,12 +37,12 @@ type Arguments struct {
Options arguments.Options
}

func (f *Ffmpeg) Input(path string) *Ffmpeg {
func (f *Ffmpeg) Input(path string) IFfmpeg {
f.arguments.Inputs.Append(path)
return f
}

func (f *Ffmpeg) Inputs(paths ...string) *Ffmpeg {
func (f *Ffmpeg) Inputs(paths ...string) IFfmpeg {
for _, p := range paths {
f.arguments.Inputs.Append(p)
}
Expand Down
2 changes: 1 addition & 1 deletion ffmpeg/filtergraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type FilterGraph struct {
MapChain []string
}

func (f *Ffmpeg) FilterGraph() *Chain {
func (f *Ffmpeg) FilterGraph() IChain {
return &Chain{
arguments: f.arguments,
Configuration: f.Configuration,
Expand Down
11 changes: 8 additions & 3 deletions ffmpeg/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package ffmpeg

import "github.com/Paxx-RnD/go-ffmpeg/arguments"

type IMap interface {
Map(output string) IMap
Output(output string) IBuilder
}

type Map Ffmpeg

func (fg *Chain) Map(output string) *Map {
func (fg *Chain) Map(output string) IMap {
m := Map{
arguments: fg.arguments,
Configuration: fg.Configuration,
Expand All @@ -14,12 +19,12 @@ func (fg *Chain) Map(output string) *Map {
return &m
}

func (m *Map) Map(output string) *Map {
func (m *Map) Map(output string) IMap {
m.arguments.FilterGraph.MapChain = append(m.arguments.FilterGraph.MapChain, output)
return m
}

func (m *Map) Output(output string) *Builder {
func (m *Map) Output(output string) IBuilder {
builder := Builder{
arguments: m.arguments,
Configuration: m.Configuration,
Expand Down
4 changes: 2 additions & 2 deletions ffmpeg/video_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/Paxx-RnD/go-ffmpeg/arguments"
)

func (f *Ffmpeg) Scale(width int, height int) *Ffmpeg {
func (f *Ffmpeg) Scale(width int, height int) IFfmpeg {
filter := arguments.Filter{
Name: "scale",
Args: []arguments.FilterArgs{
Expand All @@ -23,7 +23,7 @@ func (f *Ffmpeg) Scale(width int, height int) *Ffmpeg {
return f
}

func (f *Ffmpeg) Fps(fps float64) *Ffmpeg {
func (f *Ffmpeg) Fps(fps float64) IFfmpeg {
filter := arguments.Filter{
Name: "fps",
Args: []arguments.FilterArgs{
Expand Down
2 changes: 1 addition & 1 deletion test/a_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestMain(m *testing.M) {
m.Run()
}

func GetFfmpeg() *ffmpeg.Ffmpeg {
func GetFfmpeg() ffmpeg.IFfmpeg {
f := ffmpeg.Ffmpeg{
Configuration: configuration.GetConfiguration(),
Headers: []string{
Expand Down

0 comments on commit 24d351f

Please sign in to comment.