天天看点

Linux下音频格式转换命令行工具Sox音频转换使用Mplayer将多种格式的而文件编码至WAV使用FFmpeg将各种类型文件转换成各种类型文件使用Lame编码或者已不同的比特率重新编码MP3以Ogg Vorbis格式编码从音频文件中获取信息

这里写自定义目录标题

  • Sox音频转换
  • 使用Mplayer将多种格式的而文件编码至WAV
  • 使用FFmpeg将各种类型文件转换成各种类型文件
  • 使用Lame编码或者已不同的比特率重新编码MP3
  • 以Ogg Vorbis格式编码
  • 从音频文件中获取信息

在日常工作中,我经常需要将音频数据从一种格式转换到另一种格式。 因为我通常必须在批处理作业中执行此操作,所以我主要使用命令行工具(在Linux上),如Lame,SoX(Sound eXchange),MPlayer和FFmpeg。 请注意,我只涵盖了我最需要的操作,例如格式转换,采样率转换,转换为单声道和修剪/裁剪。 如果您需要更多/其他功能,请查看手册页或百度

Sox音频转换

SoX(Sound eXchange)称自己为“声音处理程序的瑞士军刀”,除标准音频格式和采样率转换外,还提供一组基本效果(例如音高变换,混响,低通滤波,镶边等)。 它适用于Linux(在软件包管理器中搜索’sox’),Mac OS X和Windows。

# 最小转换示例
sox input.mp3 output.wav

# 改变声道数,有两种方法
sox input.mp3 -c 1 output.wav
sox input.mp3 output.wav channels 1

# 改变采样率,同样两种方法
sox input.mp3 -r 8000 output.wav
sox input.mp3 output.wav rate 8000
# Newer versions of SoX also support
sox input.mp3 output.wav rate 8k

# 从60秒的地方开始裁剪出30秒的片段
sox input.mp3 output.wav trim 60 30

# 整合在一起(裁剪,单声道,22.05 Hz的采样率)
sox input.mp3 output.wav trim 60 30 channels 1 rate 22050
           

SoX的一个问题是,由于MP3的专利和许可问题,默认安装通常不支持编写MP3文件。 在安装“libsox-fmt-all”软件包后,一般就可以读取MP3格式的文件了

使用Mplayer将多种格式的而文件编码至WAV

MPlayer是一种支持多种多媒体格式的媒体播放器。 它通常用于使用GUI播放视频,但也可以用来(在没有GUI的批处理模式下)将音频转换为WAV格式。 MPlayer适用于Linux(包“mplayer”),Windows和Mac OS X.

调用位比这里显示的其他解码器更复杂。 为清楚起见,这里的命令分布在几行(不要忘记在一行上需要时删除反斜杠):

# Decode the audio channel to PCM (WAV) and ignore the video channels
mplayer \
    -ao pcm:fast:waveheader:file=output.wav \
    -vo null -vc null \
    input.mp3

# Use additional audio filters (-af) to resample to 22050 Hz 
# and mix down to mono.
mplayer \
    -ao pcm:fast:waveheader:file=output.wav \
    -af resample=22050,pan=1:0.5:0.5 \
    -vo null -vc null \
    input.mp3
# By default, one expects 16 bits per sample. On some setups however,
# MPlayer uses 32 bits per sample by default.
# To avoid this, set the format explicitly with:
#    -format s16le

# Pick the 30 seconds fragment at an offset of 1 minute:
mplayer \
    -ao pcm:fast:waveheader:file=output.wav \
    -vo null -vc null \
    -ss 60 -endpos 30 \
    input.mp3
           

注意:在某些平台上,我必须添加选项-format s16le以确保MPlayer编码16位PCM样本而不是24位甚至32位,这可能会导致某些音频播放器/工具出现问题。

使用FFmpeg将各种类型文件转换成各种类型文件

FFmpeg是另一种功能强大的开源工具,用于多媒体处理,如转换/转码。 使用最新的Linux发行版安装很简单,安装“ffmpeg”软件包

FFmpeg通常用于视频,但音频转码也很有用,而且非常简单:

# Minimal example: transcode from MP3 to WMA
ffmpeg -i input.mp3 output.wma

# You can get the list of supported formats with:
ffmpeg -formats

