6 Commits

Author SHA1 Message Date
1760737121 Fix CI: Checkout with history & tags 2021-05-16 13:50:35 +08:00
b517806fdb Fix CI 2021-05-16 13:43:36 +08:00
b6df09cee3 Fix CI 2021-05-16 13:41:13 +08:00
9cf42af251 Use git tag as version 2021-05-16 12:30:48 +08:00
c1c43d2a41 Sniff Output Audio Extension 2021-05-16 12:18:19 +08:00
f9686bbfc4 Decoder.GetAudioExt() return extension with . 2021-05-16 12:15:22 +08:00
11 changed files with 86 additions and 19 deletions

View File

@ -6,6 +6,7 @@ on:
- "**/*.go"
- "go.mod"
- "go.sum"
- ".github/workflows/*.yml"
pull_request:
branches: [ master ]
types: [ opened, synchronize, reopened ]
@ -22,14 +23,16 @@ jobs:
os: [ windows-latest, ubuntu-latest, macos-latest ]
include:
- os: ubuntu-latest
BIN_SUFFIX:
BIN_SUFFIX: ""
- os: macos-latest
BIN_SUFFIX:
BIN_SUFFIX: ""
- os: windows-latest
BIN_SUFFIX: .exe
BIN_SUFFIX: ".exe"
steps:
- name: Checkout codebase
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go 1.x
uses: actions/setup-go@v2
@ -38,12 +41,14 @@ jobs:
- name: Setup vars
id: vars
run: echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
run: |
echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
echo "::set-output name=git_tag::$(git describe --tags --always)"
- name: Build
env:
CGO_ENABLED: 0
run: go build -trimpath -ldflags="-w -s" -v -o um-${{ runner.os }}${{ matrix.BIN_SUFFIX }} ./cmd/um
run: go build -trimpath -ldflags="-w -s -X main.AppVersion=${{ steps.vars.outputs.git_tag }}" -v -o um-${{ runner.os }}${{ matrix.BIN_SUFFIX }} ./cmd/um
- name: Publish artifact
uses: actions/upload-artifact@v2

View File

