Some checks failed
CodeQL / Analyze (go) (push) Successful in 6m28s
Docker Image / build-docker (push) Failing after 13m26s
Lint and Testing / lint (push) Successful in 11m17s
Lint and Testing / test (push) Successful in 11m17s
Lint and Testing / golangci (push) Successful in 2m40s
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package transcode
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
)
|
|
|
|
// sniffFormat detects container/codec from file header when the path has no extension.
|
|
func sniffFormat(r io.Reader) string {
|
|
head := make([]byte, 32)
|
|
n, _ := io.ReadFull(r, head)
|
|
head = head[:n]
|
|
if len(head) < 4 {
|
|
return ""
|
|
}
|
|
if bytes.HasPrefix(head, []byte("RIFF")) && len(head) >= 12 && bytes.Equal(head[8:12], []byte("WAVE")) {
|
|
return ".wav"
|
|
}
|
|
if bytes.HasPrefix(head, []byte("ID3")) {
|
|
return ".mp3"
|
|
}
|
|
if len(head) >= 2 && head[0] == 0xFF && (head[1]&0xE0) == 0xE0 {
|
|
return ".mp3"
|
|
}
|
|
if bytes.HasPrefix(head, []byte("fLaC")) {
|
|
return ".flac"
|
|
}
|
|
if bytes.HasPrefix(head, []byte("OggS")) {
|
|
return ".ogg"
|
|
}
|
|
if len(head) >= 8 && bytes.Equal(head[4:8], []byte("ftyp")) {
|
|
return ".m4a"
|
|
}
|
|
if bytes.HasPrefix(head, []byte{0xFF, 0xF1}) || bytes.HasPrefix(head, []byte{0xFF, 0xF9}) {
|
|
return ".aac"
|
|
}
|
|
if bytes.HasPrefix(head, []byte("FORM")) && len(head) >= 12 && bytes.Equal(head[8:12], []byte("AIFF")) {
|
|
return ".aiff"
|
|
}
|
|
if bytes.HasPrefix(head, []byte{0x1A, 0x45, 0xDF, 0xA3}) {
|
|
return ".webm"
|
|
}
|
|
return ""
|
|
}
|