Пользовательский фильтр Django для получения длительности видео с помощью MoviePy не работает
Я пытаюсь использовать пользовательский фильтр для поиска длительности видео с помощью moviepy, однако функция не может получить доступ к файлам, хотя кажется, что правильный путь к видео передается в функцию в пользовательском фильтре.
Я получаю следующую ошибку:
OSError at /tape/tape/
MoviePy error: the file /media/video_files/channel_Busy/171003B_026_2k.mp4 could not be found!
Please check that you entered the correct path.
Пользовательский фильтр:
vduration.py
from django import template
import moviepy.editor
register = template.Library()
@register.filter(name='vduration')
def vduration(videourl):
video = moviepy.editor.VideoFileClip(videourl)
video_time = int(video.duration)
return video_time
views.py
def tape(request):
tapes=VideoFiles.objects.all()
context={
"videos": tapes,
}
return render(request, "tape/tape.html", context)
tape.py
{% for video in videos %}
<div class="video-time">{{video.video.url | vduration}}</div>
{% endfor %}
models.py
class VideoFiles(models.Model):
id=models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
video=models.FileField(upload_to=channel_directory_path)
def get_absolute_url(self):
return reverse('video_watch', args=[str(self.id)])
def __str__(self):
return f"video_file_{self.id}"
Используйте абсолютный путь к файлу media.
vduration.py
import os
from django import template
import moviepy.editor
from your.settings.file import BASE_DIR
register = template.Library()
@register.filter(name='vduration')
def vduration(videourl):
video = moviepy.editor.VideoFileClip(os.path.join(BASE_DIR, videourl))
video_time = int(video.duration)
return video_time