Skip to content

Commit

Permalink
switch to using libav go-bindings for decoding stream info
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Mar 5, 2018
1 parent 3736366 commit 76016ad
Show file tree
Hide file tree
Showing 26 changed files with 9,036 additions and 43 deletions.
Binary file removed lib/darwin/x86_64/ffprobe
Binary file not shown.
Binary file removed lib/linux/x86_64/ffprobe
Binary file not shown.
Binary file removed lib/windows/x86_64/ffprobe.exe
Binary file not shown.
58 changes: 15 additions & 43 deletions pkg/stream/stream.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package stream

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"time"

"github.com/imkira/go-libav/avformat"

apiconfig "github.com/juanvallejo/streaming-server/pkg/api/config"
api "github.com/juanvallejo/streaming-server/pkg/api/types"
pathutil "github.com/juanvallejo/streaming-server/pkg/server/path"
Expand All @@ -25,8 +23,6 @@ const (
STREAM_TYPE_LOCAL = "movie"
STREAM_TYPE_TWITCH = "twitch"
STREAM_TYPE_TWITCH_CLIP = "twitch#clip"

DEFAULT_LIB_AV_BIN = "ffprobe" // used to extract local media file metadata
)

type StreamMetadataCallback func(Stream, []byte, error)
Expand Down Expand Up @@ -386,17 +382,9 @@ func (s *YouTubeStream) FetchMetadata(callback StreamMetadataCallback) {
// a local filepath.
type LocalVideoStream struct {
*StreamSchema

libAvRootPath string
libAvFile string
}

func (s *LocalVideoStream) FetchMetadata(callback StreamMetadataCallback) {
if len(s.libAvRootPath) == 0 {
callback(s, []byte{}, fmt.Errorf("unsupported os. Skipping local file duration calculation."))
return
}

go func(s *LocalVideoStream, callback StreamMetadataCallback) {
data, err := FetchLocalVideoMetadata(s)
if err != nil {
Expand All @@ -412,22 +400,24 @@ func (s *LocalVideoStream) FetchMetadata(callback StreamMetadataCallback) {
func FetchLocalVideoMetadata(s *LocalVideoStream) ([]byte, error) {
fpath := pathutil.StreamDataFilePathFromUrl(s.Url)

args := []string{"-v", "error", "-select_streams", "v:0", "-show_entries", "stream=duration", "-of", "default=noprint_wrappers=1:nokey=1", fpath}
command := exec.Command(s.libAvRootPath+s.libAvFile, args...)

var buff bytes.Buffer
command.Stdout = &buff

err := command.Run()
// open format (container) context
decFmt, err := avformat.NewContextForInput()
if err != nil {
return []byte{}, err
return nil, fmt.Errorf("error decoding stream information: %v", err)
}

duration, err := strconv.ParseFloat(strings.Trim(buff.String(), "\n"), 32)
if err != nil {
return []byte{}, err
// open file for decoding
if err := decFmt.OpenInput(fpath, nil, nil); err != nil {
return nil, fmt.Errorf("error decoding stream information: %v", err)
}

// initialize context with stream information
if err := decFmt.FindStreamInfo(nil); err != nil {
return nil, fmt.Errorf("error decoding stream information: %v", err)
}

// we receive duration in microseconds, convert to seconds
duration := float64(decFmt.Duration()) / float64(1000000)
kv := map[string]interface{}{
"duration": duration,
}
Expand Down Expand Up @@ -655,30 +645,12 @@ func NewTwitchClipStream(videoUrl string) Stream {
}

func NewLocalVideoStream(filepath string) Stream {
libAvBin := DEFAULT_LIB_AV_BIN

ops := runtime.GOOS
avRootPath := "lib/linux/x86_64/"
if ops == "windows" {
avRootPath = "lib/windows/x86_64/"
libAvBin = libAvBin + ".exe"
} else if ops == "darwin" {
avRootPath = "lib/darwin/x86_64/"
} else {
if ops != "linux" {
avRootPath = ""
}
}

return &LocalVideoStream{
StreamSchema: &StreamSchema{
Url: filepath,
Kind: STREAM_TYPE_LOCAL,
Meta: NewStreamMeta(),
},

libAvRootPath: avRootPath,
libAvFile: libAvBin,
}
}

Expand Down
8 changes: 8 additions & 0 deletions vendor/github.com/imkira/go-libav/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions vendor/github.com/imkira/go-libav/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/imkira/go-libav/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions vendor/github.com/imkira/go-libav/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions vendor/github.com/imkira/go-libav/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 76016ad

Please sign in to comment.