使用 os.system() 转换音频文件的采样率

原文标题Using os.system() to convert audio files sample rate

我已经开始研究一个 NLP 项目,并且在开始时,我需要对音频文件进行下采样。为此,我找到了一个可以自动执行此操作的脚本,但是尽管我可以使用它来对我的音频进行下采样,但我很难理解它是如何工作的。

def convert_audio(audio_path, target_path, remove=False):
    """This function sets the audio `audio_path` to:
        - 16000Hz Sampling rate
        - one audio channel ( mono )
            Params:
                audio_path (str): the path of audio wav file you want to convert
                target_path (str): target path to save your new converted wav file
                remove (bool): whether to remove the old file after converting
        Note that this function requires ffmpeg installed in your system."""

    os.system(f"ffmpeg -i {audio_path} -ac 1 -ar 16000 {target_path}")
    # os.system(f"ffmpeg -i {audio_path} -ac 1 {target_path}")
    if remove:
        os.remove(audio_path)

这是给我带来麻烦的代码,我不明白底部的第 4 行是如何工作的,我相信那是对音频文件进行重新采样的行。

仓库位于:https://github.com/x4nth055/pythoncode-tutorials/

如果有人知道这是如何完成的,我很想知道,或者是否有更好的方法来降低音频文件的采样率!谢谢

原文链接:https://stackoverflow.com//questions/71491001/using-os-system-to-convert-audio-files-sample-rate

回复

我来回复
  • Lei Yang的头像
    Lei Yang 评论

    你用过ffmpeg吗?文档清楚地显示了选项(可能需要音频专业知识才能理解)

    -ac[:stream_specifier] channels (input/output,per-stream) 设置音频通道的数量。对于输出流,它默认设置为输入音频通道的数量。对于输入流,此选项仅对音频抓取设备和原始解复用器有意义,并映射到相应的解复用器选项。

    -ar[:stream_specifier] freq (input/output,per-stream) 设置音频采样频率。对于输出流,它默认设置为相应输入流的频率。对于输入流,此选项仅对音频抓取设备有意义,并且原始解复用器被映射到相应的解复用器选项。

    解释 foros.system

    在子shell中执行命令(字符串)…在Windows上,返回值是系统shell运行命令后返回的值。shell由Windows环境变量COMSPEC给出:通常是cmd.exe,返回退出状态命令运行;在使用非本地 shell 的系统上,请查阅您的 shell 文档。

    为了更好地理解,建议打印命令

    cmd_str = f"ffmpeg -i {audio_path} -ac 1 -ar 16000 {target_path}"
    print(cmd_str) # then you can copy paste to cmd/bash and run
    os.system(cmd_str)
    
    2年前 0条评论