Compare commits
31 Commits
v0.2.0-bet
...
main
Author | SHA1 | Date | |
---|---|---|---|
77729cf653 | |||
b317b89ae9 | |||
89b629304e | |||
c0c3bda9ce | |||
fa8f7a1565 | |||
d4d5e5ddf4 | |||
57c1aa3e54 | |||
b0998d8c8a | |||
f819726f3e | |||
fbad7ec450 | |||
19bc6c466e | |||
b9e2a38f82 | |||
673ea8491f | |||
0d071a82be | |||
b8e6196248 | |||
1323fb9e1a | |||
36df203bdd | |||
2afc232eb1 | |||
2abdd47c9c | |||
8b59bc026d | |||
91855f8f5b | |||
7edd326b95 | |||
0b3ad0d97c | |||
c87204c78a | |||
3630fc0c78 | |||
b3f7cd33df | |||
c6fa777db1 | |||
cab705a130 | |||
f258e3e8f2 | |||
59f3759d48 | |||
db547002f9 |
@ -8,7 +8,10 @@ local CreateRelease() = {
|
||||
settings: {
|
||||
api_key: { from_secret: 'GITEA_API_KEY' },
|
||||
base_url: 'https://git.unlock-music.dev',
|
||||
files: 'dist/*',
|
||||
files: [
|
||||
'um-*.tar.gz',
|
||||
'um-*.zip',
|
||||
],
|
||||
checksum: 'sha256',
|
||||
draft: true,
|
||||
title: '${DRONE_TAG}',
|
||||
@ -17,25 +20,36 @@ local CreateRelease() = {
|
||||
|
||||
|
||||
local StepGoBuild(GOOS, GOARCH) = {
|
||||
local filepath = 'dist/um-%s-%s.tar.gz' % [GOOS, GOARCH],
|
||||
local windows = GOOS == 'windows',
|
||||
local archiveExt = if windows then 'zip' else 'tar.gz',
|
||||
local filepath = 'dist/um-%s-%s-%s.%s' % [GOOS, GOARCH, '$(git describe --tags --always)', archiveExt],
|
||||
|
||||
local archive = if windows then [
|
||||
// Ensure zip is installed
|
||||
'command -v zip >/dev/null || (apt update && apt install -y zip)',
|
||||
'zip -9 -j -r "%s" $DIST_DIR' % filepath,
|
||||
] else [
|
||||
'tar -c -C $DIST_DIR um | gzip -9 > "%s"' % filepath,
|
||||
],
|
||||
|
||||
name: 'go build %s/%s' % [GOOS, GOARCH],
|
||||
image: 'golang:1.22',
|
||||
environment: {
|
||||
GOOS: GOOS,
|
||||
GOARCH: GOARCH,
|
||||
GOPROXY: "https://goproxy.io,direct",
|
||||
GOPROXY: 'https://goproxy.io,direct',
|
||||
},
|
||||
commands: [
|
||||
'DIST_DIR=$(mktemp -d)',
|
||||
'go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags --always)" -o $DIST_DIR ./cmd/um',
|
||||
'mkdir -p dist',
|
||||
'tar cz -f %s -C $DIST_DIR .' % filepath,
|
||||
],
|
||||
] + archive,
|
||||
};
|
||||
|
||||
local StepUploadArtifact(GOOS, GOARCH) = {
|
||||
local filename = 'um-%s-%s.tar.gz' % [GOOS, GOARCH],
|
||||
local windows = GOOS == 'windows',
|
||||
local archiveExt = if windows then 'zip' else 'tar.gz',
|
||||
local filename = 'um-%s-%s-%s.%s' % [GOOS, GOARCH, '$(git describe --tags --always)', archiveExt],
|
||||
local filepath = 'dist/%s' % filename,
|
||||
local pkgname = '${DRONE_REPO_NAME}-build',
|
||||
|
||||
@ -71,7 +85,7 @@ local PipelineBuild(GOOS, GOARCH, RUN_TEST) = {
|
||||
name: 'go test',
|
||||
image: 'golang:1.22',
|
||||
environment: {
|
||||
GOPROXY: "https://goproxy.io,direct",
|
||||
GOPROXY: 'https://goproxy.io,direct',
|
||||
},
|
||||
commands: ['go test -v ./...'],
|
||||
}] else []
|
||||
@ -100,7 +114,7 @@ local PipelineRelease() = {
|
||||
name: 'go test',
|
||||
image: 'golang:1.22',
|
||||
environment: {
|
||||
GOPROXY: "https://goproxy.io,direct",
|
||||
GOPROXY: 'https://goproxy.io,direct',
|
||||
},
|
||||
commands: ['go test -v ./...'],
|
||||
},
|
||||
@ -108,9 +122,17 @@ local PipelineRelease() = {
|
||||
StepGoBuild('linux', 'arm64'),
|
||||
StepGoBuild('linux', '386'),
|
||||
StepGoBuild('windows', 'amd64'),
|
||||
StepGoBuild('windows', 'arm64'),
|
||||
StepGoBuild('windows', '386'),
|
||||
StepGoBuild('darwin', 'amd64'),
|
||||
StepGoBuild('darwin', 'arm64'),
|
||||
{
|
||||
name: 'prepare root',
|
||||
image: 'golang:1.22',
|
||||
commands: [
|
||||
'mv dist/*.tar.gz dist/*.zip ./',
|
||||
],
|
||||
},
|
||||
CreateRelease(),
|
||||
],
|
||||
trigger: {
|
||||
|
71
.drone.yml
71
.drone.yml
@ -17,7 +17,8 @@ steps:
|
||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||
--always)" -o $DIST_DIR ./cmd/um
|
||||
- mkdir -p dist
|
||||
- tar cz -f dist/um-linux-amd64.tar.gz -C $DIST_DIR .
|
||||
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-amd64-$(git describe --tags
|
||||
--always).tar.gz"
|
||||
environment:
|
||||
GOARCH: amd64
|
||||
GOOS: linux
|
||||
@ -25,9 +26,10 @@ steps:
|
||||
image: golang:1.22
|
||||
name: go build linux/amd64
|
||||
- commands:
|
||||
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-linux-amd64.tar.gz"
|
||||
"$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-linux-amd64.tar.gz"
|
||||
- sha256sum dist/um-linux-amd64.tar.gz
|
||||
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-linux-amd64-$(git
|
||||
describe --tags --always).tar.gz" "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-linux-amd64-$(git
|
||||
describe --tags --always).tar.gz"
|
||||
- sha256sum dist/um-linux-amd64-$(git describe --tags --always).tar.gz
|
||||
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
|
||||
environment:
|
||||
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
||||
@ -53,7 +55,8 @@ steps:
|
||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||
--always)" -o $DIST_DIR ./cmd/um
|
||||
- mkdir -p dist
|
||||
- tar cz -f dist/um-windows-amd64.tar.gz -C $DIST_DIR .
|
||||
- command -v zip >/dev/null || (apt update && apt install -y zip)
|
||||
- zip -9 -j -r "dist/um-windows-amd64-$(git describe --tags --always).zip" $DIST_DIR
|
||||
environment:
|
||||
GOARCH: amd64
|
||||
GOOS: windows
|
||||
@ -61,9 +64,10 @@ steps:
|
||||
image: golang:1.22
|
||||
name: go build windows/amd64
|
||||
- commands:
|
||||
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-windows-amd64.tar.gz"
|
||||
"$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-windows-amd64.tar.gz"
|
||||
- sha256sum dist/um-windows-amd64.tar.gz
|
||||
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-windows-amd64-$(git
|
||||
describe --tags --always).zip" "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-windows-amd64-$(git
|
||||
describe --tags --always).zip"
|
||||
- sha256sum dist/um-windows-amd64-$(git describe --tags --always).zip
|
||||
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
|
||||
environment:
|
||||
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
||||
@ -89,7 +93,8 @@ steps:
|
||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||
--always)" -o $DIST_DIR ./cmd/um
|
||||
- mkdir -p dist
|
||||
- tar cz -f dist/um-darwin-amd64.tar.gz -C $DIST_DIR .
|
||||
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-darwin-amd64-$(git describe --tags
|
||||
--always).tar.gz"
|
||||
environment:
|
||||
GOARCH: amd64
|
||||
GOOS: darwin
|
||||
@ -97,9 +102,10 @@ steps:
|
||||
image: golang:1.22
|
||||
name: go build darwin/amd64
|
||||
- commands:
|
||||
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-darwin-amd64.tar.gz"
|
||||
"$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-darwin-amd64.tar.gz"
|
||||
- sha256sum dist/um-darwin-amd64.tar.gz
|
||||
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-darwin-amd64-$(git
|
||||
describe --tags --always).tar.gz" "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-darwin-amd64-$(git
|
||||
describe --tags --always).tar.gz"
|
||||
- sha256sum dist/um-darwin-amd64-$(git describe --tags --always).tar.gz
|
||||
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
|
||||
environment:
|
||||
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
||||
@ -131,7 +137,8 @@ steps:
|
||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||
--always)" -o $DIST_DIR ./cmd/um
|
||||
- mkdir -p dist
|
||||
- tar cz -f dist/um-linux-amd64.tar.gz -C $DIST_DIR .
|
||||
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-amd64-$(git describe --tags
|
||||
--always).tar.gz"
|
||||
environment:
|
||||
GOARCH: amd64
|
||||
GOOS: linux
|
||||
@ -143,7 +150,8 @@ steps:
|
||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||
--always)" -o $DIST_DIR ./cmd/um
|
||||
- mkdir -p dist
|
||||
- tar cz -f dist/um-linux-arm64.tar.gz -C $DIST_DIR .
|
||||
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-arm64-$(git describe --tags
|
||||
--always).tar.gz"
|
||||
environment:
|
||||
GOARCH: arm64
|
||||
GOOS: linux
|
||||
@ -155,7 +163,7 @@ steps:
|
||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||
--always)" -o $DIST_DIR ./cmd/um
|
||||
- mkdir -p dist
|
||||
- tar cz -f dist/um-linux-386.tar.gz -C $DIST_DIR .
|
||||
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-386-$(git describe --tags --always).tar.gz"
|
||||
environment:
|
||||
GOARCH: "386"
|
||||
GOOS: linux
|
||||
@ -167,7 +175,8 @@ steps:
|
||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||
--always)" -o $DIST_DIR ./cmd/um
|
||||
- mkdir -p dist
|
||||
- tar cz -f dist/um-windows-amd64.tar.gz -C $DIST_DIR .
|
||||
- command -v zip >/dev/null || (apt update && apt install -y zip)
|
||||
- zip -9 -j -r "dist/um-windows-amd64-$(git describe --tags --always).zip" $DIST_DIR
|
||||
environment:
|
||||
GOARCH: amd64
|
||||
GOOS: windows
|
||||
@ -179,7 +188,21 @@ steps:
|
||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||
--always)" -o $DIST_DIR ./cmd/um
|
||||
- mkdir -p dist
|
||||
- tar cz -f dist/um-windows-386.tar.gz -C $DIST_DIR .
|
||||
- command -v zip >/dev/null || (apt update && apt install -y zip)
|
||||
- zip -9 -j -r "dist/um-windows-arm64-$(git describe --tags --always).zip" $DIST_DIR
|
||||
environment:
|
||||
GOARCH: arm64
|
||||
GOOS: windows
|
||||
GOPROXY: https://goproxy.io,direct
|
||||
image: golang:1.22
|
||||
name: go build windows/arm64
|
||||
- commands:
|
||||
- DIST_DIR=$(mktemp -d)
|
||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||
--always)" -o $DIST_DIR ./cmd/um
|
||||
- mkdir -p dist
|
||||
- command -v zip >/dev/null || (apt update && apt install -y zip)
|
||||
- zip -9 -j -r "dist/um-windows-386-$(git describe --tags --always).zip" $DIST_DIR
|
||||
environment:
|
||||
GOARCH: "386"
|
||||
GOOS: windows
|
||||
@ -191,7 +214,8 @@ steps:
|
||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||
--always)" -o $DIST_DIR ./cmd/um
|
||||
- mkdir -p dist
|
||||
- tar cz -f dist/um-darwin-amd64.tar.gz -C $DIST_DIR .
|
||||
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-darwin-amd64-$(git describe --tags
|
||||
--always).tar.gz"
|
||||
environment:
|
||||
GOARCH: amd64
|
||||
GOOS: darwin
|
||||
@ -203,13 +227,18 @@ steps:
|
||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||
--always)" -o $DIST_DIR ./cmd/um
|
||||
- mkdir -p dist
|
||||
- tar cz -f dist/um-darwin-arm64.tar.gz -C $DIST_DIR .
|
||||
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-darwin-arm64-$(git describe --tags
|
||||
--always).tar.gz"
|
||||
environment:
|
||||
GOARCH: arm64
|
||||
GOOS: darwin
|
||||
GOPROXY: https://goproxy.io,direct
|
||||
image: golang:1.22
|
||||
name: go build darwin/arm64
|
||||
- commands:
|
||||
- mv dist/*.tar.gz dist/*.zip ./
|
||||
image: golang:1.22
|
||||
name: prepare root
|
||||
- image: plugins/gitea-release
|
||||
name: create release
|
||||
settings:
|
||||
@ -218,7 +247,9 @@ steps:
|
||||
base_url: https://git.unlock-music.dev
|
||||
checksum: sha256
|
||||
draft: true
|
||||
files: dist/*
|
||||
files:
|
||||
- um-*.tar.gz
|
||||
- um-*.zip
|
||||
title: ${DRONE_TAG}
|
||||
trigger:
|
||||
event:
|
||||
|
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,3 +1,7 @@
|
||||
.idea
|
||||
|
||||
/dist
|
||||
*.exe
|
||||
|
||||
/um-*.tar.gz
|
||||
/um-*.zip
|
||||
|
13
README.md
13
README.md
@ -6,6 +6,9 @@ Original: Web Edition https://git.unlock-music.dev/um/web
|
||||
- [Release Download](https://git.unlock-music.dev/um/cli/releases/latest)
|
||||
- [Latest Build](https://git.unlock-music.dev/um/-/packages/generic/cli-build/)
|
||||
|
||||
> **WARNING**
|
||||
> 在本站 fork 不会起到备份的作用,只会浪费服务器储存空间。如无必要请勿 fork 该仓库。
|
||||
|
||||
## Features
|
||||
|
||||
- [x] All Algorithm Supported By `unlock-music/web`
|
||||
@ -22,3 +25,13 @@ Original: Web Edition https://git.unlock-music.dev/um/web
|
||||
- Drag the encrypted file to `um.exe` (Tested on Windows)
|
||||
- Run: `./um [-o <output dir>] [-i] <input dir/file>`
|
||||
- Use `./um -h` to show help menu
|
||||
|
||||
## Update CI pipeline
|
||||
|
||||
1. Install [Drone CI binary](https://docs.drone.io/cli/install/)
|
||||
2. Edit `.drone.jsonnet`
|
||||
3. Update drone CI pipeline:
|
||||
|
||||
```sh
|
||||
drone jsonnet --format --stream
|
||||
```
|
||||
|
@ -18,25 +18,32 @@ type DecoderParams struct {
|
||||
}
|
||||
type NewDecoderFunc func(p *DecoderParams) Decoder
|
||||
|
||||
type decoderItem struct {
|
||||
type DecoderFactory struct {
|
||||
noop bool
|
||||
decoder NewDecoderFunc
|
||||
Suffix string
|
||||
Create NewDecoderFunc
|
||||
}
|
||||
|
||||
var DecoderRegistry = make(map[string][]decoderItem)
|
||||
var DecoderRegistry []DecoderFactory
|
||||
|
||||
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
|
||||
DecoderRegistry[ext] = append(DecoderRegistry[ext],
|
||||
decoderItem{noop: noop, decoder: dispatchFunc})
|
||||
DecoderRegistry = append(DecoderRegistry,
|
||||
DecoderFactory{noop: noop, Create: dispatchFunc, Suffix: "." + strings.TrimPrefix(ext, ".")})
|
||||
}
|
||||
|
||||
func GetDecoder(filename string, skipNoop bool) (rs []NewDecoderFunc) {
|
||||
ext := strings.ToLower(strings.TrimLeft(filepath.Ext(filename), "."))
|
||||
for _, dec := range DecoderRegistry[ext] {
|
||||
func GetDecoder(filename string, skipNoop bool) []DecoderFactory {
|
||||
var result []DecoderFactory
|
||||
// Some extensions contains multiple dots, e.g. ".kgm.flac", hence iterate
|
||||
// all decoders for each extension.
|
||||
name := strings.ToLower(filepath.Base(filename))
|
||||
for _, dec := range DecoderRegistry {
|
||||
if !strings.HasSuffix(name, dec.Suffix) {
|
||||
continue
|
||||
}
|
||||
if skipNoop && dec.noop {
|
||||
continue
|
||||
}
|
||||
rs = append(rs, dec.decoder)
|
||||
result = append(result, dec)
|
||||
}
|
||||
return
|
||||
return result
|
||||
}
|
||||
|
@ -61,4 +61,7 @@ func init() {
|
||||
common.RegisterDecoder("kgma", false, NewDecoder)
|
||||
// Viper
|
||||
common.RegisterDecoder("vpr", false, NewDecoder)
|
||||
// Kugou Android
|
||||
common.RegisterDecoder("kgm.flac", false, NewDecoder)
|
||||
common.RegisterDecoder("vpr.flac", false, NewDecoder)
|
||||
}
|
||||
|
@ -149,12 +149,18 @@ func (d *Decoder) readMetaData() error {
|
||||
}
|
||||
|
||||
func (d *Decoder) readCoverData() error {
|
||||
bCoverCRC := make([]byte, 4)
|
||||
if _, err := io.ReadFull(d.rd, bCoverCRC); err != nil {
|
||||
return fmt.Errorf("ncm read cover crc: %w", err)
|
||||
bCoverFrameLen := make([]byte, 4)
|
||||
if _, err := io.ReadFull(d.rd, bCoverFrameLen); err != nil {
|
||||
return fmt.Errorf("ncm read cover length: %w", err)
|
||||
}
|
||||
|
||||
bCoverLen := make([]byte, 4) //
|
||||
coverFrameStartOffset, err := d.rd.Seek(0, io.SeekCurrent)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ncm fetch cover frame start offset: %w", err)
|
||||
}
|
||||
coverFrameLen := binary.LittleEndian.Uint32(bCoverFrameLen)
|
||||
|
||||
bCoverLen := make([]byte, 4)
|
||||
if _, err := io.ReadFull(d.rd, bCoverLen); err != nil {
|
||||
return fmt.Errorf("ncm read cover length: %w", err)
|
||||
}
|
||||
@ -166,7 +172,10 @@ func (d *Decoder) readCoverData() error {
|
||||
}
|
||||
d.cover = coverBuf
|
||||
|
||||
return nil
|
||||
offsetAudioData := coverFrameStartOffset + int64(coverFrameLen) + 4
|
||||
_, err = d.rd.Seek(offsetAudioData, io.SeekStart)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *Decoder) parseMeta() error {
|
||||
|
@ -254,12 +254,20 @@ func init() {
|
||||
"6d3461", //QQ Music Weiyun M4a
|
||||
"776176", //QQ Music Weiyun Wav
|
||||
|
||||
"mgg", "mgg1", "mggl", //QQ Music New Ogg
|
||||
"mflac", "mflac0", "mflach", //QQ Music New Flac
|
||||
|
||||
"mmp4", // QQ Music MP4 Container, tipically used for Dolby EAC3 stream
|
||||
}
|
||||
for _, ext := range supportedExts {
|
||||
common.RegisterDecoder(ext, false, NewDecoder)
|
||||
}
|
||||
|
||||
// New ogg/flac:
|
||||
extraExtsCanHaveSuffix := []string{"mgg", "mflac"}
|
||||
// Mac also adds some extra suffix to ext:
|
||||
extraExtSuffix := []string{"0", "1", "a", "h", "l"}
|
||||
for _, ext := range extraExtsCanHaveSuffix {
|
||||
common.RegisterDecoder(ext, false, NewDecoder)
|
||||
for _, suffix := range extraExtSuffix {
|
||||
common.RegisterDecoder(ext+suffix, false, NewDecoder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
148
cmd/um/main.go
148
cmd/um/main.go
@ -8,6 +8,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
@ -33,7 +34,7 @@ import (
|
||||
"unlock-music.dev/cli/internal/utils"
|
||||
)
|
||||
|
||||
var AppVersion = "v0.0.6"
|
||||
var AppVersion = "v0.2.7"
|
||||
|
||||
var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger
|
||||
|
||||
@ -50,7 +51,7 @@ func main() {
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{Name: "input", Aliases: []string{"i"}, Usage: "path to input file or dir", Required: false},
|
||||
&cli.StringFlag{Name: "output", Aliases: []string{"o"}, Usage: "path to output dir", Required: false},
|
||||
&cli.StringFlag{Name: "qmc-mmkv", Aliases: []string{"db"}, Usage: "path to qmc mmkv (`.crc` file also required)", Required: false},
|
||||
&cli.StringFlag{Name: "qmc-mmkv", Aliases: []string{"db"}, Usage: "path to qmc mmkv (.crc file also required)", Required: false},
|
||||
&cli.StringFlag{Name: "qmc-mmkv-key", Aliases: []string{"key"}, Usage: "mmkv password (16 ascii chars)", Required: false},
|
||||
&cli.BoolFlag{Name: "remove-source", Aliases: []string{"rs"}, Usage: "remove source file", Required: false, Value: false},
|
||||
&cli.BoolFlag{Name: "skip-noop", Aliases: []string{"n"}, Usage: "skip noop decoder", Required: false, Value: true},
|
||||
@ -74,16 +75,30 @@ func main() {
|
||||
|
||||
func printSupportedExtensions() {
|
||||
var exts []string
|
||||
for ext := range common.DecoderRegistry {
|
||||
extSet := make(map[string]int)
|
||||
for _, factory := range common.DecoderRegistry {
|
||||
ext := strings.TrimPrefix(factory.Suffix, ".")
|
||||
if n, ok := extSet[ext]; ok {
|
||||
extSet[ext] = n + 1
|
||||
} else {
|
||||
extSet[ext] = 1
|
||||
}
|
||||
}
|
||||
for ext := range extSet {
|
||||
exts = append(exts, ext)
|
||||
}
|
||||
sort.Strings(exts)
|
||||
for _, ext := range exts {
|
||||
fmt.Printf("%s: %d\n", ext, len(common.DecoderRegistry[ext]))
|
||||
fmt.Printf("%s: %d\n", ext, extSet[ext])
|
||||
}
|
||||
}
|
||||
|
||||
func appMain(c *cli.Context) (err error) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if c.Bool("supported-ext") {
|
||||
printSupportedExtensions()
|
||||
return nil
|
||||
@ -92,10 +107,7 @@ func appMain(c *cli.Context) (err error) {
|
||||
if input == "" {
|
||||
switch c.Args().Len() {
|
||||
case 0:
|
||||
input, err = os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
input = cwd
|
||||
case 1:
|
||||
input = c.Args().Get(0)
|
||||
default:
|
||||
@ -104,22 +116,20 @@ func appMain(c *cli.Context) (err error) {
|
||||
}
|
||||
|
||||
output := c.String("output")
|
||||
if output == "" {
|
||||
var err error
|
||||
output, err = os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if input == output {
|
||||
return errors.New("input and output path are same")
|
||||
}
|
||||
}
|
||||
|
||||
inputStat, err := os.Stat(input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if output == "" {
|
||||
// Default to where the input is
|
||||
if inputStat.IsDir() {
|
||||
output = input
|
||||
} else {
|
||||
output = path.Dir(input)
|
||||
}
|
||||
}
|
||||
|
||||
outputStat, err := os.Stat(output)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
@ -142,6 +152,7 @@ func appMain(c *cli.Context) (err error) {
|
||||
}
|
||||
|
||||
proc := &processor{
|
||||
inputDir: input,
|
||||
outputDir: output,
|
||||
skipNoopDecoder: c.Bool("skip-noop"),
|
||||
removeSource: c.Bool("remove-source"),
|
||||
@ -150,8 +161,8 @@ func appMain(c *cli.Context) (err error) {
|
||||
}
|
||||
|
||||
if inputStat.IsDir() {
|
||||
wacthDir := c.Bool("watch")
|
||||
if !wacthDir {
|
||||
watchDir := c.Bool("watch")
|
||||
if !watchDir {
|
||||
return proc.processDir(input)
|
||||
} else {
|
||||
return proc.watchDir(input)
|
||||
@ -163,6 +174,7 @@ func appMain(c *cli.Context) (err error) {
|
||||
}
|
||||
|
||||
type processor struct {
|
||||
inputDir string
|
||||
outputDir string
|
||||
|
||||
skipNoopDecoder bool
|
||||
@ -230,34 +242,62 @@ func (p *processor) processDir(inputDir string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var lastError error = nil
|
||||
for _, item := range items {
|
||||
if item.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
filePath := filepath.Join(inputDir, item.Name())
|
||||
allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
|
||||
if len(allDec) == 0 {
|
||||
logger.Info("skipping while no suitable decoder", zap.String("source", item.Name()))
|
||||
if item.IsDir() {
|
||||
if err = p.processDir(filePath); err != nil {
|
||||
lastError = err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if err := p.process(filePath, allDec); err != nil {
|
||||
if err := p.processFile(filePath); err != nil {
|
||||
lastError = err
|
||||
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err))
|
||||
}
|
||||
}
|
||||
if lastError != nil {
|
||||
return fmt.Errorf("last error: %w", lastError)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *processor) processFile(filePath string) error {
|
||||
allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
|
||||
if len(allDec) == 0 {
|
||||
logger.Fatal("skipping while no suitable decoder")
|
||||
return errors.New("skipping while no suitable decoder")
|
||||
}
|
||||
return p.process(filePath, allDec)
|
||||
|
||||
if err := p.process(filePath, allDec); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// if source file need to be removed
|
||||
if p.removeSource {
|
||||
err := os.RemoveAll(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger.Info("source file removed after success conversion", zap.String("source", filePath))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) error {
|
||||
func (p *processor) findDecoder(decoders []common.DecoderFactory, params *common.DecoderParams) (*common.Decoder, *common.DecoderFactory, error) {
|
||||
for _, factory := range decoders {
|
||||
dec := factory.Create(params)
|
||||
err := dec.Validate()
|
||||
if err == nil {
|
||||
return &dec, &factory, nil
|
||||
}
|
||||
logger.Warn("try decode failed", zap.Error(err))
|
||||
}
|
||||
return nil, nil, errors.New("no any decoder can resolve the file")
|
||||
}
|
||||
|
||||
func (p *processor) process(inputFile string, allDec []common.DecoderFactory) error {
|
||||
file, err := os.Open(inputFile)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -265,26 +305,16 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
|
||||
defer file.Close()
|
||||
logger := logger.With(zap.String("source", inputFile))
|
||||
|
||||
decParams := &common.DecoderParams{
|
||||
pDec, decoderFactory, err := p.findDecoder(allDec, &common.DecoderParams{
|
||||
Reader: file,
|
||||
Extension: filepath.Ext(inputFile),
|
||||
FilePath: inputFile,
|
||||
Logger: logger,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var dec common.Decoder
|
||||
for _, decFunc := range allDec {
|
||||
dec = decFunc(decParams)
|
||||
if err := dec.Validate(); err == nil {
|
||||
break
|
||||
} else {
|
||||
logger.Warn("try decode failed", zap.Error(err))
|
||||
dec = nil
|
||||
}
|
||||
}
|
||||
if dec == nil {
|
||||
return errors.New("no any decoder can resolve the file")
|
||||
}
|
||||
dec := *pDec
|
||||
|
||||
params := &ffmpeg.UpdateMetadataParams{}
|
||||
|
||||
@ -341,13 +371,19 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
|
||||
}
|
||||
}
|
||||
|
||||
inFilename := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
|
||||
outPath := filepath.Join(p.outputDir, inFilename+params.AudioExt)
|
||||
inputRelDir, err := filepath.Rel(p.inputDir, filepath.Dir(inputFile))
|
||||
if err != nil {
|
||||
return fmt.Errorf("get relative dir failed: %w", err)
|
||||
}
|
||||
|
||||
inFilename := strings.TrimSuffix(filepath.Base(inputFile), decoderFactory.Suffix)
|
||||
outPath := filepath.Join(p.outputDir, inputRelDir, inFilename+params.AudioExt)
|
||||
|
||||
if !p.overwriteOutput {
|
||||
_, err := os.Stat(outPath)
|
||||
if err == nil {
|
||||
return fmt.Errorf("output file %s is already exist", outPath)
|
||||
logger.Warn("output file already exist, skip", zap.String("destination", outPath))
|
||||
return nil
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return fmt.Errorf("stat output file failed: %w", err)
|
||||
}
|
||||
@ -363,8 +399,6 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
|
||||
if _, err := io.Copy(outFile, audio); err != nil {
|
||||
return err
|
||||
}
|
||||
outFile.Close()
|
||||
|
||||
} else {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer cancel()
|
||||
@ -374,16 +408,6 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
|
||||
}
|
||||
}
|
||||
|
||||
// if source file need to be removed
|
||||
if p.removeSource {
|
||||
err := os.RemoveAll(inputFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger.Info("successfully converted, and source file is removed", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||
} else {
|
||||
logger.Info("successfully converted", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
8
go.mod
8
go.mod
@ -10,10 +10,10 @@ require (
|
||||
github.com/samber/lo v1.39.0
|
||||
github.com/urfave/cli/v2 v2.27.1
|
||||
go.uber.org/zap v1.27.0
|
||||
golang.org/x/crypto v0.22.0
|
||||
golang.org/x/crypto v0.26.0
|
||||
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
|
||||
golang.org/x/text v0.14.0
|
||||
unlock-music.dev/mmkv v0.0.0-20240424090133-0953e6901f3a
|
||||
golang.org/x/text v0.17.0
|
||||
unlock-music.dev/mmkv v0.0.0-20240424090133-31549c6a948b
|
||||
)
|
||||
|
||||
require (
|
||||
@ -21,6 +21,6 @@ require (
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
golang.org/x/sys v0.19.0 // indirect
|
||||
golang.org/x/sys v0.23.0 // indirect
|
||||
google.golang.org/protobuf v1.33.0 // indirect
|
||||
)
|
||||
|
8
go.sum
8
go.sum
@ -26,14 +26,22 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
||||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
|
||||
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
|
||||
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
||||
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
||||
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2nTQVIEhZIptnrVb1XY=
|
||||
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
|
||||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
|
||||
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
|
||||
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
unlock-music.dev/mmkv v0.0.0-20240424090133-0953e6901f3a h1:UW0sxgwsfGGC/SrKvvAbZ4HZyOQ3fqs8qr3lBxG6Fzo=
|
||||
unlock-music.dev/mmkv v0.0.0-20240424090133-0953e6901f3a/go.mod h1:qr34SM3x8xRxyUfGzefH/rSi+DUXkQZcSfXY/yfuTeo=
|
||||
unlock-music.dev/mmkv v0.0.0-20240424090133-31549c6a948b h1:VIJ0mDqj0OgX1ZvL9gbAH8kkqyrDlpVt5yUeGYSJ1/s=
|
||||
unlock-music.dev/mmkv v0.0.0-20240424090133-31549c6a948b/go.mod h1:qr34SM3x8xRxyUfGzefH/rSi+DUXkQZcSfXY/yfuTeo=
|
||||
|
Loading…
Reference in New Issue
Block a user