File size: 1,859 Bytes
1fe2f2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import re
import subprocess
from pydub.utils import mediainfo
import math
from pydub import AudioSegment

def get_audio_duration(file_path):
    try:
        audio = AudioSegment.from_file(file_path)
        return len(audio) / 1000  # Duração em segundos
    except Exception as e:
        raise ValueError(f"Error extracting duration: {e}")

def cut_audio(file_path, start_time, end_time, output_path="output_cut.wav"):
    """Corta o áudio no intervalo de tempo especificado."""
    try:
        command = f"ffmpeg -y -i {file_path} -ss {start_time} -to {end_time} -acodec copy -loglevel debug {output_path}"
        
        subprocess.run(command, shell=True, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        return output_path
    except subprocess.CalledProcessError as e:
        # Exibe a mensagem de erro completa, incluindo o erro do FFmpeg
        raise RuntimeError(f"Erro ao cortar o áudio: {e.stderr}")
    
def convert(file_path, format_extension):
        file = file_path.replace("/tmp/", "")
        ffmpeg_cmd = [ 
                "ffmpeg",
                "-y",
                "-i", file_path,
                f"out_{file}"
            ]
     
        subprocess.run(ffmpeg_cmd)
        return f"out_{file}"

def add_silence_to_audio(file_path, start_silence=0, end_silence=0, output_path="output_with_silence.wav"):
    """Adiciona silêncio no início e no final do áudio usando subprocess."""
    try:
        command = f"ffmpeg -y -i {file_path} -af \"adelay={start_silence*1000}|{start_silence*1000},apad=pad_dur={end_silence}\" {output_path}"
        subprocess.run(command, shell=True, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        return output_path
    except subprocess.CalledProcessError as e:
        raise RuntimeError(f"Erro ao adicionar silêncio no áudio: {e}")