gen2brain/x264-go

Can you add crf to Option struct?

xiaonuoz opened this issue · 1 comments

package main

import (
	"fmt"
	"log"
	"os"
	"time"

	x264 "github.com/gen2brain/x264-go"
	"github.com/kbinani/screenshot"
)

func main() {
	n := screenshot.NumActiveDisplays()
	for i := 0; i < n; i++ {
		save(i)
	}

}

func save(i int) {
	file, err := os.Create(fmt.Sprintf("%v_screen.264", i))
	if err != nil {
		log.Println("Create err:", err)
		os.Exit(1)
	}

	bounds := screenshot.GetDisplayBounds(i)

	opts := &x264.Options{
		Width:     bounds.Dx(),
		Height:    bounds.Dy(),
		FrameRate: 3,
		Tune:      "film",
		Preset:    "faster",
		Profile:   "baseline",
		LogLevel:  x264.LogError,
	}

	enc, err := x264.NewEncoder(file, opts)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err.Error())
		os.Exit(1)
	}

	defer enc.Close()

	timer := time.NewTimer(5 * time.Second)
	defer timer.Stop()

	for {
		select {
		case <-timer.C:
			enc.Flush()

			err = file.Close()
			if err != nil {
				log.Println("Close err:", err)
				os.Exit(1)
			}

			return
		default:
			img, err := screenshot.CaptureRect(bounds)
			if err != nil {
				log.Println("CaptureRect err:", err)

				continue
			}

			err = enc.Encode(img)
			if err != nil {
				log.Println("Encode err:", err)
			}
			// When sleep() is removed, the file size multiplies
			time.Sleep(300 * time.Millisecond)
		}
	}
}

The current file size is around 1500kb. I don't care about the video quality. I hope I can further compress the size without affecting performance too much

Rate control options are exposed in 5e0d7bf.