user136128
user136128

Reputation: 239

Python Opencv MJPG compression?

I am getting a significantly larger file after processing a video. Is MJPG fourcc codec compressing properly or is this supposed to happen? The original video size is 2.51 mb, the result video file size is 16.8 mb. In this code, there is no processed image to reference, it is using the same images as before, but the size increases.

Here is my code.

import os
import cv2

width=960
height=540
fps=25.0

#video=cv2.VideoWriter('ntest\\video.avi',-1,1,(width,height))
fourcc=cv2.VideoWriter_fourcc(*'MJPG')
video = cv2.VideoWriter('ntest\\output.avi',fourcc, fps, (width,height))

cap = cv2.VideoCapture('in.mp4')
count = 0

prename="ntest\\frame"
extension=".jpg"

while cap.isOpened():
    ret,frame = cap.read()
    #after processing, replace frame with processed image
    cv2.imshow('window-name',frame)
    name=prename+str(count)+extension
    cv2.imwrite(name, frame)
    a=cv2.imread(name)
    video.write(a)
    os.remove(name) #deletes image file, only keeps video
    count = count + 1
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

cap.release()
cap.destroyAllWindows()
cv2.destroyAllWindows()
video.release()

Upvotes: 0

Views: 7098

Answers (2)

Csaba Toth
Csaba Toth

Reputation: 10729

In case an MPEG codec, only the I frames (key frames) will have a (kinda) full JPEG information. Between key frames there are B or P frames which are both just differential information compared to other frames. http://www.bretl.com/mpeghtml/pixtypes.HTM

Typical patter of frames IBBPBBP...BBIBBPBBP...BBIBBPBB.... In case of well compressed MPEG4/divx videos often several seconds pass until a key frame comes along in the video stream. All the other frames are differential. You can notice that when the recording has some color distortion which starts to overtake the view area, and when suddenly the artifact disappears that's when the video stream came across an I frame and could clear up the space.

These will be totally example data, and based on MPEG2 and not MPEG4 encoding. MPEG4 is even more compressed than this.

http://www0.cs.ucl.ac.uk/teaching/GZ05/09-mpeg.pdf page 11:

| Type    | Size (KB) | Compression |
| ------- | --------- | ----------- |
| I-frame | 50        | 10:1        |
| ------- | --------- | ----------- |
| B-frame | 25        | 20:1        |
| ------- | --------- | ----------- |
| P-frame | 10        | 50:1        |
| ------- | --------- | ----------- |
| Avg.    | 18        | 29:1        |

This shows that MJPEG is approximately 3 times larger in that case of MPEG2 video. That would mean compression of a ~7.53 MB hypothetical MJPEG video to 2.51 MB MPEG2 video. MPEG4 can achieve 2 times more compression than MPEG2 easily (all depends on the codec settings), so it can compress a 15-18MB MJPEG into a 2.51 MB MPEG4. Never forget though that the compression is lossy, so some information is lost. But in an ideal case the human eye and ear does not recognize too much the artifacts of the data loss (blocking, ringing, ...).

Upvotes: 0

Martin Beckett
Martin Beckett

Reputation: 96167

If the input mp4 file uses a modern codec like h264 then it is very likely that mjpeg will be bigger. Mjpeg is simply a series of jpeg images of each frame, it can't take advantage of areas of the image which are the same from frame to frame, or any of the predictive coding that a modern video codec uses

Upvotes: 2

Related Questions