快捷方式

AudioEncoder

class torchcodec.encoders.AudioEncoder(samples: Tensor, *, sample_rate: int)[原始碼]

一個音訊編碼器。

引數:
  • samples (torch.Tensor) – 要編碼的樣本。這必須是一個形狀為 (num_channels, num_samples) 的 2D 張量,或者是一個 1D 張量(在這種情況下,假定 num_channels = 1)。值必須是 [-1, 1] 範圍內的浮點值。

  • sample_rate (int) – **輸入** samples 的取樣率。編碼後的輸出取樣率可以使用編碼方法(to_file 等)指定。

使用 AudioEncoder 的示例

使用 AudioEncoder 對音訊樣本進行編碼

使用 AudioEncoder 對音訊樣本進行編碼
to_file(dest: Union[str, Path], *, bit_rate: Optional[int] = None, num_channels: Optional[int] = None, sample_rate: Optional[int] = None) None[原始碼]

將樣本編碼到檔案中。

引數:
  • dest (str 或 pathlib.Path) – 輸出檔案的路徑,例如 audio.mp3。檔案的副檔名決定了音訊格式和容器。

  • bit_rate (int, 可選) – 輸出位元率。編碼器通常支援有限的位元率值,因此 bit_rate 將匹配其中一個支援的值。預設值由 FFmpeg 選擇。

  • num_channels (int, 可選) – 編碼輸出樣本的通道數。預設情況下,使用輸入 samples 的通道數。

  • sample_rate (int, 可選) – 編碼輸出的取樣率。預設情況下,使用輸入 samples 的取樣率。

to_tensor(format: str, *, bit_rate: Optional[int] = None, num_channels: Optional[int] = None, sample_rate: Optional[int] = None) Tensor[原始碼]

將樣本編碼為原始位元組,作為 1D uint8 Tensor。

引數:
  • format (str) – 編碼樣本的格式,例如 “mp3”、“wav” 或 “flac”。

  • bit_rate (int, 可選) – 輸出位元率。編碼器通常支援有限的位元率值,因此 bit_rate 將匹配其中一個支援的值。預設值由 FFmpeg 選擇。

  • num_channels (int, 可選) – 編碼輸出樣本的通道數。預設情況下,使用輸入 samples 的通道數。

  • sample_rate (int, 可選) – 編碼輸出的取樣率。預設情況下,使用輸入 samples 的取樣率。

返回:

編碼後的原始位元組作為 1D uint8 Tensor。

返回型別:

張量

文件

訪問全面的 PyTorch 開發者文件

檢視文件

教程

為初學者和高階開發者提供深入的教程

檢視教程

資源

查詢開發資源並讓您的問題得到解答

檢視資源