Skip to content

Commit

Permalink
trim sample audio using ffmpeg
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed Sep 3, 2024
1 parent 1643ce6 commit a71fa77
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions finder/contrib/audio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit a71fa77

Please sign in to comment.