@ -27,7 +27,7 @@ func (d RawDecoder) GetAudioData() []byte {
}
func (d RawDecoder) GetAudioExt() string {
return d.audioExt
return "." + d.audioExt
}
func (d RawDecoder) GetMeta() Meta {

44
algo/common/sniff.go Normal file
View File

@ -0,0 +1,44 @@
package common
import "bytes"
type Sniffer func(header []byte) bool
var snifferRegistry = map[string]Sniffer{
".m4a": SnifferM4A,
".ogg": SnifferOGG,
".flac": SnifferFLAC,
".wav": SnifferWAV,
".wma": SnifferWMA,
".mp3": SnifferMP3,
}
func SniffAll(header []byte) (string, bool) {
for ext, sniffer := range snifferRegistry {
if sniffer(header) {
return ext, true
}
}
return "", false
}
func SnifferM4A(header []byte) bool {
return len(header) >= 8 && bytes.Equal([]byte("ftyp"), header[4:8])
}
func SnifferOGG(header []byte) bool {
return bytes.HasPrefix(header, []byte("OggS"))
}
func SnifferFLAC(header []byte) bool {
return bytes.HasPrefix(header, []byte("fLaC"))
}
func SnifferMP3(header []byte) bool {
return bytes.HasPrefix(header, []byte("ID3"))
}
func SnifferWAV(header []byte) bool {
return bytes.HasPrefix(header, []byte("RIFF"))
}
func SnifferWMA(header []byte) bool {
return bytes.HasPrefix(header, []byte("\x30\x26\xb2\x75\x8e\x66\xcf\x11\xa6\xd9\x00\xaa\x00\x62\xce\x6c"))
}

View File

@ -40,7 +40,7 @@ func (d *Decoder) GetAudioData() []byte {
}
func (d *Decoder) GetAudioExt() string {
return d.outputExt
return "." + d.outputExt
}
func (d *Decoder) GetMeta() common.Meta {

View File

@ -201,7 +201,9 @@ func (d *Decoder) Decode() error {
func (d Decoder) GetAudioExt() string {
if d.meta != nil {
return d.meta.GetFormat()
if format := d.meta.GetFormat(); format != "" {
return "." + d.meta.GetFormat()
}
}
return ""
}

View File

@ -38,8 +38,6 @@ var (
0x92, 0x62, 0xf3, 0x74, 0xa1, 0x9f, 0xf4, 0xa0,
0x1d, 0x3f, 0x5b, 0xf0, 0x13, 0x0e, 0x09, 0x3d,
0xf9, 0xbc, 0x00, 0x11}
headerFlac = []byte{'f', 'L', 'a', 'C'}
headerOgg = []byte{'O', 'g', 'g', 'S'}
)
var key256MappingAll [][]int //[idx256][idx128]idx44
var key256Mapping128to44 map[int]int

View File

@ -3,6 +3,7 @@ package qmc
import (
"bytes"
"errors"
"github.com/unlock-music/cli/algo/common"
"github.com/unlock-music/cli/internal/logging"
"go.uber.org/zap"
)
@ -116,7 +117,7 @@ func detectMflac256Mask(input []byte) (*Key256Mask, error) {
if err != nil {
continue
}
if bytes.Equal(headerFlac, q.Decrypt(input[:len(headerFlac)])) {
if common.SnifferFLAC(q.Decrypt(input[:4])) {
rtErr = nil
break
}
@ -164,7 +165,7 @@ func detectMgg256Mask(input []byte) (*Key256Mask, error) {
if err != nil {
return nil, err
}
if bytes.Equal(headerOgg, q.Decrypt(input[:len(headerOgg)])) {
if common.SnifferOGG(q.Decrypt(input[:4])) {
return q, nil
}
return nil, ErrDetectMggMask

View File

@ -84,7 +84,10 @@ func (d Decoder) GetAudioData() []byte {
}
func (d Decoder) GetAudioExt() string {
return d.audioExt
if d.audioExt != "" {
return "." + d.audioExt
}
return ""
}
func (d Decoder) GetMeta() common.Meta {

View File

@ -25,7 +25,10 @@ func (d *Decoder) GetAudioData() []byte {
}
func (d *Decoder) GetAudioExt() string {
return d.audioExt
if d.audioExt != "" {
return "." + d.audioExt
}
return ""
}
func (d *Decoder) GetMeta() common.Meta {

View File

@ -38,7 +38,11 @@ func (d *Decoder) GetAudioData() []byte {
}
func (d *Decoder) GetAudioExt() string {
return d.outputExt
if d.outputExt != "" {
return "." + d.outputExt
}
return ""
}
func (d *Decoder) GetMeta() common.Meta {

View File

@ -18,12 +18,14 @@ import (
"strings"
)
var AppVersion = "0.0.3"
func main() {
app := cli.App{
Name: "Unlock Music CLI",
HelpName: "um",
Usage: "Unlock your encrypted music file https://github.com/unlock-music/cli",
Version: "v0.0.1",
Version: AppVersion,
Flags: []cli.Flag{
&cli.StringFlag{Name: "input", Aliases: []string{"i"}, Usage: "path to input file or dir", Required: true},
&cli.StringFlag{Name: "output", Aliases: []string{"o"}, Usage: "path to output dir", Required: true},
@ -117,14 +119,19 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu
return errors.New("failed while decoding: " + err.Error())
}
outData := dec.GetAudioData()
outExt := dec.GetAudioExt()
if outExt == "" {
outExt = "mp3"
if ext, ok := common.SniffAll(outData); ok {
outExt = ext
} else {
outExt = ".mp3"
}
}
filenameOnly := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
outPath := filepath.Join(outputDir, filenameOnly+"."+outExt)
err = os.WriteFile(outPath, dec.GetAudioData(), 0644)
outPath := filepath.Join(outputDir, filenameOnly+outExt)
err = os.WriteFile(outPath, outData, 0644)
if err != nil {
return err
}