x-movie/server/main.go
2023-07-08 03:15:48 +08:00

111 lines
2.2 KiB
Go

package main
import (
"bytes"
"io/fs"
"log"
"net/http"
"os/exec"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
)
// Movie 电影结构
type Movie struct {
Name string `json:"filename"`
Image string `json:"image"`
}
var movies []Movie
func initMovie() {
var movieDict map[string]string = make(map[string]string)
matches, err := filepath.Glob("movie/*")
if err != nil {
log.Println(err)
}
for _, filename := range matches {
base := filepath.Base(filename)
ext := filepath.Ext(base)
base = base[:strings.IndexByte(base, '.')]
if _, ok := movieDict[base]; ok {
delete(movieDict, base)
} else {
movieDict[base] = filename
}
log.Println(base, ext)
}
for key, filename := range movieDict {
// width := 160
// height := 120
log.Println(filename)
// cmd := exec.Command("ffmpeg", "-i", filename, "-vframes", "1", "-s", fmt.Sprintf("%dx%d", width, height), "movie/"+key+".png")
cmd := exec.Command("ffmpeg",
"-i", filename,
"-vf", "select='isnan(prev_selected_t)+gte(t-prev_selected_t\\,35)',scale=320:180,tile=3x3",
"-frames:v", "1",
"movie/"+key+".png",
)
var buffer bytes.Buffer
cmd.Stdout = &buffer
if cmd.Run() != nil {
log.Println(buffer.String())
panic("could not generate frame")
}
// err := generateThumbnail(filename, "movie/"+key+".png", width, height)
// if err != nil {
// panic("could not generate frame")
// }
}
filepath.Walk("./movie", func(path string, info fs.FileInfo, err error) error {
if !info.IsDir() && filepath.Ext(info.Name()) != ".png" {
base := info.Name()
log.Println(path, info.Name())
movies = append(movies, Movie{
Name: info.Name(),
Image: base[:strings.IndexByte(base, '.')] + ".png",
})
}
return nil
})
log.Printf("%##v", movies)
}
func main() {
initMovie()
eg := gin.Default()
eg.Use(Cors())
eg.Static("/res", "movie/")
// 添加以下代码以提供frontend静态文件
eg.Static("/static", "../build/static")
eg.NoRoute(func(c *gin.Context) {
path := c.Request.URL.Path
if filepath.Ext(path) == "" {
c.File("../build/index.html")
} else {
c.Status(http.StatusNotFound)
}
})
movie := eg.Group("movie")
movie.GET("/", MovieList)
eg.Run(":4444")
}