When I execute this code I get the error as specified above.
import numpy as np
import struct ## new
# create a socket object
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind to the port
print("Socket bind complete")
# queue up to 5 requests
print("Socket now lisening")
# establish a connection
cap = cv2.VideoCapture('test.mp4')
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")
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")
ret, frame = cap.read()
if not ret:
print("no more frames")
result, frame = cv2.imencode('.jpg', frame)
data = pickle.dumps(frame)
size = len(data)
conn.sendall(struct.pack(">L", size) + data)
img_counter += 1
I added spaces in some places to make it more readable.
See PEP 8 – Style Guide for Python Code
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.
Thank you for your email.
Thank you. But, I we split the video in to frames and then assemble back to video the sound can be heard
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
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.