Add video upload

This commit is contained in:
Jan Nowak 2022-06-09 15:11:48 +02:00
parent 1712349965
commit f82275df71
7 changed files with 108 additions and 2 deletions

View File

@ -117,5 +117,8 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/ # https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/' STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') #folder przechowywanych zdjec
MEDIA_URL = '/media/'

View File

@ -15,8 +15,17 @@ Including another URLconf
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('', include('core.urls')), path('', include('core.urls')),
] ]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
else:
urlpatterns += staticfiles_urlpatterns()

13
core/forms.py Normal file
View File

@ -0,0 +1,13 @@
from django import forms
from .models import VideoFile
# Form for upload video to the server
class VideoUploadForm(forms.ModelForm):
class Meta:
model = VideoFile
fields = ['file']
def __init__(self, *args, **kwargs):
super(VideoUploadForm, self).__init__(*args, **kwargs)
# In html input only accept video uploads
self.fields['file'].widget.attrs.update({'class': 'form-control border-0', 'accept': 'video/*'})

View File

@ -1,3 +1,31 @@
from django.db import models from django.db import models
from enum import Enum
# Create your models here. # Create your models here.
class VideoFile(models.Model):
"""
A class for uploading video to the server.
"""
def forDjango(cls):
cls.do_not_call_in_templates = True
return cls
@forDjango
class VideoFileStatus(Enum):
NEW = 1
PROCESSING = 4
READY = 7
file = models.FileField(upload_to="", max_length=1024) # If upload_to=="" then file will be uploaded to the MEDIA_ROOT path set in settings.
uploaded_at = models.DateTimeField(auto_now_add=True)
fps = models.IntegerField(default=25)
video_width = models.IntegerField(default=0, blank=True)
video_height = models.IntegerField(default=0, blank=True)
status = models.IntegerField(default=VideoFileStatus.NEW.value)
@property
def url(self):
return self.file.url

View File

@ -0,0 +1,31 @@
{% load static %}
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<script type="text/javascript" src="/static/js/jquery-3.6.0.min.js"></script>
<link rel="stylesheet" href="{% static 'css/main.css' %}">
<title>Web-TV2D</title>
</head>
<body>
<div>
<div class="d-flex flex-column min-vh-100 justify-content-center align-items-center">
<h1> Submit your video</h1>
<form id="form" method="POST" class="mt-4 text-center" enctype="multipart/form-data">
{% csrf_token %}
{% for message in messages %}
{% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}
<span style="color: red">{{message}}</span>
{% elif message.level == DEFAULT_MESSAGE_LEVELS.SUCCESS %}
<span style="color: green">{{message}}</span>
{% endif %}
{% endfor %}
{{ form.file }}
<input type="submit" class="btn btn-primary mt-2" value="Upload">
</form>
</div>
</div>
</body>
</html>

View File

@ -1,7 +1,29 @@
import cv2
from django.http import HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
from django.contrib import messages
from core.forms import VideoUploadForm
from core.models import VideoFile
# Create your views here. # Create your views here.
def home(request): def home(request):
return render(request, 'core/home.html') form = VideoUploadForm()
if request.method == "POST":
form = VideoUploadForm(request.POST, request.FILES)
if form.is_valid():
video_file = VideoFile(file=request.FILES['file'])
video_file.save()
video = cv2.VideoCapture(video_file.file.path)
video_file.fps = video.get(cv2.CAP_PROP_FPS)
video_file.video_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
video_file.video_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
video_file.save()
else:
messages.error(request, "Something went wrong")
context = {'form': form}
return render(request, 'core/home.html', context)