5

I am trying to stream a video file using cherrypy. When I go to localhost:8080/stream?video=video.avi it starts downloading, but after a few seconds it just "completes" the download no matter how large the file is. I'm rather new to this and cannot find out why it is doing that. Also, why doesn't it even recognize the file if it is Matroska (.mkv) ?

Here is my Stream class:

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}
DemetriOS
  • 181
  • 13
Urho
  • 2,232
  • 1
  • 24
  • 34

1 Answers1

2

I realised that all I needed to do was to change open(video) to open(video, 'rb') so that it would read the file in binary mode. After that the file downloaded completely and worked.

Urho
  • 2,232
  • 1
  • 24
  • 34
  • Doing something similar. Also getting a sort of stop when buffering and also not sure how to continue downloading the source once the buffer size has been met. – Twisty Dec 30 '13 at 18:27