From ff24be31548a760298fa982ba1e9e24597a1cbec Mon Sep 17 00:00:00 2001 From: Alex Eidt Date: Thu, 2 Dec 2021 10:44:42 -0800 Subject: [PATCH] Fixed bitrate on VideoWriter --- video.go | 4 ++-- videowriter.go | 11 +++-------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/video.go b/video.go index 334cb33..f43a128 100644 --- a/video.go +++ b/video.go @@ -29,7 +29,7 @@ func NewVideo(filename string) *Video { } // Execute ffmpeg -i command to get video information. cmd := exec.Command("ffmpeg", "-i", filename, "-") - // FFMPEG output piped to Stderr. + // ffmpeg output piped to Stderr. pipe, err := cmd.StderrPipe() if err != nil { panic(err) @@ -47,7 +47,7 @@ func NewVideo(filename string) *Video { } } cmd.Wait() - video := &Video{filename: filename, channels: 3, pipe: nil, framebuffer: nil} + video := &Video{filename: filename, channels: 3} parseFFMPEGHeader(video, string(buffer)) return video } diff --git a/videowriter.go b/videowriter.go index 9073cbf..287b2d0 100644 --- a/videowriter.go +++ b/videowriter.go @@ -13,7 +13,6 @@ type VideoWriter struct { filename string width int height int - channels int bitrate int fps float64 codec string @@ -26,9 +25,6 @@ func NewVideoWriter(filename string, video *Video) *VideoWriter { if video.width == 0 || video.height == 0 { panic("Video width and height must be set.") } - if video.channels == 0 { - video.channels = 3 // Default to RGB frames. - } if video.fps == 0 { video.fps = 25 // Default to 25 FPS. } @@ -36,7 +32,6 @@ func NewVideoWriter(filename string, video *Video) *VideoWriter { filename: filename, width: video.width, height: video.height, - channels: video.channels, bitrate: video.bitrate, fps: video.fps, codec: "mpeg4", @@ -55,11 +50,11 @@ func (writer *VideoWriter) initVideoWriter() { "-vcodec", "rawvideo", "-s", fmt.Sprintf("%dx%d", writer.width, writer.height), // frame w x h "-pix_fmt", writer.pix_fmt, - //"-b:v", fmt.Sprintf("%d", writer.bitrate), // bitrate "-r", fmt.Sprintf("%f", writer.fps), // frames per second "-i", "-", // The imput comes from stdin - "-an", // Tells FFMPEG not to expect any audio + "-an", // Tells ffmpeg not to expect any audio "-vcodec", writer.codec, + "-b:v", fmt.Sprintf("%dk", writer.bitrate), // bitrate writer.filename, ) writer.cmd = cmd @@ -83,7 +78,7 @@ func (writer *VideoWriter) Write(frame []byte) { for total < len(frame) { n, err := (*writer.pipe).Write(frame[total:]) if err != nil { - fmt.Println("Likely cause is invalid parameters to FFMPEG.") + defer fmt.Println("Likely cause is invalid parameters to ffmpeg.") panic(err) } total += n