我有一个mp4视频文件的二进制字符串,并希望将其转换回mp4文件,以便用openCV库
获取单帧import cv2
import tempfile
temp_path = tempfile.gettempdir()
video_binary_string = 'AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAQC0ttZGF0AQIUGRQmM...'
with open(temp_path+'/video.mp4', 'wb') as wfile:
wfile.write(video_binary_string)
cap = cv2.VideoCapture(temp_path+'/video.mp4')
success, frame = cap.read()
print success
>>> False
如果正确读取了帧,则 True
据我所知,这不是编写视频文件的正确方法。我试图用FFMPEG来做,但我无法理解它的文档。
请举例说明如何使用此方式或其他方式将二进制字符串转换回视频。
答案 0 :(得分:0)
我的问题的解决方案是我必须首先解码编码的字符串
import cv2
import tempfile
import base64
temp_path = tempfile.gettempdir()
video_binary_string = 'AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAQC0ttZGF0AQIUGRQmM...'
decoded_string = base64.b64decode(video_binary_string)
with open(temp_path+'/video.mp4', 'wb') as wfile:
wfile.write(decoded_string)
cap = cv2.VideoCapture(temp_path+'/video.mp4')
success, frame = cap.read()
print success
>>> True