FFmpeg I-P frame command in golang

Issue

I have been using the command below to get a specific frame from the video and get it into a buffer.

func ReadFrameAsJpeg(inFileName string, frameNum int) []byte {
    // Returns specified frame as []byte
    buf := bytes.NewBuffer(nil)
    err := ffmpeg.Input(inFileName).
        Filter("select", ffmpeg.Args{fmt.Sprintf("gte(n,%d)", frameNum)}).
        Output("pipe:", ffmpeg.KwArgs{"vframes": 1, "format": "image2", "vcodec": "mjpeg"}).
        WithOutput(buf, os.Stdout).Run()
    if err != nil {
        fmt.Println(err)
        panic(err)
    }

While getting a specific frame according to a FrameNum, I want to also check which type of frame it is. Like using "pict_type" to get that information. I tried using a filter to get the frame type below, but it showed "error parsing the argument". It should give the output with the "pict_type = P" or "pict_type = I"

Filter("select", ffmpeg.Args{fmt.Sprintf("eq(n,%d),showinfo", frameNum)}).

I am trying to implement the following command

$ ffmpeg -hide_banner -i INPUT.mp4 -filter:v "select=eq('n,3344'),showinfo" -frames:v 1 -map 0:v:0 -f null -

Solution

You are specifying showinfo as a part of select filter options, instead of defining 2 different filters. Assuming that you are using this library, you need to do something like this:

ffmpeg.Input(inFileName)
  .Filter("select", ffmpeg.Args{fmt.Sprintf("eq(n,%d)", frameNum)})
  .Filter("showinfo")
  .Output(...)...

I’m not familiar with go language so there maybe a syntax issue that you may need to sort out.

Answered By – kesh

Answer Checked By – Marilyn (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.