<h5 dir="auto">System information (version)</h5>
<ul dir="auto">
<li>OpenCV =&gt; 4.5.2</li>
<li>Operating System / Platform =&gt; Ubuntu 20.04</li>
<li>Tested with Python 3.8.5</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">I have been working with VideoCapture, everything working fine until I tested my code with an older video in .mpg format. In that video occurs the following:</p>
<ol dir="auto">
<li>When doing cap.set(cv2.CAP_PROP_POS_FRAMES, 0), the frame is not set to the first frame, but rather to a posterior frame. The same happens for other intermediate frames, where it seems to be skipping some frames.</li>
<li>When doing cap.read() it works properly (beginning from the frame 0 and reading all the intermediate frames).</li>
<li>When re-encoding the video with ffmpeg, doing cap.set(cv2.CAP_PROP_POS_FRAMES, 0) works properly (i.e. doesn't skip initial frames). If the video codec is copied (-c:v copy), it follows the same wrong behaviour.</li>
</ol>
<p dir="auto">I think it might be related to the IBP frame encoding, positioning to the closes "I" frame, but I believe it should be considered a bug if the intention of cap.set() is to position specifically to the frame of interest.</p>
<p dir="auto"><a href="https://github.com/opencv/opencv/files/6606176/2001_10_20_19_00_trim.mpg.zip">2001_10_20_19_00_trim.mpg.zip</a></p>
<p dir="auto">EDIT: Link to the video was in the middle of the text.</p>
<h5 dir="auto">Steps to reproduce</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="cap = cv2.VideoCapture('2001_10_20_19_00_trim.mpg')
# Works as expected
_, frame = cap.read()
cv2.imwrite('0_read.jpg', frame)
# Returns a posterior frame
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
_, frame = cap.read()
cv2.imwrite('0_set.jpg', frame)"><pre class="notranslate"><code class="notranslate">cap = cv2.VideoCapture('2001_10_20_19_00_trim.mpg')
# Works as expected
_, frame = cap.read()
cv2.imwrite('0_read.jpg', frame)
# Returns a posterior frame
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
_, frame = cap.read()
cv2.imwrite('0_set.jpg', frame)
</code></pre></div>
<h5 dir="auto">Issue submission checklist</h5>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I report the issue, it's not a question

</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I checked the problem with documentation, FAQ, open issues,<br>
forum.opencv.org, Stack Overflow, etc and have not found solution

</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I updated to latest OpenCV version and the issue is still there

</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> There is reproducer code and related data files: videos, images, onnx, etc

</li>
</ul>
<p dir="auto">Example and confirmed effect:</p>
<p dir="auto"><a href="http://answers.opencv.org/question/162781/videocaptureset-cap_prop_pos_frames-framenumber-not-exact-in-opencv-32-with-ffmpeg/" rel="nofollow">http://answers.opencv.org/question/162781/videocaptureset-cap_prop_pos_frames-framenumber-not-exact-in-opencv-32-with-ffmpeg/</a></p>