vue-movie/server/main.go

86 lines
1.5 KiB
Go
Raw Permalink Normal View History

2021-03-11 10:53:20 +00:00
package main
import (
"bytes"
"fmt"
"io/fs"
"log"
"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)
2023-05-10 04:46:06 +00:00
cmd := exec.Command("ffmpeg", "-i", filename, "-vframes", "1", "-s", fmt.Sprintf("%dx%d", width, height), "movie/"+key+".png")
2021-03-11 10:53:20 +00:00
var buffer bytes.Buffer
cmd.Stdout = &buffer
if cmd.Run() != nil {
panic("could not generate frame")
}
}
filepath.Walk("./movie", func(path string, info fs.FileInfo, err error) error {
2023-05-10 04:46:06 +00:00
if !info.IsDir() && filepath.Ext(info.Name()) != ".png" {
2021-03-11 10:53:20 +00:00
base := info.Name()
log.Println(path, info.Name())
movies = append(movies, Movie{
Name: info.Name(),
2023-05-10 04:46:06 +00:00
Image: base[:strings.IndexByte(base, '.')] + ".png",
2021-03-11 10:53:20 +00:00
})
}
return nil
})
log.Printf("%##v", movies)
}
func main() {
initMovie()
eg := gin.Default()
eg.Use(Cors())
eg.Static("/res", "movie/")
movie := eg.Group("movie")
movie.GET("/", MovieList)
2021-03-11 10:53:20 +00:00
eg.Run(":4444")
}