Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg extract frame timestamps from video

Tags:

video

ffmpeg

I am using this library to use FFmpeg with my Android application. I am extracting frames from a video and then adding them to a crop viewer. So each frame needs to represent some time frame within the video. Here is my current ffmpeg code to extract the frames:

ffmpeg -i inputFile -f image2 -ss mySS -r myR frame-%05d.png

When using the above command how would I add a timestamp to each frame? So i know for example frame 5 is at 9s within the video.

I dont know if the ffmpeg lib I am using has ffprobe from this link. I have also looked at other links on stackoverflow

any help is appreciated

like image 457
TheOtherguyz4kj Avatar asked Dec 19 '22 02:12

TheOtherguyz4kj


2 Answers

frame_pts option is your friend... set it to true and ffmpeg will output the frame's presentation timestamp as your filename.

ffmpeg -skip_frame nokey -i file -vsync 0 -frame_pts true out%d.png

Credits goes to this superuser answer.. more explanation of mixing framerate -r option with frame_pts

like image 141
hoohoo-b Avatar answered Dec 20 '22 14:12

hoohoo-b


To burn the timestamp in the video:

ffmpeg -i inputFile -vf drawtext=fontfile='/path/to/font':fontcolor=white:fontsize=25:x=(w-tw)/2:y=h-th-10:text='Source time: %{pts}' -ss mySS -r myR frame-%05d.png

If you have a > Nov 13 2017 build of FFmpeg, you can run

ffmpeg -i inputFile -f image2 -vf select='gte(t,mySS)' -vsync 0 -frame_pts 1 frame-%05d.png

This will number the output frames with the source frame index.

If you can't upgrade FFmpeg, then there's a longer workaround.

Run

ffmpeg -i inputFile -f image2 -vf select='gte(t,mySS)',showinfo -vsync 0 frame-%05d.png 2> log.txt

This will redirect the console to a text file. In it, you will lines like this:

...
     Metadata:
      encoder         : Lavc58.1.100 wrapped_avframe
[Parsed_showinfo_1 @ 00000000056d9dc0] n:   0 pts:  38400 pts_time:3       pos:   105928 fmt:yuv420p sar:1/1 s:320x240 i:P iskey:0 type:B checksum:F6027584 plane_checksum:[FAA4D7FC 4748A9ED 4767F37D] mean:[123 129 126] stdev:[57.1 78.2 80.6]
[Parsed_showinfo_1 @ 00000000056d9dc0] n:   1 pts:  38912 pts_time:3.04    pos:   101527 fmt:yuv420p sar:1/1 s:320x240 i:P iskey:0 type:P checksum:ECBC791A plane_checksum:[4104BBD1 4805B985 542303B5] mean:[123 129 126] stdev:[57.1 78.1 80.5]
[Parsed_showinfo_1 @ 00000000056d9dc0] n:   2 pts:  39424 pts_time:3.08    pos:   108798 fmt:yuv420p sar:1/1 s:320x240 i:P iskey:0 type:B checksum:70B2AC23 plane_checksum:[EF78A2F1 946CDA4F 39B72ED4] mean:[123 129 127] stdev:[57.1 77.8 80.1]
...

You can parse and extract the pts_time values, which have a 1:1 correspondence with the produced images.

like image 22
Gyan Avatar answered Dec 20 '22 15:12

Gyan