简体   繁体   中英

Opencv CAP_PROP_FRAME_COUNT return negative large number with h264 video

I have two identical videos. One is "video.h264" the other one is "video.avi"

When I do this

cap = cv2.VideoCapture(fn)

if not cap.isOpened(): 
    print("could not open :",fn)
    exit
    
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
width  = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps    = cap.get(cv2.CAP_PROP_FPS)

print("Length is ",length," Widht and Height (",width,",",height,") FPS ",fps)

cap = cv2.VideoCapture(fn)
property_id = int(cv2.CAP_PROP_FRAME_COUNT) 
length = int(cv2.VideoCapture.get(cap, property_id))
print( length )

If fn is the avi file I get

Length is  600  Widht and Height ( 3840 , 1920 ) FPS  10.0
600

But with the h264 file I get

Length is  -76861433640456  Widht and Height ( 3840 , 1920 ) FPS  10.0
-76861433640456

As you can see the length is a very large negative number. Why this could be happening? Is this a matter of typing?

Most likely you h264 video is corrupted somehow. You can fix this by re-encoding the video (eg with ffmpeg):

ffmpeg -i /path/to/you/video.h264 -vf fps=30 -vcodec libx264 /path/to/you/video_reencoded.h264

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM