Compare commits
10 Commits
532a34fb63
...
98365d7859
| Author | SHA1 | Date | |
|---|---|---|---|
| 98365d7859 | |||
| 161cbdfebf | |||
| 415172733f | |||
| 0e3e655939 | |||
| 6e1fd62247 | |||
| 5d54f8eb12 | |||
| 2cd8d0dc37 | |||
| d9abb0b18b | |||
| 0c097d63a6 | |||
| a8028bdc28 |
2
.github/FUNDING.yml
vendored
Normal file
2
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
github: [MoeCinnamo]
|
||||
custom: ["https://afdian.com/a/MoeCinnamo"]
|
||||
57
.github/ISSUE_TEMPLATE/bug.yml
vendored
Normal file
57
.github/ISSUE_TEMPLATE/bug.yml
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
name: 🐞 Report error
|
||||
description: Report an error (bug), please first check the FAQ and search the Issue list for the issue you want to raise.
|
||||
title: "[Bug] "
|
||||
body:
|
||||
- type: checkboxes
|
||||
id: check-answer
|
||||
attributes:
|
||||
label: Solution check
|
||||
description: Please ensure that you have completed all of the following operations.
|
||||
options:
|
||||
- label: I have searched [Issues](https://github.com/OmniX-Space/MeowBox-Core/issues).However, no similar issues were found.
|
||||
required: true
|
||||
- type: textarea
|
||||
id: expected-behavior
|
||||
attributes:
|
||||
label: Expected behavior
|
||||
description: A clear and concise description of what is expected to happen
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: actual-behavior
|
||||
attributes:
|
||||
label: Actual behavior
|
||||
description: A clear and concise description of what actually happened.
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
id: version
|
||||
attributes:
|
||||
label: MeowBox-Core version
|
||||
description: What version of MeowBox-Core are you using?
|
||||
placeholder: e.g. v0.0.2
|
||||
validations:
|
||||
required: true
|
||||
- type: input
|
||||
id: last-known-working-version
|
||||
attributes:
|
||||
label: The final normal version
|
||||
description: If so, please fill in the final normal version here.
|
||||
placeholder: e.g. v0.0.1
|
||||
- type: input
|
||||
id: operating-system-version
|
||||
attributes:
|
||||
label: Operating system version
|
||||
description: |
|
||||
What version of operating system are you using?
|
||||
On macOS, click on "Apple Menu > About This Machine";
|
||||
On Linux, execute the `lsc_release` or `uname - a` command;
|
||||
On Windows, click the Start button > Settings > System > About.
|
||||
placeholder: "e.g. macOS 11.2.3, Windows 10 20H2, Debian 12.1.0"
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: additional-information
|
||||
attributes:
|
||||
label: Additional information
|
||||
description: If your issue needs further explanation or you are facing a difficult-to-reproduce issue, please add more information here. (You can directly drag and drop images or videos into the text box)
|
||||
36
.github/ISSUE_TEMPLATE/feature.yml
vendored
Normal file
36
.github/ISSUE_TEMPLATE/feature.yml
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
name: ✨ Function request
|
||||
description: To come up with an idea for this project, please first review the frequently asked questions and search the Issue list to see if there are any issues you want to raise.
|
||||
title: "[Feature] "
|
||||
body:
|
||||
- type: checkboxes
|
||||
id: check-answer
|
||||
attributes:
|
||||
label: Solution check
|
||||
description: Please ensure that you have completed all of the following operations.
|
||||
options:
|
||||
- label: I have searched [Issues](https://github.com/OmniX-Space/MeowBox-Core/issues).However, no similar issues were found.
|
||||
required: true
|
||||
- type: textarea
|
||||
id: problem-description
|
||||
attributes:
|
||||
label: Problem description
|
||||
description: Please add a clear and concise description of the problem you want to solve.
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: proposed-solution
|
||||
attributes:
|
||||
label: Describe the solution you want
|
||||
description: Briefly and clearly describe what you are about to happen
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: alternatives-considered
|
||||
attributes:
|
||||
label: Describe the alternative solutions you have considered
|
||||
description: A concise and clear description of all alternative solutions or features you have considered
|
||||
- type: textarea
|
||||
id: additional-information
|
||||
attributes:
|
||||
label: Additional information
|
||||
description: If your question requires further explanation or if you would like to express other content, please add more information here. (Simply drag the image/video to the editing box to add it)
|
||||
18
.github/workflows/test-and-build.yml
vendored
18
.github/workflows/test-and-build.yml
vendored
@@ -16,11 +16,16 @@ on:
|
||||
jobs:
|
||||
build:
|
||||
runs-on: self-hosted
|
||||
permissions:
|
||||
contents: write
|
||||
actions: write
|
||||
pull-requests: write
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: v0.0.2-dev
|
||||
fetch-depth: 0
|
||||
- name: Test code
|
||||
run: |
|
||||
echo "Testing code"
|
||||
@@ -70,6 +75,13 @@ jobs:
|
||||
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w" -trimpath -o MeowEmbeddedMusicServer-Darwin-amd64 .
|
||||
echo "Build darwin arm64 binary"
|
||||
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags "-s -w" -trimpath -o MeowEmbeddedMusicServer-Darwin-arm64 .
|
||||
- name: Get latest tag
|
||||
id: get_latest_tag
|
||||
run: |
|
||||
git fetch --tags
|
||||
LATEST_TAG=$(git tag --sort=-refname | head -n 1)
|
||||
echo "Latest tag found: $LATEST_TAG"
|
||||
echo "previous_tag=$LATEST_TAG" >> $GITHUB_OUTPUT
|
||||
- name: Upload binaries
|
||||
uses: softprops/action-gh-release@v1
|
||||
with:
|
||||
@@ -90,7 +102,7 @@ jobs:
|
||||
- macOS amd64
|
||||
- macOS arm64
|
||||
|
||||
**Full Changelog**: https://github.com/OmniX-Space/MeowBox-Core/compare/v0.0.1...${{ github.sha }}
|
||||
**Full Changelog**: https://github.com/OmniX-Space/MeowBox-Core/compare/${{ steps.get_latest_tag.outputs.previous_tag }}...${{ github.sha }}
|
||||
files: |
|
||||
MeowEmbeddedMusicServer-Linux-i386
|
||||
MeowEmbeddedMusicServer-Linux-amd64
|
||||
@@ -103,6 +115,6 @@ jobs:
|
||||
prerelease: ${{ github.event.inputs.build_type == 'pre' }}
|
||||
make_latest: ${{ github.event.inputs.build_type != 'pre' }}
|
||||
env:
|
||||
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
GITHUB_TOKEN: ${{ secrets.CUSTOM_GITHUB_TOKEN }}
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
# GITHUB_TOKEN: ${{ secrets.CUSTOM_GITHUB_TOKEN }}
|
||||
continue-on-error: true
|
||||
28
api.go
28
api.go
@@ -165,13 +165,17 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// If still not found, request and cache the music item in a separate goroutine
|
||||
// 直接进行流式播放
|
||||
if !found {
|
||||
encodedSong := url.QueryEscape(song)
|
||||
encodedSinger := url.QueryEscape(singer)
|
||||
streamURL := scheme + "://" + r.Host + "/stream_live?song=" + encodedSong + "&singer=" + encodedSinger
|
||||
fmt.Println("[Info] Updating music item cache from API request.")
|
||||
musicItem = requestAndCacheMusic(song, singer)
|
||||
fmt.Println("[Info] Music item cache updated.")
|
||||
musicItem.FromCache = false
|
||||
musicItem.AudioURL = scheme + "://" + r.Host + musicItem.AudioURL
|
||||
musicItem.AudioFullURL = scheme + "://" + r.Host + musicItem.AudioFullURL
|
||||
musicItem.AudioURL = streamURL
|
||||
musicItem.AudioFullURL = streamURL
|
||||
musicItem.M3U8URL = scheme + "://" + r.Host + musicItem.M3U8URL
|
||||
musicItem.LyricURL = scheme + "://" + r.Host + musicItem.LyricURL
|
||||
musicItem.CoverURL = scheme + "://" + r.Host + musicItem.CoverURL
|
||||
@@ -188,7 +192,9 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
|
||||
musicItem.IP = ip
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(musicItem)
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetEscapeHTML(false)
|
||||
encoder.Encode(musicItem)
|
||||
}
|
||||
|
||||
// streamLiveHandler 实时流式转码接口 - 边下载边播放,无需等待!
|
||||
@@ -196,18 +202,18 @@ func streamLiveHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// 设置 CORS 和音频相关头
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Accept-Ranges", "bytes")
|
||||
|
||||
|
||||
queryParams := r.URL.Query()
|
||||
song := queryParams.Get("song")
|
||||
singer := queryParams.Get("singer")
|
||||
|
||||
|
||||
fmt.Printf("[Stream Live] Request: song=%s, singer=%s\n", song, singer)
|
||||
|
||||
|
||||
if song == "" {
|
||||
http.Error(w, "Missing song parameter", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// 1. 检查缓存是否存在
|
||||
dirName := fmt.Sprintf("./files/cache/music/%s-%s", singer, song)
|
||||
cachedFile := filepath.Join(dirName, "music.mp3")
|
||||
@@ -218,19 +224,19 @@ func streamLiveHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, cachedFile)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// 2. 缓存不存在,获取远程URL并实时流式转码
|
||||
fmt.Printf("[Stream Live] Cache miss, fetching from API...\n")
|
||||
|
||||
|
||||
// 调用枫雨API获取远程音乐URL(不下载,只获取URL)
|
||||
remoteURL := getRemoteMusicURLOnly(song, singer)
|
||||
if remoteURL == "" {
|
||||
http.Error(w, "Failed to get remote music URL", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
fmt.Printf("[Stream Live] Starting live stream from: %s\n", remoteURL)
|
||||
|
||||
|
||||
// 4. 实时流式转码
|
||||
if err := streamConvertToWriter(remoteURL, w); err != nil {
|
||||
fmt.Printf("[Stream Live] Error: %v\n", err)
|
||||
|
||||
202
file.go
202
file.go
@@ -1,14 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ListFiles function: Traverse all files in the specified directory and return a slice of the file path
|
||||
@@ -54,10 +52,17 @@ func GetFileContent(filePath string) ([]byte, error) {
|
||||
// fileHandler function: Handle file requests
|
||||
func fileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Server", "MeowMusicEmbeddedServer")
|
||||
// Obtain the path of the request
|
||||
filePath := r.URL.Path
|
||||
|
||||
// Check if the request path starts with "/url/"
|
||||
// 提前URL解码
|
||||
decodedPath, decodeErr := url.QueryUnescape(filePath)
|
||||
if decodeErr == nil {
|
||||
// 兼容历史数据:将+替换为空格(仅当解码成功时)
|
||||
decodedPath = strings.ReplaceAll(decodedPath, "+", " ")
|
||||
filePath = decodedPath // 后续统一使用解码后路径
|
||||
}
|
||||
|
||||
// 处理 /url/ 远程请求(保持不变)
|
||||
if strings.HasPrefix(filePath, "/url/") {
|
||||
// Extract the URL after "/url/"
|
||||
urlPath := filePath[len("/url/"):]
|
||||
@@ -102,161 +107,54 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
NotFoundHandler(w, r)
|
||||
return
|
||||
}
|
||||
// Set appropriate Content-Type based on file extension
|
||||
ext := filepath.Ext(decodedURL)
|
||||
switch ext {
|
||||
case ".mp3":
|
||||
w.Header().Set("Content-Type", "audio/mpeg")
|
||||
case ".wav":
|
||||
w.Header().Set("Content-Type", "audio/wav")
|
||||
case ".flac":
|
||||
w.Header().Set("Content-Type", "audio/flac")
|
||||
case ".aac":
|
||||
w.Header().Set("Content-Type", "audio/aac")
|
||||
case ".ogg":
|
||||
w.Header().Set("Content-Type", "audio/ogg")
|
||||
case ".m4a":
|
||||
w.Header().Set("Content-Type", "audio/mp4")
|
||||
case ".amr":
|
||||
w.Header().Set("Content-Type", "audio/amr")
|
||||
case ".jpg", ".jpeg":
|
||||
w.Header().Set("Content-Type", "image/jpeg")
|
||||
case ".png":
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
case ".gif":
|
||||
w.Header().Set("Content-Type", "image/gif")
|
||||
case ".bmp":
|
||||
w.Header().Set("Content-Type", "image/bmp")
|
||||
case ".svg":
|
||||
w.Header().Set("Content-Type", "image/svg+xml")
|
||||
case ".webp":
|
||||
w.Header().Set("Content-Type", "image/webp")
|
||||
case ".txt":
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
case ".lrc":
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
case ".mrc":
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
case ".json":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
default:
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
}
|
||||
setContentType(w, decodedURL)
|
||||
// Write file content to response
|
||||
w.Write(fileContent)
|
||||
return
|
||||
}
|
||||
|
||||
// Construct the complete file path
|
||||
fullFilePath := filepath.Join("./files", filePath)
|
||||
// 统一使用解码后路径
|
||||
fullPath := filepath.Join("./files", filePath)
|
||||
fileContent, err := GetFileContent(fullPath)
|
||||
|
||||
// Try replacing '+' with ' ' and check if the file exists
|
||||
tempFilePath := strings.ReplaceAll(fullFilePath, "+", " ")
|
||||
if _, err := os.Stat(tempFilePath); err == nil {
|
||||
fullFilePath = tempFilePath
|
||||
// 特殊处理空music.mp3
|
||||
isEmptyMusic := (err == nil && len(fileContent) == 0 && strings.HasSuffix(filePath, "/music.mp3"))
|
||||
if err != nil || isEmptyMusic {
|
||||
// 没有/空的music.mp3文件,直接返回404
|
||||
NotFoundHandler(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// Get file content
|
||||
fileContent, err := GetFileContent(fullFilePath)
|
||||
|
||||
// 检查是否为空文件(特别是 music.mp3,如果是空的也视为不存在,需要等待转码)
|
||||
if err == nil && len(fileContent) == 0 && strings.HasSuffix(filePath, "/music.mp3") {
|
||||
err = fmt.Errorf("file is empty")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
// If file not found, try replacing ' ' with '+' and check again
|
||||
tempFilePath = strings.ReplaceAll(fullFilePath, " ", "+")
|
||||
fileContent, err = GetFileContent(tempFilePath)
|
||||
|
||||
// 同样检查带 + 的路径是否为空
|
||||
if err == nil && len(fileContent) == 0 && strings.HasSuffix(filePath, "/music.mp3") {
|
||||
err = fmt.Errorf("file is empty")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
// 特殊处理:如果请求的是缓存中的文件,等待后台处理完成
|
||||
fmt.Printf("[Web Access] File not found, checking path prefix: %s\n", filePath)
|
||||
if strings.HasPrefix(filePath, "/cache/music/") {
|
||||
// music.mp3 等待最多 60 秒,歌词等待最多 10 秒
|
||||
maxWait := 10
|
||||
if strings.HasSuffix(filePath, "/music.mp3") {
|
||||
maxWait = 60
|
||||
}
|
||||
|
||||
// URL 解码路径(处理中文文件名)
|
||||
decodedPath, _ := url.QueryUnescape(filePath)
|
||||
decodedPath = strings.ReplaceAll(decodedPath, "+", " ") // + 转空格
|
||||
decodedFullPath := filepath.Join("./files", decodedPath)
|
||||
|
||||
fmt.Printf("[Web Access] Waiting for file: %s (max %d seconds)\n", decodedFullPath, maxWait)
|
||||
for i := 0; i < maxWait; i++ {
|
||||
time.Sleep(1 * time.Second)
|
||||
// 检查 URL 解码后的路径
|
||||
if _, err := os.Stat(decodedFullPath); err == nil {
|
||||
fmt.Printf("[Web Access] File ready after %d seconds: %s\n", i+1, decodedFullPath)
|
||||
http.ServeFile(w, r, decodedFullPath)
|
||||
return
|
||||
}
|
||||
// 检查原始路径
|
||||
if _, err := os.Stat(fullFilePath); err == nil {
|
||||
http.ServeFile(w, r, fullFilePath)
|
||||
return
|
||||
}
|
||||
// 检查带 + 的路径
|
||||
tempPath := strings.ReplaceAll(fullFilePath, " ", "+")
|
||||
if _, err := os.Stat(tempPath); err == nil {
|
||||
http.ServeFile(w, r, tempPath)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
NotFoundHandler(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Set appropriate Content-Type based on file extension
|
||||
ext := filepath.Ext(filePath)
|
||||
switch ext {
|
||||
case ".mp3":
|
||||
w.Header().Set("Content-Type", "audio/mpeg")
|
||||
case ".wav":
|
||||
w.Header().Set("Content-Type", "audio/wav")
|
||||
case ".flac":
|
||||
w.Header().Set("Content-Type", "audio/flac")
|
||||
case ".aac":
|
||||
w.Header().Set("Content-Type", "audio/aac")
|
||||
case ".ogg":
|
||||
w.Header().Set("Content-Type", "audio/ogg")
|
||||
case ".m4a":
|
||||
w.Header().Set("Content-Type", "audio/mp4")
|
||||
case ".amr":
|
||||
w.Header().Set("Content-Type", "audio/amr")
|
||||
case ".jpg", ".jpeg":
|
||||
w.Header().Set("Content-Type", "image/jpeg")
|
||||
case ".png":
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
case ".gif":
|
||||
w.Header().Set("Content-Type", "image/gif")
|
||||
case ".bmp":
|
||||
w.Header().Set("Content-Type", "image/bmp")
|
||||
case ".svg":
|
||||
w.Header().Set("Content-Type", "image/svg+xml")
|
||||
case ".webp":
|
||||
w.Header().Set("Content-Type", "image/webp")
|
||||
case ".txt":
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
case ".lrc":
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
case ".mrc":
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
case ".json":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
default:
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
}
|
||||
|
||||
// Write file content to response
|
||||
// 避免重复Content-Type设置
|
||||
setContentType(w, filePath)
|
||||
w.Write(fileContent)
|
||||
}
|
||||
|
||||
func setContentType(w http.ResponseWriter, path string) {
|
||||
ext := strings.ToLower(filepath.Ext(path))
|
||||
contentTypes := map[string]string{
|
||||
".mp3": "audio/mpeg",
|
||||
".wav": "audio/wav",
|
||||
".flac": "audio/flac",
|
||||
".aac": "audio/aac",
|
||||
".ogg": "audio/ogg",
|
||||
".m4a": "audio/mp4",
|
||||
".amr": "audio/amr",
|
||||
".jpg": "image/jpeg",
|
||||
".jpeg": "image/jpeg",
|
||||
".png": "image/png",
|
||||
".gif": "image/gif",
|
||||
".bmp": "image/bmp",
|
||||
".svg": "image/svg+xml",
|
||||
".webp": "image/webp",
|
||||
".txt": "text/plain",
|
||||
".lrc": "text/plain",
|
||||
".mrc": "text/plain",
|
||||
".json": "application/json",
|
||||
}
|
||||
if ct, ok := contentTypes[ext]; ok {
|
||||
w.Header().Set("Content-Type", ct)
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
}
|
||||
}
|
||||
|
||||
0
files/cache/music/.gitignore
vendored
0
files/cache/music/.gitignore
vendored
@@ -1 +1,6 @@
|
||||
{}
|
||||
{
|
||||
"favorite": {
|
||||
"name": "我喜欢",
|
||||
"songs": []
|
||||
}
|
||||
}
|
||||
27
helper.go
27
helper.go
@@ -85,7 +85,7 @@ func readFromCache(path string) (MusicItem, bool) {
|
||||
if err != nil || !info.IsDir() {
|
||||
return MusicItem{}, false
|
||||
}
|
||||
|
||||
|
||||
dirName := filepath.Base(path)
|
||||
parts := strings.SplitN(dirName, "-", 2)
|
||||
var artist, title string
|
||||
@@ -95,7 +95,7 @@ func readFromCache(path string) (MusicItem, bool) {
|
||||
} else {
|
||||
title = dirName
|
||||
}
|
||||
|
||||
|
||||
return getLocalMusicItem(title, artist), true
|
||||
}
|
||||
|
||||
@@ -115,10 +115,10 @@ func requestAndCacheMusic(song, singer string) MusicItem {
|
||||
// 直接从远程URL流式转码(边下载边转码,超快!)
|
||||
func streamConvertAudio(inputURL, outputFile string) error {
|
||||
fmt.Printf("[Info] Stream converting from URL (fast mode)\n")
|
||||
|
||||
|
||||
// 先写入临时文件,完成后再重命名(避免读取到不完整的文件)
|
||||
tempFile := outputFile + ".tmp"
|
||||
|
||||
|
||||
// ffmpeg 直接读取远程 URL 并转码
|
||||
// -t 600: 只下载前10分钟,减少80%下载量!
|
||||
// 移除 reconnect 参数,避免兼容性问题
|
||||
@@ -130,14 +130,14 @@ func streamConvertAudio(inputURL, outputFile string) error {
|
||||
"-ac", "1", "-ar", "24000", "-b:a", "32k", "-q:a", "9",
|
||||
"-bufsize", "64k",
|
||||
tempFile)
|
||||
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Printf("[Error] Stream convert failed: %v\n", err)
|
||||
os.Remove(tempFile) // 清理临时文件
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
// 检查生成的文件大小
|
||||
fileInfo, err := os.Stat(tempFile)
|
||||
if err != nil || fileInfo.Size() < 1024 {
|
||||
@@ -152,7 +152,7 @@ func streamConvertAudio(inputURL, outputFile string) error {
|
||||
fmt.Printf("[Error] Failed to rename temp file: %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
fmt.Printf("[Success] Stream convert completed: %s\n", outputFile)
|
||||
return nil
|
||||
}
|
||||
@@ -160,30 +160,31 @@ func streamConvertAudio(inputURL, outputFile string) error {
|
||||
// 实时流式转码到 HTTP Writer(边下载边播放!)
|
||||
func streamConvertToWriter(inputURL string, w http.ResponseWriter) error {
|
||||
fmt.Printf("[Info] Live streaming from URL: %s\n", inputURL)
|
||||
|
||||
|
||||
// ffmpeg 边下载边转码,输出到 stdout
|
||||
cmd := exec.Command("ffmpeg",
|
||||
"-i", inputURL,
|
||||
"-threads", "0",
|
||||
"-ac", "1", "-ar", "24000", "-b:a", "32k", "-q:a", "9",
|
||||
"-f", "mp3",
|
||||
"-map_metadata", "-1",
|
||||
"pipe:1") // 输出到 stdout
|
||||
|
||||
|
||||
// 获取 stdout pipe
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get stdout pipe: %v", err)
|
||||
}
|
||||
|
||||
|
||||
// 启动 ffmpeg
|
||||
if err := cmd.Start(); err != nil {
|
||||
return fmt.Errorf("failed to start ffmpeg: %v", err)
|
||||
}
|
||||
|
||||
|
||||
// 设置响应头
|
||||
w.Header().Set("Content-Type", "audio/mpeg")
|
||||
// 移除 Transfer-Encoding: chunked,让 Go 自动处理
|
||||
|
||||
|
||||
// 边读边写到 HTTP response
|
||||
buf := make([]byte, 8192)
|
||||
for {
|
||||
@@ -198,7 +199,7 @@ func streamConvertToWriter(inputURL string, w http.ResponseWriter) error {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
cmd.Wait()
|
||||
fmt.Printf("[Success] Live streaming completed\n")
|
||||
return nil
|
||||
|
||||
8
index.go
8
index.go
@@ -11,7 +11,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Server", "MeowMusicEmbeddedServer")
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
fmt.Printf("[Web Access] Handling request for %s\n", r.URL.Path)
|
||||
|
||||
|
||||
// Serve full music app for both / and /app
|
||||
if r.URL.Path == "/" || r.URL.Path == "/app" {
|
||||
appPath := filepath.Join("theme", "full-app.html")
|
||||
@@ -21,7 +21,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Test version available at /test
|
||||
if r.URL.Path == "/test" {
|
||||
testPath := filepath.Join("theme", "test-app.html")
|
||||
@@ -31,7 +31,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Access classic interface via /classic
|
||||
if r.URL.Path == "/classic" {
|
||||
indexPath := filepath.Join("theme", "index.html")
|
||||
@@ -43,7 +43,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
defaultIndexPage(w)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
if r.URL.Path != "/" {
|
||||
fileHandler(w, r)
|
||||
return
|
||||
|
||||
@@ -29,14 +29,14 @@ type YuafengAPIFreeResponse struct {
|
||||
// 枫雨API response handler with multiple API fallback
|
||||
func YuafengAPIResponseHandler(sources, song, singer string) MusicItem {
|
||||
fmt.Printf("[Info] Fetching music data for %s by %s\n", song, singer)
|
||||
|
||||
|
||||
// API hosts to try in order
|
||||
apiHosts := []string{
|
||||
"https://api.yuafeng.cn",
|
||||
"https://api-v2.yuafeng.cn",
|
||||
"https://api.yaohud.cn",
|
||||
}
|
||||
|
||||
|
||||
var pathSuffix string
|
||||
switch sources {
|
||||
case "kuwo":
|
||||
@@ -50,9 +50,9 @@ func YuafengAPIResponseHandler(sources, song, singer string) MusicItem {
|
||||
default:
|
||||
return MusicItem{}
|
||||
}
|
||||
|
||||
|
||||
var fallbackItem MusicItem // 保存第一个有音乐但没歌词的结果
|
||||
|
||||
|
||||
// Try each API host - 尝试所有API直到找到歌词
|
||||
for i, host := range apiHosts {
|
||||
fmt.Printf("[Info] Trying API %d/%d: %s\n", i+1, len(apiHosts), host)
|
||||
@@ -74,13 +74,13 @@ func YuafengAPIResponseHandler(sources, song, singer string) MusicItem {
|
||||
fmt.Printf("[Warning] × API %s failed, trying next...\n", host)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 所有API都试完了
|
||||
if fallbackItem.Title != "" {
|
||||
fmt.Println("[Info] ▶ All 3 APIs tried - no lyrics found, returning music without lyrics")
|
||||
return fallbackItem
|
||||
}
|
||||
|
||||
|
||||
fmt.Println("[Error] ✗ All 3 APIs failed completely")
|
||||
return MusicItem{}
|
||||
}
|
||||
@@ -98,17 +98,17 @@ func tryFetchFromAPI(APIurl, song, singer string) MusicItem {
|
||||
fmt.Println("[Error] Error reading the response body:", err)
|
||||
return MusicItem{}
|
||||
}
|
||||
|
||||
|
||||
// Check if response is HTML (starts with < character)
|
||||
bodyStr := string(body)
|
||||
if len(bodyStr) > 0 && bodyStr[0] == '<' {
|
||||
fmt.Println("[Warning] API returned HTML instead of JSON")
|
||||
fmt.Printf("[Debug] Saving HTML response to debug.html for inspection\n")
|
||||
|
||||
|
||||
// Save HTML to file for debugging
|
||||
os.WriteFile("debug_api_response.html", body, 0644)
|
||||
fmt.Println("[Info] HTML response saved to debug_api_response.html")
|
||||
|
||||
|
||||
// Try to extract JSON from HTML if embedded
|
||||
// Look for common patterns where JSON might be embedded
|
||||
if strings.Contains(bodyStr, `"song"`) && strings.Contains(bodyStr, `"singer"`) {
|
||||
@@ -127,11 +127,11 @@ func tryFetchFromAPI(APIurl, song, singer string) MusicItem {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fmt.Println("[Error] Cannot parse HTML response - API may be unavailable")
|
||||
return MusicItem{}
|
||||
}
|
||||
|
||||
|
||||
parseSuccess:
|
||||
var response YuafengAPIFreeResponse
|
||||
err = json.Unmarshal(body, &response)
|
||||
@@ -155,17 +155,17 @@ parseSuccess:
|
||||
|
||||
// 获取封面扩展名
|
||||
ext := filepath.Ext(response.Data.Cover)
|
||||
|
||||
|
||||
// 保存远程 URL 到文件,供 file.go 流式转码使用
|
||||
remoteURLFile := filepath.Join(dirName, "remote_url.txt")
|
||||
os.WriteFile(remoteURLFile, []byte(response.Data.Music), 0644)
|
||||
|
||||
|
||||
// ========== 关键优化:先返回,后台异步处理 ==========
|
||||
// 把下载、转码等耗时操作放到 goroutine 异步执行
|
||||
go func() {
|
||||
fmt.Printf("[Async] Starting background processing for: %s - %s\n", response.Data.Singer, response.Data.Song)
|
||||
var wg sync.WaitGroup
|
||||
|
||||
|
||||
// ========== 1. 歌词处理 ==========
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@@ -199,14 +199,14 @@ parseSuccess:
|
||||
downloadFile(filepath.Join(dirName, "lyric.lrc"), lyricData)
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
// ========== 2. 封面处理 ==========
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
downloadFile(filepath.Join(dirName, "cover"+ext), response.Data.Cover)
|
||||
}()
|
||||
|
||||
|
||||
// ========== 3. 音频转码 ==========
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@@ -216,7 +216,7 @@ parseSuccess:
|
||||
fmt.Println("[Async Warning] Cannot identify music format, using default .mp3:", err)
|
||||
musicExt = ".mp3"
|
||||
}
|
||||
|
||||
|
||||
outputMp3 := filepath.Join(dirName, "music.mp3")
|
||||
err = streamConvertAudio(response.Data.Music, outputMp3)
|
||||
if err != nil {
|
||||
@@ -228,7 +228,7 @@ parseSuccess:
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
wg.Wait() // 等待所有任务完成
|
||||
fmt.Printf("[Async] Background processing completed for: %s - %s\n", response.Data.Singer, response.Data.Song)
|
||||
}()
|
||||
@@ -236,7 +236,7 @@ parseSuccess:
|
||||
// ========== 立即返回 JSON,使用标准 .mp3 URL ==========
|
||||
// 注意:返回标准的 .mp3 URL,file.go 会在文件不存在时自动触发流式转码
|
||||
basePath := "/cache/music/" + url.QueryEscape(response.Data.Singer+"-"+response.Data.Song)
|
||||
|
||||
|
||||
return MusicItem{
|
||||
Title: response.Data.Song,
|
||||
Artist: response.Data.Singer,
|
||||
@@ -274,55 +274,55 @@ type YaohuLyricResponse struct {
|
||||
func fetchLyricFromYaohu(songName, artistName, dirPath string) bool {
|
||||
apiKey := "bXO9eq1pomwR1cyVhzX"
|
||||
apiURL := "https://api.yaohud.cn/api/music/qq"
|
||||
|
||||
|
||||
// 构建请求URL - QQ音乐VIP接口
|
||||
requestURL := fmt.Sprintf("%s?key=%s&msg=%s&n=1&size=hq",
|
||||
apiURL,
|
||||
apiKey,
|
||||
requestURL := fmt.Sprintf("%s?key=%s&msg=%s&n=1&size=hq",
|
||||
apiURL,
|
||||
apiKey,
|
||||
url.QueryEscape(songName))
|
||||
|
||||
|
||||
fmt.Printf("[Info] 🎵 Trying to fetch lyric from Yaohu QQ Music VIP API for: %s - %s\n", artistName, songName)
|
||||
|
||||
|
||||
// 创建带超时的HTTP客户端
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
|
||||
resp, err := client.Get(requestURL)
|
||||
if err != nil {
|
||||
fmt.Printf("[Error] Yaohu QQ Music API request failed: %v\n", err)
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Printf("[Error] Failed to read API response: %v\n", err)
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
var qqResp YaohuQQMusicResponse
|
||||
err = json.Unmarshal(body, &qqResp)
|
||||
if err != nil {
|
||||
fmt.Printf("[Error] Failed to parse API response: %v\n", err)
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
// 检查响应状态
|
||||
if qqResp.Code != 200 {
|
||||
fmt.Printf("[Warning] API returned error (code: %d, msg: %s)\n", qqResp.Code, qqResp.Msg)
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
// 检查viplrc URL是否存在
|
||||
if qqResp.Data.Viplrc == "" {
|
||||
fmt.Printf("[Warning] No lyric URL available for: %s\n", songName)
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
fmt.Printf("[Info] 🔍 Found song: %s - %s\n", qqResp.Data.Songname, qqResp.Data.Name)
|
||||
fmt.Printf("[Info] 📝 Fetching lyric from: %s\n", qqResp.Data.Viplrc)
|
||||
|
||||
|
||||
// Step 2: 获取实际歌词内容
|
||||
resp2, err := client.Get(qqResp.Data.Viplrc)
|
||||
if err != nil {
|
||||
@@ -330,22 +330,22 @@ func fetchLyricFromYaohu(songName, artistName, dirPath string) bool {
|
||||
return false
|
||||
}
|
||||
defer resp2.Body.Close()
|
||||
|
||||
|
||||
body2, err := io.ReadAll(resp2.Body)
|
||||
if err != nil {
|
||||
fmt.Printf("[Error] Failed to read lyric response: %v\n", err)
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
// viplrc URL直接返回LRC文本,不是JSON
|
||||
lyricText := string(body2)
|
||||
|
||||
|
||||
// 检查歌词内容
|
||||
if lyricText == "" || len(lyricText) < 10 {
|
||||
fmt.Printf("[Warning] No lyrics returned from viplrc URL\n")
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
// 将歌词写入文件
|
||||
lyricFilePath := filepath.Join(dirPath, "lyric.lrc")
|
||||
file, err := os.Create(lyricFilePath)
|
||||
@@ -354,14 +354,14 @@ func fetchLyricFromYaohu(songName, artistName, dirPath string) bool {
|
||||
return false
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
|
||||
// 写入歌词内容(LRC文本格式)
|
||||
_, err = file.WriteString(lyricText)
|
||||
if err != nil {
|
||||
fmt.Printf("[Error] Failed to write lyric content: %v\n", err)
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
fmt.Printf("[Success] ✅ Lyric fetched from Yaohu QQ Music VIP API and saved to %s\n", lyricFilePath)
|
||||
return true
|
||||
}
|
||||
@@ -369,50 +369,50 @@ func fetchLyricFromYaohu(songName, artistName, dirPath string) bool {
|
||||
// getRemoteMusicURLOnly 只获取远程音乐URL,不下载不处理(用于实时流式播放)
|
||||
func getRemoteMusicURLOnly(song, singer string) string {
|
||||
fmt.Printf("[Info] Getting remote music URL for: %s - %s\n", singer, song)
|
||||
|
||||
|
||||
// 尝试多个 API
|
||||
apiHosts := []string{
|
||||
"https://api.yuafeng.cn",
|
||||
"https://api-v2.yuafeng.cn",
|
||||
}
|
||||
|
||||
|
||||
sources := []string{"kuwo", "netease", "migu"}
|
||||
pathMap := map[string]string{
|
||||
"kuwo": "/API/ly/kwmusic.php",
|
||||
"netease": "/API/ly/wymusic.php",
|
||||
"migu": "/API/ly/mgmusic.php",
|
||||
}
|
||||
|
||||
|
||||
client := &http.Client{Timeout: 15 * time.Second}
|
||||
|
||||
|
||||
for _, host := range apiHosts {
|
||||
for _, source := range sources {
|
||||
path := pathMap[source]
|
||||
apiURL := fmt.Sprintf("%s%s?song=%s&singer=%s", host, path, url.QueryEscape(song), url.QueryEscape(singer))
|
||||
|
||||
apiURL := fmt.Sprintf("%s%s?msg=%s-%s&n=1", host, path, url.QueryEscape(song), url.QueryEscape(singer))
|
||||
|
||||
resp, err := client.Get(apiURL)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
var response YuafengAPIFreeResponse
|
||||
if err := json.Unmarshal(body, &response); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
if response.Data.Music != "" {
|
||||
fmt.Printf("[Success] Got remote URL from %s: %s\n", source, response.Data.Music)
|
||||
return response.Data.Music
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fmt.Println("[Error] Failed to get remote music URL from all APIs")
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user