Search code examples
pythonffmpegjupyter-notebookaudio-processing

ffmpeg command problem in python to remove silences at the beggining and end. error: returned non-zero exit status 4294967274


I have a problem regarding ffmpeg use in jupyter notebooks. I'm trying to use this funcion for an audio, but I get the below error, I have tried changing the file to wav and different specifications of the command code. But nothing works.

def remove_silence(input_file):
    """
    Remove silence from the start and end of an audio file and overwrite the original file.

    Args:
    input_file (str): Path to the audio file.

    Returns:
    str: Path to the processed audio file (same as input path).
    """
    # Construir el comando FFmpeg en una lista
    ffmpeg_cmd = [
        "ffmpeg",
        "-y",  # Sobrescribir el archivo de salida sin preguntar
        "-i", input_file,  # Archivo de entrada
        "-af", "silenceremove=start_periods=1:stop_periods=-1:start_threshold=-50dB:stop_threshold=-50dB:start_silence=0.2:stop_silence=0.2",  # Filtros de audio
        input_file  # Archivo de salida (sobrescribiendo el archivo original)
    ]
    
    try:
        # Ejecutar el comando
        subprocess.run(ffmpeg_cmd, check=True)
        print("Silence successfully removed!")
        return input_file
    except subprocess.CalledProcessError as e:
        print(f"Error during silence removal: {e}")
        return None

error message:

Error during silence removal: Command '['ffmpeg', '-y', '-i', 'BNE.wav', '-af', 'silenceremove=start_periods=1:stop_periods=-1:start_threshold=-50dB:stop_threshold=-50dB:start_silence=0.2:stop_silence=0.2', 'BNE.wav']' returned non-zero exit status 4294967274.

Solution

  • The issue is that ffmpeg can't edit file in-place. Change output file to something else and the command will work.

        output_file = f"out_{input_file}"
        ffmpeg_cmd = [
            "ffmpeg",
            "-y",  # Sobrescribir el archivo de salida sin preguntar
            "-i", 
            input_file,  # Archivo de entrada
            "-af",
            "silenceremove=start_periods=1:stop_periods=-1:start_threshold=-50dB:stop_threshold=-50dB:start_silence=0.2:stop_silence=0.2",  # Filtros de audio
            output_file  # Archivo de salida (sobrescribiendo el archivo original)
        ]