What is the opposite of cv2.VideoWriter_fourcc?
Question:
The function cv2.VideoWriter_fourcc
converts from a string (four chars) to an int.
For example, cv2.VideoWriter_fourcc(*'MJPG')
gives an int
for codec MJPG (whatever that is).
Does OpenCV provide the opposite function? I’d like to display the value as a string. I’d like to get a string from a fourcc int
value.
I could write the conversion myself, but I’d use something from OpenCV if it exists.
Answers:
I don’t think OpenCV has that conversion. Here is how to convert from fourcc numerical code to fourcc string character code (assuming the numerical number is the one returned by cv2.CAP_PROP_FOURCC
):
# python3
def decode_fourcc(cc):
return "".join([chr((int(cc) >> 8 * i) & 0xFF) for i in range(4)])
So for example if you open a video capture stream to get info about the codec or to get info about a specific codec you could try this:
c = cv2.VideoCapture(0)
# codec of current video
codec = c.get(cv2.CAP_PROP_FOURCC)
print(codec, decode_fourcc(codec))
# codec of mjpg
codec = cv2.VideoWriter_fourcc(*'MJPG')
print(codec, decode_fourcc(codec))
The function cv2.VideoWriter_fourcc
converts from a string (four chars) to an int.
For example, cv2.VideoWriter_fourcc(*'MJPG')
gives an int
for codec MJPG (whatever that is).
Does OpenCV provide the opposite function? I’d like to display the value as a string. I’d like to get a string from a fourcc int
value.
I could write the conversion myself, but I’d use something from OpenCV if it exists.
I don’t think OpenCV has that conversion. Here is how to convert from fourcc numerical code to fourcc string character code (assuming the numerical number is the one returned by cv2.CAP_PROP_FOURCC
):
# python3
def decode_fourcc(cc):
return "".join([chr((int(cc) >> 8 * i) & 0xFF) for i in range(4)])
So for example if you open a video capture stream to get info about the codec or to get info about a specific codec you could try this:
c = cv2.VideoCapture(0)
# codec of current video
codec = c.get(cv2.CAP_PROP_FOURCC)
print(codec, decode_fourcc(codec))
# codec of mjpg
codec = cv2.VideoWriter_fourcc(*'MJPG')
print(codec, decode_fourcc(codec))