pydub使用记录
读取文件
from pydub import AudioSegment
#通用方法
AudioSegment(…).from_file()
'''
format | example: "aif" | default: "mp3" Format of the output file. Supports "wav" and "raw" natively, requires ffmpeg for all other formats. "raw" files require 3 additional
keyword arguments, sample_width, frame_rate, and channels, denoted below with: raw only. This extra info is required because raw audio files do not have headers to include this info in the file itself like wav files do.
sample_width | example: 2 raw only — Use 1 for 8-bit audio 2 for 16-bit (CD quality) and 4 for 32-bit. It’s the number of bytes per sample.
channels | example: 1 raw only — 1 for mono, 2 for stereo.
frame_rate | example: 2 raw only — Also known as sample rate, common values are 44100 (44.1kHz - CD audio), and 48000 (48kHz - DVD audio)
'''
# wave and raw don’t use ffmpeg
wav_audio = AudioSegment.from_file("/path/to/sound.wav", format="wav")
raw_audio = AudioSegment.from_file("/path/to/sound.raw", format="raw",
frame_rate=44100, channels=2, sample_width=2)
# all other formats use ffmpeg
mp3_audio = AudioSegment.from_file("/path/to/sound.mp3", format="mp3")
# use a file you've already opened (advanced …ish)
with open("/path/to/sound.wav", "rb") as wav_file:
audio_segment = AudioSegment.from_file(wav_file, format="wav")
# also supports the os.PathLike protocol for python >= 3.6
from pathlib import Path
wav_path = Path("path/to/sound.wav")
wav_audio = AudioSegment.from_file(wav_path)
#直接使用原始音频数据
sound = AudioSegment(
# raw audio data (bytes)
data=b'…',
# 2 byte (16 bit) samples
sample_width=2,
# 44.1 kHz frame rate
frame_rate=44100,
# stereo
channels=2
)
#专用方法
song = AudioSegment.from_wav("never_gonna_give_you_up.wav")
song = AudioSegment.from_mp3("never_gonna_give_you_up.mp3")
ogg_version = AudioSegment.from_ogg("never_gonna_give_you_up.ogg")
flv_version = AudioSegment.from_flv("never_gonna_give_you_up.flv")
保存文件
将AudioSegment
对象写到文件,会返回一个输出文件的文件柄(file handle)。但这个文件柄并不需要进行操作。
from pydub import AudioSegment
sound = AudioSegment.from_file("/path/to/sound.wav", format="wav")
sound.export(self, out_f=None, format='mp3', codec=None, bitrate=None, parameters=None, tags=None, id3v2_version='4',cover=None)
'''
out_f :输出的音频文件地址
format | example: "aif" | default: "mp3" Format of the output file. Supports "wav" and "raw" natively, requires ffmpeg for all other formats.
codec | example: "libvorbis" For formats that may contain content encoded with different codecs, you can specify the codec you'd like the encoder to use. For example, the "ogg" format is often used with the "libvorbis" codec. (requires ffmpeg)
bitrate | example: "128k" For compressed formats, you can pass the bitrate you'd like the encoder to use (requires ffmpeg). Each codec accepts different bitrate arguments so take a look at the ffmpeg documentation for details (bitrate usually shown as -b, -ba or -a:b).
tags | example: {"album": "1989", "artist": "Taylor Swift"} Allows you to supply media info tags for the encoder (requires ffmpeg). Not all formats can receive tags (mp3 can).
parameters | example: ["-ac", "2"] Pass additional command line parameters to the ffmpeg call. These are added to the end of the call (in the output file section).
id3v2_version | example: "3" | default: "4" Set the ID3v2 version used by ffmpeg to add tags to the output file. If you want Windows Exlorer to display tags, use "3" here (source).
cover | example: "/path/to/imgfile.png" Allows you to supply a cover image (path to the image file). Currently, only MP3 files allow this keyword argument. Cover image must be a jpeg, png, bmp, or tiff file.
'''
# simple export
file_handle = sound.export