support uploading videos in any of the supported resolutions, to avoid upscaling

This commit is contained in:
j 2014-02-15 16:13:43 +00:00
commit 6321859796
4 changed files with 41 additions and 15 deletions

View file

@ -308,13 +308,19 @@ class File(models.Model):
return True
return False
def save_chunk_stream(self, chunk, chunk_id=-1, done=False):
def stream_resolution(self):
config = settings.CONFIG['video']
height = self.info['video'][0]['height'] if self.info.get('video') else None
for resolution in sorted(config['resolutions']):
if height and height <= resolution:
return resolution
return resolution
def save_chunk_stream(self, chunk, chunk_id, resolution, format, done):
if not self.available:
config = settings.CONFIG['video']
stream, created = Stream.objects.get_or_create(
file=self,
resolution=max(config['resolutions']),
format=config['formats'][0])
file=self, resolution=resolution, format=format)
if created:
stream.media.name = stream.path(stream.name())
ox.makedirs(os.path.dirname(stream.media.path))

View file

@ -112,9 +112,10 @@ def extract_stream(fileId):
file = models.File.objects.get(id=fileId)
if file.data:
config = settings.CONFIG['video']
resolution = file.stream_resolution()
stream, created = models.Stream.objects.get_or_create(
file=file, resolution=max(config['resolutions']),
format=config['formats'][0])
file=file, resolution=resolution, format=config['formats'][0]
)
if created:
stream.media.name = stream.path(stream.name())
stream.encode()

View file

@ -224,7 +224,13 @@ def firefogg_upload(request):
profile = request.GET['profile']
oshash = request.GET['id']
config = settings.CONFIG['video']
video_profile = "%sp.%s" % (max(config['resolutions']), config['formats'][0])
resolution, format = profile.split('p.')
resolution = int(resolution)
if resolution not in config['resolutions'] \
or format not in config['formats']:
response = json_response(status=500, text='invalid profile')
return render_to_json_response(response)
#handle video upload
if request.method == 'POST':
@ -232,14 +238,15 @@ def firefogg_upload(request):
if 'chunk' in request.FILES and oshash:
f = get_object_or_404(models.File, oshash=oshash)
form = ChunkForm(request.POST, request.FILES)
if form.is_valid() and profile == video_profile and f.editable(request.user):
if form.is_valid() and f.editable(request.user):
c = form.cleaned_data['chunk']
chunk_id = form.cleaned_data['chunkId']
response = {
'result': 1,
'resultUrl': request.build_absolute_uri('/%s'%f.item.itemId)
}
if not f.save_chunk_stream(c, chunk_id, form.cleaned_data['done']):
if not f.save_chunk_stream(c, chunk_id, resolution, format,
form.cleaned_data['done']):
response['result'] = -1
elif form.cleaned_data['done']:
f.uploading = False
@ -255,7 +262,7 @@ def firefogg_upload(request):
response['done'] = 1
return render_to_json_response(response)
#init upload
elif oshash and profile == video_profile:
elif oshash:
#404 if oshash is not know, files must be registered via update api first
f = get_object_or_404(models.File, oshash=oshash)
if f.editable(request.user):