0

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.

1 Answer 1

1

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)
    ]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! To overwrite the file I used: os.replace(temp_output_file, audio_file_path) with a temporary file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.