简体   繁体   中英

extracting frames using FFMPEG?

I'm trying to write code to extract 16 frames from a video file using ffmpeg (or openCV - open to suggestions) and python but I'm having a lot of trouble figuring out how to get started.
Can I use ffmpeg command lines directly in python? If so, are there commands to get the length of the clip and frame rate? How can I pull frames at equal intervals?

Any help would be appreciated! Thank you!

I can get metadata info by using ffprobe, included with ffmpeg.

The Module Subprocess is a way to use tools outside python and return exit status or check Output like in my example below. Then you can assign python variables to the parsed output of ffprobe. There are security Issues with using shell=True. This will at least get you started, and you can figure out a way to avoid using it later on.

import subprocess

dur_check = subprocess.check_output('ffprobe -v error -show_format We\ Code\ Hard.mp4 | grep duration')
duration = dur_check.split('=')[1]

Which will return:

'192.631667'

And then, a little bit different for fps.

fps_check = subprocess.check_output('ffprobe We\ Code\ Hard.mp4 2>&1 | grep fps',shell=True)
fps = fps_check.split(' ')[4].strip()

Which Returns:

'29.97 fps'

Can I use ffmpeg command lines directly in python?

Yes. I have no experience with Python, but tink3r's answer looks like an example to me.

If so, are there commands to get the length of the clip and frame rate?

Yes. Use ffprobe .

$ ffprobe -v error -select_streams v:0 -show_entries stream=duration,avg_frame_rate -of default=nw=1:nk=0 input.mp4
avg_frame_rate=24/1
duration=888.000000
  • This example will get the desired info from the first video stream only; otherwise it will also show entries for audio, etc.

  • As the example shows there is no need for additional processes such as grep , sed , awk or anything like that.

  • If you want to remove the avg_frame_rate= and duration= keys then change nk=0 to nk=1 .

  • Python people like JSON format, right? If that's the case then you can change -of default=nw=1:nk=0 to -of json .

  • See FFmpeg Wiki: FFprobe Tips for more examples.

How can I pull frames at equal intervals?

You can use the select filter . Example to select one frame every ten seconds:

ffmpeg -i -vf "select='not(mod(t\,10))'" -vsync vfr output_%03d.png

This will result in output_001.png , output_002.png , output_003.png , etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM