init commit
This commit is contained in:
0
video/__init__.py
Normal file
0
video/__init__.py
Normal file
11
video/admin.py
Normal file
11
video/admin.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from django.contrib import admin
|
||||
from .models import Video, ExerciseVideo
|
||||
|
||||
# Register your models here.
|
||||
@admin.register(Video)
|
||||
class VideoAdmin(admin.ModelAdmin):
|
||||
list_display = ("video_file", "gender",)
|
||||
|
||||
@admin.register(ExerciseVideo)
|
||||
class ExerciseVideoAdmin(admin.ModelAdmin):
|
||||
list_display = ("video_file",)
|
||||
6
video/apps.py
Normal file
6
video/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class VideoConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'video'
|
||||
23
video/migrations/0001_initial.py
Normal file
23
video/migrations/0001_initial.py
Normal file
@@ -0,0 +1,23 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-14 19:24
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Video',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('video_file', models.FileField(null=True, upload_to='videos/', verbose_name='')),
|
||||
],
|
||||
),
|
||||
]
|
||||
19
video/migrations/0002_video_gender.py
Normal file
19
video/migrations/0002_video_gender.py
Normal file
@@ -0,0 +1,19 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-14 19:28
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('video', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='video',
|
||||
name='gender',
|
||||
field=models.PositiveSmallIntegerField(choices=[(1, 'male'), (2, 'female'), (3, 'both')], default=1),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
||||
22
video/migrations/0003_exercisevideo.py
Normal file
22
video/migrations/0003_exercisevideo.py
Normal file
@@ -0,0 +1,22 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-29 17:25
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('video', '0002_video_gender'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ExerciseVideo',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('video_file', models.FileField(null=True, upload_to='exercise_videos/', verbose_name='')),
|
||||
],
|
||||
),
|
||||
]
|
||||
18
video/migrations/0004_alter_video_gender.py
Normal file
18
video/migrations/0004_alter_video_gender.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-08-08 23:32
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('video', '0003_exercisevideo'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='video',
|
||||
name='gender',
|
||||
field=models.PositiveSmallIntegerField(choices=[(1, 'male'), (2, 'female'), (3, 'anything goes')]),
|
||||
),
|
||||
]
|
||||
0
video/migrations/__init__.py
Normal file
0
video/migrations/__init__.py
Normal file
39
video/models.py
Normal file
39
video/models.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from django.db import models
|
||||
from django.db.models.signals import pre_delete
|
||||
from django.dispatch import receiver
|
||||
from .tasks import create_hls_tasks
|
||||
|
||||
# Create your models here.
|
||||
VIDEO_GENDER = (
|
||||
(1, "male"),
|
||||
(2, "female"),
|
||||
(3, "anything goes"),
|
||||
)
|
||||
|
||||
class Video(models.Model):
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
video_file = models.FileField(upload_to='videos/', null=True, verbose_name="")
|
||||
gender = models.PositiveSmallIntegerField(
|
||||
choices=VIDEO_GENDER
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.video_file)
|
||||
|
||||
def save(self, **kwargs):
|
||||
super(Video, self).save(**kwargs)
|
||||
filename = self.video_file.name
|
||||
create_hls_tasks.delay(filename)
|
||||
|
||||
|
||||
|
||||
class ExerciseVideo(models.Model):
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
video_file = models.FileField(upload_to='exercise_videos/', null=True, verbose_name="")
|
||||
|
||||
@receiver(pre_delete, sender=ExerciseVideo)
|
||||
def delete_exercise_video(sender, instance, using, **kwargs):
|
||||
video_file = instance.video_file
|
||||
video_file.delete(save=False)
|
||||
13
video/serializers.py
Normal file
13
video/serializers.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from rest_framework import serializers
|
||||
from .models import *
|
||||
|
||||
class VideoSerializer(serializers.ModelSerializer):
|
||||
gender_value = serializers.CharField(source='get_gender_display')
|
||||
video_file = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Video
|
||||
fields = ('video_file', 'gender_value',)
|
||||
|
||||
def get_video_file(self, obj):
|
||||
return '/media/' + obj.video_file.name + '_720p.m3u8'
|
||||
17
video/tasks.py
Normal file
17
video/tasks.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from celery import shared_task
|
||||
import os
|
||||
from django.conf import settings
|
||||
import ffmpeg_streaming
|
||||
from ffmpeg_streaming import Formats, Bitrate, Representation, Size
|
||||
from django.core.files.storage import default_storage
|
||||
|
||||
@shared_task()
|
||||
def create_hls_tasks(filename):
|
||||
end_location = str(settings.MEDIA_ROOT) + "/" + str(filename) +'.m3u8'
|
||||
if not default_storage.exists(end_location):
|
||||
media_location = str(settings.MEDIA_ROOT) + "/" + str(filename)
|
||||
video = ffmpeg_streaming.input(media_location)
|
||||
hls = video.hls(Formats.h264())
|
||||
_720p = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))
|
||||
hls.representations(_720p)
|
||||
hls.output(end_location)
|
||||
3
video/tests.py
Normal file
3
video/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
9
video/urls.py
Normal file
9
video/urls.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path('nsfw_videos/', views.nsfw_videos, name='nsfw videos'),
|
||||
path('create_hls/', views.create_hls, name='create hls video streaming'),
|
||||
# path('hls_video/', views.hls_videos, name='hls video streaming')
|
||||
]
|
||||
75
video/views.py
Normal file
75
video/views.py
Normal file
@@ -0,0 +1,75 @@
|
||||
from .models import *
|
||||
from .serializers import *
|
||||
|
||||
from django.shortcuts import render
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth import authenticate
|
||||
from rest_framework.authentication import TokenAuthentication
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.decorators import authentication_classes
|
||||
from rest_framework.decorators import permission_classes
|
||||
from django.shortcuts import get_object_or_404
|
||||
from datetime import datetime
|
||||
import json
|
||||
import os
|
||||
from django.core.cache import cache
|
||||
from django.http import JsonResponse
|
||||
|
||||
from django.conf import settings
|
||||
import ffmpeg_streaming
|
||||
from ffmpeg_streaming import Formats, Bitrate, Representation, Size
|
||||
from django.core.files.storage import default_storage
|
||||
from .tasks import create_hls_tasks
|
||||
|
||||
|
||||
# Create your views here.
|
||||
@api_view(['GET'])
|
||||
@authentication_classes([TokenAuthentication])
|
||||
@permission_classes([IsAuthenticated])
|
||||
def nsfw_videos(request):
|
||||
if 'nsfw_videos' in cache:
|
||||
data = cache.get('nsfw_videos')
|
||||
if len(data) > 0:
|
||||
return Response(data=data, status=status.HTTP_200_OK)
|
||||
|
||||
users = Video.objects.all()
|
||||
serializer = VideoSerializer(users, many=True)
|
||||
data = serializer.data
|
||||
cache.set('nsfw_videos', data, timeout=None)
|
||||
return Response(data=data, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
@api_view(['GET'])
|
||||
@authentication_classes([TokenAuthentication])
|
||||
@permission_classes([IsAuthenticated])
|
||||
def hls_videos(request):
|
||||
video_url = request.GET.get('video_name', '')
|
||||
type = request.GET.get('video_type', '')
|
||||
|
||||
end_location = str(settings.MEDIA_ROOT) + '/hls/'+ video_url +'.m3u8'
|
||||
end_file_name = '/media/hls/'+ video_url +'_720p.m3u8'
|
||||
|
||||
if default_storage.exists(end_location):
|
||||
return JsonResponse({'file_location': end_file_name})
|
||||
|
||||
media_location = os.path.join(settings.MEDIA_ROOT) + "/" + type + "/" + video_url
|
||||
video = ffmpeg_streaming.input(media_location)
|
||||
|
||||
hls = video.hls(Formats.h264())
|
||||
_720p = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))
|
||||
hls.representations(_720p)
|
||||
hls.output(end_location)
|
||||
# {{url}}/videos/hls_video?video_name=Spiderman_Stretch.mp4&video_type=exercise_videos
|
||||
# {{url}}/videos/hls_video?video_name=Recover_24.mp4&video_type=videos
|
||||
return JsonResponse({'file_location': end_file_name})
|
||||
|
||||
@api_view(['GET'])
|
||||
@authentication_classes([TokenAuthentication])
|
||||
@permission_classes([IsAuthenticated])
|
||||
def create_hls(request):
|
||||
create_hls_tasks.delay()
|
||||
return JsonResponse({'running': "running"})
|
||||
Reference in New Issue
Block a user