mirror of
https://github.com/cgzirim/seek-tune.git
synced 2025-12-18 09:24:19 +00:00
Write function to get metadata from song file
This commit is contained in:
parent
96127cffb2
commit
d16c6f1553
1 changed files with 52 additions and 0 deletions
52
wav/wav.go
52
wav/wav.go
|
|
@ -3,10 +3,12 @@ package wav
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// WavHeader defines the structure of a WAV header
|
||||
|
|
@ -148,3 +150,53 @@ func WavBytesToSamples(input []byte) ([]float64, error) {
|
|||
|
||||
return output, nil
|
||||
}
|
||||
|
||||
// FFmpegMetadata represents the metadata structure returned by ffprobe.
|
||||
type FFmpegMetadata struct {
|
||||
Streams []struct {
|
||||
Index int `json:"index"`
|
||||
CodecName string `json:"codec_name"`
|
||||
CodecLongName string `json:"codec_long_name"`
|
||||
CodecType string `json:"codec_type"`
|
||||
SampleFmt string `json:"sample_fmt"`
|
||||
SampleRate string `json:"sample_rate"`
|
||||
Channels int `json:"channels"`
|
||||
ChannelLayout string `json:"channel_layout"`
|
||||
BitsPerSample int `json:"bits_per_sample"`
|
||||
Duration string `json:"duration"`
|
||||
BitRate string `json:"bit_rate"`
|
||||
Disposition map[string]int `json:"disposition"`
|
||||
Tags map[string]string `json:"tags"`
|
||||
} `json:"streams"`
|
||||
Format struct {
|
||||
Streams int `json:"nb_streams"`
|
||||
FormFilename string `json:"filename"`
|
||||
NbatName string `json:"format_name"`
|
||||
FormatLongName string `json:"format_long_name"`
|
||||
StartTime string `json:"start_time"`
|
||||
Duration string `json:"duration"`
|
||||
Size string `json:"size"`
|
||||
BitRate string `json:"bit_rate"`
|
||||
Tags map[string]string `json:"tags"`
|
||||
} `json:"format"`
|
||||
}
|
||||
|
||||
// GetMetadata retrieves metadata from a file using ffprobe.
|
||||
func GetMetadata(filePath string) (FFmpegMetadata, error) {
|
||||
var metadata FFmpegMetadata
|
||||
|
||||
cmd := exec.Command("ffprobe", "-v", "quiet", "-print_format", "json", "-show_format", "-show_streams", filePath)
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return metadata, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(out.Bytes(), &metadata)
|
||||
if err != nil {
|
||||
return metadata, err
|
||||
}
|
||||
|
||||
return metadata, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue