From c66b90b8e07e014ed483c38469aa83a145293fa8 Mon Sep 17 00:00:00 2001 From: "wanglei.w" Date: Thu, 12 Nov 2020 12:05:45 +0800 Subject: [PATCH] add example showProgress --- examples/example_test.go | 6 ++- examples/showProgress.go | 83 ++++++++++++++++++++++++++++++++++++++++ examples/stream.go | 36 ++++++++--------- filters.go | 1 + 4 files changed, 107 insertions(+), 19 deletions(-) create mode 100644 examples/showProgress.go diff --git a/examples/example_test.go b/examples/example_test.go index d115dfb..d7e0903 100644 --- a/examples/example_test.go +++ b/examples/example_test.go @@ -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") +} diff --git a/examples/showProgress.go b/examples/showProgress.go new file mode 100644 index 0000000..daab0c3 --- /dev/null +++ b/examples/showProgress.go @@ -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 +} diff --git a/examples/stream.go b/examples/stream.go index 92e8055..9c4bdcc 100644 --- a/examples/stream.go +++ b/examples/stream.go @@ -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) -} diff --git a/filters.go b/filters.go index ffaf7a3..ad5c6a0 100644 --- a/filters.go +++ b/filters.go @@ -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("", "")