在使用PySoX的文件转换上设置“sample rate”属性?

2024-06-14 11:28:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用PySoX转换成音频文件:

import pysox
tfm = sox.Transformer()
tfm.build('./abc/1.raw', './abc/2.flac')

这是我得到的错误: "sox.core.SoxError:标准输出: Stderr:sox FAIL formats:文件“./abc/1.raw”的输入格式错误:未指定采样率”

如何构建包含采样率的函数并完成转换?在


Tags: coreimportbuild标准raw错误音频文件abc
1条回答
网友
1楼 · 发布于 2024-06-14 11:28:45

原因是原始音频文件不包含有关文件中音频格式的信息,因此您需要提供这些信息。采样率只是这样一个指标,所以您还需要对其他几个参数进行此操作。在

引自sox.sourceforge.net

SoX can work with ‘self-describing’ and ‘raw’ audio files. ‘self-describing’ formats (e.g. WAV, FLAC, MP3) have a header that completely describes the signal and encoding attributes of the audio data that follows. ‘raw’ or ‘headerless’ formats do not contain this information, so the audio characteristics of these must be described on the SoX command line or inferred from those of the input file.

The following four characteristics are used to describe the format of audio data such that it can be processed with SoX:

  • sample rate

    The sample rate in samples per second (‘Hertz’ or ‘Hz’). Digital telephony traditionally uses a sample rate of 8000 Hz (8 kHz), though these days, 16 and even 32 kHz are becoming more common. Audio Compact Discs use 44100 Hz (44.1 kHz). Digital Audio Tape and many computer systems use 48 kHz. Professional audio systems often use 96 kHz.

  • sample size [...]

  • data encoding [...]
  • channels [...]

pysox documentation描述了set_input_format方法:

set_input_format(file_type=None, rate=None, bits=None, channels=None, encoding=None, ignore_length=False)

Sets input file format arguments. This is primarily useful when dealing with audio files without a file extension. Overwrites any previously set input file arguments.

If this function is not explicitly called the input format is inferred from the file extension or the file’s header.

Parameters:

  • file_type : str or None, default=None

    The file type of the input audio file. Should be the same as what the file extension would be, for ex. ‘mp3’ or ‘wav’.

  • rate : float or None, default=None

    The sample rate of the input audio file. If None the sample rate is inferred.

  • [...]

因此,您应该将费率设置为:

tfm.set_input_format(file_type='raw', rate=8000, bits=16, channels=1, encoding='signed-integer')

您必须将值调整为您在原始文件中真正编码的值。此方法调用将应用于具有“raw”扩展名的所有文件,因此,如果要处理多个这样的文件,则无需再次调用上述文件。只有当不同的“原始”文件中的特征不同时,才需要使用适当的值再次调用它。在

相关问题 更多 >