ffmpeg 转码
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"msw-open-music/internal/pkg/database"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -283,6 +284,46 @@ type GetFileRequest struct {
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
func (api *API) HandleGetFileStream(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
ids := q["id"]
|
||||
if len(ids) == 0 {
|
||||
api.HandleErrorString(w, r, `parameter "id" can't be empty`)
|
||||
return
|
||||
}
|
||||
id, err := strconv.Atoi(ids[0])
|
||||
if err != nil {
|
||||
api.HandleErrorString(w, r, `parameter "id" should be an integer`)
|
||||
return
|
||||
}
|
||||
file, err := api.Db.GetFile(int64(id))
|
||||
if err != nil {
|
||||
api.HandleError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
path, err := file.Path()
|
||||
if err != nil {
|
||||
api.HandleError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
cmd := exec.Command("ffmpeg",
|
||||
"-i", path,
|
||||
"-c:a", "libopus",
|
||||
"-ab", "128k",
|
||||
"-vn",
|
||||
"-f", "matroska",
|
||||
"-",
|
||||
)
|
||||
cmd.Stdout = w
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
api.HandleError(w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (api *API) HandleGetFileDirect(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
ids := q["id"]
|
||||
@@ -375,6 +416,7 @@ func NewAPI(dbName string, Addr string) (*API, error) {
|
||||
apiMux.HandleFunc("/search_folders", api.HandleSearchFolders)
|
||||
apiMux.HandleFunc("/get_files_in_folder", api.HandleGetFilesInFolder)
|
||||
apiMux.HandleFunc("/get_random_files", api.HandleGetRandomFiles)
|
||||
apiMux.HandleFunc("/get_file_stream", api.HandleGetFileStream)
|
||||
// below needs token
|
||||
apiMux.HandleFunc("/walk", api.HandleWalk)
|
||||
apiMux.HandleFunc("/reset", api.HandleReset)
|
||||
|
||||
10
web/index.js
10
web/index.js
@@ -199,6 +199,7 @@ const component_file = {
|
||||
<td>
|
||||
<button @click="download_file(file)" :disabled="disabled">{{ computed_download_status }}</button>
|
||||
<button @click="emit_play_audio">Play</button>
|
||||
<button @click="emit_stream_audio">Stream</button>
|
||||
</td>
|
||||
`,
|
||||
data() {
|
||||
@@ -208,8 +209,13 @@ const component_file = {
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
emit_stream_audio() {
|
||||
this.file.play_back_type = 'stream',
|
||||
this.$emit("play_audio", this.file)
|
||||
},
|
||||
emit_play_audio() {
|
||||
console.log("pressed button")
|
||||
this.file.play_back_type = 'raw'
|
||||
this.$emit("play_audio", this.file)
|
||||
},
|
||||
download_file(file) {
|
||||
@@ -274,7 +280,11 @@ const component_audio_player = {
|
||||
`,
|
||||
computed: {
|
||||
computed_playing_audio_file_url() {
|
||||
if (this.file.play_back_type === 'raw') {
|
||||
return '/api/v1/get_file_direct?id=' + this.file.id
|
||||
} else if (this.file.play_back_type === 'stream') {
|
||||
return '/api/v1/get_file_stream?id=' + this.file.id
|
||||
}
|
||||
},
|
||||
computed_show() {
|
||||
return this.file.id ? true : false
|
||||
|
||||
Reference in New Issue
Block a user