0

i want to read a specific frame from the byte array. i found that byte array by reading the video file in read binary mode. i know that i can read a specific frame with the help of cv2 module. but i want to read it from byte array and want to save that frame as an image.

I have tried to read it like a text file but failed to read the full frame.

f1=open('video,mp4','rb')
f2=open('image.jpg','wb')
frame_count=0
for frame in f1:
    frame_count+=1
    if frame_count==50:
        f2.write(frame)
        f2.close()
        break
f1.close()

i got a byte string using that method but it did not work exactly.

  • checked out [this](https://stackoverflow.com/questions/51215625/python-function-to-read-video-and-convert-to-frames) Q? – FObersteiner Aug 06 '19 at 13:23
  • Using `for frame in f1` splits the file along newlines (byte `\x0A` aka `\n`), not frames. You should use an appropriate library with mp4 support unless you are very familiar with the format. – MisterMiyagi Aug 06 '19 at 13:25
  • 1
    Python doesn't know by default what a "frame" is in the context of an arbitrary binary file. You're going to need to tell it exactly what you're looking for. Do you know exactly how the file format works (at the byte level) for the file you're working with? If not, I strongly recommend using a module built for this purpose. – glibdud Aug 06 '19 at 13:26
  • I know that it is possible using opencv, but is there any way to read frame from binary data? – Grewal Grewal Aug 06 '19 at 13:32
  • in other words you can say that i want to know how opencv know that where is the end of each frame (in video file) – Grewal Grewal Aug 06 '19 at 13:35
  • Just to be clear, you realise that mp4 is a container format which supports several video and audio formats? You would have to replicate significant parts of an mp4 decoder to extract the frames. – MisterMiyagi Aug 06 '19 at 14:16
  • @GrewalGrewal The best place to get that information would be the [OpenCV source code](https://github.com/opencv/opencv). – glibdud Aug 06 '19 at 14:19

0 Answers0