1 changed files with 69 additions and 12 deletions
@ -1,32 +1,89 @@ |
|||
package ffmpeg |
|||
|
|||
import ( |
|||
"bufio" |
|||
"errors" |
|||
"fmt" |
|||
"io" |
|||
"os" |
|||
"os/exec" |
|||
"path/filepath" |
|||
) |
|||
|
|||
func Check(file string) error { |
|||
type HWAccel string |
|||
|
|||
const ( |
|||
HWAccelQSV HWAccel = "qsv" |
|||
HWAccelAuto HWAccel = "auto" |
|||
HWAccelD3D11VA HWAccel = "d3d11va" |
|||
HWAccelDXVA2 HWAccel = "dxva2" |
|||
HWAccelCUDA HWAccel = "cuda" |
|||
HWAccelVAAPI HWAccel = "vaapi" |
|||
) |
|||
|
|||
func Check(file string, hwaccel ...HWAccel) error { |
|||
stat, err := os.Stat(file) |
|||
if err != nil || stat.IsDir() { |
|||
return errors.New("file not found") |
|||
} |
|||
var args []string |
|||
if len(hwaccel) != 0 { |
|||
args = []string{ |
|||
`-v`, |
|||
`error`, |
|||
`-hwaccel`, |
|||
string(hwaccel[0]), |
|||
`-i`, |
|||
file, |
|||
`-f`, |
|||
`null`, |
|||
`-`, |
|||
} |
|||
} else { |
|||
args = []string{ |
|||
`-v`, |
|||
`error`, |
|||
`-i`, |
|||
file, |
|||
`-f`, |
|||
`null`, |
|||
`-`, |
|||
} |
|||
} |
|||
appPath := filepath.Join( |
|||
ffmpegPath, |
|||
`ffmpeg`, |
|||
) |
|||
// ffmpeg -v error -i your_video_file.mp4 -f null -
|
|||
cmd := exec.Command( |
|||
appPath, |
|||
`-v`, |
|||
`error`, |
|||
`-i`, |
|||
file, |
|||
`-f`, |
|||
`null`, |
|||
`-`, |
|||
appPath, args..., |
|||
) |
|||
err = cmd.Run() |
|||
return err |
|||
var pipe io.ReadCloser |
|||
pipe, err = cmd.StderrPipe() |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
defer func(pipe io.ReadCloser) { |
|||
_ = pipe.Close() |
|||
}(pipe) |
|||
if err = cmd.Start(); err != nil { |
|||
panic(err) |
|||
} |
|||
errCh := make(chan bool) |
|||
go func() { |
|||
scanner := bufio.NewScanner(pipe) |
|||
for scanner.Scan() { |
|||
errCh <- true |
|||
} |
|||
}() |
|||
go func() { |
|||
if err = cmd.Wait(); err != nil { |
|||
errCh <- true |
|||
return |
|||
} |
|||
errCh <- false |
|||
}() |
|||
if failed := <-errCh; failed { |
|||
return fmt.Errorf("媒体文件损坏:%s", filepath.Base(file)) |
|||
} |
|||
return nil |
|||
} |
|||
|
Loading…
Reference in new issue