FFmpeg – How to Extract Timestamps with the -r Option

ffmpeglibav

ffmpeg -i myvid.mp4  -r  25  -t  100  image-%d.jpeg 

Is the command I'm using to extract frames and it's working just like I expected. However I'd also like to look at the timestamps of the frames. At whatever precision. 100 miliseconds is good enough for me. Can ffmpeg do this?

Providing additional details,

When I run the above command i get around 100 JPEGs, I guess there is a 1 to 1 (or many to 1) correspondence between these JPEGS and frames of the video. I would like to know the timestamp of the frame that was output as a JPEG image 'i'.

Additionally I tried ffprobe but I find it reports even the video duration inaccurately 🙁

Best Answer

What you can do is "simulate" the image writing process by filtering with the fps filter, then using ffprobe to show the timestamps of the generated frames. This means that at 25 fps, the 50th frame (like your 50th image) will have a PTS of 2.00 seconds.

You do it like this:

ffprobe -f lavfi -i "movie=input.mp4,fps=fps=25[out0]" -show_frames -show_entries frame=pkt_pts_time -of csv=p=0

Will output:

0
0.04
0.08
0.12
0.16
...

These are the timestamps for each output image. You can actually combine the list of frames and the timestamps:

ls -1 image-*.jpeg > images.txt
ffprobe -f lavfi -i "movie=input.mp4,fps=fps=25[out0]" -show_frames -show_entries frame=pkt_pts_time -of csv=p=0 > frames.txt
paste images.txt frames.txt > combined.txt

Will create a file with:

image-0001.jpeg 0
image-0002.jpeg 0.04
image-0003.jpeg 0.08
image-0004.jpeg 0.12

Note that this may result in extraneous lines if there are too many frames or too many lines of info output. It seems a little inaccurate there.

Related Question