http - Serving video files from Tornado -
i have few hundred video files on server. since i'm not big business, find difficult transcode , maintain copies in storage available me. setup involves nginx , tornado.
to solve problem, decided try , transcode on fly , serve these temporary files on-demand.
before attempted create subprocess
try , convert files me, decided try , serve static video file via tornado.
class mediahandler(tornado.web.requesthandler): def serve(self, media, max_bufsize=1*1024*1024): bytes_read = 0 file_length = none remaining = none self.set_header('content-type', 'video/mp4') open(media.name, 'rb') f: while 1: # while transcoding, file size changing # compute every time file_length = os.fstat(f.fileno()).st_size # current length remaining = file_length - bytes_read # compute remaining bytes # we're producing faster we're consuming... if remaining == 0: break buffer_size = max_bufsize if max_bufsize < remaining else remaining bytes = f.read(buffer_size) assert len(bytes) == buffer_size bytes_read += buffer_size self.write(bytes) self.flush() def get(self): log('media request: %s' % (self.request.uri)) #xxx: hard-coded in sense..fix media_path = os.path.abspath(os.path.join(dir_path, self.request.uri[1:])) #strip leading '/' log('media_path: %s' % (media_path)) try: media = avprobe(media_path) except subprocessexception, e: raise e if media.video_codec not in valid_video_codecs or \ media.audio_codec not in valid_audio_codecs: # transcode , serve pass else: self.serve(media) self.finish()
i noticed when tried serve file tornado, firefox not load video @ all. upon checking, response headers follows:
http/1.1 200 ok date: thu, 02 jul 2015 00:04:50 gmt content-type: video/mp4 transfer-encoding: chunked connection: keep-alive server: tornadoserver/4.2
nginx seems generate following headers when ask serve same file:
http/1.1 206 partial content server: nginx/1.8.0 date: wed, 01 jul 2015 23:57:06 gmt content-type: video/mp4 content-length: 65796881 last-modified: thu, 09 apr 2015 21:47:56 gmt connection: keep-alive etag: "5526f38c-3ebfb11" content-range: bytes 0-65796880/65796881
it seems firefox doesn't data i'm sending it. spits out following on console:
media resource http://xxxx:8000/video_test/media/test.mp4 not decoded. candidate resources failed load. media load paused.
could point out i'm doing incorrectly , how serve static media tornado (without using staticfilehandler
..since don't think can use ultimate purpose).
also, there better way achieve on-the-fly transcoding+streaming?
browsers don't download whole file embedded video; want able issue http range
requests download partial chunks (to support seeking, etc). browsers strict , refuse load video @ if doesn't have correct content-range
header. unfortunately makes difficult serve video when file size not known in advance.
if files small enough can transcode whole thing temporary file before serving it, can , serve content-range
in same way staticfilehandler
does. if not, think may need use more complex protocol such hls or mpeg-dash make work browsers.
Comments
Post a Comment