Skip to content

Commit

Permalink
Added get progress from audio transcoding
Browse files Browse the repository at this point in the history
  • Loading branch information
frr committed Feb 1, 2018
1 parent b9b0fb7 commit 93b5225
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
54 changes: 39 additions & 15 deletions transcoder/transcoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,25 @@ func (t Transcoder) Output() (<-chan models.Progress, error) {
}

scanner := bufio.NewScanner(stderr)
filetype := utils.CheckFileType(t.MediaFile().Metadata().Streams)

split := func(data []byte, atEOF bool) (advance int, token []byte, spliterror error) {

if atEOF && len(data) == 0 {
return 0, nil, nil
}

fr := strings.Index(string(data), "frame=")
Iframe := strings.Index(string(data), "frame=")

if fr > 0 {
return fr + 1, data[fr:], nil
if filetype == "video" {
if Iframe > 0 {
return Iframe + 1, data[Iframe:], nil
}
} else {
if i := bytes.IndexByte(data, '\n'); i >= 0 {
// We have a full newline-terminated line.
return i + 1, data[0:i], nil
}
}

if atEOF {
Expand All @@ -213,7 +221,6 @@ func (t Transcoder) Output() (<-chan models.Progress, error) {
scanner.Buffer(buf, bufio.MaxScanTokenSize)

var lastProgress float64
var lastFrames string
for scanner.Scan() {
Progress := new(models.Progress)
line := scanner.Text()
Expand All @@ -224,27 +231,44 @@ func (t Transcoder) Output() (<-chan models.Progress, error) {

f := strings.Fields(st)

// Frames processed
framesProcessed := strings.Split(f[0], "=")[1]
var framesProcessed string
var currentTime string
var currentBitrate string

for j := 0; j < len(f); j++ {
field := f[j]
fieldSplit := strings.Split(field, "=")

if len(fieldSplit) > 0 {
fieldname := strings.Split(field, "=")[0]
fieldvalue := strings.Split(field, "=")[1]

if fieldname == "frame" {
framesProcessed = fieldvalue
}

// Current time processed
time := strings.Split(f[4], "=")[1]
timesec := utils.DurToSec(time)
if fieldname == "time" {
currentTime = fieldvalue
}

if fieldname == "bitrate" {
currentBitrate = fieldvalue
}
}
}

timesec := utils.DurToSec(currentTime)
dursec, _ := strconv.ParseFloat(t.MediaFile().Metadata().Format.Duration, 64)
// Progress calculation
progress := (timesec * 100) / dursec

// Current bitrate
currentBitrate := strings.Split(f[5], "=")[1]

Progress.Progress = progress
Progress.CurrentBitrate = currentBitrate
Progress.FramesProcessed = framesProcessed
Progress.CurrentTime = time
Progress.CurrentTime = currentTime

if progress != lastProgress && framesProcessed != lastFrames{
if progress != lastProgress {
lastProgress = progress
lastFrames = framesProcessed
out <- *Progress
}
}
Expand Down
13 changes: 13 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"strings"
"strconv"
"goffmpeg/models"
)

func DurToSec(dur string) (sec float64) {
Expand All @@ -20,5 +21,17 @@ func DurToSec(dur string) (sec float64) {
return secs
}

func CheckFileType(streams []models.Streams) string {
for i := 0; i < len(streams); i++ {
st := streams[i]
if st.CodecType == "video" {
return "video"
break
}
}

return "audio"
}



0 comments on commit 93b5225

Please sign in to comment.