Skip to content

Commit

Permalink
Merge pull request #5 from rohitggarg/master
Browse files Browse the repository at this point in the history
Go Gettable, Setting output path also from mediafile
  • Loading branch information
xfrr authored May 24, 2018
2 parents f014321 + 65793ce commit 9bdabfb
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ FFMPEG wrapper written in GO which allows to obtain the progress.

# Getting started
How to transcode a media file
```shell
go get github.com/xfrr/goffmpeg
```

```go
package main

import (
"goffmpeg/transcoder"
"github.com/xfrr/goffmpeg/transcoder"
)

var inputPath = "/data/testmov"
Expand All @@ -27,14 +31,14 @@ func main() {

// Create new instance of transcoder
trans := new(transcoder.Transcoder)

// Initialize transcoder passing the input file path and output file path
err := trans.Initialize( inputPath, outputPath )
// Handle error...

// Start transcoder process
done, err := trans.Run()

// This channel is used to wait for the process to end
<-done

Expand All @@ -47,22 +51,22 @@ func main() {

// Create new instance of transcoder
trans := new(transcoder.Transcoder)

// Initialize transcoder passing the input file path and output file path
err := trans.Initialize( inputPath, outputPath )
// Handle error...

// Start transcoder process
done, err := trans.Run()

// Returns a channel to get the transcoding progress
progress, err := trans.Output()

// Example of printing transcoding progress
for msg := range progress {
fmt.Println(msg)
}

// This channel is used to wait for the transcoding process to end
<-done

Expand Down Expand Up @@ -98,36 +102,42 @@ SetBufferSize
SetThreads
SetPreset
SetDuration
SetDurationInput
SetSeekTime
SetSeekTimeInput
SetSeekUsingTsInput
SetQuality
SetCopyTs
SetInputPath
SetOutputPath
```
Example
```golang
func main() {

// Create new instance of transcoder
trans := new(transcoder.Transcoder)

// Initialize transcoder passing the input file path and output file path
err := trans.Initialize( inputPath, outputPath )
// Handle error...

// SET FRAME RATE TO MEDIAFILE
trans.MediaFile().SetFrameRate(70)
// SET ULTRAFAST PRESET TO MEDIAFILE
trans.MediaFile().SetPreset("ultrafast")

// Start transcoder process
done, err := trans.Run()

// Returns a channel to get the transcoding progress
progress, err := trans.Output()

// Example of printing transcoding progress
for msg := range progress {
fmt.Println(msg)
}

// This channel is used to wait for the transcoding process to end
<-done

Expand Down
2 changes: 1 addition & 1 deletion ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os/exec"
"bytes"
"strings"
"goffmpeg/utils"
"github.com/xfrr/goffmpeg/utils"
)

type Configuration struct {
Expand Down
16 changes: 15 additions & 1 deletion models/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Mediafile struct {
seekUsingTsInput bool
seekTimeInput string
inputPath string
outputPath string
copyTs bool
}

Expand Down Expand Up @@ -140,6 +141,10 @@ func (m *Mediafile) SetInputPath(val string) {
m.inputPath = val
}

func (m *Mediafile) SetOutputPath(val string) {
m.outputPath = val
}

func (m *Mediafile) SetMetadata(v Metadata) {
m.metadata = v
}
Expand Down Expand Up @@ -250,6 +255,10 @@ func (m Mediafile) InputPath() string {
return m.inputPath
}

func (m *Mediafile) OutputPath() string {
return m.outputPath
}

func (m Mediafile) Metadata() Metadata {
return m.metadata
}
Expand All @@ -258,7 +267,7 @@ func (m Mediafile) Metadata() Metadata {
func (m Mediafile) ToStrCommand() string {
var strCommand string

opts := []string{"SeekTimeInput", "DurationInput", "SeekUsingTsInput", "InputPath", "Aspect", "VideoCodec", "FrameRate", "Resolution", "VideoBitRate", "VideoBitRateTolerance", "AudioCodec", "AudioBitRate", "AudioChannels", "VideoMaxBitRate", "VideoMinBitRate", "BufferSize", "Threads", "Preset", "Target", "Duration", "KeyframeInterval", "SeekTime", "Quality", "MuxDelay", "CopyTs"}
opts := []string{"SeekTimeInput", "DurationInput", "SeekUsingTsInput", "InputPath", "Aspect", "VideoCodec", "FrameRate", "Resolution", "VideoBitRate", "VideoBitRateTolerance", "AudioCodec", "AudioBitRate", "AudioChannels", "VideoMaxBitRate", "VideoMinBitRate", "BufferSize", "Threads", "Preset", "Target", "Duration", "KeyframeInterval", "SeekTime", "Quality", "MuxDelay", "CopyTs", "OutputPath"}
for _, name := range opts {
opt := reflect.ValueOf(&m).MethodByName(fmt.Sprintf("Obtain%s", name))
if (opt != reflect.Value{}) {
Expand Down Expand Up @@ -298,12 +307,17 @@ func (m *Mediafile) ObtainInputPath() string {
return fmt.Sprintf("-i \"%s\"", m.inputPath)
}

func (m *Mediafile) ObtainOutputPath() string {
return fmt.Sprintf("\"%s\"", m.outputPath)
}

func (m *Mediafile) ObtainVideoCodec() string {
if m.videoCodec != "" {
return fmt.Sprintf("-vcodec %s", m.videoCodec)
}
return ""
}

func (m *Mediafile) ObtainFrameRate() string {
if m.frameRate != 0 {
return fmt.Sprintf("-r %d", m.frameRate)
Expand Down
19 changes: 4 additions & 15 deletions transcoder/transcoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package transcoder
import (
"errors"
"os"
"goffmpeg/models"
"github.com/xfrr/goffmpeg/models"
"os/exec"
"fmt"
"goffmpeg/ffmpeg"
"goffmpeg/utils"
"github.com/xfrr/goffmpeg/ffmpeg"
"github.com/xfrr/goffmpeg/utils"
"bytes"
"encoding/json"
"bufio"
Expand All @@ -18,7 +18,6 @@ import (

type Transcoder struct {
process *exec.Cmd
outputPath string
mediafile *models.Mediafile
configuration ffmpeg.Configuration
}
Expand All @@ -27,10 +26,6 @@ func (t *Transcoder) SetProccess(v *exec.Cmd) {
t.process = v
}

func (t *Transcoder) SetOutputPath(v string) {
t.outputPath = v
}

func (t *Transcoder) SetMediaFile(v *models.Mediafile) {
t.mediafile = v
}
Expand All @@ -45,10 +40,6 @@ func (t Transcoder) Process() *exec.Cmd {
return t.process
}

func (t Transcoder) OutputPath() string {
return t.outputPath
}

func (t Transcoder) MediaFile() *models.Mediafile {
return t.mediafile
}
Expand All @@ -70,8 +61,6 @@ func (t Transcoder) GetCommand() string {

rcommand += media.ToStrCommand()

rcommand += " \"" + t.outputPath + "\""

return rcommand

}
Expand Down Expand Up @@ -126,9 +115,9 @@ func (t *Transcoder) Initialize(inputPath string, outputPath string) (error) {
MediaFile := new(models.Mediafile)
MediaFile.SetMetadata(Metadata)
MediaFile.SetInputPath(inputPath)
MediaFile.SetOutputPath(outputPath)
// Set transcoder configuration

t.SetOutputPath(outputPath)
t.SetMediaFile(MediaFile)
t.SetConfiguration(configuration)

Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package utils
import (
"strings"
"strconv"
"goffmpeg/models"
"github.com/xfrr/goffmpeg/models"
"runtime"
)

Expand Down

0 comments on commit 9bdabfb

Please sign in to comment.