# Convert WAV to MP3, mix down to mono (use 1 audio channel), 
# set bit rate to 64 kbps and  sample rate to 22050 Hz
ffmpeg -i input.wav -ac 1 -ab 64000 -ar 22050 output.mp3
# Note: you can also use '-ab 64k', but I'm not sure how well this 
# is supported in different version of FFmpeg

# Picking the 30 seconds fragment at an offset of 1 minute:
# In seconds
ffmpeg -i input.mp3 -ss 60 -t 30 output.wav
# In HH:MM:SS format
ffmpeg -i input.mp3 -ss 0:01:00 -t 0:00:30 output.wav
           

使用Lame编码或者已不同的比特率重新编码MP3

Lame是一个众所周知的开源MP3编码器。 在Linux上安装应该很简单:只需查看“Lame”包。

例如,您可以使用它从WAV格式编码为MP3或将MP3重新编码为不同的比特率。 一些例子:

# Minimal example of converting a wave file to MP3
lame input.wav output.mp3

# Re-encode existing MP3 to 64 kbps MP3
lame -b 64 original.mp3 new.mp3

# More interesting options
# -m m: save as mono
# -m s: save as stereo
# -m j: save as joint stereo (exploits inter-channel correlation
#       more than regular stereo)
# -q 2: quality tweaking: the lower the value, the better the 
#       quality, but the slower the algorithm. Default is 5.

# By default, lame uses constant bit rate (CBR) encoding. 
# You can also use average bit rate (ABR) encoding, 
# e.g. for an average bit rate of 123 kbps:
lame --abr 123 input.wav output.mp3
# or variable (VBR) encoding, e.g. between 32 kbps and 192 kbps:
lame -v -b 32 -B 192 input.wav output.mp3
           

以Ogg Vorbis格式编码

使用“oggenc”工具,您可以将WAV格式(或原始或AIFF)的音频编码为Ogg Vorbis格式。 在Debian上,我必须安装“vorbis-tools”软件包才能获得“oggenc”。

# Minimal example
oggenc audio.wav -o audio.ogg
# Setting the bit rate, downmix to mono and set the sample rate:
oggenc -b 32 --downmix --resample 22050 input.wav -o output.ogg
           

从音频文件中获取信息

要获得有关音频文件的基本信息(如通道数,采样率,持续时间等),有“soxi”工具,它是sox包的一部分:

soxi file.mp3
           

上面一条命令将返回

Input File     : 'file.mp3'
Channels       : 2
Sample Rate    : 44100
Precision      : 16-bit
Duration       : 00:03:55.35 = 10378847 samples = 17651.1 CDDA sectors
File Size      : 1.88M
Bit Rate       : 64.0k
Sample Encoding: MPEG audio (layer I, II or III)
           

您也可以轻松指定多个文件。

当soxi不可用时(或当soxi无法识别文件格式时,有一些基于FFmpeg和MPlayer的替代方案。

使用FFmpeg,只是不指定输出文件,例如:

ffmpeg -i file.mp3
           

将返回

... [version information] ...
Input #0, mp3, from 'file.mp3':
  Duration: 00:03:55.2, start: 0.000000, bitrate: 63 kb/s
  Stream #0.0: Audio: mp2, 44100 Hz, stereo, 64 kb/s
Must supply at least one output file
           

您可以提供多个文件,但需要在每个文件前放置-i标志。

使用MPlayer,它涉及更多:

mplayer -vo null -ao null -frames 0 -identify file.mp3
           

他将返回

... [version information] ...
Playing file.mp3.
ID_AUDIO_ID=0
Audio file file format detected.
ID_FILENAME=file.mp3
ID_DEMUXER=audio
ID_AUDIO_FORMAT=80
ID_AUDIO_BITRATE=64000
ID_AUDIO_RATE=44100
ID_AUDIO_NCH=0
ID_LENGTH=235.00
==========================================================================
Forced audio codec: mad
Opening audio decoder: [libmad] libmad mpeg audio decoder
AUDIO: 44100 Hz, 2 ch, s16le, 64.0 kbit/4.54% (ratio: 8000->176400)
ID_AUDIO_BITRATE=64000
ID_AUDIO_RATE=44100
ID_AUDIO_NCH=2
Selected audio codec: [mad] afm: libmad (libMAD MPEG layer 1-2-3)
==========================================================================
AO: [null] 44100Hz 2ch s16le (2 bytes per sample)
ID_AUDIO_CODEC=mad
Video: no video
Starting playback...


Exiting... (End of file)
           

继续阅读