Godub
Manipulate audio in a simple and elegant way.
Install / Use
/learn @iFaceless/GodubREADME
godub
godub lets you manipulate audio in an easy and elegant way. It's ported from the excellent project pydub.
Why
There are some audio packages in the Go world, but we believe that pydub provides a better way to do stuff to audios. Therefore, here we have godub! However, due to limited time and personal ability, not all the features of pydub are supported.
Features
- Load audio files, supports mp3/m4a/wav...
- Export/convert audio with custom configs.
- Slice an audio.
- Concatenate two or more audios.
- Repeat an audio.
- Overlay with other audios.
- Reverse an audio.
- ...
Quickstart
Load
func main() {
filePath := path.Join(dataDirectory(), "code-geass.mp3")
segment, _ := godub.NewLoader().Load(filePath)
fmt.Println(segment)
buf, _ := ioutil.ReadAll()
// Load from buffer, load also accepts `io.Reader` type.
segment, _ = godub.NewLoader().Load(bytes.NewReader(buf))
fmt.Println(segment)
}
Export
func main() {
filePath := path.Join(dataDirectory(), "code-geass.mp3")
segment, _ := godub.NewLoader().Load(filePath)
// Export as a mp3 file.
toFilePath := path.Join(dataDirectory(), "converted", "code-geass.mp3")
err := godub.NewExporter(toFilePath).
WithDstFormat("mp3").
WithBitRate(128000).
Export(segment)
if err != nil {
log.Fatal(err)
}
// Let's check it.
segment, _ = godub.NewLoader().Load(toFilePath)
fmt.Println(segment)
}
Convert
func main() {
filePath := path.Join(dataDirectory(), "code-geass.mp3")
toFilePath := path.Join(dataDirectory(), "converted", "code-geass.m4a")
w, _ := os.Create(toFilePath)
err := audio.NewConverter(w).
WithBitRate(64000).
WithDstFormat("m4a").
Convert(filePath)
if err != nil {
log.Fatal(err)
}
segment, _ := godub.NewLoader().Load(toFilePath)
fmt.Println(segment)
}
Slice
func slice() {
filePath := path.Join(dataDirectory(), "code-geass.mp3")
segment, _ := godub.NewLoader().Load(filePath)
// Slice from 0 sec to 100 secs.
slicedSegment, _ := segment.Slice(0, 100*time.Second)
// Then export it as mp3 file.
slicedPath := path.Join(dataDirectory(), "slice", "code-geass.mp3")
godub.NewExporter(slicedPath).WithDstFormat("mp3").Export(slicedSegment)
}
Concatenate
func main() {
filePath := path.Join(dataDirectory(), "code-geass.mp3")
segment, _ := godub.NewLoader().Load(filePath)
// Yep, you can append more than one segment.
newSeg, err := segment.Append(segment, segment)
if err != nil {
log.Fatal(err)
}
// Save the newly created audio segment as mp3 file.
newPth := path.Join(dataDirectory(), "append", "code-geass.mp3")
godub.NewExporter(newPth).WithDstFormat("mp3").WithBitRate(audio.MP3BitRatePerfect).Export(newSeg)
}
Overlay
func main() {
segment, _ := godub.NewLoader().Load(path.Join(dataDirectory(), "code-geass.mp3"))
otherSegment, _ := godub.NewLoader().Load(path.Join(dataDirectory(), "ring.mp3"))
overlaidSeg, err := segment.Overlay(otherSegment, &godub.OverlayConfig{LoopToEnd: true})
if err != nil {
log.Fatal(err)
}
godub.NewExporter(destPath).WithDstFormat("wav").Export(path.Join(tmpDataDirectory(), "overlay-ring.wav"))
}
Dependency
godub uses ffmpeg as its backend to support encoding, decoding and conversion.
References
- go-binary-pack
- Python struct
- Digital Audio - Creating a WAV (RIFF) file
- ffmpeg tutorial
- Python: manipulate raw audio data with
audioop - ffmpeg Documentation
Similar Projects
TODO
- [ ] Audio effects.
- [ ] As always, test test test!
License
godub is licensed under the MIT license. Please feel free and have fun.
Related Skills
qqbot-channel
351.2kQQ 频道管理技能。查询频道列表、子频道、成员、发帖、公告、日程等操作。使用 qqbot_channel_api 工具代理 QQ 开放平台 HTTP 接口,自动处理 Token 鉴权。当用户需要查看频道、管理子频道、查询成员、发布帖子/公告/日程时使用。
docs-writer
100.5k`docs-writer` skill instructions As an expert technical writer and editor for the Gemini CLI project, you produce accurate, clear, and consistent documentation. When asked to write, edit, or revie
model-usage
351.2kUse CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
Design
Campus Second-Hand Trading Platform \- General Design Document (v5.0 \- React Architecture \- Complete Final Version)1\. System Overall Design 1.1. Project Overview This project aims t
