-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg.go
91 lines (74 loc) · 1.65 KB
/
ffmpeg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package goarstream
import (
"bufio"
"bytes"
"fmt"
"image"
"image/png"
"io"
"io/ioutil"
"os"
"os/exec"
"strconv"
)
//Trying to get png from stdout pipe
func ffmpeg() (bufStdin *bufio.Writer, stderr io.ReadCloser, stdout io.ReadCloser, err error) {
//ffmpegCmd := exec.Command("ffmpeg", "-re", "-f", "h264", "-i", "pipe:0", "-f", "webm", "pipe:1")
ffmpegCmd := exec.Command("ffmpeg", "-i", "-", "-f", "image2pipe", "png", "pipe:1")
//ffmpegCmd := exec.Command("ffmpeg", "-i", "pipe:0", "-f", "image2pipe", "-codec:v", "png", "-strict", "-2", "$out > /dev/null 2>&1")
//ffmpegCmd := exec.Command("ffplay", "-f", "h264", "-i", "pipe:0")
//ffmpegCmd.Stdin = os.Stdin
//ffmpegCmd.Stdout = os.Stdout
stderr, err = ffmpegCmd.StderrPipe()
if err != nil {
return
}
stdin, err := ffmpegCmd.StdinPipe()
if err != nil {
return
}
bufStdin = bufio.NewWriter(stdin)
stdout, err = ffmpegCmd.StdoutPipe()
if err != nil {
return
}
go func() {
for {
buf, err := ioutil.ReadAll(stderr)
if err != nil {
fmt.Println(err)
}
if len(buf) > 0 {
fmt.Println(string(buf))
}
}
}()
go func() {
for {
count := 0
buf, err := ioutil.ReadAll(stdout)
if err != nil {
fmt.Println(err)
}
if len(buf) > 0 {
img, _, _ := image.Decode(bytes.NewReader(buf))
//save the imgByte to file
out, err := os.Create("./img-" + strconv.Itoa(count) + ".png")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = png.Encode(out, img)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
}()
err = ffmpegCmd.Start()
if err != nil {
fmt.Println(err)
}
return bufStdin, stderr, nil, nil
}