<p>You can also use: </p>

<pre><code>ffmpeg -ss XXX -i input.mp4 -vframes 1 output.png
</code></pre>

<p>where XXX is the time in seconds. If you know which exactly frame you want to extract you can calculate the XXX by multiplying the number of the wanted frame * frame duration which is 1/fps. </p>

<p>Please also note that since H.264 is using I, P and B frames it would be best if you first decode your video to some intra-frame codec or raw. You can easily do that by: </p>

<pre><code>ffmpeg -i input.mp4 -vsync 0 output.y4m
</code></pre>

<p>And then you can extract the desired frame. But y4m is a format with no compression which means that the output file will be huge. You can also use ffv1 to do that since it is lossless codec, but make sure you use <code>-g 1</code> option, since you need only I-frames at the output. </p>

CC BY-SA 4.0
2018-06-07T07:44:59.307Z
2018-06-07T07:44:59.307Z
Georgi Stoyanov
<p>To extract frame # <code>x</code> through <code>y</code>, use</p>

<pre><code>ffmpeg -i in.mp4 -vf select='between(n\,x\,y)' -vsync 0 frames%d.png
</code></pre>

CC BY-SA 3.0
2016-12-08T12:18:56.810Z
Hi Mulvya, this is working great so far. Thank you again. I was wondering though, can you modify this code so that the frame numbers are printed in the name of the output file? As oppose to the current naming convention which always begins with "frame1" even if specifying, for example, to start on the 200th frame.
Jono
CC BY-SA 3.0
2016-12-09T08:38:10.733Z
You can do it manually: `ffmpeg -i in.mp4 -vf select='between(n,x,y)' -vsync 0 -start_number x frames%d.png`
Gyan
CC BY-SA 4.0
2019-02-06T14:51:49.920Z
@Gyan Its giving an error, I had to escape the commas in `between(n,x,y)` with backslash
burglarhobbit
CC BY-SA 4.0
2019-02-06T15:02:32.237Z
@burglarhobbit yes, depending on your shell, it's needed.
Gyan
CC BY-SA 4.0
2019-02-06T15:08:55.867Z
@Gyan I came to know of this from an another similar answer of yours. Please do edit it for other users convenience. I have the latest ffmpeg version.
burglarhobbit
CC BY-SA 4.0
2016-11-19T03:25:26.863Z
2019-02-06T15:09:52.557Z
2019-02-06T15:09:52.557Z
Gyan
<p>I am looking for a Windows based open source method to extract specific frames from video files (*.avi). Ideally the method will be suited to R.</p>

<p>I need to do this as I have annotated (many) video files using Solomon Coder. This is an open source program which, quickly and easily, allows for user specified frame by frame annotation.</p>

<p>I have then summarized the data using R. In my summary I have a behaviour event and the start and end frame numbers. I also have the start and end time in seconds, epoch time and as a POSIXct object.</p>

<p>I'd like to extract the specific frames in some instances.</p>

<p>I am aware of the following options:</p>

<ol>
<li><p>ffmpeg in Matlab. However, I do not have access to Matlab nor I am that competent using it.</p></li>
<li><p>Using the command prompt and VLC. However, many images seem to get skipped and one ends up with lots of corrupt files. (e.g: <a href="https://www.raymond.cc/blog/extract-video-frames-to-images-using-vlc-media-player/" rel="nofollow noreferrer">https://www.raymond.cc/blog/extract-video-frames-to-images-using-vlc-media-player/</a>)</p></li>
<li><p>Potentially calling Octave through R and using similar approach of ffmpeg. Again, this is a bit beyond my ability for the moment.</p></li>
</ol>

<p>If anyone has some quick guidance that would be greatly appreciated.</p>

CC BY-SA 3.0
2016-11-18T03:56:18.670Z
How about ffmpeg from the command line? I can supply a command if you're interested.
Gyan
CC BY-SA 3.0
2016-11-18T13:56:58.707Z
Hi Mulvya, that would be great. Thank you! I have been going through the ffmpeg documentation. It is quite new to me so if you could guide me through the code that would be greatly appreciated!
Jono
<video>
Extract specific video frames
Jono
# Extract specific video frames

(Asked by: username_0 on 2016-11-17)

I am looking for a Windows based open source method to extract specific frames from video files (\*.avi). Ideally the method will be suited to R.

I need to do this as I have annotated (many) video files using Solomon Coder. This is an open source program which, quickly and easily, allows for user specified frame by frame annotation.

I have then summarized the data using R. In my summary I have a behaviour event and the start and end frame numbers. I also have the start and end time in seconds, epoch time and as a POSIXct object.

I'd like to extract the specific frames in some instances.

I am aware of the following options:

1. ffmpeg in Matlab. However, I do not have access to Matlab nor I am that competent using it.
2. Using the command prompt and VLC. However, many images seem to get skipped and one ends up with lots of corrupt files. (e.g: <https://www.raymond.cc/blog/extract-video-frames-to-images-using-vlc-media-player/>)
3. Potentially calling Octave through R and using similar approach of ffmpeg. Again, this is a bit beyond my ability for the moment.

If anyone has some quick guidance that would be greatly appreciated.

**Tags:** video

> **Comments:**
> **username_1**: How about ffmpeg from the command line? I can supply a command if you're interested.  
> **username_0**: Hi Mulvya, that would be great. Thank you! I have been going through the ffmpeg documentation. It is quite new to me so if you could guide me through the code that would be greatly appreciated!  

---

## 2 answers

### Answer by username_1 on 2016-11-19. Score: 6 | Accepted answer

To extract frame # x through y, use
ffmpeg -i in.mp4 -vf select='between(n\,x\,y)' -vsync 0 frames%d.png

> **Comments:**
> **username_0**: Hi Mulvya, this is working great so far. Thank you again. I was wondering though, can you modify this code so that the frame numbers are printed in the name of the output file? As oppose to the current naming convention which always begins with "frame1" even if specifying, for example, to start on the 200th frame.  
> **username_1**: You can do it manually: `ffmpeg -i in.mp4 -vf select='between(n,x,y)' -vsync 0 -start_number x frames%d.png`  
> **username_3**: @username_1 Its giving an error, I had to escape the commas in `between(n,x,y)` with backslash  
> **username_1**: @username_3 yes, depending on your shell, it's needed.  
> **username_3**: @username_1 I came to know of this from an another similar answer of yours. Please do edit it for other users convenience. I have the latest ffmpeg version.  

---

### Answer by username_2 on 2018-06-07. Score: 3

You can also use: 
ffmpeg -ss XXX -i input.mp4 -vframes 1 output.png

where XXX is the time in seconds. If you know which exactly frame you want to extract you can calculate the XXX by multiplying the number of the wanted frame * frame duration which is 1/fps. 
Please also note that since H.264 is using I, P and B frames it would be best if you first decode your video to some intra-frame codec or raw. You can easily do that by: 
ffmpeg -i input.mp4 -vsync 0 output.y4m

And then you can extract the desired frame. But y4m is a format with no compression which means that the output file will be huge. You can also use ffv1 to do that since it is lossless codec, but make sure you use -g 1 option, since you need only I-frames at the output.


---

