3d0c / gmf

Go Media Framework
MIT License
885 stars 170 forks source link

How can I get one image file from video by per seconds? #118

Open hanluner opened 5 years ago

hanluner commented 5 years ago

hi, May I ask a question, How can I get image file from video by per seconds? Is there have any example code? Thank u very much. My English is very poor.

hanluner commented 5 years ago

@3d0c Can you help me?

3d0c commented 5 years ago

Hi, @hanluner

To get a current intput stream duration in seconds (to get a current second) you can use (*Packet).Time() method, e.g.:

currSec = pkt.Time(ist.TimeBase())

read this code snippet whithin the video-to-goImage.go example context.

So, you've got a packet — you can count a current duration in seconds, then just skip extra packets you don't need to decode and dump.

But, remember that not always packet == frame, so if you need better precision, you can decode a packet, count a frames and skip/get them according TimeBase.

hanluner commented 5 years ago

@3d0c Thanks, I'll try.

hanluner commented 5 years ago

Hi, @3d0c I'm still confused of several details related to packet. What is the time unit of each packet? I.e. We transfer the video stream into packets by the unit of second or micro-second?

3d0c commented 5 years ago

It returns seconds, calculated according timebase and current packet pts.

func (p *Packet) Time(timebase AVRational) int {
    return int(float64(timebase.AVR().Num) / float64(timebase.AVR().Den) * float64(p.Pts()))
}