Skip to content

about how to use cv2:: codacodec:: VdieoReader::set() #3944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
RookieDeng33 opened this issue May 21, 2025 · 6 comments
Open

about how to use cv2:: codacodec:: VdieoReader::set() #3944

RookieDeng33 opened this issue May 21, 2025 · 6 comments
Labels
category: cuda question (invalid tracker) ask questions and other "no action" items here: https://forum.opencv.org/

Comments

@RookieDeng33
Copy link

How to use similar functions in cv2:: codacodec:: VdieoReader like cv2:: VideoCapture:: set (CAP_PROP_POS_FRAMES, target)

I need to read the video frame from the customized key frame position, or repeat the reading from the specified frame when reading the video to the last frame

just like

def get_cap_frames_by_range(f_cap, frames_idx, prepared_f_frames):
      min_index, max_index = min(frames_idx), max(frames_idx)
      # print(min_index, max_index)
      temp_counter = {}
      temp_frames = {}  
      for item in frames_idx:
          if item not in temp_counter:
          temp_counter[item] = 0
          temp_counter[item] += 1
      if int(f_cap.get(cv2.CAP_PROP_POS_FRAMES)) != min_index:
          # print("switch CAP_PROP_POS_FRAMES")
          f_cap.set(cv2.CAP_PROP_POS_FRAMES, min_index)
      ...
@asmorkalov asmorkalov added the question (invalid tracker) ask questions and other "no action" items here: https://forum.opencv.org/ label May 21, 2025
@asmorkalov
Copy link
Contributor

cc @cudawarped

@cudawarped
Copy link
Contributor

@RookieDeng33 You can seek to an initial frame position when initializing the decoder, see this test, however
for efficient decoding cudacodec::VideoReader uses a separate thread to demux the video source and queue the frames for decoding. This means that it is not possible to seek to a specific frame during decoding process.

If there is a small gap between the frames you require then I would just decode and skip the intermediate frames. This could be equivalent to seeking if the gap between those frames is less than the gap between key frames. e.g. If you want frames 10,20,30,...,100 but the key frame is every 250 frames the internal logic when seeking would have to decode all of the frames from frame 0 anyway making the current behaviour of cudacodec::VideoReader equivalent to cv::VideoCapture.

@opencv-alalek opencv-alalek transferred this issue from opencv/opencv May 22, 2025
@RookieDeng33
Copy link
Author

@RookieDeng33 You can seek to an initial frame position when initializing the decoder, see this test, however for efficient decoding cudacodec::VideoReader uses a separate thread to demux the video source and queue the frames for decoding. This means that it is not possible to seek to a specific frame during decoding process.

If there is a small gap between the frames you require then I would just decode and skip the intermediate frames. This could be equivalent to seeking if the gap between those frames is less than the gap between key frames. e.g. If you want frames 10,20,30,...,100 but the key frame is every 250 frames the internal logic when seeking would have to decode all of the frames from frame 0 anyway making the current behaviour of cudacodec::VideoReader equivalent to cv::VideoCapture.

Thank you for your reply, which has helped me a lot; However, I still have some doubts. Why not reset the queue but recreate the cudacodec:: VideoReader instance; For video files, is it more efficient to reset the queue than to reload the video source?

@cudawarped
Copy link
Contributor

Why not reset the queue but recreate the cudacodec:: VideoReader instance; For video files, is it more efficient to reset the queue than to reload the video source?

I am not clear on your what you are asking?

To clarify I am not suggesting you create a new instance of cudacodec::VideoReader for each frame when you want to seek. I am informing you that you cannot seek with cudacodec::VideoReader due to the way it is currently implemented, but you can if you want choose the index of the first frame to start your decoding session from.

I am also pointing out that when you skip to a frame in between key frame the decoder has to decode every frame from that key frame to your desired frame anyway. This means that depending on your use case (how close together the frames you want to skip to are and the GOP size etc.) you can simply decode all the frames and ignore the ones you don't need.

@RookieDeng33
Copy link
Author

Why not reset the queue but recreate the cudacodec:: VideoReader instance; For video files, is it more efficient to reset the queue than to reload the video source?

I am not clear on your what you are asking?

To clarify I am not suggesting you create a new instance of cudacodec::VideoReader for each frame when you want to seek. I am informing you that you cannot seek with cudacodec::VideoReader due to the way it is currently implemented, but you can if you want choose the index of the first frame to start your decoding session from.

I am also pointing out that when you skip to a frame in between key frame the decoder has to decode every frame from that key frame to your desired frame anyway. This means that depending on your use case (how close together the frames you want to skip to are and the GOP size etc.) you can simply decode all the frames and ignore the ones you don't need.

Sorry for not describing my problem clearly. Parameter frames_idx of function get_cap_frames_by_range is a batch of consecutive video frame idx. For example, the first batch of frames_idx is [1,2,3,4,5], and the second batch may be [4,5,6,7,8] , and the third batch may be [9,10,11,12]. I mean that when decoding each batch, it may need to create a new instance of cudacodec::VideoReader, but not necessarily.
What I asked earlier is that when reading from the specified firstFrameIdx, you may use the instantiated VideoReader to clear The FrameQueue,and then re put the frames which start with firstFrameIdx to FrameQueue for decoding.is it more efficient than create a new instance of cudacodec::VideoReader?

@cudawarped
Copy link
Contributor

@RookieDeng33 I still don't fully understand what you are asking. That said if you altered the implementation so that you could seek to a specifiec location within the file during decode then it should be more efficient to do that than create a new instance of cudacodec::Videoreader.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: cuda question (invalid tracker) ask questions and other "no action" items here: https://forum.opencv.org/
Projects
None yet
Development

No branches or pull requests

4 participants