Compare commits
104 Commits
v0.2.0-tes
...
main
Author | SHA1 | Date | |
---|---|---|---|
28ca8cef60 | |||
2b0bd2985e | |||
72ace9fc62 | |||
074e4f874f | |||
2bfb5ffddf | |||
2c9de7c56c | |||
b374c11c86 | |||
6493b2c5fc | |||
f753b9c67d | |||
8829a3b3ba | |||
b2ef27761f | |||
7f7cb66fe5 | |||
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 | |||
05a1affa03 | |||
68e0c06246 | |||
e010d1b308 | |||
9b4e3b87ef | |||
1ccc507f61 | |||
119a8cf380 | |||
6179bcf0c7 | |||
c9ed772ce0 | |||
9c0acb794e | |||
bda32bc53e | |||
3f6566f7a9 | |||
1c5310ffae | |||
2941a9ac76 | |||
1835f9852a | |||
4f4d8db3d5 | |||
877f37f01e | |||
7b12f61a97 | |||
f6bc911125 | |||
|
9ddbd5f702 | ||
|
d480c9a2f8 | ||
907fe4143b | |||
4ad9a721f0 | |||
1bbc707dcc | |||
dfbb807e41 | |||
a928611a8d | |||
2754c14fa6 | |||
8858ff37ad | |||
d0d9836990 | |||
1a508fc2b6 | |||
0acf905d08 | |||
f3cbd2f3b4 | |||
fd6f830916 | |||
02e065aac4 | |||
8319df6ca3 | |||
3857ba91a6 | |||
9856f52070 | |||
112d9ab28e | |||
f60f0b3d07 | |||
3cf542c84c | |||
9494a535a9 | |||
138adbf846 | |||
c878bb8ca4 | |||
e9e63ce175 | |||
2c378d4d46 | |||
21eab14e6c | |||
0a0179c614 | |||
12be881d42 | |||
6f033af336 | |||
79d00b356f | |||
f6149c9109 | |||
3739638ddf | |||
423767ba63 | |||
a9c976f47d | |||
5fbcdb77d4 | |||
743c672c44 | |||
9241512f2d | |||
52e986e644 | |||
d2019b04ec | |||
ea3236e14b | |||
ad64a0f91d | |||
921f9b2ae6 |
@ -8,7 +8,10 @@ local CreateRelease() = {
|
|||||||
settings: {
|
settings: {
|
||||||
api_key: { from_secret: 'GITEA_API_KEY' },
|
api_key: { from_secret: 'GITEA_API_KEY' },
|
||||||
base_url: 'https://git.unlock-music.dev',
|
base_url: 'https://git.unlock-music.dev',
|
||||||
files: 'dist/*',
|
files: [
|
||||||
|
'um-*.tar.gz',
|
||||||
|
'um-*.zip',
|
||||||
|
],
|
||||||
checksum: 'sha256',
|
checksum: 'sha256',
|
||||||
draft: true,
|
draft: true,
|
||||||
title: '${DRONE_TAG}',
|
title: '${DRONE_TAG}',
|
||||||
@ -17,29 +20,41 @@ local CreateRelease() = {
|
|||||||
|
|
||||||
|
|
||||||
local StepGoBuild(GOOS, GOARCH) = {
|
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],
|
name: 'go build %s/%s' % [GOOS, GOARCH],
|
||||||
image: 'golang:1.19',
|
image: 'golang:1.23',
|
||||||
environment: {
|
environment: {
|
||||||
GOOS: GOOS,
|
GOOS: GOOS,
|
||||||
GOARCH: GOARCH,
|
GOARCH: GOARCH,
|
||||||
|
GOPROXY: 'https://goproxy.io,direct',
|
||||||
},
|
},
|
||||||
commands: [
|
commands: [
|
||||||
'DIST_DIR=$(mktemp -d)',
|
'DIST_DIR=$(mktemp -d)',
|
||||||
'go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags --always)" -o $DIST_DIR ./cmd/um',
|
'go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags --always)" -o $DIST_DIR ./cmd/um',
|
||||||
'mkdir -p dist',
|
'mkdir -p dist',
|
||||||
'tar cz -f %s -C $DIST_DIR .' % filepath,
|
] + archive,
|
||||||
],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
local StepUploadArtifact(GOOS, GOARCH) = {
|
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 filepath = 'dist/%s' % filename,
|
||||||
local pkgname = '${DRONE_REPO_NAME}-build',
|
local pkgname = '${DRONE_REPO_NAME}-build',
|
||||||
|
|
||||||
name: 'upload artifact',
|
name: 'upload artifact',
|
||||||
image: 'golang:1.19', // reuse golang:1.19 for curl
|
image: 'golang:1.23', // reuse golang:1.19 for curl
|
||||||
environment: {
|
environment: {
|
||||||
DRONE_GITEA_SERVER: 'https://git.unlock-music.dev',
|
DRONE_GITEA_SERVER: 'https://git.unlock-music.dev',
|
||||||
GITEA_API_KEY: { from_secret: 'GITEA_API_KEY' },
|
GITEA_API_KEY: { from_secret: 'GITEA_API_KEY' },
|
||||||
@ -68,7 +83,10 @@ local PipelineBuild(GOOS, GOARCH, RUN_TEST) = {
|
|||||||
(
|
(
|
||||||
if RUN_TEST then [{
|
if RUN_TEST then [{
|
||||||
name: 'go test',
|
name: 'go test',
|
||||||
image: 'golang:1.19',
|
image: 'golang:1.23',
|
||||||
|
environment: {
|
||||||
|
GOPROXY: 'https://goproxy.io,direct',
|
||||||
|
},
|
||||||
commands: ['go test -v ./...'],
|
commands: ['go test -v ./...'],
|
||||||
}] else []
|
}] else []
|
||||||
)
|
)
|
||||||
@ -94,16 +112,27 @@ local PipelineRelease() = {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'go test',
|
name: 'go test',
|
||||||
image: 'golang:1.19',
|
image: 'golang:1.23',
|
||||||
|
environment: {
|
||||||
|
GOPROXY: 'https://goproxy.io,direct',
|
||||||
|
},
|
||||||
commands: ['go test -v ./...'],
|
commands: ['go test -v ./...'],
|
||||||
},
|
},
|
||||||
StepGoBuild('linux', 'amd64'),
|
StepGoBuild('linux', 'amd64'),
|
||||||
StepGoBuild('linux', 'arm64'),
|
StepGoBuild('linux', 'arm64'),
|
||||||
StepGoBuild('linux', '386'),
|
StepGoBuild('linux', '386'),
|
||||||
StepGoBuild('windows', 'amd64'),
|
StepGoBuild('windows', 'amd64'),
|
||||||
|
StepGoBuild('windows', 'arm64'),
|
||||||
StepGoBuild('windows', '386'),
|
StepGoBuild('windows', '386'),
|
||||||
StepGoBuild('darwin', 'amd64'),
|
StepGoBuild('darwin', 'amd64'),
|
||||||
StepGoBuild('darwin', 'arm64'),
|
StepGoBuild('darwin', 'arm64'),
|
||||||
|
{
|
||||||
|
name: 'prepare root',
|
||||||
|
image: 'golang:1.23',
|
||||||
|
commands: [
|
||||||
|
'mv dist/*.tar.gz dist/*.zip ./',
|
||||||
|
],
|
||||||
|
},
|
||||||
CreateRelease(),
|
CreateRelease(),
|
||||||
],
|
],
|
||||||
trigger: {
|
trigger: {
|
||||||
|
115
.drone.yml
115
.drone.yml
@ -8,29 +8,34 @@ steps:
|
|||||||
name: fetch tags
|
name: fetch tags
|
||||||
- commands:
|
- commands:
|
||||||
- go test -v ./...
|
- go test -v ./...
|
||||||
image: golang:1.19
|
environment:
|
||||||
|
GOPROXY: https://goproxy.io,direct
|
||||||
|
image: golang:1.23
|
||||||
name: go test
|
name: go test
|
||||||
- commands:
|
- commands:
|
||||||
- DIST_DIR=$(mktemp -d)
|
- DIST_DIR=$(mktemp -d)
|
||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- 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:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: linux
|
GOOS: linux
|
||||||
image: golang:1.19
|
GOPROXY: https://goproxy.io,direct
|
||||||
|
image: golang:1.23
|
||||||
name: go build linux/amd64
|
name: go build linux/amd64
|
||||||
- commands:
|
- commands:
|
||||||
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-linux-amd64.tar.gz"
|
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-linux-amd64-$(git
|
||||||
"$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-linux-amd64.tar.gz"
|
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
|
||||||
- sha256sum dist/um-linux-amd64.tar.gz
|
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}
|
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
|
||||||
environment:
|
environment:
|
||||||
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
||||||
GITEA_API_KEY:
|
GITEA_API_KEY:
|
||||||
from_secret: GITEA_API_KEY
|
from_secret: GITEA_API_KEY
|
||||||
image: golang:1.19
|
image: golang:1.23
|
||||||
name: upload artifact
|
name: upload artifact
|
||||||
trigger:
|
trigger:
|
||||||
event:
|
event:
|
||||||
@ -50,22 +55,25 @@ steps:
|
|||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- 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:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: windows
|
GOOS: windows
|
||||||
image: golang:1.19
|
GOPROXY: https://goproxy.io,direct
|
||||||
|
image: golang:1.23
|
||||||
name: go build windows/amd64
|
name: go build windows/amd64
|
||||||
- commands:
|
- commands:
|
||||||
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-windows-amd64.tar.gz"
|
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-windows-amd64-$(git
|
||||||
"$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-windows-amd64.tar.gz"
|
describe --tags --always).zip" "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-windows-amd64-$(git
|
||||||
- sha256sum dist/um-windows-amd64.tar.gz
|
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}
|
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
|
||||||
environment:
|
environment:
|
||||||
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
||||||
GITEA_API_KEY:
|
GITEA_API_KEY:
|
||||||
from_secret: GITEA_API_KEY
|
from_secret: GITEA_API_KEY
|
||||||
image: golang:1.19
|
image: golang:1.23
|
||||||
name: upload artifact
|
name: upload artifact
|
||||||
trigger:
|
trigger:
|
||||||
event:
|
event:
|
||||||
@ -85,22 +93,25 @@ steps:
|
|||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- 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:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: darwin
|
GOOS: darwin
|
||||||
image: golang:1.19
|
GOPROXY: https://goproxy.io,direct
|
||||||
|
image: golang:1.23
|
||||||
name: go build darwin/amd64
|
name: go build darwin/amd64
|
||||||
- commands:
|
- commands:
|
||||||
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-darwin-amd64.tar.gz"
|
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-darwin-amd64-$(git
|
||||||
"$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-darwin-amd64.tar.gz"
|
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
|
||||||
- sha256sum dist/um-darwin-amd64.tar.gz
|
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}
|
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
|
||||||
environment:
|
environment:
|
||||||
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
DRONE_GITEA_SERVER: https://git.unlock-music.dev
|
||||||
GITEA_API_KEY:
|
GITEA_API_KEY:
|
||||||
from_secret: GITEA_API_KEY
|
from_secret: GITEA_API_KEY
|
||||||
image: golang:1.19
|
image: golang:1.23
|
||||||
name: upload artifact
|
name: upload artifact
|
||||||
trigger:
|
trigger:
|
||||||
event:
|
event:
|
||||||
@ -117,85 +128,117 @@ steps:
|
|||||||
name: fetch tags
|
name: fetch tags
|
||||||
- commands:
|
- commands:
|
||||||
- go test -v ./...
|
- go test -v ./...
|
||||||
image: golang:1.19
|
environment:
|
||||||
|
GOPROXY: https://goproxy.io,direct
|
||||||
|
image: golang:1.23
|
||||||
name: go test
|
name: go test
|
||||||
- commands:
|
- commands:
|
||||||
- DIST_DIR=$(mktemp -d)
|
- DIST_DIR=$(mktemp -d)
|
||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- 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:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: linux
|
GOOS: linux
|
||||||
image: golang:1.19
|
GOPROXY: https://goproxy.io,direct
|
||||||
|
image: golang:1.23
|
||||||
name: go build linux/amd64
|
name: go build linux/amd64
|
||||||
- commands:
|
- commands:
|
||||||
- DIST_DIR=$(mktemp -d)
|
- DIST_DIR=$(mktemp -d)
|
||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- 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:
|
environment:
|
||||||
GOARCH: arm64
|
GOARCH: arm64
|
||||||
GOOS: linux
|
GOOS: linux
|
||||||
image: golang:1.19
|
GOPROXY: https://goproxy.io,direct
|
||||||
|
image: golang:1.23
|
||||||
name: go build linux/arm64
|
name: go build linux/arm64
|
||||||
- commands:
|
- commands:
|
||||||
- DIST_DIR=$(mktemp -d)
|
- DIST_DIR=$(mktemp -d)
|
||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- 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:
|
environment:
|
||||||
GOARCH: "386"
|
GOARCH: "386"
|
||||||
GOOS: linux
|
GOOS: linux
|
||||||
image: golang:1.19
|
GOPROXY: https://goproxy.io,direct
|
||||||
|
image: golang:1.23
|
||||||
name: go build linux/386
|
name: go build linux/386
|
||||||
- commands:
|
- commands:
|
||||||
- DIST_DIR=$(mktemp -d)
|
- DIST_DIR=$(mktemp -d)
|
||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- 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:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: windows
|
GOOS: windows
|
||||||
image: golang:1.19
|
GOPROXY: https://goproxy.io,direct
|
||||||
|
image: golang:1.23
|
||||||
name: go build windows/amd64
|
name: go build windows/amd64
|
||||||
- commands:
|
- commands:
|
||||||
- DIST_DIR=$(mktemp -d)
|
- DIST_DIR=$(mktemp -d)
|
||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- 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.23
|
||||||
|
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:
|
environment:
|
||||||
GOARCH: "386"
|
GOARCH: "386"
|
||||||
GOOS: windows
|
GOOS: windows
|
||||||
image: golang:1.19
|
GOPROXY: https://goproxy.io,direct
|
||||||
|
image: golang:1.23
|
||||||
name: go build windows/386
|
name: go build windows/386
|
||||||
- commands:
|
- commands:
|
||||||
- DIST_DIR=$(mktemp -d)
|
- DIST_DIR=$(mktemp -d)
|
||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- 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:
|
environment:
|
||||||
GOARCH: amd64
|
GOARCH: amd64
|
||||||
GOOS: darwin
|
GOOS: darwin
|
||||||
image: golang:1.19
|
GOPROXY: https://goproxy.io,direct
|
||||||
|
image: golang:1.23
|
||||||
name: go build darwin/amd64
|
name: go build darwin/amd64
|
||||||
- commands:
|
- commands:
|
||||||
- DIST_DIR=$(mktemp -d)
|
- DIST_DIR=$(mktemp -d)
|
||||||
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
|
||||||
--always)" -o $DIST_DIR ./cmd/um
|
--always)" -o $DIST_DIR ./cmd/um
|
||||||
- mkdir -p dist
|
- 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:
|
environment:
|
||||||
GOARCH: arm64
|
GOARCH: arm64
|
||||||
GOOS: darwin
|
GOOS: darwin
|
||||||
image: golang:1.19
|
GOPROXY: https://goproxy.io,direct
|
||||||
|
image: golang:1.23
|
||||||
name: go build darwin/arm64
|
name: go build darwin/arm64
|
||||||
|
- commands:
|
||||||
|
- mv dist/*.tar.gz dist/*.zip ./
|
||||||
|
image: golang:1.23
|
||||||
|
name: prepare root
|
||||||
- image: plugins/gitea-release
|
- image: plugins/gitea-release
|
||||||
name: create release
|
name: create release
|
||||||
settings:
|
settings:
|
||||||
@ -204,7 +247,9 @@ steps:
|
|||||||
base_url: https://git.unlock-music.dev
|
base_url: https://git.unlock-music.dev
|
||||||
checksum: sha256
|
checksum: sha256
|
||||||
draft: true
|
draft: true
|
||||||
files: dist/*
|
files:
|
||||||
|
- um-*.tar.gz
|
||||||
|
- um-*.zip
|
||||||
title: ${DRONE_TAG}
|
title: ${DRONE_TAG}
|
||||||
trigger:
|
trigger:
|
||||||
event:
|
event:
|
||||||
|
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,3 +1,7 @@
|
|||||||
.idea
|
.idea
|
||||||
|
|
||||||
/dist
|
/dist
|
||||||
|
*.exe
|
||||||
|
|
||||||
|
/um-*.tar.gz
|
||||||
|
/um-*.zip
|
||||||
|
43
README.md
43
README.md
@ -1,25 +1,48 @@
|
|||||||
# Unlock Music Project - CLI Edition
|
# Unlock Music Project - CLI Edition
|
||||||
|
|
||||||
Original: Web Edition https://github.com/ix64/unlock-music
|
Original: Web Edition https://git.unlock-music.dev/um/web
|
||||||
|
|
||||||
- [Release Download](https://github.com/unlock-music/cli/releases/latest)
|
- [![Build Status](https://ci.unlock-music.dev/api/badges/um/cli/status.svg)](https://ci.unlock-music.dev/um/cli)
|
||||||
|
- [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
|
## Features
|
||||||
|
|
||||||
- [x] All Algorithm Supported By `ix64/unlock-music`
|
- [x] All Algorithm Supported By `unlock-music/web`
|
||||||
- [ ] Complete Cover Image
|
- [x] Complete Metadata & Cover Image
|
||||||
- [ ] Parse Meta Data
|
|
||||||
- [ ] Complete Meta Data
|
|
||||||
|
|
||||||
## Hou to Build
|
## Release
|
||||||
|
|
||||||
- Requirements: **Golang 1.17**
|
[Latest release](https://git.unlock-music.dev/um/cli/releases/latest).
|
||||||
|
|
||||||
1. Clone this repo `git clone https://github.com/unlock-music/cli && cd cli`
|
## Install from source
|
||||||
2. Build the executable `go build ./cmd/um`
|
|
||||||
|
- Requirements: **Golang 1.23.3**
|
||||||
|
|
||||||
|
1. run `go install unlock-music.dev/cli/cmd/um@master`
|
||||||
|
|
||||||
|
### Build from repo source
|
||||||
|
|
||||||
|
1. Pull repo source.
|
||||||
|
2. Build with `go build ./cmd/um`.
|
||||||
|
|
||||||
|
It will produce `um` or `um.exe` (Windows).
|
||||||
|
|
||||||
## How to use
|
## How to use
|
||||||
|
|
||||||
- Drag the encrypted file to `um.exe` (Tested on Windows)
|
- Drag the encrypted file to `um.exe` (Tested on Windows)
|
||||||
- Run: `./um [-o <output dir>] [-i] <input dir/file>`
|
- Run: `./um [-o <output dir>] [-i] <input dir/file>`
|
||||||
- Use `./um -h` to show help menu
|
- 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
|
||||||
|
```
|
||||||
|
@ -4,28 +4,46 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NewDecoderFunc func(rd io.ReadSeeker) Decoder
|
type DecoderParams struct {
|
||||||
|
Reader io.ReadSeeker // required
|
||||||
|
Extension string // required, source extension, eg. ".mp3"
|
||||||
|
|
||||||
type decoderItem struct {
|
FilePath string // optional, source file path
|
||||||
noop bool
|
|
||||||
decoder NewDecoderFunc
|
Logger *zap.Logger // required
|
||||||
|
}
|
||||||
|
type NewDecoderFunc func(p *DecoderParams) Decoder
|
||||||
|
|
||||||
|
type DecoderFactory struct {
|
||||||
|
noop bool
|
||||||
|
Suffix string
|
||||||
|
Create NewDecoderFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
var DecoderRegistry = make(map[string][]decoderItem)
|
var DecoderRegistry []DecoderFactory
|
||||||
|
|
||||||
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
|
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
|
||||||
DecoderRegistry[ext] = append(DecoderRegistry[ext],
|
DecoderRegistry = append(DecoderRegistry,
|
||||||
decoderItem{noop: noop, decoder: dispatchFunc})
|
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), "."))
|
func GetDecoder(filename string, skipNoop bool) []DecoderFactory {
|
||||||
for _, dec := range DecoderRegistry[ext] {
|
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 {
|
if skipNoop && dec.noop {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
rs = append(rs, dec.decoder)
|
result = append(result, dec)
|
||||||
}
|
}
|
||||||
return
|
return result
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,10 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type StreamDecoder interface {
|
||||||
|
Decrypt(buf []byte, offset int)
|
||||||
|
}
|
||||||
|
|
||||||
type Decoder interface {
|
type Decoder interface {
|
||||||
Validate() error
|
Validate() error
|
||||||
io.Reader
|
io.Reader
|
||||||
@ -14,12 +18,12 @@ type CoverImageGetter interface {
|
|||||||
GetCoverImage(ctx context.Context) ([]byte, error)
|
GetCoverImage(ctx context.Context) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Meta interface {
|
type AudioMeta interface {
|
||||||
GetArtists() []string
|
GetArtists() []string
|
||||||
GetTitle() string
|
GetTitle() string
|
||||||
GetAlbum() string
|
GetAlbum() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type StreamDecoder interface {
|
type AudioMetaGetter interface {
|
||||||
Decrypt(buf []byte, offset int)
|
GetAudioMeta(ctx context.Context) (AudioMeta, error)
|
||||||
}
|
}
|
50
algo/common/meta.go
Normal file
50
algo/common/meta.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type filenameMeta struct {
|
||||||
|
artists []string
|
||||||
|
title string
|
||||||
|
album string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *filenameMeta) GetArtists() []string {
|
||||||
|
return f.artists
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *filenameMeta) GetTitle() string {
|
||||||
|
return f.title
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *filenameMeta) GetAlbum() string {
|
||||||
|
return f.album
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseFilenameMeta(filename string) (meta AudioMeta) {
|
||||||
|
partName := strings.TrimSuffix(filename, path.Ext(filename))
|
||||||
|
items := strings.Split(partName, "-")
|
||||||
|
ret := &filenameMeta{}
|
||||||
|
|
||||||
|
switch len(items) {
|
||||||
|
case 0:
|
||||||
|
// no-op
|
||||||
|
case 1:
|
||||||
|
ret.title = strings.TrimSpace(items[0])
|
||||||
|
default:
|
||||||
|
ret.title = strings.TrimSpace(items[len(items)-1])
|
||||||
|
|
||||||
|
for _, v := range items[:len(items)-1] {
|
||||||
|
artists := strings.FieldsFunc(v, func(r rune) bool {
|
||||||
|
return r == ',' || r == '_'
|
||||||
|
})
|
||||||
|
for _, artist := range artists {
|
||||||
|
ret.artists = append(ret.artists, strings.TrimSpace(artist))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
38
algo/common/meta_test.go
Normal file
38
algo/common/meta_test.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseFilenameMeta(t *testing.T) {
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
wantMeta AudioMeta
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "test1",
|
||||||
|
wantMeta: &filenameMeta{title: "test1"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "周杰伦 - 晴天.flac",
|
||||||
|
wantMeta: &filenameMeta{artists: []string{"周杰伦"}, title: "晴天"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Alan Walker _ Iselin Solheim - Sing Me to Sleep.flac",
|
||||||
|
wantMeta: &filenameMeta{artists: []string{"Alan Walker", "Iselin Solheim"}, title: "Sing Me to Sleep"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Christopher,Madcon - Limousine.flac",
|
||||||
|
wantMeta: &filenameMeta{artists: []string{"Christopher", "Madcon"}, title: "Limousine"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if gotMeta := ParseFilenameMeta(tt.name); !reflect.DeepEqual(gotMeta, tt.wantMeta) {
|
||||||
|
t.Errorf("ParseFilenameMeta() = %v, want %v", gotMeta, tt.wantMeta)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -14,8 +14,8 @@ type RawDecoder struct {
|
|||||||
audioExt string
|
audioExt string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRawDecoder(rd io.ReadSeeker) Decoder {
|
func NewRawDecoder(p *DecoderParams) Decoder {
|
||||||
return &RawDecoder{rd: rd}
|
return &RawDecoder{rd: p.Reader}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *RawDecoder) Validate() error {
|
func (d *RawDecoder) Validate() error {
|
||||||
|
@ -16,8 +16,8 @@ type Decoder struct {
|
|||||||
header header
|
header header
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
return &Decoder{rd: rd}
|
return &Decoder{rd: p.Reader}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate checks if the file is a valid Kugou (.kgm, .vpr, .kgma) file.
|
// Validate checks if the file is a valid Kugou (.kgm, .vpr, .kgma) file.
|
||||||
@ -61,4 +61,7 @@ func init() {
|
|||||||
common.RegisterDecoder("kgma", false, NewDecoder)
|
common.RegisterDecoder("kgma", false, NewDecoder)
|
||||||
// Viper
|
// Viper
|
||||||
common.RegisterDecoder("vpr", false, NewDecoder)
|
common.RegisterDecoder("vpr", false, NewDecoder)
|
||||||
|
// Kugou Android
|
||||||
|
common.RegisterDecoder("kgm.flac", false, NewDecoder)
|
||||||
|
common.RegisterDecoder("vpr.flac", false, NewDecoder)
|
||||||
}
|
}
|
||||||
|
@ -30,8 +30,8 @@ func (d *Decoder) GetAudioExt() string {
|
|||||||
return "." + d.outputExt
|
return "." + d.outputExt
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
return &Decoder{rd: rd}
|
return &Decoder{rd: p.Reader}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate checks if the file is a valid Kuwo .kw file.
|
// Validate checks if the file is a valid Kuwo .kw file.
|
||||||
|
@ -1,64 +1,85 @@
|
|||||||
package ncm
|
package ncm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"go.uber.org/zap"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"unlock-music.dev/cli/algo/common"
|
"unlock-music.dev/cli/algo/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RawMeta interface {
|
type ncmMeta interface {
|
||||||
common.Meta
|
common.AudioMeta
|
||||||
|
|
||||||
|
// GetFormat return the audio format, e.g. mp3, flac
|
||||||
GetFormat() string
|
GetFormat() string
|
||||||
|
|
||||||
|
// GetAlbumImageURL return the album image url
|
||||||
GetAlbumImageURL() string
|
GetAlbumImageURL() string
|
||||||
}
|
}
|
||||||
type RawMetaMusic struct {
|
|
||||||
Format string `json:"format"`
|
type ncmMetaMusic struct {
|
||||||
MusicID int `json:"musicId"`
|
logger *zap.Logger
|
||||||
MusicName string `json:"musicName"`
|
|
||||||
Artist [][]interface{} `json:"artist"`
|
Format string `json:"format"`
|
||||||
Album string `json:"album"`
|
MusicName string `json:"musicName"`
|
||||||
AlbumID int `json:"albumId"`
|
Artist interface{} `json:"artist"`
|
||||||
AlbumPicDocID interface{} `json:"albumPicDocId"`
|
Album string `json:"album"`
|
||||||
AlbumPic string `json:"albumPic"`
|
AlbumPicDocID interface{} `json:"albumPicDocId"`
|
||||||
MvID int `json:"mvId"`
|
AlbumPic string `json:"albumPic"`
|
||||||
Flag int `json:"flag"`
|
Flag int `json:"flag"`
|
||||||
Bitrate int `json:"bitrate"`
|
Bitrate int `json:"bitrate"`
|
||||||
Duration int `json:"duration"`
|
Duration int `json:"duration"`
|
||||||
Alias []interface{} `json:"alias"`
|
Alias []interface{} `json:"alias"`
|
||||||
TransNames []interface{} `json:"transNames"`
|
TransNames []interface{} `json:"transNames"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaMusic) GetAlbumImageURL() string {
|
func newNcmMetaMusic(logger *zap.Logger) *ncmMetaMusic {
|
||||||
|
ncm := new(ncmMetaMusic)
|
||||||
|
ncm.logger = logger.With(zap.String("module", "ncmMetaMusic"))
|
||||||
|
return ncm
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ncmMetaMusic) GetAlbumImageURL() string {
|
||||||
return m.AlbumPic
|
return m.AlbumPic
|
||||||
}
|
}
|
||||||
func (m RawMetaMusic) GetArtists() (artists []string) {
|
|
||||||
for _, artist := range m.Artist {
|
func (m *ncmMetaMusic) GetArtists() []string {
|
||||||
for _, item := range artist {
|
m.logger.Debug("ncm artists", zap.Any("artists", m.Artist))
|
||||||
name, ok := item.(string)
|
|
||||||
if ok {
|
var artists []string = nil
|
||||||
|
if jsonArtists, ok := m.Artist.([][]string); ok {
|
||||||
|
for _, artist := range jsonArtists {
|
||||||
|
for _, name := range artist {
|
||||||
artists = append(artists, name)
|
artists = append(artists, name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if artist, ok := m.Artist.(string); ok {
|
||||||
|
// #78: artist is a string type.
|
||||||
|
// https://git.unlock-music.dev/um/cli/issues/78
|
||||||
|
artists = []string{artist}
|
||||||
|
} else {
|
||||||
|
m.logger.Warn("unexpected artist type", zap.Any("artists", m.Artist))
|
||||||
}
|
}
|
||||||
return
|
return artists
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaMusic) GetTitle() string {
|
func (m *ncmMetaMusic) GetTitle() string {
|
||||||
return m.MusicName
|
return m.MusicName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaMusic) GetAlbum() string {
|
func (m *ncmMetaMusic) GetAlbum() string {
|
||||||
return m.Album
|
return m.Album
|
||||||
}
|
}
|
||||||
func (m RawMetaMusic) GetFormat() string {
|
|
||||||
|
func (m *ncmMetaMusic) GetFormat() string {
|
||||||
return m.Format
|
return m.Format
|
||||||
}
|
}
|
||||||
|
|
||||||
//goland:noinspection SpellCheckingInspection
|
//goland:noinspection SpellCheckingInspection
|
||||||
type RawMetaDJ struct {
|
type ncmMetaDJ struct {
|
||||||
ProgramID int `json:"programId"`
|
ProgramID int `json:"programId"`
|
||||||
ProgramName string `json:"programName"`
|
ProgramName string `json:"programName"`
|
||||||
MainMusic RawMetaMusic `json:"mainMusic"`
|
MainMusic ncmMetaMusic `json:"mainMusic"`
|
||||||
DjID int `json:"djId"`
|
DjID int `json:"djId"`
|
||||||
DjName string `json:"djName"`
|
DjName string `json:"djName"`
|
||||||
DjAvatarURL string `json:"djAvatarUrl"`
|
DjAvatarURL string `json:"djAvatarUrl"`
|
||||||
@ -80,32 +101,32 @@ type RawMetaDJ struct {
|
|||||||
RadioPurchaseCount int `json:"radioPurchaseCount"`
|
RadioPurchaseCount int `json:"radioPurchaseCount"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaDJ) GetArtists() []string {
|
func (m *ncmMetaDJ) GetArtists() []string {
|
||||||
if m.DjName != "" {
|
if m.DjName != "" {
|
||||||
return []string{m.DjName}
|
return []string{m.DjName}
|
||||||
}
|
}
|
||||||
return m.MainMusic.GetArtists()
|
return m.MainMusic.GetArtists()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaDJ) GetTitle() string {
|
func (m *ncmMetaDJ) GetTitle() string {
|
||||||
if m.ProgramName != "" {
|
if m.ProgramName != "" {
|
||||||
return m.ProgramName
|
return m.ProgramName
|
||||||
}
|
}
|
||||||
return m.MainMusic.GetTitle()
|
return m.MainMusic.GetTitle()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaDJ) GetAlbum() string {
|
func (m *ncmMetaDJ) GetAlbum() string {
|
||||||
if m.Brand != "" {
|
if m.Brand != "" {
|
||||||
return m.Brand
|
return m.Brand
|
||||||
}
|
}
|
||||||
return m.MainMusic.GetAlbum()
|
return m.MainMusic.GetAlbum()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaDJ) GetFormat() string {
|
func (m *ncmMetaDJ) GetFormat() string {
|
||||||
return m.MainMusic.GetFormat()
|
return m.MainMusic.GetFormat()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m RawMetaDJ) GetAlbumImageURL() string {
|
func (m *ncmMetaDJ) GetAlbumImageURL() string {
|
||||||
if strings.HasPrefix(m.MainMusic.GetAlbumImageURL(), "http") {
|
if strings.HasPrefix(m.MainMusic.GetAlbumImageURL(), "http") {
|
||||||
return m.MainMusic.GetAlbumImageURL()
|
return m.MainMusic.GetAlbumImageURL()
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"go.uber.org/zap"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@ -29,21 +30,20 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
return &Decoder{
|
return &Decoder{rd: p.Reader, logger: p.Logger.With(zap.String("module", "ncm"))}
|
||||||
rd: rd,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
rd io.ReadSeeker // rd is the original file reader
|
logger *zap.Logger
|
||||||
|
rd io.ReadSeeker // rd is the original file reader
|
||||||
|
|
||||||
offset int
|
offset int
|
||||||
cipher common.StreamDecoder
|
cipher common.StreamDecoder
|
||||||
|
|
||||||
metaRaw []byte
|
metaRaw []byte
|
||||||
metaType string
|
metaType string
|
||||||
meta RawMeta
|
meta ncmMeta
|
||||||
cover []byte
|
cover []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ func (d *Decoder) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := d.parseMeta(); err != nil {
|
if err := d.parseMeta(); err != nil {
|
||||||
return fmt.Errorf("parse meta failed: %w", err)
|
return fmt.Errorf("parse meta failed: %w (raw json=%s)", err, string(d.metaRaw))
|
||||||
}
|
}
|
||||||
|
|
||||||
d.cipher = newNcmCipher(keyData)
|
d.cipher = newNcmCipher(keyData)
|
||||||
@ -151,12 +151,18 @@ func (d *Decoder) readMetaData() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) readCoverData() error {
|
func (d *Decoder) readCoverData() error {
|
||||||
bCoverCRC := make([]byte, 4)
|
bCoverFrameLen := make([]byte, 4)
|
||||||
if _, err := io.ReadFull(d.rd, bCoverCRC); err != nil {
|
if _, err := io.ReadFull(d.rd, bCoverFrameLen); err != nil {
|
||||||
return fmt.Errorf("ncm read cover crc: %w", err)
|
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 {
|
if _, err := io.ReadFull(d.rd, bCoverLen); err != nil {
|
||||||
return fmt.Errorf("ncm read cover length: %w", err)
|
return fmt.Errorf("ncm read cover length: %w", err)
|
||||||
}
|
}
|
||||||
@ -168,16 +174,19 @@ func (d *Decoder) readCoverData() error {
|
|||||||
}
|
}
|
||||||
d.cover = coverBuf
|
d.cover = coverBuf
|
||||||
|
|
||||||
return nil
|
offsetAudioData := coverFrameStartOffset + int64(coverFrameLen) + 4
|
||||||
|
_, err = d.rd.Seek(offsetAudioData, io.SeekStart)
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) parseMeta() error {
|
func (d *Decoder) parseMeta() error {
|
||||||
switch d.metaType {
|
switch d.metaType {
|
||||||
case "music":
|
case "music":
|
||||||
d.meta = new(RawMetaMusic)
|
d.meta = newNcmMetaMusic(d.logger)
|
||||||
return json.Unmarshal(d.metaRaw, d.meta)
|
return json.Unmarshal(d.metaRaw, d.meta)
|
||||||
case "dj":
|
case "dj":
|
||||||
d.meta = new(RawMetaDJ)
|
d.meta = new(ncmMetaDJ)
|
||||||
return json.Unmarshal(d.metaRaw, d.meta)
|
return json.Unmarshal(d.metaRaw, d.meta)
|
||||||
default:
|
default:
|
||||||
return errors.New("unknown ncm meta type: " + d.metaType)
|
return errors.New("unknown ncm meta type: " + d.metaType)
|
||||||
@ -234,8 +243,8 @@ func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) {
|
|||||||
return d.cover, nil
|
return d.cover, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) GetMeta() common.Meta {
|
func (d *Decoder) GetAudioMeta(_ context.Context) (common.AudioMeta, error) {
|
||||||
return d.meta
|
return d.meta, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
146
algo/qmc/client/base.go
Normal file
146
algo/qmc/client/base.go
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type QQMusic struct {
|
||||||
|
http *http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) rpcDoRequest(ctx context.Context, reqBody any) ([]byte, error) {
|
||||||
|
reqBodyBuf, err := json.Marshal(reqBody)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcDoRequest] marshal request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
const endpointURL = "https://u.y.qq.com/cgi-bin/musicu.fcg"
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost,
|
||||||
|
endpointURL+fmt.Sprintf("?pcachetime=%d", time.Now().Unix()),
|
||||||
|
bytes.NewReader(reqBodyBuf),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcDoRequest] create request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Accept", "*/*")
|
||||||
|
req.Header.Set("Accept-Language", "zh-CN")
|
||||||
|
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)")
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
// req.Header.Set("Accept-Encoding", "gzip, deflate")
|
||||||
|
|
||||||
|
reqp, err := c.http.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcDoRequest] send request: %w", err)
|
||||||
|
}
|
||||||
|
defer reqp.Body.Close()
|
||||||
|
|
||||||
|
respBodyBuf, err := io.ReadAll(reqp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcDoRequest] read response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return respBodyBuf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type rpcRequest struct {
|
||||||
|
Method string `json:"method"`
|
||||||
|
Module string `json:"module"`
|
||||||
|
Param any `json:"param"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type rpcResponse struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Ts int64 `json:"ts"`
|
||||||
|
StartTs int64 `json:"start_ts"`
|
||||||
|
TraceID string `json:"traceid"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type rpcSubResponse struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Data json.RawMessage `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) rpcCall(ctx context.Context,
|
||||||
|
protocol string, method string, module string,
|
||||||
|
param any,
|
||||||
|
) (json.RawMessage, error) {
|
||||||
|
reqBody := map[string]any{protocol: rpcRequest{
|
||||||
|
Method: method,
|
||||||
|
Module: module,
|
||||||
|
Param: param,
|
||||||
|
}}
|
||||||
|
|
||||||
|
respBodyBuf, err := c.rpcDoRequest(ctx, reqBody)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] do request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// check rpc response status
|
||||||
|
respStatus := rpcResponse{}
|
||||||
|
if err := json.Unmarshal(respBodyBuf, &respStatus); err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] unmarshal response: %w", err)
|
||||||
|
}
|
||||||
|
if respStatus.Code != 0 {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] rpc error: %d", respStatus.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse response data
|
||||||
|
var respBody map[string]json.RawMessage
|
||||||
|
if err := json.Unmarshal(respBodyBuf, &respBody); err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] unmarshal response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
subRespBuf, ok := respBody[protocol]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] sub-response not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
subResp := rpcSubResponse{}
|
||||||
|
if err := json.Unmarshal(subRespBuf, &subResp); err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] unmarshal sub-response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if subResp.Code != 0 {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[rpcCall] sub-response error: %d", subResp.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
return subResp.Data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) downloadFile(ctx context.Context, url string) ([]byte, error) {
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qmc[downloadFile] init request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//req.Header.Set("Accept", "image/webp,image/*,*/*;q=0.8") // jpeg is preferred to embed in audio
|
||||||
|
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.5;q=0.4")
|
||||||
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.47.134 Safari/537.36 QBCore/3.53.47.400 QQBrowser/9.0.2524.400")
|
||||||
|
|
||||||
|
resp, err := c.http.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qmc[downloadFile] send request: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("qmc[downloadFile] unexpected http status %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
return io.ReadAll(resp.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewQQMusicClient() *QQMusic {
|
||||||
|
return &QQMusic{
|
||||||
|
http: &http.Client{
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
21
algo/qmc/client/cover.go
Normal file
21
algo/qmc/client/cover.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *QQMusic) AlbumCoverByID(ctx context.Context, albumID int) ([]byte, error) {
|
||||||
|
u := fmt.Sprintf("https://imgcache.qq.com/music/photo/album/%s/albumpic_%s_0.jpg",
|
||||||
|
strconv.Itoa(albumID%100),
|
||||||
|
strconv.Itoa(albumID),
|
||||||
|
)
|
||||||
|
return c.downloadFile(ctx, u)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) AlbumCoverByMediaID(ctx context.Context, mediaID string) ([]byte, error) {
|
||||||
|
// original: https://y.gtimg.cn/music/photo_new/T002M000%s.jpg
|
||||||
|
u := fmt.Sprintf("https://y.gtimg.cn/music/photo_new/T002R500x500M000%s.jpg", mediaID)
|
||||||
|
return c.downloadFile(ctx, u)
|
||||||
|
}
|
52
algo/qmc/client/search.go
Normal file
52
algo/qmc/client/search.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type searchParams struct {
|
||||||
|
Grp int `json:"grp"`
|
||||||
|
NumPerPage int `json:"num_per_page"`
|
||||||
|
PageNum int `json:"page_num"`
|
||||||
|
Query string `json:"query"`
|
||||||
|
RemotePlace string `json:"remoteplace"`
|
||||||
|
SearchType int `json:"search_type"`
|
||||||
|
//SearchID string `json:"searchid"` // todo: it seems generated randomly
|
||||||
|
}
|
||||||
|
|
||||||
|
type searchResponse struct {
|
||||||
|
Body struct {
|
||||||
|
Song struct {
|
||||||
|
List []*TrackInfo `json:"list"`
|
||||||
|
} `json:"song"`
|
||||||
|
} `json:"body"`
|
||||||
|
Code int `json:"code"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) Search(ctx context.Context, keyword string) ([]*TrackInfo, error) {
|
||||||
|
|
||||||
|
resp, err := c.rpcCall(ctx,
|
||||||
|
"music.search.SearchCgiService",
|
||||||
|
"DoSearchForQQMusicDesktop",
|
||||||
|
"music.search.SearchCgiService",
|
||||||
|
&searchParams{
|
||||||
|
SearchType: 0, Query: keyword,
|
||||||
|
PageNum: 1, NumPerPage: 40,
|
||||||
|
|
||||||
|
// static values
|
||||||
|
Grp: 1, RemotePlace: "sizer.newclient.song",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[Search] rpc call: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
respData := searchResponse{}
|
||||||
|
if err := json.Unmarshal(resp, &respData); err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[Search] unmarshal response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return respData.Body.Song.List, nil
|
||||||
|
|
||||||
|
}
|
178
algo/qmc/client/track.go
Normal file
178
algo/qmc/client/track.go
Normal file
@ -0,0 +1,178 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/samber/lo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type getTrackInfoParams struct {
|
||||||
|
Ctx int `json:"ctx"`
|
||||||
|
Ids []int `json:"ids"`
|
||||||
|
Types []int `json:"types"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type getTrackInfoResponse struct {
|
||||||
|
Tracks []*TrackInfo `json:"tracks"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) GetTracksInfo(ctx context.Context, songIDs []int) ([]*TrackInfo, error) {
|
||||||
|
resp, err := c.rpcCall(ctx,
|
||||||
|
"Protocol_UpdateSongInfo",
|
||||||
|
"CgiGetTrackInfo",
|
||||||
|
"music.trackInfo.UniformRuleCtrl",
|
||||||
|
&getTrackInfoParams{Ctx: 0, Ids: songIDs, Types: []int{0}},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[GetTrackInfo] rpc call: %w", err)
|
||||||
|
}
|
||||||
|
respData := getTrackInfoResponse{}
|
||||||
|
if err := json.Unmarshal(resp, &respData); err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[GetTrackInfo] unmarshal response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return respData.Tracks, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *QQMusic) GetTrackInfo(ctx context.Context, songID int) (*TrackInfo, error) {
|
||||||
|
tracks, err := c.GetTracksInfo(ctx, []int{songID})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[GetTrackInfo] get tracks info: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(tracks) == 0 {
|
||||||
|
return nil, fmt.Errorf("qqMusicClient[GetTrackInfo] track not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return tracks[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type TrackSinger struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Mid string `json:"mid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Type int `json:"type"`
|
||||||
|
Uin int `json:"uin"`
|
||||||
|
Pmid string `json:"pmid"`
|
||||||
|
}
|
||||||
|
type TrackAlbum struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Mid string `json:"mid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Subtitle string `json:"subtitle"`
|
||||||
|
Pmid string `json:"pmid"`
|
||||||
|
}
|
||||||
|
type TrackInfo struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Type int `json:"type"`
|
||||||
|
Mid string `json:"mid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Subtitle string `json:"subtitle"`
|
||||||
|
Singer []TrackSinger `json:"singer"`
|
||||||
|
Album TrackAlbum `json:"album"`
|
||||||
|
Mv struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Vid string `json:"vid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Vt int `json:"vt"`
|
||||||
|
} `json:"mv"`
|
||||||
|
Interval int `json:"interval"`
|
||||||
|
Isonly int `json:"isonly"`
|
||||||
|
Language int `json:"language"`
|
||||||
|
Genre int `json:"genre"`
|
||||||
|
IndexCd int `json:"index_cd"`
|
||||||
|
IndexAlbum int `json:"index_album"`
|
||||||
|
TimePublic string `json:"time_public"`
|
||||||
|
Status int `json:"status"`
|
||||||
|
Fnote int `json:"fnote"`
|
||||||
|
File struct {
|
||||||
|
MediaMid string `json:"media_mid"`
|
||||||
|
Size24Aac int `json:"size_24aac"`
|
||||||
|
Size48Aac int `json:"size_48aac"`
|
||||||
|
Size96Aac int `json:"size_96aac"`
|
||||||
|
Size192Ogg int `json:"size_192ogg"`
|
||||||
|
Size192Aac int `json:"size_192aac"`
|
||||||
|
Size128Mp3 int `json:"size_128mp3"`
|
||||||
|
Size320Mp3 int `json:"size_320mp3"`
|
||||||
|
SizeApe int `json:"size_ape"`
|
||||||
|
SizeFlac int `json:"size_flac"`
|
||||||
|
SizeDts int `json:"size_dts"`
|
||||||
|
SizeTry int `json:"size_try"`
|
||||||
|
TryBegin int `json:"try_begin"`
|
||||||
|
TryEnd int `json:"try_end"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
SizeHires int `json:"size_hires"`
|
||||||
|
HiresSample int `json:"hires_sample"`
|
||||||
|
HiresBitdepth int `json:"hires_bitdepth"`
|
||||||
|
B30S int `json:"b_30s"`
|
||||||
|
E30S int `json:"e_30s"`
|
||||||
|
Size96Ogg int `json:"size_96ogg"`
|
||||||
|
Size360Ra []interface{} `json:"size_360ra"`
|
||||||
|
SizeDolby int `json:"size_dolby"`
|
||||||
|
SizeNew []interface{} `json:"size_new"`
|
||||||
|
} `json:"file"`
|
||||||
|
Pay struct {
|
||||||
|
PayMonth int `json:"pay_month"`
|
||||||
|
PriceTrack int `json:"price_track"`
|
||||||
|
PriceAlbum int `json:"price_album"`
|
||||||
|
PayPlay int `json:"pay_play"`
|
||||||
|
PayDown int `json:"pay_down"`
|
||||||
|
PayStatus int `json:"pay_status"`
|
||||||
|
TimeFree int `json:"time_free"`
|
||||||
|
} `json:"pay"`
|
||||||
|
Action struct {
|
||||||
|
Switch int `json:"switch"`
|
||||||
|
Msgid int `json:"msgid"`
|
||||||
|
Alert int `json:"alert"`
|
||||||
|
Icons int `json:"icons"`
|
||||||
|
Msgshare int `json:"msgshare"`
|
||||||
|
Msgfav int `json:"msgfav"`
|
||||||
|
Msgdown int `json:"msgdown"`
|
||||||
|
Msgpay int `json:"msgpay"`
|
||||||
|
Switch2 int `json:"switch2"`
|
||||||
|
Icon2 int `json:"icon2"`
|
||||||
|
} `json:"action"`
|
||||||
|
Ksong struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Mid string `json:"mid"`
|
||||||
|
} `json:"ksong"`
|
||||||
|
Volume struct {
|
||||||
|
Gain float64 `json:"gain"`
|
||||||
|
Peak float64 `json:"peak"`
|
||||||
|
Lra float64 `json:"lra"`
|
||||||
|
} `json:"volume"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
Ppurl string `json:"ppurl"`
|
||||||
|
Bpm int `json:"bpm"`
|
||||||
|
Version int `json:"version"`
|
||||||
|
Trace string `json:"trace"`
|
||||||
|
DataType int `json:"data_type"`
|
||||||
|
ModifyStamp int `json:"modify_stamp"`
|
||||||
|
Aid int `json:"aid"`
|
||||||
|
Tid int `json:"tid"`
|
||||||
|
Ov int `json:"ov"`
|
||||||
|
Sa int `json:"sa"`
|
||||||
|
Es string `json:"es"`
|
||||||
|
Vs []string `json:"vs"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TrackInfo) GetArtists() []string {
|
||||||
|
return lo.Map(t.Singer, func(v TrackSinger, i int) string {
|
||||||
|
return v.Name
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TrackInfo) GetTitle() string {
|
||||||
|
return t.Title
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TrackInfo) GetAlbum() string {
|
||||||
|
return t.Album.Name
|
||||||
|
}
|
158
algo/qmc/key_mmkv.go
Normal file
158
algo/qmc/key_mmkv.go
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
package qmc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/samber/lo"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
|
"golang.org/x/text/unicode/norm"
|
||||||
|
"unlock-music.dev/mmkv"
|
||||||
|
)
|
||||||
|
|
||||||
|
var streamKeyVault mmkv.Vault
|
||||||
|
|
||||||
|
// TODO: move to factory
|
||||||
|
func readKeyFromMMKV(file string, logger *zap.Logger) ([]byte, error) {
|
||||||
|
if file == "" {
|
||||||
|
return nil, errors.New("file path is required while reading key from mmkv")
|
||||||
|
}
|
||||||
|
|
||||||
|
//goland:noinspection GoBoolExpressions
|
||||||
|
if runtime.GOOS != "darwin" {
|
||||||
|
return nil, errors.New("mmkv vault not supported on this platform")
|
||||||
|
}
|
||||||
|
|
||||||
|
if streamKeyVault == nil {
|
||||||
|
mmkvDir, err := getRelativeMMKVDir(file)
|
||||||
|
if err != nil {
|
||||||
|
mmkvDir, err = getDefaultMMKVDir()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("mmkv key valut not found: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mgr, err := mmkv.NewManager(mmkvDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("init mmkv manager: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
streamKeyVault, err = mgr.OpenVault("MMKVStreamEncryptId")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("open mmkv vault: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Debug("mmkv vault opened", zap.Strings("keys", streamKeyVault.Keys()))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, partName := filepath.Split(file)
|
||||||
|
partName = normalizeUnicode(partName)
|
||||||
|
|
||||||
|
buf, err := streamKeyVault.GetBytes(file)
|
||||||
|
if buf == nil {
|
||||||
|
filePaths := streamKeyVault.Keys()
|
||||||
|
fileNames := lo.Map(filePaths, func(filePath string, _ int) string {
|
||||||
|
_, name := filepath.Split(filePath)
|
||||||
|
return normalizeUnicode(name)
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, key := range fileNames { // fallback: match filename only
|
||||||
|
if key != partName {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
idx := slices.Index(fileNames, key)
|
||||||
|
buf, err = streamKeyVault.GetBytes(filePaths[idx])
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("read key from mmkv", zap.String("key", filePaths[idx]), zap.Error(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(buf) == 0 {
|
||||||
|
return nil, errors.New("key not found in mmkv vault")
|
||||||
|
}
|
||||||
|
|
||||||
|
return deriveKey(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func OpenMMKV(mmkvPath string, key string, logger *zap.Logger) error {
|
||||||
|
filePath, fileName := filepath.Split(mmkvPath)
|
||||||
|
mgr, err := mmkv.NewManager(filepath.Dir(filePath))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("init mmkv manager: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If `vaultKey` is empty, the key is ignored.
|
||||||
|
streamKeyVault, err = mgr.OpenVaultCrypto(fileName, key)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("open mmkv vault: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Debug("mmkv vault opened", zap.Strings("keys", streamKeyVault.Keys()))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// /
|
||||||
|
func readKeyFromMMKVCustom(mid string) ([]byte, error) {
|
||||||
|
if streamKeyVault == nil {
|
||||||
|
return nil, fmt.Errorf("mmkv vault not loaded")
|
||||||
|
}
|
||||||
|
|
||||||
|
// get ekey from mmkv vault
|
||||||
|
eKey, err := streamKeyVault.GetBytes(mid)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("get eKey error: %w", err)
|
||||||
|
}
|
||||||
|
return deriveKey(eKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// / getRelativeMMKVDir get mmkv dir relative to file (legacy QQMusic for macOS behaviour)
|
||||||
|
func getRelativeMMKVDir(file string) (string, error) {
|
||||||
|
mmkvDir := filepath.Join(filepath.Dir(file), "../mmkv")
|
||||||
|
if _, err := os.Stat(mmkvDir); err != nil {
|
||||||
|
return "", fmt.Errorf("stat default mmkv dir: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
keyFile := filepath.Join(mmkvDir, "MMKVStreamEncryptId")
|
||||||
|
if _, err := os.Stat(keyFile); err != nil {
|
||||||
|
return "", fmt.Errorf("stat default mmkv file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mmkvDir, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getDefaultMMKVDir() (string, error) {
|
||||||
|
homeDir, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("get user home dir: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mmkvDir := filepath.Join(
|
||||||
|
homeDir,
|
||||||
|
"Library/Containers/com.tencent.QQMusicMac/Data",
|
||||||
|
"Library/Application Support/QQMusicMac/mmkv",
|
||||||
|
)
|
||||||
|
if _, err := os.Stat(mmkvDir); err != nil {
|
||||||
|
return "", fmt.Errorf("stat default mmkv dir: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
keyFile := filepath.Join(mmkvDir, "MMKVStreamEncryptId")
|
||||||
|
if _, err := os.Stat(keyFile); err != nil {
|
||||||
|
return "", fmt.Errorf("stat default mmkv file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mmkvDir, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalizeUnicode normalizes unicode string to NFC.
|
||||||
|
// since macOS may change some characters in the file name.
|
||||||
|
// e.g. "ぜ"(e3 81 9c) -> "ぜ"(e3 81 9b e3 82 99)
|
||||||
|
func normalizeUnicode(str string) string {
|
||||||
|
return norm.NFC.String(str)
|
||||||
|
}
|
@ -5,7 +5,9 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"go.uber.org/zap"
|
||||||
"io"
|
"io"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -14,7 +16,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
raw io.ReadSeeker // raw is the original file reader
|
raw io.ReadSeeker // raw is the original file reader
|
||||||
|
params *common.DecoderParams
|
||||||
|
|
||||||
audio io.Reader // audio is the encrypted audio data
|
audio io.Reader // audio is the encrypted audio data
|
||||||
audioLen int // audioLen is the audio data length
|
audioLen int // audioLen is the audio data length
|
||||||
@ -23,8 +26,20 @@ type Decoder struct {
|
|||||||
decodedKey []byte // decodedKey is the decoded key for cipher
|
decodedKey []byte // decodedKey is the decoded key for cipher
|
||||||
cipher common.StreamDecoder
|
cipher common.StreamDecoder
|
||||||
|
|
||||||
rawMetaExtra1 int
|
songID int
|
||||||
rawMetaExtra2 int
|
rawMetaExtra2 int
|
||||||
|
|
||||||
|
albumID int
|
||||||
|
albumMediaID string
|
||||||
|
|
||||||
|
// cache
|
||||||
|
meta common.AudioMeta
|
||||||
|
cover []byte
|
||||||
|
embeddedCover bool // embeddedCover is true if the cover is embedded in the file
|
||||||
|
probeBuf *bytes.Buffer // probeBuf is the buffer for sniffing metadata, TODO: consider pipe?
|
||||||
|
|
||||||
|
// provider
|
||||||
|
logger *zap.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read implements io.Reader, offer the decrypted audio data.
|
// Read implements io.Reader, offer the decrypted audio data.
|
||||||
@ -34,12 +49,14 @@ func (d *Decoder) Read(p []byte) (int, error) {
|
|||||||
if n > 0 {
|
if n > 0 {
|
||||||
d.cipher.Decrypt(p[:n], d.offset)
|
d.cipher.Decrypt(p[:n], d.offset)
|
||||||
d.offset += n
|
d.offset += n
|
||||||
|
|
||||||
|
_, _ = d.probeBuf.Write(p[:n]) // bytes.Buffer.Write never return error
|
||||||
}
|
}
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDecoder(r io.ReadSeeker) common.Decoder {
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
return &Decoder{raw: r}
|
return &Decoder{raw: p.Reader, params: p, logger: p.Logger}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Validate() error {
|
func (d *Decoder) Validate() error {
|
||||||
@ -75,6 +92,9 @@ func (d *Decoder) Validate() error {
|
|||||||
}
|
}
|
||||||
d.audio = io.LimitReader(d.raw, int64(d.audioLen))
|
d.audio = io.LimitReader(d.raw, int64(d.audioLen))
|
||||||
|
|
||||||
|
// prepare for sniffing metadata
|
||||||
|
d.probeBuf = bytes.NewBuffer(make([]byte, 0, d.audioLen))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +104,7 @@ func (d *Decoder) validateDecode() error {
|
|||||||
return fmt.Errorf("qmc seek to start: %w", err)
|
return fmt.Errorf("qmc seek to start: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, 16)
|
buf := make([]byte, 64)
|
||||||
if _, err := io.ReadFull(d.raw, buf); err != nil {
|
if _, err := io.ReadFull(d.raw, buf); err != nil {
|
||||||
return fmt.Errorf("qmc read header: %w", err)
|
return fmt.Errorf("qmc read header: %w", err)
|
||||||
}
|
}
|
||||||
@ -97,11 +117,22 @@ func (d *Decoder) validateDecode() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) searchKey() error {
|
func (d *Decoder) searchKey() (err error) {
|
||||||
fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd)
|
fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
fileSize := int(fileSizeM4) + 4
|
||||||
|
|
||||||
|
//goland:noinspection GoBoolExpressions
|
||||||
|
if runtime.GOOS == "darwin" && !strings.HasPrefix(d.params.Extension, ".qmc") {
|
||||||
|
d.decodedKey, err = readKeyFromMMKV(d.params.FilePath, d.logger)
|
||||||
|
if err == nil {
|
||||||
|
d.audioLen = fileSize
|
||||||
|
return
|
||||||
|
}
|
||||||
|
d.logger.Warn("read key from mmkv failed", zap.Error(err))
|
||||||
|
}
|
||||||
|
|
||||||
suffixBuf := make([]byte, 4)
|
suffixBuf := make([]byte, 4)
|
||||||
if _, err := io.ReadFull(d.raw, suffixBuf); err != nil {
|
if _, err := io.ReadFull(d.raw, suffixBuf); err != nil {
|
||||||
@ -113,6 +144,17 @@ func (d *Decoder) searchKey() error {
|
|||||||
return d.readRawMetaQTag()
|
return d.readRawMetaQTag()
|
||||||
case "STag":
|
case "STag":
|
||||||
return errors.New("qmc: file with 'STag' suffix doesn't contains media key")
|
return errors.New("qmc: file with 'STag' suffix doesn't contains media key")
|
||||||
|
case "cex\x00":
|
||||||
|
footer, err := NewMusicExTag(d.raw)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
d.audioLen = fileSize - int(footer.TagSize)
|
||||||
|
d.decodedKey, err = readKeyFromMMKVCustom(footer.MediaFileName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
default:
|
default:
|
||||||
size := binary.LittleEndian.Uint32(suffixBuf)
|
size := binary.LittleEndian.Uint32(suffixBuf)
|
||||||
|
|
||||||
@ -121,7 +163,7 @@ func (d *Decoder) searchKey() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// try to use default static cipher
|
// try to use default static cipher
|
||||||
d.audioLen = int(fileSizeM4 + 4)
|
d.audioLen = fileSize
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,7 +224,7 @@ func (d *Decoder) readRawMetaQTag() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
d.rawMetaExtra1, err = strconv.Atoi(items[1])
|
d.songID, err = strconv.Atoi(items[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -212,10 +254,20 @@ func init() {
|
|||||||
"6d3461", //QQ Music Weiyun M4a
|
"6d3461", //QQ Music Weiyun M4a
|
||||||
"776176", //QQ Music Weiyun Wav
|
"776176", //QQ Music Weiyun Wav
|
||||||
|
|
||||||
"mgg", "mgg1", "mggl", //QQ Music New Ogg
|
"mmp4", // QQ Music MP4 Container, tipically used for Dolby EAC3 stream
|
||||||
"mflac", "mflac0", //QQ Music New Flac
|
|
||||||
}
|
}
|
||||||
for _, ext := range supportedExts {
|
for _, ext := range supportedExts {
|
||||||
common.RegisterDecoder(ext, false, NewDecoder)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
93
algo/qmc/qmc_footer_musicex.go
Normal file
93
algo/qmc/qmc_footer_musicex.go
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
package qmc
|
||||||
|
|
||||||
|
import (
|
||||||
|
bytes "bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MusicExTagV1 struct {
|
||||||
|
SongID uint32 // Song ID
|
||||||
|
Unknown1 uint32 // unused & unknown
|
||||||
|
Unknown2 uint32 // unused & unknown
|
||||||
|
MediaID string // Media ID
|
||||||
|
MediaFileName string // real file name
|
||||||
|
Unknown3 uint32 // unused; uninitialized memory?
|
||||||
|
|
||||||
|
// 16 byte at the end of tag.
|
||||||
|
// TagSize should be respected when parsing.
|
||||||
|
TagSize uint32 // 19.57: fixed value: 0xC0
|
||||||
|
TagVersion uint32 // 19.57: fixed value: 0x01
|
||||||
|
TagMagic []byte // fixed value "musicex\0" (8 bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMusicExTag(f io.ReadSeeker) (*MusicExTagV1, error) {
|
||||||
|
_, err := f.Seek(-16, io.SeekEnd)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("musicex seek error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer := make([]byte, 16)
|
||||||
|
bytesRead, err := f.Read(buffer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("get musicex error: %w", err)
|
||||||
|
}
|
||||||
|
if bytesRead != 16 {
|
||||||
|
return nil, fmt.Errorf("MusicExV1: read %d bytes (expected %d)", bytesRead, 16)
|
||||||
|
}
|
||||||
|
|
||||||
|
tag := &MusicExTagV1{
|
||||||
|
TagSize: binary.LittleEndian.Uint32(buffer[0x00:0x04]),
|
||||||
|
TagVersion: binary.LittleEndian.Uint32(buffer[0x04:0x08]),
|
||||||
|
TagMagic: buffer[0x04:0x0C],
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal(tag.TagMagic, []byte("musicex\x00")) {
|
||||||
|
return nil, errors.New("MusicEx magic mismatch")
|
||||||
|
}
|
||||||
|
if tag.TagVersion != 1 {
|
||||||
|
return nil, errors.New(fmt.Sprintf("unsupported musicex tag version. expecting 1, got %d", tag.TagVersion))
|
||||||
|
}
|
||||||
|
|
||||||
|
if tag.TagSize < 0xC0 {
|
||||||
|
return nil, errors.New(fmt.Sprintf("unsupported musicex tag size. expecting at least 0xC0, got 0x%02x", tag.TagSize))
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = make([]byte, tag.TagSize)
|
||||||
|
bytesRead, err = f.Read(buffer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if uint32(bytesRead) != tag.TagSize {
|
||||||
|
return nil, fmt.Errorf("MusicExV1: read %d bytes (expected %d)", bytesRead, tag.TagSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
tag.SongID = binary.LittleEndian.Uint32(buffer[0x00:0x04])
|
||||||
|
tag.Unknown1 = binary.LittleEndian.Uint32(buffer[0x04:0x08])
|
||||||
|
tag.Unknown2 = binary.LittleEndian.Uint32(buffer[0x08:0x0C])
|
||||||
|
tag.MediaID = readUnicodeTagName(buffer[0x0C:], 30*2)
|
||||||
|
tag.MediaFileName = readUnicodeTagName(buffer[0x48:], 50*2)
|
||||||
|
tag.Unknown3 = binary.LittleEndian.Uint32(buffer[0xAC:0xB0])
|
||||||
|
return tag, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// readUnicodeTagName reads a buffer to maxLen.
|
||||||
|
// reconstruct text by skipping alternate char (ascii chars encoded in UTF-16-LE),
|
||||||
|
// until finding a zero or reaching maxLen.
|
||||||
|
func readUnicodeTagName(buffer []byte, maxLen int) string {
|
||||||
|
builder := strings.Builder{}
|
||||||
|
|
||||||
|
for i := 0; i < maxLen; i += 2 {
|
||||||
|
chr := buffer[i]
|
||||||
|
if chr != 0 {
|
||||||
|
builder.WriteByte(chr)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.String()
|
||||||
|
}
|
128
algo/qmc/qmc_meta.go
Normal file
128
algo/qmc/qmc_meta.go
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
package qmc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/samber/lo"
|
||||||
|
|
||||||
|
"unlock-music.dev/cli/algo/common"
|
||||||
|
"unlock-music.dev/cli/algo/qmc/client"
|
||||||
|
"unlock-music.dev/cli/internal/ffmpeg"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (d *Decoder) GetAudioMeta(ctx context.Context) (common.AudioMeta, error) {
|
||||||
|
if d.meta != nil {
|
||||||
|
return d.meta, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.songID != 0 {
|
||||||
|
if err := d.getMetaBySongID(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return d.meta, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
embedMeta, err := ffmpeg.ProbeReader(ctx, d.probeBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qmc[GetAudioMeta] probe reader: %w", err)
|
||||||
|
}
|
||||||
|
d.meta = embedMeta
|
||||||
|
d.embeddedCover = embedMeta.HasAttachedPic()
|
||||||
|
|
||||||
|
if !d.embeddedCover && embedMeta.HasMetadata() {
|
||||||
|
if err := d.searchMetaOnline(ctx, embedMeta); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return d.meta, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.meta, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) getMetaBySongID(ctx context.Context) error {
|
||||||
|
c := client.NewQQMusicClient() // todo: use global client
|
||||||
|
trackInfo, err := c.GetTrackInfo(ctx, d.songID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("qmc[GetAudioMeta] get track info: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.meta = trackInfo
|
||||||
|
d.albumID = trackInfo.Album.Id
|
||||||
|
if trackInfo.Album.Pmid == "" {
|
||||||
|
d.albumMediaID = trackInfo.Album.Pmid
|
||||||
|
} else {
|
||||||
|
d.albumMediaID = trackInfo.Album.Mid
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) searchMetaOnline(ctx context.Context, original common.AudioMeta) error {
|
||||||
|
c := client.NewQQMusicClient() // todo: use global client
|
||||||
|
keyword := lo.WithoutEmpty(append(
|
||||||
|
[]string{original.GetTitle(), original.GetAlbum()},
|
||||||
|
original.GetArtists()...),
|
||||||
|
)
|
||||||
|
if len(keyword) == 0 {
|
||||||
|
return errors.New("qmc[searchMetaOnline] no keyword")
|
||||||
|
}
|
||||||
|
|
||||||
|
trackList, err := c.Search(ctx, strings.Join(keyword, " "))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("qmc[searchMetaOnline] search: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(trackList) == 0 {
|
||||||
|
return errors.New("qmc[searchMetaOnline] no result")
|
||||||
|
}
|
||||||
|
|
||||||
|
meta := trackList[0]
|
||||||
|
d.meta = meta
|
||||||
|
d.albumID = meta.Album.Id
|
||||||
|
if meta.Album.Pmid == "" {
|
||||||
|
d.albumMediaID = meta.Album.Pmid
|
||||||
|
} else {
|
||||||
|
d.albumMediaID = meta.Album.Mid
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) {
|
||||||
|
if d.cover != nil {
|
||||||
|
return d.cover, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.embeddedCover {
|
||||||
|
img, err := ffmpeg.ExtractAlbumArt(ctx, d.probeBuf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qmc[GetCoverImage] extract album art: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.cover = img.Bytes()
|
||||||
|
|
||||||
|
return d.cover, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c := client.NewQQMusicClient() // todo: use global client
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if d.albumMediaID != "" {
|
||||||
|
d.cover, err = c.AlbumCoverByMediaID(ctx, d.albumMediaID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qmc[GetCoverImage] get cover by media id: %w", err)
|
||||||
|
}
|
||||||
|
} else if d.albumID != 0 {
|
||||||
|
d.cover, err = c.AlbumCoverByID(ctx, d.albumID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("qmc[GetCoverImage] get cover by id: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil, errors.New("qmc[GetAudioMeta] album (or media) id is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.cover, nil
|
||||||
|
|
||||||
|
}
|
@ -7,6 +7,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"unlock-music.dev/cli/algo/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadTestDataQmcDecoder(filename string) ([]byte, []byte, error) {
|
func loadTestDataQmcDecoder(filename string) ([]byte, []byte, error) {
|
||||||
@ -29,13 +31,14 @@ func loadTestDataQmcDecoder(filename string) ([]byte, []byte, error) {
|
|||||||
func TestMflac0Decoder_Read(t *testing.T) {
|
func TestMflac0Decoder_Read(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
fileExt string
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{"mflac0_rc4", false},
|
{"mflac0_rc4", ".mflac0", false},
|
||||||
{"mflac_rc4", false},
|
{"mflac_rc4", ".mflac", false},
|
||||||
{"mflac_map", false},
|
{"mflac_map", ".mflac", false},
|
||||||
{"mgg_map", false},
|
{"mgg_map", ".mgg", false},
|
||||||
{"qmc0_static", false},
|
{"qmc0_static", ".qmc0", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@ -45,7 +48,10 @@ func TestMflac0Decoder_Read(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
d := NewDecoder(bytes.NewReader(raw))
|
d := NewDecoder(&common.DecoderParams{
|
||||||
|
Reader: bytes.NewReader(raw),
|
||||||
|
Extension: tt.fileExt,
|
||||||
|
})
|
||||||
if err := d.Validate(); err != nil {
|
if err := d.Validate(); err != nil {
|
||||||
t.Errorf("validate file error = %v", err)
|
t.Errorf("validate file error = %v", err)
|
||||||
}
|
}
|
||||||
@ -81,7 +87,10 @@ func TestMflac0Decoder_Validate(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
d := NewDecoder(bytes.NewReader(raw))
|
d := NewDecoder(&common.DecoderParams{
|
||||||
|
Reader: bytes.NewReader(raw),
|
||||||
|
Extension: tt.fileExt,
|
||||||
|
})
|
||||||
|
|
||||||
if err := d.Validate(); err != nil {
|
if err := d.Validate(); err != nil {
|
||||||
t.Errorf("read bytes from decoder error = %v", err)
|
t.Errorf("read bytes from decoder error = %v", err)
|
||||||
|
@ -43,8 +43,8 @@ func (d *Decoder) Read(buf []byte) (int, error) {
|
|||||||
return d.audio.Read(buf)
|
return d.audio.Read(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTmDecoder(rd io.ReadSeeker) common.Decoder {
|
func NewTmDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
return &Decoder{raw: rd}
|
return &Decoder{raw: p.Reader}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -37,8 +37,8 @@ func (d *Decoder) GetAudioExt() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
return &Decoder{rd: rd}
|
return &Decoder{rd: p.Reader}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate checks if the file is a valid xiami .xm file.
|
// Validate checks if the file is a valid xiami .xm file.
|
||||||
|
@ -16,8 +16,8 @@ type Decoder struct {
|
|||||||
audio io.Reader
|
audio io.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDecoder(rd io.ReadSeeker) common.Decoder {
|
func NewDecoder(p *common.DecoderParams) common.Decoder {
|
||||||
return &Decoder{rd: rd}
|
return &Decoder{rd: p.Reader}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Validate() error {
|
func (d *Decoder) Validate() error {
|
||||||
|
384
cmd/um/main.go
384
cmd/um/main.go
@ -2,35 +2,38 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/fsnotify/fsnotify"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"go.uber.org/zap/zapcore"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
|
|
||||||
"unlock-music.dev/cli/algo/common"
|
"unlock-music.dev/cli/algo/common"
|
||||||
_ "unlock-music.dev/cli/algo/kgm"
|
_ "unlock-music.dev/cli/algo/kgm"
|
||||||
_ "unlock-music.dev/cli/algo/kwm"
|
_ "unlock-music.dev/cli/algo/kwm"
|
||||||
_ "unlock-music.dev/cli/algo/ncm"
|
_ "unlock-music.dev/cli/algo/ncm"
|
||||||
_ "unlock-music.dev/cli/algo/qmc"
|
"unlock-music.dev/cli/algo/qmc"
|
||||||
_ "unlock-music.dev/cli/algo/tm"
|
_ "unlock-music.dev/cli/algo/tm"
|
||||||
_ "unlock-music.dev/cli/algo/xiami"
|
_ "unlock-music.dev/cli/algo/xiami"
|
||||||
_ "unlock-music.dev/cli/algo/ximalaya"
|
_ "unlock-music.dev/cli/algo/ximalaya"
|
||||||
"unlock-music.dev/cli/internal/logging"
|
"unlock-music.dev/cli/internal/ffmpeg"
|
||||||
"unlock-music.dev/cli/internal/sniff"
|
"unlock-music.dev/cli/internal/sniff"
|
||||||
|
"unlock-music.dev/cli/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var AppVersion = "v0.0.6"
|
var AppVersion = "v0.2.11"
|
||||||
|
|
||||||
var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger
|
var logger = setupLogger(false) // TODO: inject logger to application, instead of using global logger
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
module, ok := debug.ReadBuildInfo()
|
module, ok := debug.ReadBuildInfo()
|
||||||
@ -45,9 +48,16 @@ func main() {
|
|||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{Name: "input", Aliases: []string{"i"}, Usage: "path to input file or dir", Required: false},
|
&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: "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-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: "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},
|
&cli.BoolFlag{Name: "skip-noop", Aliases: []string{"n"}, Usage: "skip noop decoder", Required: false, Value: true},
|
||||||
&cli.BoolFlag{Name: "supported-ext", Usage: "Show supported file extensions and exit", Required: false, Value: false},
|
&cli.BoolFlag{Name: "verbose", Aliases: []string{"V"}, Usage: "verbose logging", Required: false, Value: false},
|
||||||
|
&cli.BoolFlag{Name: "update-metadata", Usage: "update metadata & album art from network", Required: false, Value: false},
|
||||||
|
&cli.BoolFlag{Name: "overwrite", Usage: "overwrite output file without asking", Required: false, Value: false},
|
||||||
|
&cli.BoolFlag{Name: "watch", Usage: "watch the input dir and process new files", Required: false, Value: false},
|
||||||
|
|
||||||
|
&cli.BoolFlag{Name: "supported-ext", Usage: "show supported file extensions and exit", Required: false, Value: false},
|
||||||
},
|
},
|
||||||
|
|
||||||
Action: appMain,
|
Action: appMain,
|
||||||
@ -55,22 +65,59 @@ func main() {
|
|||||||
HideHelpCommand: true,
|
HideHelpCommand: true,
|
||||||
UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-i] /path/to/input",
|
UsageText: "um [-o /path/to/output/dir] [--extra-flags] [-i] /path/to/input",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := app.Run(os.Args)
|
err := app.Run(os.Args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("run app failed", zap.Error(err))
|
logger.Fatal("run app failed", zap.Error(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func printSupportedExtensions() {
|
func printSupportedExtensions() {
|
||||||
var exts []string
|
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)
|
exts = append(exts, ext)
|
||||||
}
|
}
|
||||||
sort.Strings(exts)
|
sort.Strings(exts)
|
||||||
for _, ext := range exts {
|
for _, ext := range exts {
|
||||||
fmt.Printf("%s: %d\n", ext, len(common.DecoderRegistry[ext]))
|
fmt.Printf("%s: %d\n", ext, extSet[ext])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupLogger(verbose bool) *zap.Logger {
|
||||||
|
logConfig := zap.NewProductionEncoderConfig()
|
||||||
|
logConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||||
|
logConfig.EncodeTime = zapcore.RFC3339TimeEncoder
|
||||||
|
enabler := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
|
||||||
|
if verbose {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return level >= zapcore.InfoLevel
|
||||||
|
})
|
||||||
|
|
||||||
|
return zap.New(zapcore.NewCore(
|
||||||
|
zapcore.NewConsoleEncoder(logConfig),
|
||||||
|
os.Stdout,
|
||||||
|
enabler,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
func appMain(c *cli.Context) (err error) {
|
func appMain(c *cli.Context) (err error) {
|
||||||
|
logger = setupLogger(c.Bool("verbose"))
|
||||||
|
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if c.Bool("supported-ext") {
|
if c.Bool("supported-ext") {
|
||||||
printSupportedExtensions()
|
printSupportedExtensions()
|
||||||
return nil
|
return nil
|
||||||
@ -79,10 +126,7 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
if input == "" {
|
if input == "" {
|
||||||
switch c.Args().Len() {
|
switch c.Args().Len() {
|
||||||
case 0:
|
case 0:
|
||||||
input, err = os.Getwd()
|
input = cwd
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case 1:
|
case 1:
|
||||||
input = c.Args().Get(0)
|
input = c.Args().Get(0)
|
||||||
default:
|
default:
|
||||||
@ -90,26 +134,34 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
output := c.String("output")
|
input, absErr := filepath.Abs(input)
|
||||||
if output == "" {
|
if absErr != nil {
|
||||||
var err error
|
return fmt.Errorf("get abs path failed: %w", absErr)
|
||||||
output, err = os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if input == output {
|
|
||||||
return errors.New("input and output path are same")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
skipNoop := c.Bool("skip-noop")
|
output := c.String("output")
|
||||||
removeSource := c.Bool("remove-source")
|
|
||||||
|
|
||||||
inputStat, err := os.Stat(input)
|
inputStat, err := os.Stat(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var inputDir string
|
||||||
|
if inputStat.IsDir() {
|
||||||
|
inputDir = input
|
||||||
|
} else {
|
||||||
|
inputDir = filepath.Dir(input)
|
||||||
|
}
|
||||||
|
inputDir, absErr = filepath.Abs(inputDir)
|
||||||
|
if absErr != nil {
|
||||||
|
return fmt.Errorf("get abs path (inputDir) failed: %w", absErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if output == "" {
|
||||||
|
// Default to where the input dir is
|
||||||
|
output = inputDir
|
||||||
|
}
|
||||||
|
logger.Debug("resolve input/output path", zap.String("inputDir", inputDir), zap.String("input", input), zap.String("output", output))
|
||||||
|
|
||||||
outputStat, err := os.Stat(output)
|
outputStat, err := os.Stat(output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
@ -122,94 +174,276 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
return errors.New("output should be a writable directory")
|
return errors.New("output should be a writable directory")
|
||||||
}
|
}
|
||||||
|
|
||||||
if inputStat.IsDir() {
|
if mmkv := c.String("qmc-mmkv"); mmkv != "" {
|
||||||
return dealDirectory(input, output, skipNoop, removeSource)
|
// If key is not set, the mmkv vault will be treated as unencrypted.
|
||||||
} else {
|
key := c.String("qmc-mmkv-key")
|
||||||
allDec := common.GetDecoder(inputStat.Name(), skipNoop)
|
err := qmc.OpenMMKV(mmkv, key, logger)
|
||||||
if len(allDec) == 0 {
|
if err != nil {
|
||||||
logger.Fatal("skipping while no suitable decoder")
|
return err
|
||||||
}
|
}
|
||||||
return tryDecFile(input, output, allDec, removeSource)
|
}
|
||||||
|
|
||||||
|
proc := &processor{
|
||||||
|
logger: logger,
|
||||||
|
inputDir: inputDir,
|
||||||
|
outputDir: output,
|
||||||
|
skipNoopDecoder: c.Bool("skip-noop"),
|
||||||
|
removeSource: c.Bool("remove-source"),
|
||||||
|
updateMetadata: c.Bool("update-metadata"),
|
||||||
|
overwriteOutput: c.Bool("overwrite"),
|
||||||
|
}
|
||||||
|
|
||||||
|
if inputStat.IsDir() {
|
||||||
|
watchDir := c.Bool("watch")
|
||||||
|
if !watchDir {
|
||||||
|
return proc.processDir(input)
|
||||||
|
} else {
|
||||||
|
return proc.watchDir(input)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return proc.processFile(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
func dealDirectory(inputDir string, outputDir string, skipNoop bool, removeSource bool) error {
|
|
||||||
|
type processor struct {
|
||||||
|
logger *zap.Logger
|
||||||
|
inputDir string
|
||||||
|
outputDir string
|
||||||
|
|
||||||
|
skipNoopDecoder bool
|
||||||
|
removeSource bool
|
||||||
|
updateMetadata bool
|
||||||
|
overwriteOutput bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *processor) watchDir(inputDir string) error {
|
||||||
|
if err := p.processDir(inputDir); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
watcher, err := fsnotify.NewWatcher()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create watcher: %w", err)
|
||||||
|
}
|
||||||
|
defer watcher.Close()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case event, ok := <-watcher.Events:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if event.Has(fsnotify.Create) || event.Has(fsnotify.Write) {
|
||||||
|
// try open with exclusive mode, to avoid file is still writing
|
||||||
|
f, err := os.OpenFile(event.Name, os.O_RDONLY, os.ModeExclusive)
|
||||||
|
if err != nil {
|
||||||
|
logger.Debug("failed to open file exclusively", zap.String("path", event.Name), zap.Error(err))
|
||||||
|
time.Sleep(1 * time.Second) // wait for file writing complete
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_ = f.Close()
|
||||||
|
|
||||||
|
if err := p.processFile(event.Name); err != nil {
|
||||||
|
logger.Warn("failed to process file", zap.String("path", event.Name), zap.Error(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case err, ok := <-watcher.Errors:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logger.Error("file watcher got error", zap.Error(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = watcher.Add(inputDir)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to watch dir %s: %w", inputDir, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
signalCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||||
|
defer stop()
|
||||||
|
|
||||||
|
<-signalCtx.Done()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *processor) processDir(inputDir string) error {
|
||||||
items, err := os.ReadDir(inputDir)
|
items, err := os.ReadDir(inputDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var lastError error = nil
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
|
filePath := filepath.Join(inputDir, item.Name())
|
||||||
if item.IsDir() {
|
if item.IsDir() {
|
||||||
continue
|
if err = p.processDir(filePath); err != nil {
|
||||||
}
|
lastError = err
|
||||||
allDec := common.GetDecoder(item.Name(), skipNoop)
|
}
|
||||||
if len(allDec) == 0 {
|
|
||||||
logger.Info("skipping while no suitable decoder", zap.String("file", item.Name()))
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err := tryDecFile(filepath.Join(inputDir, item.Name()), outputDir, allDec, removeSource)
|
if err := p.processFile(filePath); err != nil {
|
||||||
if err != nil {
|
lastError = err
|
||||||
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFunc, removeSource bool) error {
|
func (p *processor) processFile(filePath string) error {
|
||||||
|
p.logger.Debug("processFile", zap.String("file", filePath), zap.String("inputDir", p.inputDir))
|
||||||
|
|
||||||
|
allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
|
||||||
|
if len(allDec) == 0 {
|
||||||
|
return errors.New("skipping while no suitable decoder")
|
||||||
|
}
|
||||||
|
|
||||||
|
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) 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)
|
file, err := os.Open(inputFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
logger := logger.With(zap.String("source", inputFile))
|
||||||
|
|
||||||
var dec common.Decoder
|
pDec, decoderFactory, err := p.findDecoder(allDec, &common.DecoderParams{
|
||||||
for _, decFunc := range allDec {
|
Reader: file,
|
||||||
dec = decFunc(file)
|
Extension: filepath.Ext(inputFile),
|
||||||
if err := dec.Validate(); err == nil {
|
FilePath: inputFile,
|
||||||
break
|
Logger: logger,
|
||||||
} else {
|
})
|
||||||
logger.Warn("try decode failed", zap.Error(err))
|
if err != nil {
|
||||||
dec = nil
|
return err
|
||||||
}
|
|
||||||
}
|
|
||||||
if dec == nil {
|
|
||||||
return errors.New("no any decoder can resolve the file")
|
|
||||||
}
|
}
|
||||||
|
dec := *pDec
|
||||||
|
|
||||||
|
params := &ffmpeg.UpdateMetadataParams{}
|
||||||
|
|
||||||
header := bytes.NewBuffer(nil)
|
header := bytes.NewBuffer(nil)
|
||||||
_, err = io.CopyN(header, dec, 16)
|
_, err = io.CopyN(header, dec, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("read header failed: %w", err)
|
return fmt.Errorf("read header failed: %w", err)
|
||||||
}
|
}
|
||||||
|
audio := io.MultiReader(header, dec)
|
||||||
|
params.AudioExt = sniff.AudioExtensionWithFallback(header.Bytes(), ".mp3")
|
||||||
|
|
||||||
outExt := sniff.AudioExtensionWithFallback(header.Bytes(), ".mp3")
|
if p.updateMetadata {
|
||||||
inFilename := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
|
if audioMetaGetter, ok := dec.(common.AudioMetaGetter); ok {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
outPath := filepath.Join(outputDir, inFilename+outExt)
|
// since ffmpeg doesn't support multiple input streams,
|
||||||
outFile, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
// we need to write the audio to a temp file.
|
||||||
|
// since qmc decoder doesn't support seeking & relying on ffmpeg probe, we need to read the whole file.
|
||||||
|
// TODO: support seeking or using pipe for qmc decoder.
|
||||||
|
params.Audio, err = utils.WriteTempFile(audio, params.AudioExt)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("updateAudioMeta write temp file: %w", err)
|
||||||
|
}
|
||||||
|
defer os.Remove(params.Audio)
|
||||||
|
|
||||||
|
params.Meta, err = audioMetaGetter.GetAudioMeta(ctx)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("get audio meta failed", zap.Error(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
if params.Meta == nil { // reset audio meta if failed
|
||||||
|
audio, err = os.Open(params.Audio)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("updateAudioMeta open temp file: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.updateMetadata && params.Meta != nil {
|
||||||
|
if coverGetter, ok := dec.(common.CoverImageGetter); ok {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if cover, err := coverGetter.GetCoverImage(ctx); err != nil {
|
||||||
|
logger.Warn("get cover image failed", zap.Error(err))
|
||||||
|
} else if imgExt, ok := sniff.ImageExtension(cover); !ok {
|
||||||
|
logger.Warn("sniff cover image type failed", zap.Error(err))
|
||||||
|
} else {
|
||||||
|
params.AlbumArtExt = imgExt
|
||||||
|
params.AlbumArt = cover
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inputRelDir, err := filepath.Rel(p.inputDir, filepath.Dir(inputFile))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("get relative dir failed: %w", err)
|
||||||
}
|
|
||||||
defer outFile.Close()
|
|
||||||
|
|
||||||
if _, err := io.Copy(outFile, header); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err := io.Copy(outFile, dec); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if source file need to be removed
|
inFilename := strings.TrimSuffix(filepath.Base(inputFile), decoderFactory.Suffix)
|
||||||
if removeSource {
|
outPath := filepath.Join(p.outputDir, inputRelDir, inFilename+params.AudioExt)
|
||||||
err := os.RemoveAll(inputFile)
|
|
||||||
|
if !p.overwriteOutput {
|
||||||
|
_, err := os.Stat(outPath)
|
||||||
|
if err == nil {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if params.Meta == nil {
|
||||||
|
outFile, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logger.Info("successfully converted, and source file is removed", zap.String("source", inputFile), zap.String("destination", outPath))
|
defer outFile.Close()
|
||||||
|
|
||||||
|
if _, err := io.Copy(outFile, audio); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.Info("successfully converted", zap.String("source", inputFile), zap.String("destination", outPath))
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if err := ffmpeg.UpdateMeta(ctx, outPath, params, logger); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Info("successfully converted", zap.String("source", inputFile), zap.String("destination", outPath))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
25
go.mod
25
go.mod
@ -1,17 +1,26 @@
|
|||||||
module unlock-music.dev/cli
|
module unlock-music.dev/cli
|
||||||
|
|
||||||
go 1.17
|
go 1.23.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/urfave/cli/v2 v2.23.5
|
github.com/fsnotify/fsnotify v1.8.0
|
||||||
go.uber.org/zap v1.23.0
|
github.com/go-flac/flacpicture v0.3.0
|
||||||
golang.org/x/crypto v0.3.0
|
github.com/go-flac/flacvorbis v0.2.0
|
||||||
|
github.com/go-flac/go-flac v1.0.0
|
||||||
|
github.com/samber/lo v1.47.0
|
||||||
|
github.com/urfave/cli/v2 v2.27.5
|
||||||
|
go.uber.org/zap v1.27.0
|
||||||
|
golang.org/x/crypto v0.29.0
|
||||||
|
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
|
||||||
|
golang.org/x/text v0.20.0
|
||||||
|
unlock-music.dev/mmkv v0.1.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
|
||||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
|
||||||
go.uber.org/atomic v1.10.0 // indirect
|
go.uber.org/multierr v1.11.0 // indirect
|
||||||
go.uber.org/multierr v1.8.0 // indirect
|
golang.org/x/sys v0.27.0 // indirect
|
||||||
|
google.golang.org/protobuf v1.35.2 // indirect
|
||||||
)
|
)
|
||||||
|
132
go.sum
132
go.sum
@ -1,88 +1,60 @@
|
|||||||
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
|
||||||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
|
||||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
|
||||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/go-flac/flacpicture v0.3.0 h1:LkmTxzFLIynwfhHiZsX0s8xcr3/u33MzvV89u+zOT8I=
|
||||||
|
github.com/go-flac/flacpicture v0.3.0/go.mod h1:DPbrzVYQ3fJcvSgLFp9HXIrEQEdfdk/+m0nQCzwodZI=
|
||||||
|
github.com/go-flac/flacvorbis v0.2.0 h1:KH0xjpkNTXFER4cszH4zeJxYcrHbUobz/RticWGOESs=
|
||||||
|
github.com/go-flac/flacvorbis v0.2.0/go.mod h1:uIysHOtuU7OLGoCRG92bvnkg7QEqHx19qKRV6K1pBrI=
|
||||||
|
github.com/go-flac/go-flac v1.0.0 h1:6qI9XOVLcO50xpzm3nXvO31BgDgHhnr/p/rER/K/doY=
|
||||||
|
github.com/go-flac/go-flac v1.0.0/go.mod h1:WnZhcpmq4u1UdZMNn9LYSoASpWOCMOoxXxcWEHSzkW8=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
|
||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
|
||||||
github.com/urfave/cli/v2 v2.23.5 h1:xbrU7tAYviSpqeR3X4nEFWUdB/uDZ6DE+HxmRU7Xtyw=
|
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
|
||||||
github.com/urfave/cli/v2 v2.23.5/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
|
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
|
||||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
|
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
|
||||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
|
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
|
||||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
|
||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
|
||||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
|
||||||
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
|
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||||
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
||||||
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
|
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||||
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
|
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
||||||
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
|
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
||||||
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
|
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
|
||||||
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
|
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
|
||||||
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
|
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
|
||||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
|
||||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
|
||||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
|
||||||
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
|
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
|
||||||
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
|
||||||
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
|
||||||
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
|
||||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
unlock-music.dev/mmkv v0.1.0 h1:hgUHo0gJVoiKZ6bOcFOw2LHFqNiefIe+jb5o0OyL720=
|
||||||
|
unlock-music.dev/mmkv v0.1.0/go.mod h1:qr34SM3x8xRxyUfGzefH/rSi+DUXkQZcSfXY/yfuTeo=
|
||||||
|
126
internal/ffmpeg/ffmpeg.go
Normal file
126
internal/ffmpeg/ffmpeg.go
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"unlock-music.dev/cli/algo/common"
|
||||||
|
"unlock-music.dev/cli/internal/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExtractAlbumArt(ctx context.Context, rd io.Reader) (*bytes.Buffer, error) {
|
||||||
|
cmd := exec.CommandContext(ctx, "ffmpeg",
|
||||||
|
"-i", "pipe:0", // input from stdin
|
||||||
|
"-an", // disable audio
|
||||||
|
"-codec:v", "copy", // copy video(image) codec
|
||||||
|
"-f", "image2", // use image2 muxer
|
||||||
|
"pipe:1", // output to stdout
|
||||||
|
)
|
||||||
|
|
||||||
|
cmd.Stdin = rd
|
||||||
|
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
|
||||||
|
cmd.Stdout, cmd.Stderr = stdout, stderr
|
||||||
|
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return nil, fmt.Errorf("ffmpeg run: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return stdout, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdateMetadataParams struct {
|
||||||
|
Audio string // required
|
||||||
|
AudioExt string // required
|
||||||
|
|
||||||
|
Meta common.AudioMeta // required
|
||||||
|
|
||||||
|
AlbumArt []byte // optional
|
||||||
|
AlbumArtExt string // required if AlbumArt is not nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateMeta(ctx context.Context, outPath string, params *UpdateMetadataParams, logger *zap.Logger) error {
|
||||||
|
if params.AudioExt == ".flac" {
|
||||||
|
return updateMetaFlac(ctx, outPath, params, logger.With(zap.String("module", "updateMetaFlac")))
|
||||||
|
} else {
|
||||||
|
return updateMetaFFmpeg(ctx, outPath, params)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func updateMetaFFmpeg(ctx context.Context, outPath string, params *UpdateMetadataParams) error {
|
||||||
|
builder := newFFmpegBuilder()
|
||||||
|
|
||||||
|
out := newOutputBuilder(outPath) // output to file
|
||||||
|
builder.SetFlag("y") // overwrite output file
|
||||||
|
builder.AddOutput(out)
|
||||||
|
|
||||||
|
// input audio -> output audio
|
||||||
|
builder.AddInput(newInputBuilder(params.Audio)) // input 0: audio
|
||||||
|
out.AddOption("map", "0:a")
|
||||||
|
out.AddOption("codec:a", "copy")
|
||||||
|
|
||||||
|
// input cover -> output cover
|
||||||
|
if params.AlbumArt != nil &&
|
||||||
|
params.AudioExt != ".wav" /* wav doesn't support attached image */ {
|
||||||
|
|
||||||
|
// write cover to temp file
|
||||||
|
artPath, err := utils.WriteTempFile(bytes.NewReader(params.AlbumArt), params.AlbumArtExt)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("updateAudioMeta write temp file: %w", err)
|
||||||
|
}
|
||||||
|
defer os.Remove(artPath)
|
||||||
|
|
||||||
|
builder.AddInput(newInputBuilder(artPath)) // input 1: cover
|
||||||
|
out.AddOption("map", "1:v")
|
||||||
|
|
||||||
|
switch params.AudioExt {
|
||||||
|
case ".ogg": // ogg only supports theora codec
|
||||||
|
out.AddOption("codec:v", "libtheora")
|
||||||
|
case ".m4a": // .m4a(mp4) requires set codec, disposition, stream metadata
|
||||||
|
out.AddOption("codec:v", "mjpeg")
|
||||||
|
out.AddOption("disposition:v", "attached_pic")
|
||||||
|
out.AddMetadata("s:v", "title", "Album cover")
|
||||||
|
out.AddMetadata("s:v", "comment", "Cover (front)")
|
||||||
|
case ".mp3":
|
||||||
|
out.AddOption("codec:v", "mjpeg")
|
||||||
|
out.AddMetadata("s:v", "title", "Album cover")
|
||||||
|
out.AddMetadata("s:v", "comment", "Cover (front)")
|
||||||
|
default: // other formats use default behavior
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set file metadata
|
||||||
|
album := params.Meta.GetAlbum()
|
||||||
|
if album != "" {
|
||||||
|
out.AddMetadata("", "album", album)
|
||||||
|
}
|
||||||
|
|
||||||
|
title := params.Meta.GetTitle()
|
||||||
|
if album != "" {
|
||||||
|
out.AddMetadata("", "title", title)
|
||||||
|
}
|
||||||
|
|
||||||
|
artists := params.Meta.GetArtists()
|
||||||
|
if len(artists) != 0 {
|
||||||
|
// TODO: it seems that ffmpeg doesn't support multiple artists
|
||||||
|
out.AddMetadata("", "artist", strings.Join(artists, " / "))
|
||||||
|
}
|
||||||
|
|
||||||
|
if params.AudioExt == ".mp3" {
|
||||||
|
out.AddOption("write_id3v1", "true")
|
||||||
|
out.AddOption("id3v2_version", "3")
|
||||||
|
}
|
||||||
|
|
||||||
|
// execute ffmpeg
|
||||||
|
cmd := builder.Command(ctx)
|
||||||
|
|
||||||
|
if stdout, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
return fmt.Errorf("ffmpeg run: %w, %s", err, string(stdout))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
141
internal/ffmpeg/ffprobe.go
Normal file
141
internal/ffmpeg/ffprobe.go
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/samber/lo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Result struct {
|
||||||
|
Format *Format `json:"format"`
|
||||||
|
Streams []*Stream `json:"streams"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Result) HasAttachedPic() bool {
|
||||||
|
return lo.ContainsBy(r.Streams, func(s *Stream) bool {
|
||||||
|
return s.CodecType == "video"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Result) getTagByKey(key string) string {
|
||||||
|
for k, v := range r.Format.Tags {
|
||||||
|
if key == strings.ToLower(k) {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, stream := range r.Streams { // try to find in streams
|
||||||
|
if stream.CodecType != "audio" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for k, v := range stream.Tags {
|
||||||
|
if key == strings.ToLower(k) {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
func (r *Result) GetTitle() string {
|
||||||
|
return r.getTagByKey("title")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Result) GetAlbum() string {
|
||||||
|
return r.getTagByKey("album")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Result) GetArtists() []string {
|
||||||
|
artists := strings.Split(r.getTagByKey("artist"), "/")
|
||||||
|
for i := range artists {
|
||||||
|
artists[i] = strings.TrimSpace(artists[i])
|
||||||
|
}
|
||||||
|
return artists
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Result) HasMetadata() bool {
|
||||||
|
return r.GetTitle() != "" || r.GetAlbum() != "" || len(r.GetArtists()) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type Format struct {
|
||||||
|
Filename string `json:"filename"`
|
||||||
|
NbStreams int `json:"nb_streams"`
|
||||||
|
NbPrograms int `json:"nb_programs"`
|
||||||
|
FormatName string `json:"format_name"`
|
||||||
|
FormatLongName string `json:"format_long_name"`
|
||||||
|
StartTime string `json:"start_time"`
|
||||||
|
Duration string `json:"duration"`
|
||||||
|
BitRate string `json:"bit_rate"`
|
||||||
|
ProbeScore int `json:"probe_score"`
|
||||||
|
Tags map[string]string `json:"tags"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Stream struct {
|
||||||
|
Index int `json:"index"`
|
||||||
|
CodecName string `json:"codec_name"`
|
||||||
|
CodecLongName string `json:"codec_long_name"`
|
||||||
|
CodecType string `json:"codec_type"`
|
||||||
|
CodecTagString string `json:"codec_tag_string"`
|
||||||
|
CodecTag string `json:"codec_tag"`
|
||||||
|
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"`
|
||||||
|
RFrameRate string `json:"r_frame_rate"`
|
||||||
|
AvgFrameRate string `json:"avg_frame_rate"`
|
||||||
|
TimeBase string `json:"time_base"`
|
||||||
|
StartPts int `json:"start_pts"`
|
||||||
|
StartTime string `json:"start_time"`
|
||||||
|
BitRate string `json:"bit_rate"`
|
||||||
|
Disposition *ProbeDisposition `json:"disposition"`
|
||||||
|
Tags map[string]string `json:"tags"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProbeDisposition struct {
|
||||||
|
Default int `json:"default"`
|
||||||
|
Dub int `json:"dub"`
|
||||||
|
Original int `json:"original"`
|
||||||
|
Comment int `json:"comment"`
|
||||||
|
Lyrics int `json:"lyrics"`
|
||||||
|
Karaoke int `json:"karaoke"`
|
||||||
|
Forced int `json:"forced"`
|
||||||
|
HearingImpaired int `json:"hearing_impaired"`
|
||||||
|
VisualImpaired int `json:"visual_impaired"`
|
||||||
|
CleanEffects int `json:"clean_effects"`
|
||||||
|
AttachedPic int `json:"attached_pic"`
|
||||||
|
TimedThumbnails int `json:"timed_thumbnails"`
|
||||||
|
Captions int `json:"captions"`
|
||||||
|
Descriptions int `json:"descriptions"`
|
||||||
|
Metadata int `json:"metadata"`
|
||||||
|
Dependent int `json:"dependent"`
|
||||||
|
StillImage int `json:"still_image"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProbeReader(ctx context.Context, rd io.Reader) (*Result, error) {
|
||||||
|
cmd := exec.CommandContext(ctx, "ffprobe",
|
||||||
|
"-v", "quiet", // disable logging
|
||||||
|
"-print_format", "json", // use json format
|
||||||
|
"-show_format", "-show_streams", "-show_error", // retrieve format and streams
|
||||||
|
"pipe:0", // input from stdin
|
||||||
|
)
|
||||||
|
|
||||||
|
cmd.Stdin = rd
|
||||||
|
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
|
||||||
|
cmd.Stdout, cmd.Stderr = stdout, stderr
|
||||||
|
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := new(Result)
|
||||||
|
if err := json.Unmarshal(stdout.Bytes(), ret); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
94
internal/ffmpeg/meta_flac.go
Normal file
94
internal/ffmpeg/meta_flac.go
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"mime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-flac/flacpicture"
|
||||||
|
"github.com/go-flac/flacvorbis"
|
||||||
|
"github.com/go-flac/go-flac"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
|
)
|
||||||
|
|
||||||
|
func updateMetaFlac(_ context.Context, outPath string, m *UpdateMetadataParams, logger *zap.Logger) error {
|
||||||
|
f, err := flac.ParseFile(m.Audio)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate comment block
|
||||||
|
comment := flacvorbis.MetaDataBlockVorbisComment{Vendor: "unlock-music.dev"}
|
||||||
|
|
||||||
|
// add metadata
|
||||||
|
title := m.Meta.GetTitle()
|
||||||
|
if title != "" {
|
||||||
|
_ = comment.Add(flacvorbis.FIELD_TITLE, title)
|
||||||
|
}
|
||||||
|
|
||||||
|
album := m.Meta.GetAlbum()
|
||||||
|
if album != "" {
|
||||||
|
_ = comment.Add(flacvorbis.FIELD_ALBUM, album)
|
||||||
|
}
|
||||||
|
|
||||||
|
artists := m.Meta.GetArtists()
|
||||||
|
for _, artist := range artists {
|
||||||
|
_ = comment.Add(flacvorbis.FIELD_ARTIST, artist)
|
||||||
|
}
|
||||||
|
|
||||||
|
existCommentIdx := slices.IndexFunc(f.Meta, func(b *flac.MetaDataBlock) bool {
|
||||||
|
return b.Type == flac.VorbisComment
|
||||||
|
})
|
||||||
|
if existCommentIdx >= 0 { // copy existing comment fields
|
||||||
|
exist, err := flacvorbis.ParseFromMetaDataBlock(*f.Meta[existCommentIdx])
|
||||||
|
if err != nil {
|
||||||
|
for _, s := range exist.Comments {
|
||||||
|
if strings.HasPrefix(s, flacvorbis.FIELD_TITLE+"=") && title != "" ||
|
||||||
|
strings.HasPrefix(s, flacvorbis.FIELD_ALBUM+"=") && album != "" ||
|
||||||
|
strings.HasPrefix(s, flacvorbis.FIELD_ARTIST+"=") && len(artists) != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
comment.Comments = append(comment.Comments, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// add / replace flac comment
|
||||||
|
cmtBlock := comment.Marshal()
|
||||||
|
if existCommentIdx < 0 {
|
||||||
|
f.Meta = append(f.Meta, &cmtBlock)
|
||||||
|
} else {
|
||||||
|
f.Meta[existCommentIdx] = &cmtBlock
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.AlbumArt != nil {
|
||||||
|
coverMime := mime.TypeByExtension(m.AlbumArtExt)
|
||||||
|
logger.Debug("cover image mime detect", zap.String("mime", coverMime))
|
||||||
|
cover, err := flacpicture.NewFromImageData(
|
||||||
|
flacpicture.PictureTypeFrontCover,
|
||||||
|
"Front cover",
|
||||||
|
m.AlbumArt,
|
||||||
|
coverMime,
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("failed to create flac cover", zap.Error(err))
|
||||||
|
} else {
|
||||||
|
coverBlock := cover.Marshal()
|
||||||
|
f.Meta = append(f.Meta, &coverBlock)
|
||||||
|
|
||||||
|
// add / replace flac cover
|
||||||
|
coverIdx := slices.IndexFunc(f.Meta, func(b *flac.MetaDataBlock) bool {
|
||||||
|
return b.Type == flac.Picture
|
||||||
|
})
|
||||||
|
if coverIdx < 0 {
|
||||||
|
f.Meta = append(f.Meta, &coverBlock)
|
||||||
|
} else {
|
||||||
|
f.Meta[coverIdx] = &coverBlock
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.Save(outPath)
|
||||||
|
}
|
131
internal/ffmpeg/options.go
Normal file
131
internal/ffmpeg/options.go
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
package ffmpeg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ffmpegBuilder struct {
|
||||||
|
binary string // ffmpeg binary path
|
||||||
|
options map[string]string // global options
|
||||||
|
inputs []*inputBuilder // input options
|
||||||
|
outputs []*outputBuilder // output options
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFFmpegBuilder() *ffmpegBuilder {
|
||||||
|
return &ffmpegBuilder{
|
||||||
|
binary: "ffmpeg",
|
||||||
|
options: make(map[string]string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) AddInput(src *inputBuilder) {
|
||||||
|
b.inputs = append(b.inputs, src)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) AddOutput(dst *outputBuilder) {
|
||||||
|
b.outputs = append(b.outputs, dst)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) SetBinary(bin string) {
|
||||||
|
b.binary = bin
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) SetFlag(flag string) {
|
||||||
|
b.options[flag] = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) SetOption(name, value string) {
|
||||||
|
b.options[name] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) Args() (args []string) {
|
||||||
|
for name, val := range b.options {
|
||||||
|
args = append(args, "-"+name)
|
||||||
|
if val != "" {
|
||||||
|
args = append(args, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, input := range b.inputs {
|
||||||
|
args = append(args, input.Args()...)
|
||||||
|
}
|
||||||
|
for _, output := range b.outputs {
|
||||||
|
args = append(args, output.Args()...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ffmpegBuilder) Command(ctx context.Context) *exec.Cmd {
|
||||||
|
bin := "ffmpeg"
|
||||||
|
if b.binary != "" {
|
||||||
|
bin = b.binary
|
||||||
|
}
|
||||||
|
|
||||||
|
return exec.CommandContext(ctx, bin, b.Args()...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// inputBuilder is the builder for ffmpeg input options
|
||||||
|
type inputBuilder struct {
|
||||||
|
path string
|
||||||
|
options map[string][]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newInputBuilder(path string) *inputBuilder {
|
||||||
|
return &inputBuilder{
|
||||||
|
path: path,
|
||||||
|
options: make(map[string][]string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *inputBuilder) AddOption(name, value string) {
|
||||||
|
b.options[name] = append(b.options[name], value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *inputBuilder) Args() (args []string) {
|
||||||
|
for name, values := range b.options {
|
||||||
|
for _, val := range values {
|
||||||
|
args = append(args, "-"+name, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return append(args, "-i", b.path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// outputBuilder is the builder for ffmpeg output options
|
||||||
|
type outputBuilder struct {
|
||||||
|
path string
|
||||||
|
options map[string][]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newOutputBuilder(path string) *outputBuilder {
|
||||||
|
return &outputBuilder{
|
||||||
|
path: path,
|
||||||
|
options: make(map[string][]string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *outputBuilder) AddOption(name, value string) {
|
||||||
|
b.options[name] = append(b.options[name], value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *outputBuilder) Args() (args []string) {
|
||||||
|
for name, values := range b.options {
|
||||||
|
for _, val := range values {
|
||||||
|
args = append(args, "-"+name, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return append(args, b.path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddMetadata is the shortcut for adding "metadata" option
|
||||||
|
func (b *outputBuilder) AddMetadata(stream, key, value string) {
|
||||||
|
optVal := strings.TrimSpace(key) + "=" + strings.TrimSpace(value)
|
||||||
|
|
||||||
|
if stream != "" {
|
||||||
|
b.AddOption("metadata:"+stream, optVal)
|
||||||
|
} else {
|
||||||
|
b.AddOption("metadata", optVal)
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,11 @@
|
|||||||
package sniff
|
package sniff
|
||||||
|
|
||||||
import "bytes"
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
|
)
|
||||||
|
|
||||||
type Sniffer interface {
|
type Sniffer interface {
|
||||||
Sniff(header []byte) bool
|
Sniff(header []byte) bool
|
||||||
@ -8,7 +13,7 @@ type Sniffer interface {
|
|||||||
|
|
||||||
var audioExtensions = map[string]Sniffer{
|
var audioExtensions = map[string]Sniffer{
|
||||||
// ref: https://mimesniff.spec.whatwg.org
|
// ref: https://mimesniff.spec.whatwg.org
|
||||||
".mp3": prefixSniffer("ID3"),
|
".mp3": prefixSniffer("ID3"), // todo: check mp3 without ID3v2 tag
|
||||||
".ogg": prefixSniffer("OggS"),
|
".ogg": prefixSniffer("OggS"),
|
||||||
".wav": prefixSniffer("RIFF"),
|
".wav": prefixSniffer("RIFF"),
|
||||||
|
|
||||||
@ -19,8 +24,8 @@ var audioExtensions = map[string]Sniffer{
|
|||||||
},
|
},
|
||||||
|
|
||||||
// ref: https://www.garykessler.net/library/file_sigs.html
|
// ref: https://www.garykessler.net/library/file_sigs.html
|
||||||
".m4a": mpeg4Sniffer{}, // MPEG-4 container, m4a treat as audio
|
".m4a": m4aSniffer{}, // MPEG-4 container, Apple Lossless Audio Codec
|
||||||
".aac": prefixSniffer{0xFF, 0xF1}, // MPEG-4 AAC-LC
|
".mp4": &mpeg4Sniffer{}, // MPEG-4 container, other fallback
|
||||||
|
|
||||||
".flac": prefixSniffer("fLaC"), // ref: https://xiph.org/flac/format.html
|
".flac": prefixSniffer("fLaC"), // ref: https://xiph.org/flac/format.html
|
||||||
".dff": prefixSniffer("FRM8"), // DSDIFF, ref: https://www.sonicstudio.com/pdf/dsd/DSDIFF_1.5_Spec.pdf
|
".dff": prefixSniffer("FRM8"), // DSDIFF, ref: https://www.sonicstudio.com/pdf/dsd/DSDIFF_1.5_Spec.pdf
|
||||||
@ -54,8 +59,48 @@ func (s prefixSniffer) Sniff(header []byte) bool {
|
|||||||
return bytes.HasPrefix(header, s)
|
return bytes.HasPrefix(header, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type m4aSniffer struct{}
|
||||||
|
|
||||||
|
func (m4aSniffer) Sniff(header []byte) bool {
|
||||||
|
box := readMpeg4FtypBox(header)
|
||||||
|
if box == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return box.majorBrand == "M4A " || slices.Contains(box.compatibleBrands, "M4A ")
|
||||||
|
}
|
||||||
|
|
||||||
type mpeg4Sniffer struct{}
|
type mpeg4Sniffer struct{}
|
||||||
|
|
||||||
func (mpeg4Sniffer) Sniff(header []byte) bool {
|
func (s *mpeg4Sniffer) Sniff(header []byte) bool {
|
||||||
return len(header) >= 8 && bytes.Equal([]byte("ftyp"), header[4:8])
|
return readMpeg4FtypBox(header) != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type mpeg4FtpyBox struct {
|
||||||
|
majorBrand string
|
||||||
|
minorVersion uint32
|
||||||
|
compatibleBrands []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func readMpeg4FtypBox(header []byte) *mpeg4FtpyBox {
|
||||||
|
if (len(header) < 8) || !bytes.Equal([]byte("ftyp"), header[4:8]) {
|
||||||
|
return nil // not a valid ftyp box
|
||||||
|
}
|
||||||
|
|
||||||
|
size := binary.BigEndian.Uint32(header[0:4]) // size
|
||||||
|
if size < 16 || size%4 != 0 {
|
||||||
|
return nil // invalid ftyp box
|
||||||
|
}
|
||||||
|
|
||||||
|
box := mpeg4FtpyBox{
|
||||||
|
majorBrand: string(header[8:12]),
|
||||||
|
minorVersion: binary.BigEndian.Uint32(header[12:16]),
|
||||||
|
}
|
||||||
|
|
||||||
|
// compatible brands
|
||||||
|
for i := 16; i < int(size) && i+4 < len(header); i += 4 {
|
||||||
|
box.compatibleBrands = append(box.compatibleBrands, string(header[i:i+4]))
|
||||||
|
}
|
||||||
|
|
||||||
|
return &box
|
||||||
}
|
}
|
||||||
|
30
internal/sniff/image.go
Normal file
30
internal/sniff/image.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package sniff
|
||||||
|
|
||||||
|
// ref: https://mimesniff.spec.whatwg.org
|
||||||
|
var imageMIMEs = map[string]Sniffer{
|
||||||
|
"image/jpeg": prefixSniffer{0xFF, 0xD8, 0xFF},
|
||||||
|
"image/png": prefixSniffer{0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n'},
|
||||||
|
"image/bmp": prefixSniffer("BM"),
|
||||||
|
"image/webp": prefixSniffer("RIFF"),
|
||||||
|
"image/gif": prefixSniffer("GIF8"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// ImageMIME sniffs the well-known image types, and returns its MIME.
|
||||||
|
func ImageMIME(header []byte) (string, bool) {
|
||||||
|
for ext, sniffer := range imageMIMEs {
|
||||||
|
if sniffer.Sniff(header) {
|
||||||
|
return ext, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ImageExtension is equivalent to ImageMIME, but returns file extension
|
||||||
|
func ImageExtension(header []byte) (string, bool) {
|
||||||
|
ext, ok := ImageMIME(header)
|
||||||
|
if !ok {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
// todo: use mime.ExtensionsByType
|
||||||
|
return "." + ext[6:], true // "image/" is 6 bytes
|
||||||
|
}
|
24
internal/utils/temp.go
Normal file
24
internal/utils/temp.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func WriteTempFile(rd io.Reader, ext string) (string, error) {
|
||||||
|
audioFile, err := os.CreateTemp("", "*"+ext)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("ffmpeg create temp file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.Copy(audioFile, rd); err != nil {
|
||||||
|
return "", fmt.Errorf("ffmpeg write temp file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := audioFile.Close(); err != nil {
|
||||||
|
return "", fmt.Errorf("ffmpeg close temp file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return audioFile.Name(), nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user