wrong frame order of .avi files

asked 2017-06-13 21:46:34 -0600

Hi,

I have .avi file, an output from Debut Video Capture Software. As a practice, I use the following very simple code to read and re-output it, using the same settings as the input file for the output file:

cap = cv2.VideoCapture(Infile)

### get video info
fps = cap.get(cv2.CAP_PROP_FPS)
Fcount = cap.get(cv2.CAP_PROP_FRAME_COUNT)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)  
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
videotime = Fcount/fps
fcc = int(cap.get(cv2.CAP_PROP_FOURCC)) 

### set videowriter
out = cv2.VideoWriter(Outfile,fcc, fps, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        out.write(frame)

    else:
        break

However, the output file have fewer frames! The followings are the outputs from CAP_PROP:

--- Input file :  testAviH264.avi 
Frames per second
Frame count: 996.0
Length: 33.2 
FOURCC: 875967048 

--- output file :  testAviH264out.avi
Frames per second: 30.0
Frame count: 548.0 
Length: 18.266666666666666 
FOURCC: 875967048

Why does the input file has 996 frames and the output only 548? To do a test, I use a loop to output the input video frame by frame:

for frame_no in range(0,995):
    cap.set(1,frame_no)
    ret,frame = cap.read()
    cv2.imwrite("frame%d.jpg" % frame_no, frame)

The result is weird. The output jpgs of frame_no 1, 3, 25-748, 926-948 have valid images, while others are empty. In total there are 740 valid images, not 548 as the video captured with the previous code. Further more, the order of the 740 images are not completely the same as the video sequence! It appears to have some redundant blocks.

The test file above is .avi format with H264 encoding. I generated further test videos from Debut Video Capture Software with avi/mpeg4, mp4/H264 and mp4/mpeg4 formats, avi/mpeg4 also gives rise to similar problems (fewer output frames with the first code, wrong order jpgs with the second code). Mp4 files behalves normal.

If anyone has any idea about how could this happen, and how to get "correct frames" with the .avi files, please let me know.

Many thanks for your time.

edit retag flag offensive close merge delete