music-library/ffmpeg.go

42 lines
839 B
Go
Raw Normal View History

2025-01-15 12:48:20 +01:00
package main
import (
"context"
"fmt"
"os/exec"
)
func GetStreamsFromFile(path string) (string, error) {
cmd := exec.Command(
"ffprobe",
"-loglevel", "error",
"-show_entries", "stream=codec_type",
"-of", "csv=p=0",
path,
)
2025-01-16 13:37:23 +01:00
data, err := cmd.CombinedOutput()
2025-01-15 12:48:20 +01:00
return string(data), err
}
func ConvertToAudio(ctx context.Context, videoFile, targetPath string) error {
cmd := exec.CommandContext(
ctx, "ffmpeg",
"-i", videoFile,
"-y", // overwrite existing file
"-vn", // no video
"-ar", "44100", // sample rate
"-ac", "2", // audio channels
"-ab", "192k", // dont know dont care
"-f", "mp3", // output format
targetPath,
)
data, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error during process execution: %w\nOutput:\n%s", err, string(data))
}
return nil
}