I’ve wrapped your code in backticks so it gets displayed right. a good quality post has a higher chance of getting answers.
please make it easy for others to help you. that includes removing irrelevant parts of your code.
you should use a debugger. please show that you’ve spent some appropriate effort to solve your problem on your own. show that you know what your own code does.
Thank you for your reply. It is a server program which sends the video file (test.mp4) to the client. On the client side, the video gets played but the sound couldn’t be heard. On the server side, i get the error message which i had posted earlier. I used imwrite() method to see the frames. Actually, the video test.mp4 has 132 frames. when the imencode() method is used I get 133 frames in which 133rd frame is empty. So, I feel due to the 133rd frame i am getting the error message.
Before you try to use frame you should check what you get in ret, frame and evetually exit loop when there is no more frames.
if not ret: # eventually `if ret is False:`
print("No more frames")
break
or
if frame is None: # will not work `if not frame:` because `frame` can be numpy array which may need `any()` or `all()` to check if it is True or False.
print("No more frames")
break
while True:
ret, frame = cap.read()
if not ret:
print("no more frames")
break
result, frame = cv2.imencode('.jpg', frame)
data = pickle.dumps(frame)
size = len(data)
print("{}:{}".format(img_counter, size))
conn.sendall(struct.pack(">L", size) + data)
img_counter += 1
As for sound: openCV doesn’t read sound. It was created only to work with video. Besides you convert to jpg which also can’t keep sound. It was created only to work with image.
If you want to stream video with sound then you have to use something different - ie. ffmpeg.
You can’t do this with cv2 because it doesn’t work with sound - it gives only image and sound is lost.
You would use ffmpeg to split file into two files - file with video, file with audio. Next you could use cv2 to change video and create new file with video. After that you could use again ffmpeg to join audio and video into one file again. And later you may send it as normal video file - ie. .mp4 - with video and audio.
I don’t know method to send it as single frames with audio.