118 lines
2.8 KiB
Go
118 lines
2.8 KiB
Go
package transcode
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/akamensky/argparse"
|
|
"github.com/rs/zerolog/log"
|
|
"gitlab.com/ChaotiCryptidz/musicutil/types"
|
|
"gitlab.com/ChaotiCryptidz/musicutil/utils/transcoder"
|
|
)
|
|
|
|
type TranscodeCommandArgs struct {
|
|
Source *string
|
|
Dest *string
|
|
TranscodePreset *string
|
|
IgnoreExtension *bool
|
|
}
|
|
|
|
func RegisterTranscodeCommand(parser *argparse.Parser) (*argparse.Command, *TranscodeCommandArgs) {
|
|
cmd := parser.NewCommand("transcode", "Transcode a audio file from one format to another")
|
|
|
|
arguments := &TranscodeCommandArgs{}
|
|
arguments.Source = cmd.String("s", "src", &argparse.Options{
|
|
Required: true,
|
|
Validate: func(args []string) error {
|
|
if _, err := os.Stat(args[0]); err != nil {
|
|
return fmt.Errorf("Source file does not exist")
|
|
}
|
|
|
|
return nil
|
|
},
|
|
})
|
|
|
|
arguments.Dest = cmd.String("d", "dest", &argparse.Options{})
|
|
|
|
arguments.TranscodePreset = cmd.String("p", "transcode-preset", &argparse.Options{
|
|
Required: true,
|
|
Validate: func(args []string) error {
|
|
if args[0] == "list" {
|
|
transcoder.PrintTranscodePresets()
|
|
os.Exit(0)
|
|
}
|
|
|
|
if _, err := transcoder.GetPresetByName(args[0]); err != nil {
|
|
return fmt.Errorf("Preset does not exist")
|
|
}
|
|
|
|
return nil
|
|
},
|
|
})
|
|
|
|
arguments.IgnoreExtension = cmd.Flag("", "ignore-extension", &argparse.Options{})
|
|
|
|
return cmd, arguments
|
|
}
|
|
|
|
type TranscodeCommand struct {
|
|
Args *TranscodeCommandArgs
|
|
}
|
|
|
|
func NewTranscodeCommand(args *TranscodeCommandArgs) *TranscodeCommand {
|
|
return &TranscodeCommand{
|
|
Args: args,
|
|
}
|
|
}
|
|
|
|
func (c *TranscodeCommand) Run() {
|
|
log.Info().
|
|
Str("src", *c.Args.Source).
|
|
Str("dest", *c.Args.Dest).
|
|
Msg("Transcoding")
|
|
|
|
var transcode_config *transcoder.TranscodeConfig
|
|
|
|
// TODO: Reimplement custom transcode config file loading
|
|
if len(*c.Args.TranscodePreset) != 0 {
|
|
preset, err := transcoder.GetPresetByName(*c.Args.TranscodePreset)
|
|
if err != nil {
|
|
panic(err)
|
|
} else {
|
|
transcode_config = preset.Config
|
|
}
|
|
}
|
|
|
|
input_file := types.FileFromPath("", *c.Args.Source)
|
|
output_file := types.FileFromPath("", *c.Args.Dest)
|
|
|
|
if !*c.Args.IgnoreExtension && transcode_config.FileExtension != output_file.Extension {
|
|
log.Error().Msgf(
|
|
"%s is not the recommended "+
|
|
"extension for specified transcode config "+
|
|
"please change it to %s "+
|
|
"or run with --ignore-extension",
|
|
output_file.Extension, transcode_config.FileExtension)
|
|
}
|
|
|
|
progress_chan := make(chan string)
|
|
go func() {
|
|
for {
|
|
progress, isnt_closed := <-progress_chan
|
|
if isnt_closed {
|
|
log.Info().
|
|
Str("src", *c.Args.Source).
|
|
Str("progress", progress).
|
|
Msg("Transcoding")
|
|
} else {
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
output, err := transcoder.Transcode(input_file, transcode_config, progress_chan, output_file.JoinPathTo())
|
|
if err != nil {
|
|
log.Fatal().Err(err).Str("output", output).Msg("Transcode Failed")
|
|
}
|
|
}
|