From a71fa77161f6c078849048c12f32ccbe5c2966b5 Mon Sep 17 00:00:00 2001 From: Jacob Rief Date: Tue, 3 Sep 2024 23:30:03 +0200 Subject: [PATCH] trim sample audio using ffmpeg --- finder/contrib/audio/models.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/finder/contrib/audio/models.py b/finder/contrib/audio/models.py index 73fb8f8c4..bf00f2101 100644 --- a/finder/contrib/audio/models.py +++ b/finder/contrib/audio/models.py @@ -6,7 +6,7 @@ from filer import settings as filer_settings from finder.models.file import FileModel -from pydub import AudioSegment +import ffmpeg SAMPLE_DURATION = 5 @@ -30,12 +30,13 @@ def get_sample_url(self): sample_path = self.get_sample_path(sample_start, sample_duration) if not default_storage.exists(sample_path): (default_storage.base_location / sample_path.parent).mkdir(parents=True, exist_ok=True) - audio_sample = AudioSegment.from_file( - default_storage.path(self.file_path), - start_second=sample_start, - duration=sample_duration, - ) - audio_sample.export(default_storage.path(sample_path)) + stream = ffmpeg.input(default_storage.path(self.file_path)) + stream = ffmpeg.filter(stream.audio, 'atrim', start=sample_start, duration=SAMPLE_DURATION) + stream = ffmpeg.output(stream, default_storage.path(sample_path)) + try: + ffmpeg.run(stream) + except ffmpeg.Error as exp: + return self.fallback_thumbnail_url return default_storage.url(sample_path) def get_sample_path(self, sample_start, sample_duration):