add example showProgress

This commit is contained in:
wanglei.w
2020-11-12 12:05:45 +08:00
parent 003ae6618e
commit c66b90b8e0
4 changed files with 107 additions and 19 deletions

View File

@@ -10,7 +10,7 @@ func TestExampleStream(t *testing.T) {
ExampleStream("./sample_data/in1.mp4", "./sample_data/out1.mp4", false)
}
func TestExampleReadFramAsJpeg(t *testing.T) {
func TestExampleReadFrameAsJpeg(t *testing.T) {
reader := ExampleReadFrameAsJpeg("./sample_data/in1.mp4", 5)
img, err := imaging.Decode(reader)
if err != nil {
@@ -21,3 +21,7 @@ func TestExampleReadFramAsJpeg(t *testing.T) {
t.Fatal(err)
}
}
func TestExampleShowProgess(t *testing.T) {
ExampleShowProgess("./sample_data/in1.mp4", "./sample_data/out2.mp4")
}

83
examples/showProgress.go Normal file
View File

@@ -0,0 +1,83 @@
package examples
import (
"fmt"
"log"
"math/rand"
"net"
"os"
"path"
"regexp"
"strconv"
"strings"
"time"
"github.com/tidwall/gjson"
ffmpeg "github.com/u2takey/ffmpeg-go"
)
// ExampleShowProgess is an example of using the ffmpeg `-progress` option with a
// unix-domain socket to report progress
func ExampleShowProgess(inFileName, outFileName string) {
a, err := ffmpeg.Probe(inFileName)
if err != nil {
panic(err)
}
totalDuration := gjson.Get(a, "format.duration").Float()
err = ffmpeg.Input(inFileName).
Output(outFileName, ffmpeg.KwArgs{"c:v": "libx264", "preset": "veryslow"}).
GlobalArgs("-progress", "unix://"+TempSock(totalDuration)).
OverWriteOutput().
Run()
if err != nil {
panic(err)
}
}
func TempSock(totalDuration float64) string {
// serve
rand.Seed(time.Now().Unix())
sockFileName := path.Join(os.TempDir(), fmt.Sprintf("%d_sock", rand.Int()))
l, err := net.Listen("unix", sockFileName)
if err != nil {
panic(err)
}
go func() {
re := regexp.MustCompile(`out_time_ms=(\d+)`)
fd, err := l.Accept()
if err != nil {
log.Fatal("accept error:", err)
}
buf := make([]byte, 16)
data := ""
progress := ""
for {
_, err := fd.Read(buf)
if err != nil {
return
}
data += string(buf)
a := re.FindAllStringSubmatch(data, -1)
cp := ""
if len(a) > 0 && len(a[len(a)-1]) > 0 {
c, _ := strconv.Atoi(a[len(a)-1][len(a[len(a)-1])-1])
cp = fmt.Sprintf("%.2f", float64(c)/totalDuration/1000000)
}
if strings.Contains(data, "progress=end") {
cp = "done"
}
if cp == "" {
cp = ".0"
}
if cp != progress {
progress = cp
fmt.Println("progress: ", progress)
}
}
}()
return sockFileName
}

View File

@@ -9,6 +9,24 @@ import (
ffmpeg "github.com/u2takey/ffmpeg-go"
)
// ExampleStream
// inFileName: input filename
// outFileName: output filename
// dream: Use DeepDream frame processing (requires tensorflow)
func ExampleStream(inFileName, outFileName string, dream bool) {
if inFileName == "" {
inFileName = "./in1.mp4"
}
if outFileName == "" {
outFileName = "./out.mp4"
}
if dream {
panic("Use DeepDream With Tensorflow haven't been implemented")
}
runExampleStream(inFileName, outFileName)
}
func getVideoSize(fileName string) (int, int) {
log.Println("Getting video size for", fileName)
data, err := ffmpeg.Probe(fileName)
@@ -117,21 +135,3 @@ func runExampleStream(inFile, outFile string) {
}
log.Println("Done")
}
// ExampleStream
// inFileName: input filename
// outFileName: output filename
// dream: Use DeepDream frame processing (requires tensorflow)
func ExampleStream(inFileName, outFileName string, dream bool) {
if inFileName == "" {
inFileName = "./in1.mp4"
}
if outFileName == "" {
outFileName = "./out.mp4"
}
if dream {
panic("Use DeepDream With Tensorflow haven't been implemented")
}
runExampleStream(inFileName, outFileName)
}

View File

@@ -128,6 +128,7 @@ func (s *Stream) Hue(kwargs ...KwArgs) *Stream {
return NewFilterNode("hue", []*Stream{s}, 1, nil, MergeKwArgs(kwargs)).Stream("", "")
}
// todo fix this
func (s *Stream) ColorChannelMixer(kwargs ...KwArgs) *Stream {
AssetType(s.Type, "FilterableStream", "colorchannelmixer")
return NewFilterNode("colorchannelmixer", []*Stream{s}, 1, nil, MergeKwArgs(kwargs)).Stream("", "")