Skip to content

Commit

Permalink
Merge pull request #20 from arbisoft/feature/update-custom-admin-model
Browse files Browse the repository at this point in the history
📹 Override VideoAsset save method to include fize size and duration
  • Loading branch information
sameeramin authored Feb 6, 2025
2 parents cab5896 + 37c5d4c commit 298d809
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ FROM python:3.12-slim AS base
RUN apt-get update && apt-get install -y \
build-essential \
libpq-dev \
ffmpeg \
&& rm -rf /var/lib/apt/lists/*

ENV PYTHONUNBUFFERED 1
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ services:
command: bash -c "python manage.py runserver 0.0.0.0:${DJANGO_PORT}"
volumes:
- .:/app
- ./media:/app/media
- ./media:/app/arbisoft_sessions_portal/media
ports:
- "${DJANGO_PORT}:${DJANGO_PORT}"
depends_on:
Expand Down
2 changes: 1 addition & 1 deletion events/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class VideoAssetForm(forms.ModelForm):

class Meta:
model = VideoAsset
fields = ('title', 'video_file', 'duration', 'thumbnail', 'file_size')
fields = ('title', 'video_file', 'thumbnail')

def clean(self):
""" Infer, save and clean the data related to videoasset """
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.16 on 2025-02-05 15:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('events', '0002_remove_videoasset_cdn_url_and_more'),
]

operations = [
migrations.AlterField(
model_name='videoasset',
name='duration',
field=models.IntegerField(default=0),
),
migrations.AlterField(
model_name='videoasset',
name='file_size',
field=models.BigIntegerField(default=0),
),
]
26 changes: 24 additions & 2 deletions events/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ffmpeg

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.files.storage import FileSystemStorage
Expand Down Expand Up @@ -65,12 +67,32 @@ class VideoStatus(models.TextChoices):
event = models.ForeignKey(Event, on_delete=models.DO_NOTHING, related_name='videos')
title = models.CharField(max_length=255)
video_file = models.FileField(storage=video_storage, null=True, blank=True)
duration = models.IntegerField() # in seconds
duration = models.IntegerField(default=0) # in seconds
thumbnail = models.ImageField(storage=thumbnail_storage, null=True, blank=True)
status = models.CharField(max_length=20, choices=VideoStatus.choices)
file_size = models.BigIntegerField() # in bytes
file_size = models.BigIntegerField(default=0) # in bytes
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)

def save(self, *args, **kwargs):
""" Override save method to extract file size and duration """
if self.video_file:
super().save(*args, **kwargs)

self.file_size = self.video_file.size
video_path = self.video_file.path
try:
metadata = ffmpeg.probe(video_path)
duration = float(metadata['format']['duration'])
self.duration = int(duration) # Convert to seconds
except ffmpeg.Error as e:
print(f"FFmpeg processing error: {e.stderr.decode() if hasattr(e, 'stderr') else e}")
except KeyError:
print("Metadata does not contain 'duration'. Invalid file format.")
except ValueError:
print("Invalid duration value, unable to convert to float.")

super().save(*args, **kwargs)

def __str__(self):
return self.title
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ django-filter==24.3
djangorestframework==3.15.2
djangorestframework-simplejwt==5.3.1
drf-spectacular==0.28.0
ffmpeg-python==0.2.0
idna==3.10
Markdown==3.7
pillow==11.1.0
Expand Down

0 comments on commit 298d809

Please sign in to comment.