使用这个多功能的 Linux 命令转换音频文件
音频 SoX

> 
> SoX Sound Exchange 甚至可以为你的音频文件添加特效。
> 
> 
> 


![](/data/attachment/album/202211/13/104314skttlizoioyeaw3w.jpg "HiFi vintage stereo")


我工作需要使用音视频媒体，不管你处理哪种媒体，你肯定知道标准化是一种有价值的工具。就像你不会试图把一个分数加到一个小数上而不转换其中一个一样，我已经知道，把不同格式的媒体组合起来并不理想。为了方便用户，大多数爱好者级应用程序使转换过程不可见。然而，对于那些需要控制媒体细节的用户的灵活软件，会通常让你自己提前将媒体转换为所需的格式。我有一些最喜欢的音频转换工具，其中之一就是号称“音频的瑞士军刀” —— [SoX](http://sox.sourceforge.net/sox.html)。


### 安装


在 Linux 或 BSD 上，可以从软件存储库或 Ports 树中安装 `sox` 命令（，以及一些有用的符号链接）。


你也可以从 [Sourceforge.net](http://sox.sourceforge.net) 上安装 SoX。它不经常发布，但它的代码库往往是稳定的，所以如果你想要最新的功能（如 Opus 支持），构建它是容易和安全的。


SoX 主要提供了 `sox` 命令，但是创建了一些有用的符号链接：`play`、`rec` 和 `soxi`。


### 使用 SoX 获取文件信息


SoX 可以读取和重写音频数据。它是否存储重写的音频数据取决于你。在有些情况下，你不需要存储转换后的数据，例如，当你将输出直接发送到扬声器进行回放时。然而，在进行任何转换之前，最好首先确定要处理的是什么。


使用 `soxi` 命令也可以收集音频文件信息。`soxi` 会符号链接到 `soxi --info`。



```
$ soxi countdown.mp3
Input File（输入文件）    : '/home/tux/countdown.mp3'
Channels（通道数）       : 1
Sample Rate（采样率）    : 44100
Precision（数据精度）      : 16-bit（16 比特）
Duration（时长）       : 00:00:11.21 = 494185 samples...（11.21 秒 = 494185 采样点）
File Size（文件大小）      : 179k
Bit Rate（比特率）       : 128k
Sample Encoding（编码格式）: MPEG audio (layer I, II or III)

```

这个输出可以让你很好地了解音频文件的编码方式、文件长度、文件大小、采样率和通道数。其中一些你可能*认为*你已经知道了，但当客户把媒体带到我面前时，我从不相信这些假设。使用 `soxi` 验证媒体属性。


### 转换文件


在本例中，，一个游戏节目倒计时的音频是以MP3文件的形式提供的。虽然几乎所有的编辑应用程序都接受压缩音频，但它们并不是在压缩的数据上进行编辑。转换是在某个地方发生的，可能是一个秘密的后台任务，也可能提示让你保存一份副本。我通常喜欢自己提前完成转换。这样，我可以控制使用的格式。我可以在夜间批量处理大量的媒体，而不是浪费宝贵的制作时间，等待编辑应用程序按需处理它们。


`sox` 命令用于转换音频文件。在 `sox` 流程中有几个阶段：


* 输入
* 合并
* 特效
* 输出


但在命令语法中，特效步骤令人困惑地放到了*最后一步*。这意味着 `sox` 流程是这样组成的:



```
输入 → 合并 → 输出 → 特效

```

### 编码


最简单的转换命令只涉及一个输入文件和一个输出文件。下面是转换 MP3 文件为无损 FLAC 文件的命令：



```
$ sox countdown.mp3 output.flac
$ soxi output.flac

Input File（输入文件）     : 'output.flac'
Channels（通道数）       : 1
Sample Rate（采样率）    : 44100
Precision（数据精度）      : 16-bit（16 比特）
Duration（时长）       : 00:00:11.18 = 493056 samples...（11.18 秒 = 493056 采样点）
File Size（文件大小）      : 545k
Bit Rate（比特率）       : 390k
Sample Encoding（编码格式）: 16-bit FLAC
Comment（注释）        : 'Comment=Processed by SoX'

```

#### 特效


特效可以在命令末尾指定。它可以在将数据发送到最终目的地之前更改音频。例如，有时声音太大会在转换过程中造成问题：



```
$ sox bad.wav bad.ogg
sox WARN sox: `bad.ogg' output clipped 126 samples; decrease volume?

```

应用**增益**（`gain`）效果通常可以解决此问题：



```
$ sox bad.wav bad.ogg gain -1

```

#### 淡入淡出


另一个常用的效果是**淡入淡出**（`fade`）。此效果允许你定义淡入或淡出的类型，以及你希望淡入淡出效果持续的时间。


下面是一个使用倒抛物线的 6 秒淡入示例：



```
$ sox intro.ogg intro.flac fade p 6

```

这将对音频的头部应用 3 秒的淡入，并从 8 秒标记开始淡出（这段音乐只有 11 秒，因此在这种情况下淡出也是 3 秒）：



```
$ sox intro.ogg intro.flac fade p 3 8

```

`sox` 手册页中列出了不同类型的淡入淡出（正弦、线性、倒抛物线等）以及淡入淡出提供的选项。


#### 特效语法


每个特效插件都有自己的语法，因此请参阅手册页了解如何调用每个特效插件的详细信息。


效果可以在一个命令中以菊花链的方式进行，至少在你想组合它们的范围内是如此。换句话说，没有语法可以只在六秒钟的淡出期间应用一个镶边效果。对于如此精确的东西，你需要一个图形声波编辑器或数字音频工作站，例如 [LMMS](https://opensource.com/life/16/2/linux-multimedia-studio) 或 [Rosegarden](https://opensource.com/article/18/3/make-sweet-music-digital-audio-workstation-rosegarden)。但是，如果你只想应用一次特效，可以在同一命令中将它们一起列出。


此命令应用了一个 -1 的**增益**效果、1.35 的节奏**拉伸**和**淡出**：



```
$ sox intro.ogg output.flac gain -1 stretch 1.35 fade p 0 6
$ soxi output.flac

Input File（输入文件）     : 'output.flac'
Channels（通道数）       : 1
Sample Rate（采样率）    : 44100
Precision（数据精度）      : 16-bit（16 比特）
Duration（时长）        : 00:00:15.10 = 665808 samples...（15.10 秒 = 665808 采样点）
File Size（文件大小）      : 712k
Bit Rate（比特率）       : 377k
Sample Encoding（编码格式）: 16-bit FLAC
Comment（注释）        : 'Comment=Processed by SoX'

```

### 组合音频


SoX 还可以通过连接或混合音频文件来组合音频文件。


要连接（或者说*拼接*）文件合并为一个文件，请在命令中提供多个输入文件：



```
$ sox countdown.mp3 intro.ogg output.flac

```

在本例中，`output.flac` 现在包含 `countdown.mp3` 音频，紧接着是 `intro.ogg` 音乐。


但是，如果你希望两首曲目同时播放，可以使用 `--combine mix` 选项：



```
$ sox --combine mix countdown.mp3 intro.ogg output.flac

```

然而，想象一下，这两个输入文件的不同之处不仅仅在于它们的编解码器。人声音轨用单声道（一个声道）录制并不少见，但音乐至少要用立体声（至少两个声道）来录制。SoX 不会给出默认的解决方案，因此你必须首先自己标准化这两个文件的格式。


#### 更改音频文件


选项与后面列出文件名有关。例如，此命令中的 `--channels` 选项将*仅仅*应用于 `input.wav`，而*不被*应用于 `example.ogg` 和 **output.flac**：



```
$ sox --channels 2 input.wav example.ogg output.flac

```

这意味着在 SoX 中，选项的位置非常重要。如果你在命令开始时指定一个选项，那么实际上只会覆盖 SoX 自己从输入文件中收集的内容。然而，在*输出文件*名前的选项决定了 SoX 如何写入音频数据。


要解决前面的通道不兼容问题，你可以首先标准化输入，然后混合：



```
$ sox countdown.mp3 --channels 2 countdown-stereo.flac gain -1
$ soxi countdown-stereo.flac

Input File（输入文件）     : 'countdown-stereo.flac'
Channels（通道数）       : 2
Sample Rate（采样率）    : 44100
Precision（数据精度）      : 16-bit（16 比特）
Duration（时长）       : 00:00:11.18 = 493056 samples...（11.18 秒 = 493056 采样点）
File Size（文件大小）      : 545k
Bit Rate（比特率）       : 390k
Sample Encoding（编码格式）: 16-bit FLAC
Comment（注释）        : 'Comment=Processed by SoX'

$ sox --combine mix \
  countdown-stereo.flac \
  intro.ogg \
  output.flac

```

SoX 绝对需要多个命令来执行复杂的操作，因此根据需要创建几个临时和中间文件是正常的。


### 多通道音频


当然，并非所有音频都被限制在一个或两个声道。如果你想将多个音频通道组合成一个文件，可以使用 SoX 的 `--combine merge` 选项：



```
$ sox --combine merge countdown.mp3 intro.ogg output.flac
$ soxi output.flac

Input File     : 'output.flac'
Channels       : 3
[...]

```

### 简单的音频操作


在没有视觉界面的情况下操作音频似乎很奇怪，而且对于某些任务来说，SoX 绝对不是最好的工具。然而，对于许多任务，SoX 提供了一个简单而轻量级的工具包。SoX 是一个具有强大潜力的简单命令。有了它，你可以转换音频，操纵通道和波形，甚至生成自己的声音。本文仅简要概述了其功能，因此请阅读其手册页或 [在线文档](http://sox.sourceforge.net/sox.html)，然后看看你能创造什么。




---


via: <https://opensource.com/article/20/2/linux-sox>


作者：[Klaatu](https://opensource.com/users/klaatu) 选题：[lujun9972](https://github.com/lujun9972) 译者：[FYJNEVERFOLLOWS](https://github.com/FYJNEVERFOLLOWS) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
I work with media, and when you work with any kind of media, you learn pretty quickly that standardization is a valuable tool. Just as you wouldn't try to add a fraction to a decimal without converting one or the other, I've learned that it's not ideal to combine media of differing formats. Most hobbyist-level applications make the conversion process invisible to the user as a convenience. Flexible software aimed at users needing control over the fine details of their assets, however, often leave it up to you to convert your media to your desired format in advance. I have a few favorite tools for conversion, and one of those is the so-called *Swiss army knife of sound*, [SoX](http://sox.sourceforge.net/sox.html).

## Installing


On Linux or BSD, you can install the **sox** command (and some helpful symlinks) from your software repository or ports tree.

You can also install SoX from its home on [Sourceforge.net](http://sox.sourceforge.net). It doesn't release often, but its codebase tends to be stable, so if you want the latest features (such as Opus support), it's easy and safe to build.

SoX provides primarily the **sox** command, but installation also creates a few useful symlinks: **play**, **rec**, and **soxi**.

## Getting information about files with SoX

SoX reads and rewrites audio data. Whether it stores the rewritten audio data is up to you. There are use cases in which you don't need to store the converted data, for instance, when you're sending the output directly to your speakers for playback. Before doing any conversion, however, it's usually a good idea to determine exactly what you're dealing with in the first place.

To gather information about an audio file, use the **soxi** command. This is a symlink to **sox --info**.

```
$ soxi countdown.mp3
Input File : '/home/tux/countdown.mp3'
Channels : 1
Sample Rate : 44100
Precision : 16-bit
Duration : 00:00:11.21 = 494185 samples...
File Size : 179k
Bit Rate : 128k
Sample Encoding: MPEG audio (layer I, II or III)
```


This output gives you a good idea of what codec the audio file is encoded in, the file length, file size, sample rate, and the number of channels. Some of these you might *think* you already know, but I never trust assumptions when media is brought to me by a client. Verify media attributes with **soxi**.

## Converting files

In this example, the audio of a game show countdown has been delivered as an MP3 file. While nearly all editing applications accept compressed audio, none of them actually edit the compressed data. Conversion is happening somewhere, whether it's a secret background task or a prompt for you to save a copy. I generally prefer to do the conversion myself, in advance. This way, I can control what format I'm using. I can do lots of media in batches overnight instead of wasting valuable production time waiting for an editing application to churn through them on demand.

The **sox** command is meant for converting audio files. There are a few stages in the **sox** pipeline:

- input
- combine
- effects
- output

In command syntax, the effects step is, confusingly, written *last*. That means the pipeline is composed this way:

`input → combine → output → effects`


## Encoding

The simplest conversion command involves only an input file and an output file. Here's the command to convert an MP3 file to a lossless FLAC file:

```
$ sox countdown.mp3 output.flac
$ soxi output.flac
Input File : 'output.flac'
Channels : 1
Sample Rate : 44100
Precision : 16-bit
Duration : 00:00:11.18 = 493056 samples...
File Size : 545k
Bit Rate : 390k
Sample Encoding: 16-bit FLAC
Comment : 'Comment=Processed by SoX'
```


### Effects

The effects chain is specified at the end of a command. It can alter audio prior to sending the data to its final destination. For instance, sometimes audio that's too loud can cause problems during conversion:

```
$ sox bad.wav bad.ogg
sox WARN sox: `bad.ogg' output clipped 126 samples; decrease volume?
```


Applying a **gain** effect can often solve this problem:

`$ sox bad.wav bad.ogg gain -1`


### Fade

Another useful effect is **fade**. This effect lets you define the shape of a fade-in or fade-out, along with how many seconds you want the fade to span.

Here's an example of a six-second fade-in using an inverted parabola:

`$ sox intro.ogg intro.flac fade p 6`


This applies a three-second fade-in to the head of the audio and a fade-out starting at the eight-second mark (the intro music is only 11 seconds, so the fade-out is also three-seconds in this case):

`$ sox intro.ogg intro.flac fade p 3 8`


The different kinds of fades (sine, linear, inverted parabola, and so on), as well as the options **fade** offers (fade-in, fade-out), are listed in the **sox** man page.

### Effect syntax

Each effect plugin has its own syntax, so refer to the man page for details on how to invoke each one.

Effects can be daisy-chained in one command, at least to the extent that you want to combine them. In other words, there's no syntax to apply a **flanger** effect only during a six-second fade-out. For something that precise, you need a graphical sound wave editor or a digital audio workstation such as [LMMS](https://opensource.com/life/16/2/linux-multimedia-studio) or [Rosegarden](https://opensource.com/article/18/3/make-sweet-music-digital-audio-workstation-rosegarden). However, if you just have effects that you want to apply once, you can list them together in the same command.

This command applies a -1 **gain** effect, a tempo **stretch** of 1.35, and a **fade-out**:

```
$ sox intro.ogg output.flac gain -1 stretch 1.35 fade p 0 6
$ soxi output.flac
Input File : 'output.flac'
Channels : 1
Sample Rate : 44100
Precision : 16-bit
Duration : 00:00:15.10 = 665808 samples...
File Size : 712k
Bit Rate : 377k
Sample Encoding: 16-bit FLAC
Comment : 'Comment=Processed by SoX'
```


## Combining audio

SoX can also combine audio files, either by concatenating them or by mixing them.

To join (or *concatenate*) files into one, provide more than one input file in your command:

`$ sox countdown.mp3 intro.ogg output.flac`


In this example, **output.flac** now contains **countdown **audio, followed immediately by **intro** music.

If you want the two tracks to play over one another at the same time, though, you can use the **--combine mix** option:

`$ sox --combine mix countdown.mp3 intro.ogg output.flac`


Imagine, however, that the two input files differed in more than just their codecs. It's not uncommon for vocal tracks to be recorded in mono (one channel), but for music to be recorded in at least stereo (two channels). SoX won't default to a solution, so you have to standardize the format of the two files yourself first.

### Altering audio files

Options related to the file name listed *after* it. For instance, the **--channels** option in this command applies *only* to **input.wav** and NOT to **example.ogg** or **output.flac**:

`$ sox --channels 2 input.wav example.ogg output.flac`


This means that the position of an option is very significant in SoX. Should you specify an option at the start of your command, you're essentially only overriding what SoX gleans from the input files on its own. Options placed immediately before the *output* file, however, determine how SoX writes the audio data.

To solve the previous problem of incompatible channels, you can first standardize your inputs, and then mix:

```
$ sox countdown.mp3 --channels 2 countdown-stereo.flac gain -1
$ soxi countdown-stereo.flac
Input File : 'countdown-stereo.flac'
Channels : 2
Sample Rate : 44100
Precision : 16-bit
Duration : 00:00:11.18 = 493056 samples...
File Size : 545k
Bit Rate : 390k
Sample Encoding: 16-bit FLAC
Comment : 'Comment=Processed by SoX'
$ sox --combine mix \
countdown-stereo.flac \
intro.ogg \
output.flac
```


SoX absolutely requires multiple commands for complex actions, so it's normal to create several temporary and intermediate files as needed.

## Multichannel audio

Not all audio is constrained to one or two channels, of course. If you want to combine several audio channels into one file, you can do that with SoX and the **--combine merge** option:

```
$ sox --combine merge countdown.mp3 intro.ogg output.flac
$ soxi output.flac
Input File : 'output.flac'
Channels : 3
[...]
```


## Easy audio manipulation

It might seem strange to work with audio using no visual interface, and for some tasks, SoX definitely isn't the best tool. However, for many tasks, SoX provides an easy and lightweight toolkit. SoX is a simple command with powerful potential. With it, you can convert audio, manipulate channels and waveforms, and even generate your own sounds. This article has only provided a brief overview of its capabilities, so go read its man page or [online documentation](http://sox.sourceforge.net/sox.html) and then see what you can create.

## Comments are closed.