feat: Replace pydub with ffmpeg for audio duration and metadata extraction in sound services

This commit is contained in:
JSC
2025-07-19 09:40:31 +02:00
parent 4cfc2ec0a2
commit b1f9667edd
4 changed files with 32 additions and 39 deletions

View File

@@ -7,7 +7,6 @@ import re
from pathlib import Path
import ffmpeg
from pydub import AudioSegment
from app.database import db
from app.models.sound import Sound
@@ -632,9 +631,17 @@ class SoundNormalizerService:
# Calculate file hash
file_hash = SoundNormalizerService._calculate_file_hash(file_path)
# Get duration using pydub
audio = AudioSegment.from_wav(file_path)
duration = len(audio) # Duration in milliseconds
# Get duration using ffmpeg
probe = ffmpeg.probe(file_path)
audio_stream = next(
(s for s in probe['streams'] if s['codec_type'] == 'audio'),
None
)
if audio_stream and 'duration' in audio_stream:
duration = int(float(audio_stream['duration']) * 1000) # Convert to milliseconds
else:
duration = 0
return {
"duration": duration,