199 lines
4.3 KiB
Go
199 lines
4.3 KiB
Go
package transcoder
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
type Preset struct {
|
|
Name string `json:"name"`
|
|
Config *TranscodeConfig `json:"config"`
|
|
}
|
|
|
|
var TranscodePresets map[string][]*Preset
|
|
|
|
func add_to_presets(category string, items []*Preset) {
|
|
if _, ok := TranscodePresets[category]; !ok {
|
|
TranscodePresets[category] = make([]*Preset, 0)
|
|
}
|
|
for _, preset := range items {
|
|
TranscodePresets[category] = append(TranscodePresets[category], preset)
|
|
}
|
|
}
|
|
|
|
func add_mp3_presets() {
|
|
mp3_presets := make([]*Preset, 0)
|
|
|
|
mp3_bitrates := []int{8, 16, 24, 32, 40, 48, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}
|
|
|
|
for _, bitrate := range mp3_bitrates {
|
|
mp3_presets = append(mp3_presets, &Preset{
|
|
Name: fmt.Sprintf("mp3-%dk", bitrate),
|
|
Config: &TranscodeConfig{
|
|
Encoder: "libmp3lame",
|
|
Container: "mp3",
|
|
FileExtension: "mp3",
|
|
UseBitrate: true,
|
|
Bitrate: fmt.Sprintf("%dk", bitrate),
|
|
},
|
|
})
|
|
}
|
|
|
|
mp3_qualities := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
|
for _, quality := range mp3_qualities {
|
|
mp3_presets = append(mp3_presets, &Preset{
|
|
Name: fmt.Sprintf("mp3-v%d", quality),
|
|
Config: &TranscodeConfig{
|
|
Encoder: "libmp3lame",
|
|
Container: "mp3",
|
|
FileExtension: "mp3",
|
|
UseQuality: true,
|
|
Quality: fmt.Sprintf("%d", quality),
|
|
},
|
|
})
|
|
}
|
|
|
|
add_to_presets("mp3", mp3_presets)
|
|
}
|
|
|
|
func add_opus_presets() {
|
|
opus_presets := make([]*Preset, 0)
|
|
|
|
opus_bitrates := []int{16, 24, 32, 64, 96, 128, 256}
|
|
|
|
for _, bitrate := range opus_bitrates {
|
|
opus_presets = append(opus_presets, &Preset{
|
|
Name: fmt.Sprintf("opus-%dk", bitrate),
|
|
Config: &TranscodeConfig{
|
|
Encoder: "libopus",
|
|
Container: "ogg",
|
|
FileExtension: "opus",
|
|
UseBitrate: true,
|
|
Bitrate: fmt.Sprintf("%dk", bitrate),
|
|
},
|
|
})
|
|
}
|
|
|
|
add_to_presets("opus", opus_presets)
|
|
}
|
|
|
|
func add_vorbis_presets() {
|
|
vorbis_presets := make([]*Preset, 0)
|
|
|
|
vorbis_qualities := []int{-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
|
|
|
for _, quality := range vorbis_qualities {
|
|
vorbis_presets = append(vorbis_presets, &Preset{
|
|
Name: fmt.Sprintf("vorbis-v%d", quality),
|
|
Config: &TranscodeConfig{
|
|
Encoder: "libvorbis",
|
|
Container: "ogg",
|
|
FileExtension: "ogg",
|
|
UseQuality: true,
|
|
Quality: fmt.Sprintf("%d", quality),
|
|
},
|
|
})
|
|
}
|
|
|
|
add_to_presets("vorbis", vorbis_presets)
|
|
}
|
|
|
|
func add_g726_presets() {
|
|
g726_presets := make([]*Preset, 0)
|
|
|
|
g726_bitrates := []int{16, 24, 32, 40}
|
|
|
|
for _, bitrate := range g726_bitrates {
|
|
g726_presets = append(g726_presets, &Preset{
|
|
Name: fmt.Sprintf("g726-%dk", bitrate),
|
|
Config: &TranscodeConfig{
|
|
Encoder: "g726",
|
|
Container: "matroska",
|
|
FileExtension: "mka",
|
|
SampleRate: "8000",
|
|
Channels: "1",
|
|
UseBitrate: true,
|
|
Bitrate: fmt.Sprintf("%dk", bitrate),
|
|
},
|
|
})
|
|
}
|
|
add_to_presets("g726", g726_presets)
|
|
}
|
|
|
|
func add_speex_presets() {
|
|
speex_presets := make([]*Preset, 0)
|
|
|
|
speex_qualities := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
|
|
|
for _, quality := range speex_qualities {
|
|
speex_presets = append(speex_presets, &Preset{
|
|
Name: fmt.Sprintf("speex-q%d", quality),
|
|
Config: &TranscodeConfig{
|
|
Encoder: "libspeex",
|
|
Container: "ogg",
|
|
FileExtension: "ogg",
|
|
UseQuality: true,
|
|
Quality: fmt.Sprintf("%d", quality),
|
|
},
|
|
})
|
|
}
|
|
|
|
add_to_presets("speex", speex_presets)
|
|
}
|
|
|
|
func add_flac_preset() {
|
|
add_to_presets("flac", []*Preset{
|
|
{
|
|
Name: "flac",
|
|
Config: &TranscodeConfig{
|
|
Encoder: "flac",
|
|
Container: "flac",
|
|
FileExtension: "flac",
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func add_wav_preset() {
|
|
add_to_presets("wav", []*Preset{
|
|
{
|
|
Name: "wav",
|
|
Config: &TranscodeConfig{
|
|
Container: "wav",
|
|
FileExtension: "wav",
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func GetPresetByName(name string) (*Preset, error) {
|
|
for _, category := range TranscodePresets {
|
|
for _, preset := range category {
|
|
if preset.Name == name {
|
|
return preset, nil
|
|
}
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("No preset with name %s", name)
|
|
}
|
|
|
|
func PrintTranscodePresets() {
|
|
for cat_name, cat := range TranscodePresets {
|
|
fmt.Printf("Category %s:\n", cat_name)
|
|
for _, preset := range cat {
|
|
fmt.Printf("- %s\n", preset.Name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
TranscodePresets = make(map[string][]*Preset, 0)
|
|
|
|
add_mp3_presets()
|
|
add_opus_presets()
|
|
add_vorbis_presets()
|
|
add_g726_presets()
|
|
add_speex_presets()
|
|
add_flac_preset()
|
|
add_wav_preset()
|
|
}
|