From 7fdae917cf9d89fdd2e6c9d3e7ec98d0544a5df7 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 1 Jan 2011 15:55:13 +0530 Subject: [PATCH] chop to disk to get index and stream from where instead of loading it all into ram --- pandora/archive/extract.py | 20 ++++++++++++-------- pandora/item/views.py | 18 ++++++++++++------ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/pandora/archive/extract.py b/pandora/archive/extract.py index fd8472df..95a64128 100644 --- a/pandora/archive/extract.py +++ b/pandora/archive/extract.py @@ -392,10 +392,10 @@ def timeline_strip(item, cuts, info, prefix): print 'writing', timeline_file timeline_image.save(timeline_file) -def chop(response, video, start, end): - if end <= start: - return '' +def chop(video, start, end): t = end - start + tmp = tempfile.mkdtemp() + choped_video = '%s/tmp.webm' % tmp cmd = [ 'ffmpeg', '-y', @@ -405,9 +405,13 @@ def chop(response, video, start, end): '-vcodec', 'copy', '-acodec', 'copy', '-f', 'webm', - '/dev/stdout' + choped_video ] - p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=open('/dev/null', 'w')) - stdout, stderr = p.communicate() - return stdout - + p = subprocess.Popen(cmd, stdin=subprocess.PIPE, + stdout=open('/dev/null', 'w'), + stderr=open('/dev/null', 'w')) + p.wait() + f = open(choped_video, 'r') + os.unlink(choped_video) + os.rmdir(tmp) + return f diff --git a/pandora/item/views.py b/pandora/item/views.py index 8b2826cf..33882587 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -425,13 +425,11 @@ def video(request, id, profile): path = stream.video.path content_type = path.endswith('.mp4') and 'video/mp4' or 'video/webm' #server side cutting - #FIXME: find way to not load video into ram, - # this can easily get to much for the server t = request.GET.get('t', None) - if t: + if content_type == 'video/webm' and t: t = map(float, t.split(',')) - if len(t) == 2: - response = HttpResponse(content_type=content_type) + if len(t) == 2 and t[1] > t[0] and stream.duration>=t[1]: + response = HttpResponse(extract.chop(path, t[0], t[1]), content_type=content_type) filename = "Clip of %s - %s-%s - %s %s.webm" % ( item.get('title'), ox.formatDuration(t[0] * 1000), @@ -440,7 +438,15 @@ def video(request, id, profile): item.itemId ) response['Content-Disposition'] = 'attachment; filename="%s"' % filename - response.write(extract.chop(response, path, t[0], t[1])) + return response + else: + filename = "%s - %s %s.webm" % ( + item.get('title'), + settings.SITENAME, + item.itemId + ) + response = HttpFileResponse(path, content_type=content_type) + response['Content-Disposition'] = 'attachment; filename="%s"' % filename return response return HttpFileResponse(path, content_type=content_type)