VideoHelp Forum
+ Reply to Thread
Results 1 to 29 of 29
Thread
  1. Banned
    Join Date
    Oct 2023
    Location
    Los Angeles
    Search Comp PM
    How do I losslessly extract keyframes, and ONLY keyframes from videos? They must retain the original quality of the video.
    Quote Quote  
  2. Member
    Join Date
    Mar 2008
    Location
    United States
    Search Comp PM
    Are you talking about saving it to a lossless image format, like png?
    Quote Quote  
  3. Banned
    Join Date
    Oct 2023
    Location
    Los Angeles
    Search Comp PM
    Originally Posted by davexnet View Post
    Are you talking about saving it to a lossless image format, like png?
    Yes, like if it came from the actual video, and yes, in a png file. Sorry I took so long to respond.
    Quote Quote  
  4. Member pchan's Avatar
    Join Date
    Mar 2003
    Location
    Singapore
    Search Comp PM
    Can try VLC.
    1. Launch VLC
    2. Drag and drop your video onto VLC
    3. Tool -> Preference and select Video. Browse to the directory that you want your png files.
    4. Select the frame that you want
    5. Video -> Take Snapshot
    Quote Quote  
  5. Banned
    Join Date
    Oct 2023
    Location
    Los Angeles
    Search Comp PM
    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?
    Quote Quote  
  6. Member pchan's Avatar
    Join Date
    Mar 2003
    Location
    Singapore
    Search Comp PM
    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
    Quote Quote  
  7. 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
    Quote Quote  
  8. Banned
    Join Date
    Oct 2023
    Location
    Los Angeles
    Search Comp PM
    Originally Posted by pandy View Post
    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
    Could you explain the different parts of this script and what should I put in it?
    Quote Quote  
  9. Banned
    Join Date
    Oct 2023
    Location
    Los Angeles
    Search Comp PM
    Originally Posted by pchan View Post
    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
    For videos with constant frame rate?
    Quote Quote  
  10. Member pchan's Avatar
    Join Date
    Mar 2003
    Location
    Singapore
    Search Comp PM
    Originally Posted by Jay123210599 View Post
    Originally Posted by pchan View Post
    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
    For videos with constant frame rate?
    test.mp4 is CBR. I ran the cmd line and the I frame pictures match with VirtualDub.

    Sry. Frame rate mode = Constant
    Last edited by pchan; 7th Nov 2023 at 17:44.
    Quote Quote  
  11. Originally Posted by Jay123210599 View Post
    Originally Posted by pandy View Post
    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
    Could you explain the different parts of this script and what should I put in it?
    -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).
    Quote Quote  
  12. 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
    Quote Quote  
  13. Banned
    Join Date
    Oct 2023
    Location
    Los Angeles
    Search Comp PM
    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?
    Quote Quote  
  14. Member
    Join Date
    Aug 2006
    Location
    United States
    Search Comp PM
    Originally Posted by Jay123210599 View Post
    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?
    If you are using a lossless codec that does only intra-frame compression, every frame is a keyframe
    Last edited by usually_quiet; 8th Nov 2023 at 09:13. Reason: added "only"
    Ignore list: hello_hello, tried, TechLord, Snoopy329
    Quote Quote  
  15. Banned
    Join Date
    Oct 2023
    Location
    Los Angeles
    Search Comp PM
    Originally Posted by usually_quiet View Post
    Originally Posted by Jay123210599 View Post
    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?
    If you are using a lossless codec that does only intra-frame compression, every frame is a keyframe
    So what should I do if I only want to extract keyframes from those videos?
    Quote Quote  
  16. Member
    Join Date
    Aug 2006
    Location
    United States
    Search Comp PM
    Originally Posted by Jay123210599 View Post
    Originally Posted by usually_quiet View Post
    Originally Posted by Jay123210599 View Post
    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?
    If you are using a lossless codec that does only intra-frame compression, every frame is a keyframe
    So what should I do if I only want to extract keyframes from those videos?
    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
    Quote Quote  
  17. Banned
    Join Date
    Oct 2023
    Location
    Los Angeles
    Search Comp PM
    Originally Posted by usually_quiet View Post
    Originally Posted by Jay123210599 View Post
    Originally Posted by usually_quiet View Post
    Originally Posted by Jay123210599 View Post
    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?
    If you are using a lossless codec that does only intra-frame compression, every frame is a keyframe
    So what should I do if I only want to extract keyframes from those videos?
    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?
    Something like that.
    Quote Quote  
  18. 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.
    Quote Quote  
  19. Banned
    Join Date
    Oct 2023
    Location
    Los Angeles
    Search Comp PM
    Originally Posted by pandy View Post
    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.
    Open this file in Avidemux and move only by keyframe, you understand what I mean. How do I losslessly extract only keyframes using that tool?
    Image Attached Files
    Quote Quote  
  20. Member
    Join Date
    Aug 2006
    Location
    United States
    Search Comp PM
    Originally Posted by Jay123210599 View Post
    Open this file in Avidemux and move only by keyframe, you understand what I mean. How do I losslessly extract only keyframes using that tool?
    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
    Quote Quote  
  21. 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.
    Quote Quote  
  22. Banned
    Join Date
    Oct 2023
    Location
    Los Angeles
    Search Comp PM
    Originally Posted by jagabo View Post
    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.
    What tools can differentiate them?
    Quote Quote  
  23. Banned
    Join Date
    Nov 2023
    Location
    Europe
    Search Comp PM
    Originally Posted by Jay123210599 View Post
    How do I losslessly extract keyframes, and ONLY keyframes from videos? They must retain the original quality of the video.
    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.
    Quote Quote  
  24. Banned
    Join Date
    Oct 2023
    Location
    Los Angeles
    Search Comp PM
    Originally Posted by Swedaniel View Post
    Originally Posted by Jay123210599 View Post
    How do I losslessly extract keyframes, and ONLY keyframes from videos? They must retain the original quality of the video.
    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?
    Keyframes are I-frames, or intra-frames.
    Quote Quote  
  25. Originally Posted by Jay123210599 View Post
    What tools can differentiate them?
    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.
    Quote Quote  
  26. Banned
    Join Date
    Oct 2023
    Location
    Los Angeles
    Search Comp PM
    Originally Posted by jagabo View Post
    Originally Posted by Jay123210599 View Post
    What tools can differentiate them?
    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.
    What's K and I?
    Quote Quote  
  27. Originally Posted by Jay123210599 View Post
    Originally Posted by jagabo View Post
    Originally Posted by Jay123210599 View Post
    What tools can differentiate them?
    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.
    What's 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"
    Quote Quote  
  28. Yes, ffprobe shows the keyframes. Here's the output of ffprobe in CSV (spreadsheet) format:

    Image
    [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"
    Quote Quote  
  29. Banned
    Join Date
    Oct 2023
    Location
    Los Angeles
    Search Comp PM
    thanks
    Last edited by Jay123210599; 10th Nov 2023 at 08:26.
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!