BlenderBender
BlenderBender

Reputation: 566

ffmpeg: extracting images from video together with their frame numbers?

I'm extracting images from a (variable framerate) .avi file with

ffmpeg -i movie.avi -r 25 %05d.png

(the -roption should be equivalent to the filter -vf='fps=fps=25'), but would now like to know the frame number in the original video stream each generated image corresponds to. Is there an easy way to do that?

Upvotes: 2

Views: 1713

Answers (2)

BlenderBender
BlenderBender

Reputation: 566

I found some way to do what I want, which is not an easy one. With the "showinfo" filter, it is possible to get the position of a frame with respect to the source file, i.e. running

ffmpeg -i movie.avi -filter:v 'showinfo' output.avi

would give for example

[Parsed_showinfo_1 @ 0x7ffaebd01bc0] n: 0 pts: 0 pts_time:0 pos: 630548 fmt:yuv420p sar:1/1 s:1280x720 i:P iskey:0 type:P checksum:B4E5E664 plane_checksum:[6B3569DD 72B2AE10 31F8CE68] mean:[76 111 155] stdev:[13.1 3.8 8.7]

where "pos: 630548" is the position of the first frame in the source file. Given the information which position corresponds to which frame in the source file, one could then do

ffmpeg -i movie.avi -filter:v "fps=fps=25, showinfo" %05d.png > output.txt 2>&1

which extracts the images as before, but also gives the position of the extracted frames in the input file, and from these two informations one can piece together the mapping from extracted frames to frames in the input file.

However, I don't think this solution can be called 'easy' (or pretty).

Edit: Following the suggestion of @Mulvya, you can save yourself the extra step of running ffmpeg -i movie.avi -filter:v 'showinfo' output.avi to extract the position of the frames in the original video, if you instead run

ffmpeg -i movie.avi -filter:v "showinfo, fps=fps=25, showinfo" %05d.png > output.txt 2>&1

This will emit the showinfo of the original and the filtered video stream at the same time, e.g. (original video stream corresponds to lines starting with Parsed_showinfo_0, the filtered stream corresponds to lines starting with Parsed_showinfo_2)

[Parsed_showinfo_0 @ 00a38820] n:   0 pts:      0 pts_time:0       pos:      508 fmt:pal8 sar:0/1 s:320x200 i:P iskey:1 type:I checksum:CF3AE73F plane_checksum:[CF3AE73F] mean:[117] stdev:[48.3]
[Parsed_showinfo_0 @ 00a38820] n:   1 pts:      1 pts_time:0.0285362 pos:    26710 fmt:pal8 sar:0/1 s:320x200 i:P iskey:0 type:P checksum:CF3AE73F plane_checksum:[CF3AE73F] mean:[117] stdev:[48.3]
[Parsed_showinfo_2 @ 00a38ca0] n:   0 pts:      0 pts_time:0       pos:      508 fmt:pal8 sar:0/1 s:320x200 i:P iskey:1 type:I checksum:CF3AE73F plane_checksum:[CF3AE73F] mean:[117] stdev:[48.3]
[Parsed_showinfo_0 @ 00a38820] n:   2 pts:      2 pts_time:0.0570725 pos:    31680 fmt:pal8 sar:0/1 s:320x200 i:P iskey:0 type:P checksum:512EEB5E plane_checksum:[512EEB5E] mean:[117] stdev:[48.2]

Upvotes: 1

Gyan
Gyan

Reputation: 93018

If you use

ffmpeg -i movie.avi -vsync 0 %05d.png

No frames will be duplicated or dropped, so each frame serial no. should correspond to its index in the video.

Upvotes: 0

Related Questions