How do I losslessly extract keyframes, and ONLY keyframes from videos? They must retain the original quality of the video.
+ Reply to Thread
Results 1 to 29 of 29
-
-
Are you talking about saving it to a lossless image format, like png?
-
-
What if I want to losslessly batch extract only keyframes from multiple videos at once?
Also, how do I losslessly extract keyframes from videos with constant frame rate? -
This link may have what you are looking for.
https://superuser.com/questions/669716/how-to-extract-all-key-frames-from-a-video-clip
I tried this command line and it worked.
ffmpeg -i test.mp4 -vf "select=eq(pict_type\,I)" -vsync vfr frame-%02d.png -
You may try this, it should be more accurate and perhaps marginally faster than previously proposed script:
Code:@setlocal @set filename=%1 @ffmpeg.exe -y -hide_banner -v 32 -stats -skip_frame nokey -an -sn -dn -color_range 2 -i "%filename%" -vsync 0 -q:v 50 "%~n1_%%06d.png" @endlocal @pause
-
-
test.mp4 is CBR. I ran the cmd line and the I frame pictures match with VirtualDub.
Sry. Frame rate mode = ConstantLast edited by pchan; 7th Nov 2023 at 17:44.
-
-skip_frame nokey - ffmpeg ignore all frames not being keyframes
-an -sn -dn - ffmpeg don't process audio, subtitle, data
-color_range 2 - set ffmpeg to full quantization range at least in theory - this can be important flag but ffmpeg behavior can be different - than expected - placing this flag before input should force ffmpeg to not perform conversion from limited to full quantization range so in theory you should get true video values in produced png but - in MPEG-2 video quantization range conversion is performed, in h.264 stream quantization range conversion is not performed. Perhaps this can be workaround by using -vf and for example zscale filter where explicitly quantization range conversion may be disabled.
remaining parts of commandline are rather form of nice to have but not critical settings.
As you requested exact video content then most difficult is quantization range - described behavior can be or my error (wrong assumption) or perhaps ffmpeg issue (behavior of "-color_range 2" option depends on video codec and perhaps is beyond regular user control). -
You may try to use this one to keep original quantization range:
Code:@setlocal @set filename=%1 @ffmpeg.exe -y -hide_banner -v 32 -stats -skip_frame nokey -an -sn -dn -color_range 2 -i "%filename%" -vf scale=in_range=full:out_range=full -vsync 0 -q:v 50 "%~n1_%%06d.png" @endlocal @pause
-
I finally found the problem. i have been using videos with lossless codecs like FFV1 and YUV and ffmpeg extracted all frames. But when I use videos with normal H.264/H.265 codecs, it only extracted keyframes. How do I only losslessly extract keyframes from videos with lossless codecs?
-
Last edited by usually_quiet; 8th Nov 2023 at 09:13. Reason: added "only"
Ignore list: hello_hello, tried, TechLord, Snoopy329 -
-
FFV1 and YUV (MagicYUV?) are both lossless codecs that use only intra-frame compression. Since every single frame encoded using those codecs is a keyframe, you will get each and every frame in the videos as a PNG using the script you have.
I am not sure that I understand what you want. Are you asking for a script to extract only the frames in the losslessly encoded video that correspond to keyframes that you previously extracted from the same video encoded using a lossy codec?Last edited by usually_quiet; 8th Nov 2023 at 12:38. Reason: clarity
Ignore list: hello_hello, tried, TechLord, Snoopy329 -
In your files every frame is keyframe so you extracting all frames.
It is not clear what is your goal...
Of course you can extract only some keyframes based on arbitrary criteria - time or scene change (usually luminance change).
It is up to you to decide - we can help but we need to know what is your goal. -
You don't seem to understand what a keyframe (aka an I-frame) is. A keyframe is self-contained. A keyframe doesn't need any information from a previous frame or a successive frame to be decoded.
In a video where lossy interframe encoding is used rather than lossless intraframe encoding, there are GOPs. A GOP (group of pictures), starts with a keyframe (an I-frame) and ends at the next keyframe. The other types of frames in the group of pictures are P-frames and B-frames. Decoding P-frames requires information from previous frames Decoding B-frames requires information from both previous and successive frames.
You'll need to extract every 12th frame, 15th frame, or 24th frame (or whatever number you decide that you want to use) from losslessly encoded video to get a similar effect to extracting the I-frames from a video encoded using lossy compression.Last edited by usually_quiet; 8th Nov 2023 at 18:20. Reason: clarity
Ignore list: hello_hello, tried, TechLord, Snoopy329 -
FFV1 isn't strictly an intra-frame codec. It can use some inter-frame techniques.
https://trac.ffmpeg.org/wiki/Encode/FFV1
https://en.wikipedia.org/wiki/FFV1#Compression_details
In the sample video there is a keyframe frame every 12'th frame. Ie, 12 frame GOPs. The GOP pattern doesn't appear to be sensitive to scene changes. Ie, the GOP size is always 12 frames, even if there is a scene change in the middle of a GOP.
ffmpeg doesn't seem to differentiate those "keyframes" in ffv1 videos. But you can get the correct result by simply exporting every 12'th frame.Last edited by jagabo; 9th Nov 2023 at 07:32.
-
With Simple Video Editor v.1.4.2.0 you can drag and drop an video onto the program and it will load the video frames and it is then possible to select an frame and export and save it into .png and .bmp.. Im testing while typing to verify the quality, and as far as i can see there is no loss of quality.
As someone said up above it is possible to use VLC Media Player, which i havent tested the function. This program is an full featured media player which take a lot of storage, if you're low on storage the VideoLAN/CodeProject/Video Screenshot team, maybe more im not sure, have made an simple video player with the main feature to take screenshots from videos and save them into JPG/PNG or BMP images.
Video Screenshot v.1.0 (nVLC - CodeProject, libVLC and libVLCcore - VideoLAN). The program installation file is 10MB, the program is simple and easy to use.
Edit: I dont know what KeyFrames is, is it the "Key/Main/Most Important" frames or is it some sort of hidden frames containing some unlocking Keys?Last edited by Swedaniel; 9th Nov 2023 at 09:00.
-
-
AviDemux shows them as I and P (the P frames aren't really predicted, they just use the same context as the I frame before them). VirtualDub shows them as K and I.
-
Probably K from Keyframe and I from Intra.
I think you can safely rise this misbehavior of the ffmeg as a bug (congratulations). In your video file Keyframes are properly recognized and reported by ffprobe so seem there is inconsistency in behavior between ffmpeg keyframe detection and ignoring "-skip_frame nokey" -
Yes, ffprobe shows the keyframes. Here's the output of ffprobe in CSV (spreadsheet) format:
[Attachment 74794 - Click to enlarge]
You can see keyframes (columb D) at line 2, 14, 26, etc.
The drag/drop batch file I used to get that:
Code:@set name=%1 @echo entry,media_type,stream_index,key_frame,pkt_pts,pkt_pts_time,pkt_dts,pkt_dts_time,best_effort_timestamp,best_effort_timestamp_time,pkt_duration,pkt_duration_time,pkt_pos,pkt_size,width,height,pix_fmt,sample_aspect_ratio,pict_type,coded_picture_number,display_picture_number,interlaced_frame,top_field_first,repeat_pict,color_range,color_space,?,?,chroma_location > "%~n1.csv" @"g:\program files\ffmpeg64\bin\ffprobe.exe" -v 32 -stats -y -hide_banner -i %name% -select_streams v -print_format csv -of csv -show_entries frame >> "%~n1.csv"
Similar Threads
-
Problem forcing keyframes
By jack616 in forum Video ConversionReplies: 0Last Post: 13th Aug 2023, 08:54 -
adding keyframes
By anctop in forum EditingReplies: 7Last Post: 12th Dec 2022, 20:50 -
Clipping Video Using FFMPEG and KeyFrames - Need More KeyFrames?
By Alan2106 in forum EditingReplies: 1Last Post: 3rd Jun 2021, 07:31 -
Difficulty converting this ts to mp4 losslessly
By jimdagys in forum Video ConversionReplies: 5Last Post: 8th Jan 2021, 20:32 -
Losslessly Convert 2.0 to 5.1
By koberulz in forum Newbie / General discussionsReplies: 3Last Post: 5th Sep 2019, 11:05