Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.2K
Visualizações
OpenCV VideoCapture returns strange frame offset for different versions

I'm using opencv-python and when I execute the following code:

index = 0
cap = cv2.VideoCapture(video_path)
while True:
    offset = cap.get(cv2.CAP_PROP_POS_MSEC)
    print(cv2.__version__, index, offset)

    ok, frame = cap.read()
    if not ok:
        break
    index += 1

I get the following output:

3.4.7 0 0.0
3.4.7 1 33.36666666666667
3.4.7 2 66.73333333333333
3.4.7 3 100.10000000000001
3.4.7 4 133.46666666666667

If I execute this code on version 3.4.8.29, I get the following output:

3.4.8 0 0.0
3.4.8 1 0.0
3.4.8 2 33.36666666666667
3.4.8 3 66.73333333333333
3.4.8 4 100.10000000000001

And if I execute it on version 4.5.2.52 I get:

4.5.2 0 0.0
4.5.2 1 0.0
4.5.2 2 0.0
4.5.2 3 0.0
4.5.2 4 0.0

The question is first of all, which one is the correct one? It seems like 3.4.7 is correct, but it also seems to be changing randomly between versions.

And also how can I modify the other versions to get the proper result, same as 3.4.7

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I read the OpenCV docs and they said: "Reading / writing properties involves many layers. Some unexpected result might happens along this chain. Effective behaviour depends from device hardware, driver and API Backend." (source: https://docs.opencv.org/3.4.15/d4/d15/group__videoio__flags__base.html#gaeb8dd9c89c10a5c63c139bf7c4f5704d)

So in other words OpenCV does not guaranty consistent and reliable behavior of this function.

Also I installed openCV 4.5.2.52 and applied your script to one of my '.mp4' videos. Then I got the same result as you had for openCV version 3.4.8.29. So I think the behaviour you experience is not a 'bug', but rather the unreliable behavior of this function.

As work around you can compute the "offset" by dividing the frame number by the FPS count (see code below). Then you have more control over the behaviour and maybe more consisted results.

index = 0
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
while True:
    offset = cap.get(cv2.CAP_PROP_POS_MSEC)

    ok, frame = cap.read()
    if not ok:
        break
    
    # CAP_PROP_POS_MSEC
    print("CAP_PROP_POS_MSEC: ", index, offset)
    
    # Devide fps by frame number
    offset = cap.get(cv2.CAP_PROP_POS_FRAMES) / fps * 1000
    print("cv2.CAP_PROP_POS_FRAMES", index, offset)
     
    index += 1
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda