我正在尝试使用cherrypy 流式传输视频文件。当我转到localhost:8080/stream?video=video.avi时,它开始下载,但几秒钟后,无论文件有多大,它都会“完成”下载。我对此很陌生,无法找出它为什么这样做。另外,如果它是 Matroska (.mkv) 为什么它甚至不能识别文件?
这是我的流类:
class Stream(object):
@cherrypy.expose
def default(self, video=None):
BASE_PATH = ".."
video = os.path.join(BASE_PATH, video)
if video == None:
return "no file specified!"
if not os.path.exists(video):
return "file not found!"
f = open(video)
size = os.path.getsize(video)
mime = mimetypes.guess_type(video)[0]
print(mime)
cherrypy.response.headers["Content-Type"] = mime
cherrypy.response.headers["Content-Disposition"] = 'attachment; filename="%s"' % os.path.basename(video)
cherrypy.response.headers["Content-Length"] = size
BUF_SIZE = 1024 * 5
def stream():
data = f.read(BUF_SIZE)
while len(data) > 0:
yield data
data = f.read(BUF_SIZE)
return stream()
default._cp_config = {'response.stream': True}