用 vlc 替换 ffplay 的播放方式用来修复视频播放问题
This commit is contained in:
53
pkg/video_old/paly.go
Normal file
53
pkg/video_old/paly.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package video_old
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"go.uber.org/zap"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func Play(ctx context.Context, file string) error {
|
||||
if file == "" {
|
||||
zap.S().Infoln("video file is empty")
|
||||
return nil
|
||||
}
|
||||
// 判断文件是否存在
|
||||
if _, err := os.Stat(file); err != nil {
|
||||
zap.S().Errorf("视频文件不存在: %v", err)
|
||||
return err
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, "ffplay", "-autoexit", "-fs", file)
|
||||
pipe, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var wait sync.WaitGroup
|
||||
defer wait.Wait()
|
||||
|
||||
wait.Add(1)
|
||||
go func() {
|
||||
defer wait.Done()
|
||||
reader := bufio.NewReader(pipe)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
line, _, err := reader.ReadLine()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return
|
||||
}
|
||||
break
|
||||
}
|
||||
zap.L().Info(string(line))
|
||||
}
|
||||
}
|
||||
}()
|
||||
return cmd.Run()
|
||||
}
|
||||
Reference in New Issue
Block a user