librelist archives

« back to archive

Need some help with streaming a movie (mp4) file

Need some help with streaming a movie (mp4) file

From:
hbd
Date:
2014-01-20 @ 08:38
Hi I am hoping someone can help me with this one.  I am trying to write 
some code to stream a very large (~450mb) mp4 file using Flask.

Here is my code so far:

@app.route('/streammovie',methods=['GET'])	
def streamMovie():
	def generate():
		with open('/var/www/ho/carol-2011.mp4', "rb") as f:
			byte = f.read(1)
			while byte:
				yield byte
				byte = f.read(1)
	t = os.stat('/var/www/ho/carol-2011.mp4')
	sz = str(t.st_size)
	return 

Response(generate(),mimetype='video/mp4',headers={"Content-Type":"video/mp4","Content-Disposition":"inline","Content-Transfer-Enconding":"binary","Content-Length":sz})


After waiting for the browser to return for a few minutes, my Apache 
error log finally prints:
(32)Broken pipe: core_output_filter: writing data to the network
mod_wsgi (pid=27062): Exception occurred processing WSGI script 
'/var/www/ho/ho.wsgi'.
IOError: failed to write data

I've looked over the headers, and they seem to be right.  It does seem 
to read a byte at a time and yield it.  So now I'm stuck.  Any help is 
much appreciated, thanks.


Re: [flask] Need some help with streaming a movie (mp4) file

From:
Leiser Fernandez Gallo
Date:
2014-01-20 @ 17:11
Don't do that! You should use a static server for this kind of tasks 
(nginex, lighttpd ...)
----- Mensaje original -----
> Hi I am hoping someone can help me with this one.  I am trying to write
> some code to stream a very large (~450mb) mp4 file using Flask.
> 
> Here is my code so far:
> 
> @app.route('/streammovie',methods=['GET'])
> def streamMovie():
> 	def generate():
> 		with open('/var/www/ho/carol-2011.mp4', "rb") as f:
> 			byte = f.read(1)
> 			while byte:
> 				yield byte
> 				byte = f.read(1)
> 	t = os.stat('/var/www/ho/carol-2011.mp4')
> 	sz = str(t.st_size)
> 	return
> 
Response(generate(),mimetype='video/mp4',headers={"Content-Type":"video/mp4","Content-Disposition":"inline","Content-Transfer-Enconding":"binary","Content-Length":sz})
> 
> 
> After waiting for the browser to return for a few minutes, my Apache
> error log finally prints:
> (32)Broken pipe: core_output_filter: writing data to the network
> mod_wsgi (pid=27062): Exception occurred processing WSGI script
> '/var/www/ho/ho.wsgi'.
> IOError: failed to write data
> 
> I've looked over the headers, and they seem to be right.  It does seem
> to read a byte at a time and yield it.  So now I'm stuck.  Any help is
> much appreciated, thanks.
> 
> 
> 
> 

________________________________________________________________________________________________
III Escuela Internacional de Invierno en la UCI del 17 al 28 de febrero 
del 2014. Ver www.uci.cu

Re: [flask] Need some help with streaming a movie (mp4) file

From:
Denis Gaydukov
Date:
2014-01-20 @ 09:19
Hi, your code works for me:

from flask import Flask, Response
import os

app = Flask(__name__)

PATH = os.path.abspath(os.path.dirname(__file__))
PATH = os.path.join(PATH, 'static', 'video_test_512kb.mp4')


@app.route('/streammovie', methods=['GET'])
def streamMovie():
    def generate():
        with open(PATH, "rb") as f:
            byte = f.read(512)
            while byte:
                yield byte
                byte = f.read(512)

    t = os.stat(PATH)
    sz = str(t.st_size)
    return

Response(generate(),mimetype='video/mp4',headers={"Content-Type":"video/mp4","Content-Disposition":"inline","Content-Transfer-Enconding":"binary","Content-Length":sz})


if __name__ == '__main__':
    app.run(debug=True)


But I've read 512 bytes at a time.

I'm using Windows 7 and built-in server from Flask. I think, trouble can be
in your web-server settings
Or try to user HTTP headers:

Accept-Ranges: bytes
Content-Range: bytes 0-file_size/file_size


Reply with results, cheers

Re: [flask] Need some help with streaming a movie (mp4) file

From:
Matthias Urlichs
Date:
2014-01-20 @ 10:52
Hi,

hbd:
> I've looked over the headers, and they seem to be right.  It does seem 
> to read a byte at a time and yield it.

Why the *censored* do you read _one_byte_ at a time? That's much too slow.

Also, Apache timeouts can be changed.

-- 
-- Matthias Urlichs