80 lines
2 KiB
Go
80 lines
2 KiB
Go
package transcoder
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"gitlab.com/ChaotiCryptidz/musicutil/types"
|
|
)
|
|
|
|
type TranscodeConfig struct {
|
|
UseQuality bool `json:"use_quality"`
|
|
UseBitrate bool `json:"use_bitrate"`
|
|
Encoder string `json:"encoder"`
|
|
FileExtension string `json:"file_extension"`
|
|
Container string `json:"container"`
|
|
Bitrate string `json:"bitrate"`
|
|
Quality string `json:"quality"`
|
|
SampleRate string `json:"sample_rate"`
|
|
Channels string `json:"channels"`
|
|
}
|
|
|
|
func isNotEmptyString(x string) bool {
|
|
if len(strings.TrimSpace(x)) == 0 {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func Transcode(file *types.File, config *TranscodeConfig, dest string) (string, error) {
|
|
command_args := make([]string, 0)
|
|
command_args = append(command_args, "-y")
|
|
command_args = append(command_args, "-hide_banner")
|
|
|
|
command_args = append(command_args, "-i")
|
|
command_args = append(command_args, file.JoinPathTo())
|
|
|
|
if isNotEmptyString(config.Encoder) {
|
|
command_args = append(command_args, "-c:a")
|
|
command_args = append(command_args, config.Encoder)
|
|
}
|
|
|
|
if isNotEmptyString(config.Container) {
|
|
command_args = append(command_args, "-f")
|
|
command_args = append(command_args, config.Container)
|
|
}
|
|
|
|
if isNotEmptyString(config.SampleRate) {
|
|
command_args = append(command_args, "-ar")
|
|
command_args = append(command_args, config.SampleRate)
|
|
}
|
|
|
|
if isNotEmptyString(config.Channels) {
|
|
command_args = append(command_args, "-ac")
|
|
command_args = append(command_args, config.Channels)
|
|
}
|
|
|
|
if config.UseQuality {
|
|
command_args = append(command_args, "-q:a")
|
|
command_args = append(command_args, config.Quality)
|
|
}
|
|
|
|
if config.UseBitrate {
|
|
command_args = append(command_args, "-b:a")
|
|
command_args = append(command_args, config.Bitrate)
|
|
}
|
|
|
|
command_args = append(command_args, dest)
|
|
|
|
cmd := exec.Command("ffmpeg", command_args...)
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
fmt.Println(output)
|
|
return string(output), errors.New("ffmpeg error")
|
|
}
|
|
return string(output), nil
|
|
}
|