0

I need to read frames from video file and create new video from these frames. Then I read the frames from the new video and I need these frames to be identical to frames I read from the original video.

This is what I done (for the example I did it with only one frame) -

Read frame from video:

vidcap = cv2.VideoCapture('video_name.mp4')
success,orig_frame = vidcap.read()

Write new video from this frame:

size = (1920,1080)
vidWriter = cv2.VideoWriter('new_video.mp4', cv2.VideoWriter_fourcc(*'MP4V'), 25, size)
vidWriter.write(orig_frame)
vidWriter.release()

Read frame from new_video:

vidcap = cv2.VideoCapture('new_video.mp4')
success,new_frame = vidcap.read()

Comparing orig_frame with new_frame shows that they are not identical.

orig_frame:

enter image description here

new_frame:

enter image description here

How can I get identical frames from original video and new video?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
RafaelJan
  • 3,118
  • 1
  • 28
  • 46

1 Answers1

1

This is not possible with MP4s as they use lossy encoding to write the video data. You will not be able to get exact pixel-by-pixel matching between the two videos.

However, if you want to get an exact matching you will need to use a lossless codec.

It depends on what system you're using to determine what codecs are supported, but a long shot would be to use PNG encoding to write the video frames to file.

Try:

size = (1920,1080)
vidWriter = cv2.VideoWriter('new_video.avi', cv2.VideoWriter_fourcc(*'png '), 25, size)
vidWriter.write(orig_frame)
vidWriter.release()

If the above is not satisfactory, try consulting: Lossless compression for video in opencv


If I can suggest something, I would not use OpenCV to do this since you have no control on the output bitrate, distribution of colours, etc. You have better control of this using FFMPEG. Please see this post on how you can duplicate a video with lossless compression here: Lossless ffmpeg conversion/encoding

rayryeng
  • 102,964
  • 22
  • 184
  • 193