ffmpeg 转码

This commit is contained in:
2021-05-22 19:18:49 +08:00
parent 69121a5cc2
commit 958c6002c8
2 changed files with 53 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ import (
"msw-open-music/internal/pkg/database" "msw-open-music/internal/pkg/database"
"net/http" "net/http"
"os" "os"
"os/exec"
"strconv" "strconv"
) )
@@ -283,6 +284,46 @@ type GetFileRequest struct {
ID int64 `json:"id"` 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) { func (api *API) HandleGetFileDirect(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query() q := r.URL.Query()
ids := q["id"] ids := q["id"]
@@ -375,6 +416,7 @@ func NewAPI(dbName string, Addr string) (*API, error) {
apiMux.HandleFunc("/search_folders", api.HandleSearchFolders) apiMux.HandleFunc("/search_folders", api.HandleSearchFolders)
apiMux.HandleFunc("/get_files_in_folder", api.HandleGetFilesInFolder) apiMux.HandleFunc("/get_files_in_folder", api.HandleGetFilesInFolder)
apiMux.HandleFunc("/get_random_files", api.HandleGetRandomFiles) apiMux.HandleFunc("/get_random_files", api.HandleGetRandomFiles)
apiMux.HandleFunc("/get_file_stream", api.HandleGetFileStream)
// below needs token // below needs token
apiMux.HandleFunc("/walk", api.HandleWalk) apiMux.HandleFunc("/walk", api.HandleWalk)
apiMux.HandleFunc("/reset", api.HandleReset) apiMux.HandleFunc("/reset", api.HandleReset)

View File

@@ -199,6 +199,7 @@ const component_file = {
<td> <td>
<button @click="download_file(file)" :disabled="disabled">{{ computed_download_status }}</button> <button @click="download_file(file)" :disabled="disabled">{{ computed_download_status }}</button>
<button @click="emit_play_audio">Play</button> <button @click="emit_play_audio">Play</button>
<button @click="emit_stream_audio">Stream</button>
</td> </td>
`, `,
data() { data() {
@@ -208,8 +209,13 @@ const component_file = {
} }
}, },
methods: { methods: {
emit_stream_audio() {
this.file.play_back_type = 'stream',
this.$emit("play_audio", this.file)
},
emit_play_audio() { emit_play_audio() {
console.log("pressed button") console.log("pressed button")
this.file.play_back_type = 'raw'
this.$emit("play_audio", this.file) this.$emit("play_audio", this.file)
}, },
download_file(file) { download_file(file) {
@@ -274,7 +280,11 @@ const component_audio_player = {
`, `,
computed: { computed: {
computed_playing_audio_file_url() { computed_playing_audio_file_url() {
if (this.file.play_back_type === 'raw') {
return '/api/v1/get_file_direct?id=' + this.file.id 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() { computed_show() {
return this.file.id ? true : false return this.file.id ? true : false