Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Added GitNbChannel function that return number of channels #20

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 127 additions & 4 deletions ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
@@ -7,14 +7,15 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strconv"
"strings"

"github.com/floostack/transcoder"
"github.com/floostack/transcoder/utils"
"github.com/sashker/transcoder"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace by github.com/floostack/transcoder

"github.com/sashker/transcoder/utils"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace by github.com/floostack/transcoder

)

// Transcoder ...
@@ -56,10 +57,19 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
}

// Append input file and standard options
args := append([]string{"-i", t.input}, opts.GetStrArguments()...)
progressOpts := []string{"-loglevel", "error", "-nostats", "-progress", "-"}
defaultOpts := []string{"-i", t.input}

args := []string{}
args = append(args, progressOpts...)
args = append(args, defaultOpts...)

args = append(args, opts.GetStrArguments()...)
outputLength := len(t.output)
optionsLength := len(t.options)



if outputLength == 1 && optionsLength == 0 {
// Just append the 1 output file we've got
args = append(args, t.output[0])
@@ -81,17 +91,22 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
}
}

//progressOpts := []string{"-loglevel panic", "-nostats", "-progress -"}
//args = append(args, progressOpts...)
//a := strings.Join(args, " ")
//print(a)
// Initialize command
cmd := exec.Command(t.config.FfmpegBinPath, args...)

// If progresss enabled, get stderr pipe and start progress process
if t.config.ProgressEnabled && !t.config.Verbose {
stderrIn, err = cmd.StderrPipe()
stderrIn, err = cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("Failed getting transcoding progress (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err)
}
}


if t.config.Verbose {
cmd.Stderr = os.Stdout
}
@@ -105,6 +120,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
if t.config.ProgressEnabled && !t.config.Verbose {
go func() {
t.progress(stderrIn, out)
//t.progress2(stderrIn)
}()

go func() {
@@ -118,6 +134,11 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
return out, nil
}

func (t *Transcoder) progress2(stream io.ReadCloser) {
msg, _ := ioutil.ReadAll(stream)
fmt.Printf("%s\n", msg)
}

// Input ...
func (t *Transcoder) Input(arg string) transcoder.Transcoder {
t.input = arg
@@ -258,9 +279,13 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
buf := make([]byte, 2)
scanner.Buffer(buf, bufio.MaxScanTokenSize)

//var re = regexp.MustCompile(`=\s+`)

progress := &Progress{}
for scanner.Scan() {
Progress := new(Progress)
line := scanner.Text()
//fmt.Println(line)

if strings.Contains(line, "frame=") && strings.Contains(line, "time=") && strings.Contains(line, "bitrate=") {
var re = regexp.MustCompile(`=\s+`)
@@ -311,6 +336,104 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress

out <- *Progress
}

/*
bitrate= 256.1kbits/s
total_size=410220
out_time_us=12816000
out_time_ms=12816000
out_time=00:00:12.816000
dup_frames=0
drop_frames=0
speed= 789x
progress=end
*/

if strings.Contains(line, "bitrate=") {
//st := re.ReplaceAllString(line, `=`)
removedEqualSign := strings.ReplaceAll(line, "=", " ")
f := strings.Fields(removedEqualSign)

progress.CurrentBitrate = f[1]
}

var totalSize int64
if strings.Contains(line, "total_size=") {
//st := re.ReplaceAllString(line, `=`)
removedEqualSign := strings.ReplaceAll(line, "=", " ")
f := strings.Fields(removedEqualSign)
ts, err := strconv.ParseInt(f[1], 10, 64)
if err != nil {
totalSize = 0
} else {
totalSize = ts
}
}

if strings.Contains(line, "out_time") {
//st := re.ReplaceAllString(line, `=`)
removedEqualSign := strings.ReplaceAll(line, "=", " ")
f := strings.Fields(removedEqualSign)

//progress := (timesec * 100) / dursec
//ms, err := strconv.ParseInt(f[1], 10, 64)
progress.CurrentTime = f[1]
}

if strings.Contains(line, "out_time") {
//st := re.ReplaceAllString(line, `=`)
removedEqualSign := strings.ReplaceAll(line, "=", " ")
f := strings.Fields(removedEqualSign)

//progress := (timesec * 100) / dursec
//ms, err := strconv.ParseInt(f[1], 10, 64)
progress.CurrentTime = f[1]
}

if strings.Contains(line, "speed") {
//st := re.ReplaceAllString(line, `=`)
removedEqualSign := strings.ReplaceAll(line, "=", " ")
f := strings.Fields(removedEqualSign)

//progress := (timesec * 100) / dursec
//ms, err := strconv.ParseInt(f[1], 10, 64)
progress.Speed = f[1]
}

if strings.Contains(line, "progress") {
//st := re.ReplaceAllString(line, `=`)
removedEqualSign := strings.ReplaceAll(line, "=", " ")
f := strings.Fields(removedEqualSign)

//progress := (timesec * 100) / dursec
//ms, err := strconv.ParseInt(f[1], 10, 64)
if len(f) == 2 {
if f[1] == "continue" {
size, err := strconv.ParseInt(t.metadata.GetFormat().GetSize(), 10, 64)
if err != nil {
progress.Progress = 0.0
} else {

}

if totalSize > 0 {
progr := (size/totalSize)*100
progress.Progress = float64(progr)
} else {
progress.Progress = 0.0
}

}
if f[1] == "end" {
progress.Progress = 1.0
}
} else {
progress.Progress = 0.0
}


out <- *progress
}
}
}

8 changes: 7 additions & 1 deletion ffmpeg/metadata.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ffmpeg

import "github.com/floostack/transcoder"
import "github.com/sashker/transcoder"

// Metadata ...
type Metadata struct {
@@ -29,6 +29,7 @@ type Streams struct {
CodecName string `json:"codec_name"`
CodecLongName string `json:"codec_long_name"`
Profile string `json:"profile"`
Channels int `json:"channels"`
CodecType string `json:"codec_type"`
CodecTimeBase string `json:"codec_time_base"`
CodecTagString string `json:"codec_tag_string"`
@@ -167,6 +168,11 @@ func (s Streams) GetProfile() string {
return s.Profile
}

//GetNbChannels returns number of channels for the stream
func (s Streams) GetNbChannels() int {
return s.Channels
}

//GetCodecType ...
func (s Streams) GetCodecType() string {
return s.CodecType
5 changes: 3 additions & 2 deletions ffmpeg/options.go
Original file line number Diff line number Diff line change
@@ -65,6 +65,7 @@ type Options struct {
PixFmt *string `flag:"-pix_fmt"`
WhiteListProtocols []string `flag:"-protocol_whitelist"`
Overwrite *bool `flag:"-y"`
MapChannel *string `flag:"-map_channel"`
ExtraArgs map[string]interface{}
}

@@ -102,7 +103,7 @@ func (opts Options) GetStrArguments() []string {
values = append(values, flag, fmt.Sprintf("%v:%v", k, v))
}
}

if vi, ok := value.(*int); ok {
values = append(values, flag, fmt.Sprintf("%d", *vi))
}
@@ -111,4 +112,4 @@ func (opts Options) GetStrArguments() []string {
}

return values
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/floostack/transcoder
module github.com/sashker/transcoder

go 1.13
1 change: 1 addition & 0 deletions metadata.go
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ type Streams interface {
GetCodecName() string
GetCodecLongName() string
GetProfile() string
GetNbChannels() int
GetCodecType() string
GetCodecTimeBase() string
GetCodecTagString() string