package main
import (
"fmt"
"io"
"os"
"syscall"
"unsafe"
ffmpeg "github.com/qrtc/ffmpeg-dev-go"
)
const (
INBUF_SIZE = 4096
)
func pgmSave(buf *uint8, wrap, xsize, ysize int32, filename string) {
f, _ := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
fmt.Fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255)
bufSlice := unsafe.Slice(buf, xsize*ysize)
for i := int32(0); i < ysize; i++ {
f.Write(bufSlice[i+wrap : i+wrap+xsize])
}
f.Close()
}
func decode(decCtx *ffmpeg.AVCodecContext, frame *ffmpeg.AVFrame, pkt *ffmpeg.AVPacket, filename string) {
ret := ffmpeg.AvCodecSendPacket(decCtx, pkt)
if ret < 0 {
fmt.Fprintf(os.Stderr, "Error sending a packet for decoding\n")
os.Exit(1)
}
for ret >= 0 {
ret = ffmpeg.AvCodecReceiveFrame(decCtx, frame)
if ret == ffmpeg.AVERROR(syscall.EAGAIN) || ret == ffmpeg.AVERROR_EOF {
return
} else if ret < 0 {
fmt.Fprintf(os.Stderr, "Error during decoding\n")
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "saving frame %3d\n", decCtx.GetFrameNumber())
// the picture is allocated by the decoder. no need to free it
fname := fmt.Sprintf("%s-%d", filename, decCtx.GetFrameNumber())
pgmSave(frame.GetData()[0], frame.GetLinesize()[0], frame.GetWidth(), frame.GetHeight(), fname)
}
}
func main() {
if len(os.Args) <= 2 {
fmt.Fprintf(os.Stdout, "Usage: %s