From b06797a099beb3d3ffd17a469f1c76431e1ebf28 Mon Sep 17 00:00:00 2001 From: xfrr Date: Thu, 2 Apr 2020 23:14:26 +0200 Subject: [PATCH] go modules added, fixed mediafile and tests --- go.mod | 5 ++ go.sum | 11 +++ models/media.go | 23 ++++-- tests/transcoding_test.go | 154 ++++++++++++-------------------------- 4 files changed, 79 insertions(+), 114 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..84c2326 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/xfrr/goffmpeg + +go 1.14 + +require github.com/stretchr/testify v1.5.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..331fa69 --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/models/media.go b/models/media.go index 81482e7..e7b4825 100644 --- a/models/media.go +++ b/models/media.go @@ -3,6 +3,7 @@ package models import ( "fmt" "io" + "os/exec" "reflect" "strconv" "strings" @@ -48,6 +49,7 @@ type Mediafile struct { inputPipeReader *io.PipeReader inputPipeWriter *io.PipeWriter outputPipe bool + inputPipeCommand *exec.Cmd outputPipeReader *io.PipeReader outputPipeWriter *io.PipeWriter movFlags string @@ -237,6 +239,10 @@ func (m *Mediafile) SetInputPipe(val bool) { m.inputPipe = val } +func (m *Mediafile) SetInputPipeCommand(command *exec.Cmd) { + m.inputPipeCommand = command +} + func (m *Mediafile) SetInputPipeReader(r *io.PipeReader) { m.inputPipeReader = r } @@ -512,6 +518,10 @@ func (m *Mediafile) InputPipe() bool { return m.inputPipe } +func (m *Mediafile) InputPipeCommand() *exec.Cmd { + return m.inputPipeCommand +} + func (m *Mediafile) InputPipeReader() *io.PipeReader { return m.inputPipeReader } @@ -642,6 +652,7 @@ func (m *Mediafile) ToStrCommand() []string { "HardwareAcceleration", "InputPath", "InputPipe", + "InputPipeCommand", "HideBanner", "Aspect", "Resolution", @@ -755,7 +766,7 @@ func (m *Mediafile) ObtainInputPath() []string { return nil } -func (m *Mediafile) ObtainInputPipe() []string { +func (m *Mediafile) ObtainInputPipeCommand() []string { if m.inputPipe { return []string{"-i", "pipe:0"} } @@ -1133,10 +1144,10 @@ func (m *Mediafile) ObtainCompressionLevel() []string { func (m *Mediafile) ObtainMapMetadata() []string { if m.mapMetadata != "" { return []string{"-map_metadata", m.mapMetadata} - } - return nil + } + return nil } - + func (m *Mediafile) ObtainEncryptionKey() []string { return []string{"-hls_key_info_file", m.encryptionKey} } @@ -1155,6 +1166,6 @@ func (m *Mediafile) ObtainTags() []string { result = append(result, []string{"-metadata", fmt.Sprintf("%s=%s", key, val)}...) } return result - } - return nil + } + return nil } diff --git a/tests/transcoding_test.go b/tests/transcoding_test.go index 1f38f0c..2aca97a 100755 --- a/tests/transcoding_test.go +++ b/tests/transcoding_test.go @@ -13,231 +13,169 @@ import ( func TestInputNotFound(t *testing.T) { - var inputPath = "/data/testmkv" - var outputPath = "/data/out/testmp4.mp4" + var inputPath = "/tmp/ffmpeg/mkv" + var outputPath = "/tmp/ffmpeg/out/mkv.mp4" trans := new(transcoder.Transcoder) err := trans.Initialize(inputPath, outputPath) - if err != nil { - return - } + assert.Nil(t, err) } func TestTranscoding3GP(t *testing.T) { - var inputPath = "/data/test3gp" - var outputPath = "/data/testmp4.mp4" + var inputPath = "/tmp/ffmpeg/3gp" + var outputPath = "/tmp/ffmpeg/out/3gp.mp4" trans := new(transcoder.Transcoder) err := trans.Initialize(inputPath, outputPath) - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) done := trans.Run(false) err = <-done - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) } func TestTranscodingAVI(t *testing.T) { - var inputPath = "/data/testavi" - var outputPath = "/data/out/testmp4.mp4" + var inputPath = "/tmp/ffmpeg/avi" + var outputPath = "/tmp/ffmpeg/out/avi.mp4" trans := new(transcoder.Transcoder) err := trans.Initialize(inputPath, outputPath) - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) done := trans.Run(false) err = <-done - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) } func TestTranscodingFLV(t *testing.T) { - var inputPath = "/data/testflv" - var outputPath = "/data/out/testmp4.mp4" + var inputPath = "/tmp/ffmpeg/flv" + var outputPath = "/tmp/ffmpeg/out/flv.mp4" trans := new(transcoder.Transcoder) err := trans.Initialize(inputPath, outputPath) - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) done := trans.Run(false) err = <-done - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) } func TestTranscodingMKV(t *testing.T) { - var inputPath = "/data/testmkv" - var outputPath = "/data/out/testmp4.mp4" + var inputPath = "/tmp/ffmpeg/mkv" + var outputPath = "/tmp/ffmpeg/out/mkv.mp4" trans := new(transcoder.Transcoder) err := trans.Initialize(inputPath, outputPath) - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) done := trans.Run(false) err = <-done - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) } func TestTranscodingMOV(t *testing.T) { - var inputPath = "/data/testmov" - var outputPath = "/data/out/testmp4.mp4" + var inputPath = "/tmp/ffmpeg/mov" + var outputPath = "/tmp/ffmpeg/out/mov.mp4" trans := new(transcoder.Transcoder) err := trans.Initialize(inputPath, outputPath) - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) done := trans.Run(false) err = <-done - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) } func TestTranscodingMPEG(t *testing.T) { - var inputPath = "/data/testmpeg" - var outputPath = "/data/out/testmp4.mp4" + var inputPath = "/tmp/ffmpeg/mpeg" + var outputPath = "/tmp/ffmpeg/out/mpeg.mp4" trans := new(transcoder.Transcoder) err := trans.Initialize(inputPath, outputPath) - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) done := trans.Run(false) err = <-done - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) } func TestTranscodingOGG(t *testing.T) { - var inputPath = "/data/testogg" - var outputPath = "/data/out/testmp4.mp4" + var inputPath = "/tmp/ffmpeg/ogg" + var outputPath = "/tmp/ffmpeg/out/ogg.mp4" trans := new(transcoder.Transcoder) err := trans.Initialize(inputPath, outputPath) - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) done := trans.Run(false) err = <-done - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) } func TestTranscodingWAV(t *testing.T) { - var inputPath = "/data/testwav" - var outputPath = "/data/out/testmp4.mp4" + var inputPath = "/tmp/ffmpeg/wav" + var outputPath = "/tmp/ffmpeg/out/wav.mp4" trans := new(transcoder.Transcoder) err := trans.Initialize(inputPath, outputPath) - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) done := trans.Run(false) err = <-done - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) } func TestTranscodingWEBM(t *testing.T) { - var inputPath = "/data/testwebm" - var outputPath = "/data/out/testmp4.mp4" + var inputPath = "/tmp/ffmpeg/webm" + var outputPath = "/tmp/ffmpeg/out/webm.mp4" trans := new(transcoder.Transcoder) err := trans.Initialize(inputPath, outputPath) - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) done := trans.Run(false) err = <-done - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) } func TestTranscodingWMV(t *testing.T) { - var inputPath = "/data/testwmv" - var outputPath = "/data/out/testmp4.mp4" + var inputPath = "/tmp/ffmpeg/wmv" + var outputPath = "/tmp/ffmpeg/out/wmv.mp4" trans := new(transcoder.Transcoder) err := trans.Initialize(inputPath, outputPath) - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) done := trans.Run(false) err = <-done - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) } func TestTranscodingProgress(t *testing.T) { - var inputPath = "/data/testavi" - var outputPath = "/data/out/testmp4.mp4" + var inputPath = "/tmp/ffmpeg/avi" + var outputPath = "/tmp/ffmpeg/out/avi.mp4" trans := new(transcoder.Transcoder) @@ -262,7 +200,7 @@ func TestTranscodingProgress(t *testing.T) { } func TestTranscodePipes(t *testing.T) { - c1 := exec.Command("cat", "/tmp/data/testmkv") + c1 := exec.Command("cat", "/tmp/ffmpeg/mkv") trans := new(transcoder.Transcoder)