mirror of
https://github.com/u2takey/ffmpeg-go.git
synced 2025-10-04 15:42:41 +08:00
and stream example
This commit is contained in:
7
examples/go.mod
Normal file
7
examples/go.mod
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
module github.com/u2takey/ffmpeg-go/examples
|
||||||
|
|
||||||
|
go 1.14
|
||||||
|
|
||||||
|
replace github.com/u2takey/ffmpeg-go => ../../ffmpeg-go
|
||||||
|
|
||||||
|
require github.com/u2takey/ffmpeg-go v0.0.0-00010101000000-000000000000
|
44
examples/go.sum
Normal file
44
examples/go.sum
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
|
||||||
|
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||||
|
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
|
||||||
|
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||||
|
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||||
|
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||||
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
|
||||||
|
github.com/opencontainers/selinux v1.5.2/go.mod h1:yTcKuYAh6R95iDpefGLQaPaRwJFwyzAJufJyiTt7s0g=
|
||||||
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
|
||||||
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||||
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
github.com/tidwall/gjson v1.6.3 h1:aHoiiem0dr7GHkW001T1SMTJ7X5PvyekH5WX0whWGnI=
|
||||||
|
github.com/tidwall/gjson v1.6.3/go.mod h1:BaHyNc5bjzYkPqgLq7mdVzeiRtULKULXLgZFKsxEHI0=
|
||||||
|
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
|
||||||
|
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
|
||||||
|
github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU=
|
||||||
|
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
|
||||||
|
github.com/u2takey/go-utils v0.0.0-20200713025200-4704d09fc2c7 h1:PT7mE8HJE1mwaSazrOdSeByJ1FoV33/fHUZrBB+zwVU=
|
||||||
|
github.com/u2takey/go-utils v0.0.0-20200713025200-4704d09fc2c7/go.mod h1:ATqKFpgjUIlhGRs8j59gXmu8Cmpo1QQEHV6vwu1hs28=
|
||||||
|
golang.org/x/sys v0.0.0-20191115151921-52ab43148777/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
|
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
|
BIN
examples/in1.mp4
Normal file
BIN
examples/in1.mp4
Normal file
Binary file not shown.
155
examples/stream.go
Normal file
155
examples/stream.go
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
ffmpeg "github.com/u2takey/ffmpeg-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
InputFile = flag.String("in_filename", "./in1.mp4", "Input filename")
|
||||||
|
OutputFile = flag.String("out_filename", "./out.mp4", "Input filename")
|
||||||
|
Dream = flag.Bool("dream", false, "Use DeepDream frame processing (requires tensorflow)")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Buffer is a goroutine safe bytes.Buffer
|
||||||
|
type Buffer struct {
|
||||||
|
buffer bytes.Buffer
|
||||||
|
mutex sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Buffer) Write(p []byte) (n int, err error) {
|
||||||
|
s.mutex.Lock()
|
||||||
|
defer s.mutex.Unlock()
|
||||||
|
return s.buffer.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Buffer) Read(p []byte) (n int, err error) {
|
||||||
|
s.mutex.Lock()
|
||||||
|
defer s.mutex.Unlock()
|
||||||
|
return s.buffer.Read(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getVideoSize(fileName string) (int, int) {
|
||||||
|
log.Println("Getting video size for", fileName)
|
||||||
|
data, err := ffmpeg.Probe(fileName)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
log.Println("got video info", data)
|
||||||
|
type VideoInfo struct {
|
||||||
|
Streams []struct {
|
||||||
|
CodecType string `json:"codec_type"`
|
||||||
|
Width int
|
||||||
|
Height int
|
||||||
|
} `json:"streams"`
|
||||||
|
}
|
||||||
|
vInfo := &VideoInfo{}
|
||||||
|
err = json.Unmarshal([]byte(data), vInfo)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
for _, s := range vInfo.Streams {
|
||||||
|
if s.CodecType == "video" {
|
||||||
|
return s.Width, s.Height
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func startFFmpegProcess1(infileName string, writer io.WriteCloser) <-chan error {
|
||||||
|
log.Println("Starting ffmpeg process1")
|
||||||
|
done := make(chan error)
|
||||||
|
go func() {
|
||||||
|
err := ffmpeg.Input(infileName).
|
||||||
|
Output("pipe:",
|
||||||
|
ffmpeg.KwArgs{
|
||||||
|
"format": "rawvideo", "pix_fmt": "rgb24",
|
||||||
|
}).
|
||||||
|
WithOutput(writer).
|
||||||
|
Run()
|
||||||
|
log.Println("ffmpeg process1 done")
|
||||||
|
_ = writer.Close()
|
||||||
|
done <- err
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
return done
|
||||||
|
}
|
||||||
|
|
||||||
|
func startFFmpegProcess2(outfileName string, buf io.Reader, width, height int) <-chan error {
|
||||||
|
log.Println("Starting ffmpeg process2")
|
||||||
|
done := make(chan error)
|
||||||
|
go func() {
|
||||||
|
err := ffmpeg.Input("pipe:",
|
||||||
|
ffmpeg.KwArgs{"format": "rawvideo",
|
||||||
|
"pix_fmt": "rgb24", "s": fmt.Sprintf("%dx%d", width, height),
|
||||||
|
}).
|
||||||
|
Output(outfileName, ffmpeg.KwArgs{"pix_fmt": "yuv420p"}).
|
||||||
|
OverWriteOutput().
|
||||||
|
WithInput(buf).
|
||||||
|
Run()
|
||||||
|
log.Println("ffmpeg process2 done")
|
||||||
|
done <- err
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
return done
|
||||||
|
}
|
||||||
|
|
||||||
|
func process(reader io.ReadCloser, writer io.WriteCloser, w, h int) {
|
||||||
|
go func() {
|
||||||
|
frameSize := w * h * 3
|
||||||
|
buf := make([]byte, frameSize, frameSize)
|
||||||
|
for {
|
||||||
|
n, err := io.ReadFull(reader, buf)
|
||||||
|
if n == 0 || err == io.EOF {
|
||||||
|
_ = writer.Close()
|
||||||
|
return
|
||||||
|
} else if n != frameSize || err != nil {
|
||||||
|
panic(fmt.Sprintf("read error: %d, %s", n, err))
|
||||||
|
}
|
||||||
|
for i := range buf {
|
||||||
|
buf[i] = buf[i] / 3
|
||||||
|
}
|
||||||
|
n, err = writer.Write(buf)
|
||||||
|
if n != frameSize || err != nil {
|
||||||
|
panic(fmt.Sprintf("write error: %d, %s", n, err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func run(inFile, outFile string) {
|
||||||
|
w, h := getVideoSize(inFile)
|
||||||
|
log.Println(w, h)
|
||||||
|
|
||||||
|
pr1, pw1 := io.Pipe()
|
||||||
|
pr2, pw2 := io.Pipe()
|
||||||
|
done1 := startFFmpegProcess1(inFile, pw1)
|
||||||
|
process(pr1, pw2, w, h)
|
||||||
|
done2 := startFFmpegProcess2(outFile, pr2, w, h)
|
||||||
|
err := <-done1
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
err = <-done2
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
log.Println("Done")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
if *Dream == true {
|
||||||
|
fmt.Println("tensorflow mode not implemented, todo")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
run(*InputFile, *OutputFile)
|
||||||
|
}
|
4
probe.go
4
probe.go
@@ -15,8 +15,8 @@ import (
|
|||||||
// The stderr output can be retrieved by accessing the
|
// The stderr output can be retrieved by accessing the
|
||||||
// ``stderr`` property of the exception.
|
// ``stderr`` property of the exception.
|
||||||
|
|
||||||
func Probe(fileName string, kwargs KwArgs) (string, error) {
|
func Probe(fileName string, kwargs ...KwArgs) (string, error) {
|
||||||
return ProbeWithTimeout(fileName, 0, kwargs)
|
return ProbeWithTimeout(fileName, 0, MergeKwArgs(kwargs))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProbeWithTimeout(fileName string, timeOut time.Duration, kwargs KwArgs) (string, error) {
|
func ProbeWithTimeout(fileName string, timeOut time.Duration, kwargs KwArgs) (string, error) {
|
||||||
|
Reference in New Issue
Block a user