用python从字节保存视频

用python从字节保存视频,python,rest,opencv,Python,Rest,Opencv,我有两个微服务,A是用java编写的,并以bytes[]的形式向B发送用python编写的视频 B正在使用openCV对视频进行处理,尤其是使用此命令 stream = cv2.VideoCapture(video) 当由流媒体或现成的本地视频提供时,该命令可以正常工作,但是当我向它发出请求时 TypeError:需要整数(获取类型字节) 所以我的问题是: 有没有办法将我从java接收到的字节保存到磁盘上,或者我可以直接将字节发送给cv2.capture 谢谢。我这样解决了我的问题: FIL

我有两个微服务,A是用java编写的,并以bytes[]的形式向B发送用python编写的视频

B正在使用openCV对视频进行处理,尤其是使用此命令

 stream = cv2.VideoCapture(video)
当由流媒体或现成的本地视频提供时,该命令可以正常工作,但是当我向它发出请求时

TypeError:需要整数(获取类型字节)

所以我的问题是:

有没有办法将我从java接收到的字节保存到磁盘上,或者我可以直接将字节发送给cv2.capture


谢谢。

我这样解决了我的问题:

FILE_OUTPUT = 'output.avi'

# Checks and deletes the output file
# You cant have a existing file or it will through an error
if os.path.isfile(FILE_OUTPUT):
    os.remove(FILE_OUTPUT)

out_file = open(FILE_OUTPUT, "wb") # open for [w]riting as [b]inary
out_file.write(request.data)
out_file.close()

我这样解决了我的问题:

FILE_OUTPUT = 'output.avi'

# Checks and deletes the output file
# You cant have a existing file or it will through an error
if os.path.isfile(FILE_OUTPUT):
    os.remove(FILE_OUTPUT)

out_file = open(FILE_OUTPUT, "wb") # open for [w]riting as [b]inary
out_file.write(request.data)
out_file.close()

只需对您自己的解决方案稍加改进:即使发生意外情况,也可以将
上下文管理器一起使用来关闭文件:

FILE_OUTPUT = 'output.avi'

# Checks and deletes the output file
# You cant have a existing file or it will through an error
if os.path.isfile(FILE_OUTPUT):
    os.remove(FILE_OUTPUT)

# opens the file 'output.avi' which is accessable as 'out_file'
with open(FILE_OUTPUT, "wb") as out_file:  # open for [w]riting as [b]inary
    out_file.write(request.data)

只需对您自己的解决方案稍加改进:即使发生意外情况,也可以将
上下文管理器一起使用来关闭文件:

FILE_OUTPUT = 'output.avi'

# Checks and deletes the output file
# You cant have a existing file or it will through an error
if os.path.isfile(FILE_OUTPUT):
    os.remove(FILE_OUTPUT)

# opens the file 'output.avi' which is accessable as 'out_file'
with open(FILE_OUTPUT, "wb") as out_file:  # open for [w]riting as [b]inary
    out_file.write(request.data)

可能是重复的不,因为我没有小视频,效率不高,一个字节一个字节地读取是不好的,所以你想保存流视频文件吗?我有一个从java发送到python的字节数组,我想通过命令cv2.capture给open cv。因为它不接受字节数组,所以我正在寻找一种方法,要么将视频保存到磁盘,然后将其提供给她,要么尝试配置她或其他东西,以便它接受字节数组可能是不的重复因为我没有一个小视频,一个字节一个字节地读取是不好的,所以你想保存流视频文件吗?我有一个从java发送到python的字节数组,我想通过命令cv2.capture给open cv。因为它不接受字节数组,所以我正在寻找一种方法,要么将视频保存到磁盘,然后将其提供给她,要么尝试配置她或其他东西,以便它接受字节数组和我删除结束部分?确切地说-只要您“离开”with语句,它就会自动关闭文件:)而我删除结束部分?确切地说-只要您“离开”with语句,它就会自动关闭文件:)