mirror of
https://github.com/u2takey/ffmpeg-go.git
synced 2025-10-05 07:56:51 +08:00
add example showProgress
This commit is contained in:
83
examples/showProgress.go
Normal file
83
examples/showProgress.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user