Go Gettable, Setting output path also from mediafile

This commit is contained in:
Rohit Garg
2018-05-23 14:44:14 +05:30
parent f0143213f2
commit 65793ce83b
5 changed files with 41 additions and 28 deletions

View File

@@ -13,11 +13,15 @@ FFMPEG wrapper written in GO which allows to obtain the progress.
# Getting started # Getting started
How to transcode a media file How to transcode a media file
```shell
go get github.com/xfrr/goffmpeg
```
```go ```go
package main package main
import ( import (
"goffmpeg/transcoder" "github.com/xfrr/goffmpeg/transcoder"
) )
var inputPath = "/data/testmov" var inputPath = "/data/testmov"
@@ -27,14 +31,14 @@ func main() {
// Create new instance of transcoder // Create new instance of transcoder
trans := new(transcoder.Transcoder) trans := new(transcoder.Transcoder)
// Initialize transcoder passing the input file path and output file path // Initialize transcoder passing the input file path and output file path
err := trans.Initialize( inputPath, outputPath ) err := trans.Initialize( inputPath, outputPath )
// Handle error... // Handle error...
// Start transcoder process // Start transcoder process
done, err := trans.Run() done, err := trans.Run()
// This channel is used to wait for the process to end // This channel is used to wait for the process to end
<-done <-done
@@ -47,14 +51,14 @@ func main() {
// Create new instance of transcoder // Create new instance of transcoder
trans := new(transcoder.Transcoder) trans := new(transcoder.Transcoder)
// Initialize transcoder passing the input file path and output file path // Initialize transcoder passing the input file path and output file path
err := trans.Initialize( inputPath, outputPath ) err := trans.Initialize( inputPath, outputPath )
// Handle error... // Handle error...
// Start transcoder process // Start transcoder process
done, err := trans.Run() done, err := trans.Run()
// Returns a channel to get the transcoding progress // Returns a channel to get the transcoding progress
progress, err := trans.Output() progress, err := trans.Output()
@@ -62,7 +66,7 @@ func main() {
for msg := range progress { for msg := range progress {
fmt.Println(msg) fmt.Println(msg)
} }
// This channel is used to wait for the transcoding process to end // This channel is used to wait for the transcoding process to end
<-done <-done
@@ -98,8 +102,14 @@ SetBufferSize
SetThreads SetThreads
SetPreset SetPreset
SetDuration SetDuration
SetDurationInput
SetSeekTime SetSeekTime
SetSeekTimeInput
SetSeekUsingTsInput
SetQuality SetQuality
SetCopyTs
SetInputPath
SetOutputPath
``` ```
Example Example
```golang ```golang
@@ -107,11 +117,11 @@ func main() {
// Create new instance of transcoder // Create new instance of transcoder
trans := new(transcoder.Transcoder) trans := new(transcoder.Transcoder)
// Initialize transcoder passing the input file path and output file path // Initialize transcoder passing the input file path and output file path
err := trans.Initialize( inputPath, outputPath ) err := trans.Initialize( inputPath, outputPath )
// Handle error... // Handle error...
// SET FRAME RATE TO MEDIAFILE // SET FRAME RATE TO MEDIAFILE
trans.MediaFile().SetFrameRate(70) trans.MediaFile().SetFrameRate(70)
// SET ULTRAFAST PRESET TO MEDIAFILE // SET ULTRAFAST PRESET TO MEDIAFILE
@@ -119,7 +129,7 @@ func main() {
// Start transcoder process // Start transcoder process
done, err := trans.Run() done, err := trans.Run()
// Returns a channel to get the transcoding progress // Returns a channel to get the transcoding progress
progress, err := trans.Output() progress, err := trans.Output()
@@ -127,7 +137,7 @@ func main() {
for msg := range progress { for msg := range progress {
fmt.Println(msg) fmt.Println(msg)
} }
// This channel is used to wait for the transcoding process to end // This channel is used to wait for the transcoding process to end
<-done <-done

View File

@@ -4,7 +4,7 @@ import (
"os/exec" "os/exec"
"bytes" "bytes"
"strings" "strings"
"goffmpeg/utils" "github.com/xfrr/goffmpeg/utils"
) )
type Configuration struct { type Configuration struct {

View File

@@ -35,6 +35,7 @@ type Mediafile struct {
seekUsingTsInput bool seekUsingTsInput bool
seekTimeInput string seekTimeInput string
inputPath string inputPath string
outputPath string
copyTs bool copyTs bool
} }
@@ -140,6 +141,10 @@ func (m *Mediafile) SetInputPath(val string) {
m.inputPath = val m.inputPath = val
} }
func (m *Mediafile) SetOutputPath(val string) {
m.outputPath = val
}
func (m *Mediafile) SetMetadata(v Metadata) { func (m *Mediafile) SetMetadata(v Metadata) {
m.metadata = v m.metadata = v
} }
@@ -250,6 +255,10 @@ func (m Mediafile) InputPath() string {
return m.inputPath return m.inputPath
} }
func (m *Mediafile) OutputPath() string {
return m.outputPath
}
func (m Mediafile) Metadata() Metadata { func (m Mediafile) Metadata() Metadata {
return m.metadata return m.metadata
} }
@@ -258,7 +267,7 @@ func (m Mediafile) Metadata() Metadata {
func (m Mediafile) ToStrCommand() string { func (m Mediafile) ToStrCommand() string {
var strCommand 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 { for _, name := range opts {
opt := reflect.ValueOf(&m).MethodByName(fmt.Sprintf("Obtain%s", name)) opt := reflect.ValueOf(&m).MethodByName(fmt.Sprintf("Obtain%s", name))
if (opt != reflect.Value{}) { if (opt != reflect.Value{}) {
@@ -298,12 +307,17 @@ func (m *Mediafile) ObtainInputPath() string {
return fmt.Sprintf("-i \"%s\"", m.inputPath) return fmt.Sprintf("-i \"%s\"", m.inputPath)
} }
func (m *Mediafile) ObtainOutputPath() string {
return fmt.Sprintf("\"%s\"", m.outputPath)
}
func (m *Mediafile) ObtainVideoCodec() string { func (m *Mediafile) ObtainVideoCodec() string {
if m.videoCodec != "" { if m.videoCodec != "" {
return fmt.Sprintf("-vcodec %s", m.videoCodec) return fmt.Sprintf("-vcodec %s", m.videoCodec)
} }
return "" return ""
} }
func (m *Mediafile) ObtainFrameRate() string { func (m *Mediafile) ObtainFrameRate() string {
if m.frameRate != 0 { if m.frameRate != 0 {
return fmt.Sprintf("-r %d", m.frameRate) return fmt.Sprintf("-r %d", m.frameRate)

View File

@@ -3,11 +3,11 @@ package transcoder
import ( import (
"errors" "errors"
"os" "os"
"goffmpeg/models" "github.com/xfrr/goffmpeg/models"
"os/exec" "os/exec"
"fmt" "fmt"
"goffmpeg/ffmpeg" "github.com/xfrr/goffmpeg/ffmpeg"
"goffmpeg/utils" "github.com/xfrr/goffmpeg/utils"
"bytes" "bytes"
"encoding/json" "encoding/json"
"bufio" "bufio"
@@ -18,7 +18,6 @@ import (
type Transcoder struct { type Transcoder struct {
process *exec.Cmd process *exec.Cmd
outputPath string
mediafile *models.Mediafile mediafile *models.Mediafile
configuration ffmpeg.Configuration configuration ffmpeg.Configuration
} }
@@ -27,10 +26,6 @@ func (t *Transcoder) SetProccess(v *exec.Cmd) {
t.process = v t.process = v
} }
func (t *Transcoder) SetOutputPath(v string) {
t.outputPath = v
}
func (t *Transcoder) SetMediaFile(v *models.Mediafile) { func (t *Transcoder) SetMediaFile(v *models.Mediafile) {
t.mediafile = v t.mediafile = v
} }
@@ -45,10 +40,6 @@ func (t Transcoder) Process() *exec.Cmd {
return t.process return t.process
} }
func (t Transcoder) OutputPath() string {
return t.outputPath
}
func (t Transcoder) MediaFile() *models.Mediafile { func (t Transcoder) MediaFile() *models.Mediafile {
return t.mediafile return t.mediafile
} }
@@ -70,8 +61,6 @@ func (t Transcoder) GetCommand() string {
rcommand += media.ToStrCommand() rcommand += media.ToStrCommand()
rcommand += " \"" + t.outputPath + "\""
return rcommand return rcommand
} }
@@ -126,9 +115,9 @@ func (t *Transcoder) Initialize(inputPath string, outputPath string) (error) {
MediaFile := new(models.Mediafile) MediaFile := new(models.Mediafile)
MediaFile.SetMetadata(Metadata) MediaFile.SetMetadata(Metadata)
MediaFile.SetInputPath(inputPath) MediaFile.SetInputPath(inputPath)
MediaFile.SetOutputPath(outputPath)
// Set transcoder configuration // Set transcoder configuration
t.SetOutputPath(outputPath)
t.SetMediaFile(MediaFile) t.SetMediaFile(MediaFile)
t.SetConfiguration(configuration) t.SetConfiguration(configuration)

View File

@@ -3,7 +3,7 @@ package utils
import ( import (
"strings" "strings"
"strconv" "strconv"
"goffmpeg/models" "github.com/xfrr/goffmpeg/models"
"runtime" "runtime"
) )