Linux下音频格式转换命令行工具

这篇博客介绍了在Linux环境下使用SoX、Mplayer、FFmpeg和Lame等命令行工具进行音频格式转换的方法,包括从不同格式转为WAV、MP3、Ogg Vorbis等,以及获取音频文件信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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)
1、Java实现wav音频文件转换为pcm音频文件(AudioUtils.java) 2、Java实现播放pcm音频文件(PCMPlay.java) WAVwav是一种无损的音频文件格式WAV符合 PIFF(Resource Interchange File Format)规范。所有的WAV都有一个文件头,这个文件头音频流的编码参数。WAV音频流的编码没有硬性规定,除了PCM之外,还有几乎所有支持ACM规范的编码都可以为WAV音频流进行编码。 PCM:PCM(Pulse Code Modulation----脉码调制录音)。所谓PCM录音就是将声音等模拟信号变成符号化的脉冲列,再予以记录。PCM信号是由[1]、[0]等符号构成的数字信号,而未经过任何编码和压缩处理。与模拟信号比,它不易受传送系统的杂波及失真的影响。动态范围宽,可得到音质相当好的影响效果。 简单来说:wav是一种无损的音频文件格式,pcm是没有压缩的编码方式。 WAV和PCM的关系 WAV可以使用多种音频编码来压缩其音频流,不过我们常见的都是音频流被PCM编码处理的WAV,但这不表示WAV只能使用PCM编码,MP3编码同样也可以运用在WAV中,和AVI一样,只要安装好了相应的Decode,就可以欣赏这些WAV了。在Windows平台下,基于PCM编码的WAV是被支持得最好的音频格式,所有音频软件都能完美支持,由于本身可以达到较高的音质的要求,因此,WAV也是音乐编辑创作的首选格式,适合保存音乐素材。因此,基于PCM编码的WAV被作为了一种中介的格式,常常使用在其他编码的相互转换之中,例如MP3转换成WMA。 简单来说:pcm是无损wav文件中音频数据的一种编码方式,但wav还可以用其它方式编码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值