52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
|
|
import os
|
||
|
|
import ox
|
||
|
|
|
||
|
|
from django.conf import settings
|
||
|
|
from django.shortcuts import get_object_or_404
|
||
|
|
|
||
|
|
import requests
|
||
|
|
|
||
|
|
from oxdjango.http import HttpFileResponse
|
||
|
|
from item import models
|
||
|
|
from archive import extract
|
||
|
|
|
||
|
|
def extract_source_frame(self, position, format):
|
||
|
|
offset = 0
|
||
|
|
streams = self.streams()
|
||
|
|
for stream in streams:
|
||
|
|
if stream.duration + offset < position:
|
||
|
|
offset += stream.duration
|
||
|
|
else:
|
||
|
|
if not stream.file.is_video or not stream.file.info.get('video'):
|
||
|
|
return None
|
||
|
|
position = position - offset
|
||
|
|
path = None
|
||
|
|
if stream.file.data:
|
||
|
|
height = stream.file.info['video'][0]['height']
|
||
|
|
video_path = stream.file.data.path
|
||
|
|
path = os.path.join(settings.MEDIA_ROOT, stream.path(),
|
||
|
|
'source-frames', "%dp" % height, "%s.%s" % (position, format))
|
||
|
|
if not os.path.exists(path) and stream.media:
|
||
|
|
extract.frame(video_path, path, position, height, info=stream.file.info)
|
||
|
|
if not os.path.exists(path):
|
||
|
|
return None
|
||
|
|
return path
|
||
|
|
|
||
|
|
def source_frame(request, id, position=None, format="jpg"):
|
||
|
|
if format not in ("jpg", "png"):
|
||
|
|
format = "jpg"
|
||
|
|
content_type = "image/jpeg" if format == "jpg" else "image/png"
|
||
|
|
item = get_object_or_404(models.Item, public_id=id)
|
||
|
|
if not item.access(request.user):
|
||
|
|
return HttpResponseForbidden()
|
||
|
|
frame = None
|
||
|
|
if not position:
|
||
|
|
position = item.poster_frame
|
||
|
|
else:
|
||
|
|
position = float(position.replace(',', '.'))
|
||
|
|
|
||
|
|
frame = extract_source_frame(item, position, format)
|
||
|
|
response = HttpFileResponse(frame, content_type=content_type)
|
||
|
|
if request.method == 'OPTIONS':
|
||
|
|
response.allow_access()
|
||
|
|
return response
|