This commit is contained in:
Nite07 2024-10-23 22:47:37 +08:00
parent e46e16f8c9
commit ac45ad5b44
9 changed files with 907 additions and 149 deletions

View File

@ -39,12 +39,22 @@ type PlayConfig struct {
CustomArgs string `json:"custom_args"`
}
type LogConfig struct {
PlayState bool `json:"play_state"`
}
type AuthConfig struct {
Key string `json:"key"`
}
type Config struct {
Input []any `json:"input"`
InputItems []InputItem `json:"-"` // contains video file or dir
VideoList []InputItem `json:"-"` // only contains video file
Play PlayConfig `json:"play"`
Output OutputConfig `json:"output"`
Log LogConfig `json:"log"`
Auth AuthConfig `json:"auth"`
}
var GlobalConfig Config
@ -162,7 +172,7 @@ func validatePlayConfig() error {
GlobalConfig.Play.BufSize = "12000k"
}
if GlobalConfig.Play.Scale == "" {
GlobalConfig.Play.Scale = "1920:1080"
GlobalConfig.Play.Scale = "1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2"
}
if GlobalConfig.Play.FrameRate == 0 {
GlobalConfig.Play.FrameRate = 30

42
go.mod
View File

@ -4,4 +4,44 @@ go 1.23.2
require github.com/fsnotify/fsnotify v1.7.0
require golang.org/x/sys v0.4.0 // indirect
require (
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.10.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pterm/pterm v0.12.79 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

176
go.sum
View File

@ -1,4 +1,180 @@
atomicgo.dev/cursor v0.2.0 h1:H6XN5alUJ52FZZUkI7AlJbUc1aW38GWZalpYRPpoPOw=
atomicgo.dev/cursor v0.2.0/go.mod h1:Lr4ZJB3U7DfPPOkbH7/6TOtJ4vFGHlgj1nc+n900IpU=
atomicgo.dev/keyboard v0.2.9 h1:tOsIid3nlPLZ3lwgG8KZMp/SFmr7P0ssEN5JUsm78K8=
atomicgo.dev/keyboard v0.2.9/go.mod h1:BC4w9g00XkxH/f1HXhW2sXmJFOCWbKn9xrOunSFtExQ=
atomicgo.dev/schedule v0.1.0 h1:nTthAbhZS5YZmgYbb2+DH8uQIZcTlIrd4eYr3UQxEjs=
atomicgo.dev/schedule v0.1.0/go.mod h1:xeUa3oAkiuHYh8bKiQBRojqAMq3PXXbJujjb0hw8pEU=
github.com/MarvinJWendt/testza v0.1.0/go.mod h1:7AxNvlfeHP7Z/hDQ5JtE3OKYT3XFUeLCDE2DQninSqs=
github.com/MarvinJWendt/testza v0.2.1/go.mod h1:God7bhG8n6uQxwdScay+gjm9/LnO4D3kkcZX4hv9Rp8=
github.com/MarvinJWendt/testza v0.2.8/go.mod h1:nwIcjmr0Zz+Rcwfh3/4UhBp7ePKVhuBExvZqnKYWlII=
github.com/MarvinJWendt/testza v0.2.10/go.mod h1:pd+VWsoGUiFtq+hRKSU1Bktnn+DMCSrDrXDpX2bG66k=
github.com/MarvinJWendt/testza v0.2.12/go.mod h1:JOIegYyV7rX+7VZ9r77L/eH6CfJHHzXjB69adAhzZkI=
github.com/MarvinJWendt/testza v0.3.0/go.mod h1:eFcL4I0idjtIx8P9C6KkAuLgATNKpX4/2oUqKc6bF2c=
github.com/MarvinJWendt/testza v0.4.2/go.mod h1:mSdhXiKH8sg/gQehJ63bINcCKp7RtYewEjXsvsVUPbE=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ=
github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pterm/pterm v0.12.27/go.mod h1:PhQ89w4i95rhgE+xedAoqous6K9X+r6aSOI2eFF7DZI=
github.com/pterm/pterm v0.12.29/go.mod h1:WI3qxgvoQFFGKGjGnJR849gU0TsEOvKn5Q8LlY1U7lg=
github.com/pterm/pterm v0.12.30/go.mod h1:MOqLIyMOgmTDz9yorcYbcw+HsgoZo3BQfg2wtl3HEFE=
github.com/pterm/pterm v0.12.31/go.mod h1:32ZAWZVXD7ZfG0s8qqHXePte42kdz8ECtRyEejaWgXU=
github.com/pterm/pterm v0.12.33/go.mod h1:x+h2uL+n7CP/rel9+bImHD5lF3nM9vJj80k9ybiiTTE=
github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5bUw8T8=
github.com/pterm/pterm v0.12.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkGTYf8s=
github.com/pterm/pterm v0.12.79 h1:lH3yrYMhdpeqX9y5Ep1u7DejyHy7NSQg9qrBjF9dFT4=
github.com/pterm/pterm v0.12.79/go.mod h1:1v/gzOF1N0FsjbgTHZ1wVycRkKiatFvJSJC4IGaQAAo=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

7
logger/log.go Normal file
View File

@ -0,0 +1,7 @@
package logger
type Logger interface {
Print(v ...interface{})
Println(v ...interface{})
Printf(format string, v ...interface{})
}

55
main.go
View File

@ -1,8 +1,9 @@
package main
import (
"bufio"
"live-streamer/config"
"live-streamer/logger"
"live-streamer/server"
"live-streamer/streamer"
"live-streamer/utils"
"log"
@ -13,37 +14,41 @@ import (
)
var GlobalStreamer *streamer.Streamer
var Logger logger.Logger
func main() {
server.NewServer(":8080", input)
server.GlobalServer.Run()
Logger = server.GlobalServer
if !utils.HasFFMPEG() {
log.Fatal("ffmpeg not found")
}
GlobalStreamer = streamer.NewStreamer(config.GlobalConfig.VideoList)
go input()
GlobalStreamer = streamer.NewStreamer(config.GlobalConfig.VideoList, Logger)
go startWatcher()
GlobalStreamer.Stream()
GlobalStreamer.Close()
}
func input() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
switch scanner.Text() {
case "prev":
GlobalStreamer.Prev()
case "next":
GlobalStreamer.Next()
case "quit":
GlobalStreamer.Close()
os.Exit(0)
case "list":
list := GlobalStreamer.GetVideoListPath()
log.Println("\nvideo list:\n", strings.Join(list, "\n"))
case "current":
log.Println("current video: ", GlobalStreamer.GetCurrentVideo())
default:
log.Println("unknown command")
func input(msg string) {
switch msg {
case "prev":
GlobalStreamer.Prev()
case "next":
GlobalStreamer.Next()
case "quit":
GlobalStreamer.Close()
os.Exit(0)
case "list":
list := GlobalStreamer.GetVideoListPath()
Logger.Println("\nvideo list:\n", strings.Join(list, "\n"))
case "current":
videoPath, err := GlobalStreamer.GetCurrentVideoPath()
if err != nil {
Logger.Println("current video: none")
}
Logger.Println("current video: ", videoPath)
default:
Logger.Println("unknown command")
}
}
@ -60,7 +65,7 @@ func startWatcher() {
if err != nil {
log.Fatalf("failed to add dir to watcher: %v", err)
}
log.Println("watching dir:", item.Path)
Logger.Println("watching dir:", item.Path)
}
}
if err != nil {
@ -75,19 +80,19 @@ func startWatcher() {
}
if event.Op&fsnotify.Create == fsnotify.Create {
if utils.IsSupportedVideo(event.Name) {
log.Println("new video added:", event.Name)
Logger.Println("new video added:", event.Name)
GlobalStreamer.Add(event.Name)
}
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
log.Println("video removed:", event.Name)
Logger.Println("video removed:", event.Name)
GlobalStreamer.Remove(event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("watcher error:", err)
Logger.Println("watcher error:", err)
}
}
}

39
server/handler.go Normal file
View File

@ -0,0 +1,39 @@
package server
import (
"live-streamer/streamer"
"net/http"
"github.com/gin-gonic/gin"
)
func GetCurrentVideo(c *gin.Context) {
type response struct {
Success bool `json:"success"`
Data string `json:"data"`
Message string `json:"message"`
}
videoPath, err := streamer.GlobalStreamer.GetCurrentVideoPath()
if err != nil {
c.JSON(http.StatusOK, response{
Success: false,
Message: err.Error(),
})
}
c.JSON(http.StatusOK, response{
Success: true,
Data: videoPath,
})
}
func GetVideoList(c *gin.Context) {
type response struct {
Success bool `json:"success"`
Data []string `json:"data"`
}
list := streamer.GlobalStreamer.GetVideoListPath()
c.JSON(http.StatusOK, response{
Success: true,
Data: list,
})
}

125
server/server.go Normal file
View File

@ -0,0 +1,125 @@
package server
import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
//go:embed static
var staticFiles embed.FS
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
type InputFunc func(string)
type Server struct {
addr string
outputChan chan string
dealInputFunc InputFunc
clients []*Client
historyOutput string
}
type Client struct {
conn *websocket.Conn
}
var GlobalServer *Server
func NewServer(addr string, dealInputFunc InputFunc) {
GlobalServer = &Server{
addr: addr,
outputChan: make(chan string),
dealInputFunc: dealInputFunc,
}
}
func (s *Server) Run() {
router := gin.Default()
tpl, err := template.ParseFS(staticFiles, "static/*")
if err != nil {
log.Fatalf("Error parsing templates: %v", err)
}
router.SetHTMLTemplate(tpl)
router.GET("/ws", s.handleWebSocket)
router.GET("/video/current", GetCurrentVideo)
router.GET("/video/list", GetVideoList)
router.GET(
"/", func(c *gin.Context) {
c.HTML(200, "index.html", nil)
},
)
go func() {
if err := router.Run(s.addr); err != nil {
log.Fatalf("Error starting server: %v", err)
}
}()
go func() {
for {
output := <-s.outputChan
s.historyOutput += output
for _, client := range s.clients {
_ = client.conn.WriteMessage(websocket.TextMessage, []byte(output))
}
}
}()
}
func (s *Server) handleWebSocket(c *gin.Context) {
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
return
}
defer ws.Close()
client := &Client{conn: ws}
s.clients = append(s.clients, client)
_ = client.conn.WriteMessage(websocket.TextMessage, []byte(s.historyOutput))
defer func() {
for i, c := range s.clients {
if c == client {
s.clients = append(s.clients[:i], s.clients[i+1:]...)
break
}
}
}()
for {
// recive message
_, msg, err := ws.ReadMessage()
if err != nil {
log.Printf("Websocket reading message error: %v", err)
break
}
s.dealInputFunc(string(msg))
}
}
func (s *Server) Print(msg ...any) {
s.outputChan <- fmt.Sprint(msg...)
}
func (s *Server) Println(msg ...any) {
s.outputChan <- fmt.Sprintln(msg...)
}
func (s *Server) Printf(format string, args ...interface{}) {
s.outputChan <- fmt.Sprintf(format, args...)
}
func (s *Server) Close() {
close(s.outputChan)
}

372
server/static/index.html Normal file
View File

@ -0,0 +1,372 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Live Streamer</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
<link
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"
rel="stylesheet"
/>
<style>
body,
html {
height: 100vh;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
background-color: #f0f2f5;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
overflow: hidden;
}
.container-fluid {
flex: 1;
display: flex;
flex-direction: column;
padding: 15px;
height: 100%;
gap: 15px;
overflow: hidden;
}
.header {
flex: 0 0 auto;
background: linear-gradient(135deg, #6e8efb, #4a6cf7);
color: white;
padding: 15px 20px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.header h2 {
margin: 0;
font-weight: 600;
}
#status {
flex: 0 0 auto;
background-color: white;
padding: 10px 15px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
#output-container {
flex: 1;
min-height: 100px;
display: flex;
flex-direction: column;
}
#messages {
flex: 1;
height: auto !important;
resize: none;
border-radius: 8px;
padding: 15px;
font-family: "Consolas", monospace;
font-size: 0.9rem;
line-height: 1.5;
background-color: #2b2b2b;
color: #e0e0e0;
border: none;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
#app-container {
flex: 1;
min-height: 0;
display: flex;
flex-direction: column;
gap: 15px;
min-height: 200px;
}
#current-video {
flex: 0 0 60px;
background-color: white;
padding: 15px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
display: flex;
align-items: center;
}
.bottom-section {
flex: 1;
display: flex;
gap: 15px;
min-height: 160px;
max-height: 300px;
}
#control-panel {
flex: 0 0 150px;
display: flex;
flex-direction: column;
gap: 10px;
padding: 15px;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
.btn {
border-radius: 6px;
font-weight: 500;
text-transform: uppercase;
font-size: 0.85rem;
letter-spacing: 0.5px;
padding: 8px 15px;
transition: all 0.3s ease;
}
.btn-primary {
background: linear-gradient(135deg, #6e8efb, #4a6cf7);
border: none;
}
.btn-primary:hover {
background: linear-gradient(135deg, #5d7df9, #3959f5);
transform: translateY(-1px);
}
.btn-danger {
background: linear-gradient(135deg, #ff6b6b, #ee5253);
border: none;
}
.btn-danger:hover {
background: linear-gradient(135deg, #ff5252, #ed4444);
transform: translateY(-1px);
}
#video-list-container {
flex: 1;
background-color: white;
padding: 15px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
display: flex;
flex-direction: column;
}
#video-list {
font-weight: 600;
color: #333;
margin-bottom: 10px;
flex: 0 0 auto;
}
.list-group {
flex: 1;
overflow-y: auto;
padding-right: 5px;
margin-bottom: 0;
}
.list-group-item {
border: none;
border-radius: 6px !important;
margin-bottom: 5px;
padding: 12px 15px;
background-color: #f8f9fa;
transition: all 0.2s ease;
}
.list-group-item:last-child {
margin-bottom: 0;
}
.list-group-item:hover {
background-color: #e9ecef;
transform: translateX(5px);
}
::-webkit-scrollbar {
width: 8px;
}
::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 4px;
}
::-webkit-scrollbar-thumb {
background: #888;
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: #555;
}
#status::before {
content: "";
display: inline-block;
width: 8px;
height: 8px;
border-radius: 50%;
margin-right: 8px;
background-color: #dc3545;
}
#status.connected::before {
background-color: #28a745;
}
@media (max-height: 600px) {
.container-fluid {
gap: 10px;
padding: 10px;
}
.header {
padding: 10px;
}
#current-video {
flex: 0 0 40px;
padding: 10px;
}
.bottom-section {
gap: 10px;
}
}
@media (max-width: 768px) {
#control-panel {
flex: 0 0 120px;
}
.btn {
padding: 6px 12px;
font-size: 0.8rem;
}
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="header">
<h2><i class="fas fa-video me-2"></i>Live Streamer</h2>
</div>
<div id="status">WebSocket Status: Disconnected</div>
<div id="output-container">
<textarea id="messages" class="form-control" readonly>
消息区域</textarea
>
</div>
<div id="app-container">
<div id="current-video">
<i class="fas fa-play-circle me-2"></i><span>当前播放: 无</span>
</div>
<div class="bottom-section">
<div id="control-panel">
<button class="btn btn-primary" onclick="previousVideo()">
<i class="fas fa-step-backward me-2"></i>上一个
</button>
<button class="btn btn-primary" onclick="nextVideo()">
<i class="fas fa-step-forward me-2"></i>下一个
</button>
<button class="btn btn-danger" onclick="closeConnection()">
<i class="fas fa-power-off me-2"></i>关闭推流
</button>
</div>
<div id="video-list-container">
<div id="video-list"><i class="fas fa-list me-2"></i>视频列表</div>
<ul class="list-group list-group-flush">
<!-- <li class="list-group-item">
<i class="fas fa-file-video me-2"></i>Cras justo odio
</li> -->
</ul>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
const ws = new WebSocket("ws://localhost:8080/ws");
const statusDisplay = document.getElementById("status");
const currentVideo = document.getElementById("current-video");
const videoList = document.getElementById("video-list");
const messagesArea = document.getElementById("messages");
ws.onopen = function () {
statusDisplay.textContent = "WebSocket Status: Connected";
statusDisplay.classList.add("connected");
};
ws.onmessage = function (evt) {
appendMessage(evt.data);
};
ws.onclose = function () {
statusDisplay.textContent = "WebSocket Status: Disconnected";
statusDisplay.classList.remove("connected");
};
function appendMessage(message) {
const timestamp = new Date().toLocaleTimeString();
messagesArea.value += `[${timestamp}] ${message}\n`;
messagesArea.scrollTop = messagesArea.scrollHeight;
}
function updateCurrentVideo() {
fetch("/video/current")
.then((response) => response.json())
.then((data) => {
if (data.success) {
document.querySelector("#current-video>span").innerHTML =
data.data;
}
});
}
function updateVideoList() {
fetch("/video/list")
.then((response) => response.json())
.then((data) => {
const listContainer = document.querySelector(
"#video-list-container .list-group"
);
listContainer.innerHTML = "";
if (data.success) {
for (let item of data.data) {
listContainer.innerHTML += `<li class="list-group-item">
<i class="fas fa-file-video me-2"></i>${item}</li>`;
}
}
});
}
updateCurrentVideo();
updateVideoList();
setInterval(updateCurrentVideo, 5000);
setInterval(updateVideoList, 5000);
function previousVideo() {
ws.send("prev");
}
function nextVideo() {
ws.send("next");
}
function closeConnection() {
if (confirm("确定要关闭服务器吗?")) {
ws.send("quit");
}
}
</script>
</body>
</html>

View File

@ -3,13 +3,12 @@ package streamer
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"live-streamer/config"
"log"
"os"
"live-streamer/logger"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
@ -19,28 +18,92 @@ type Streamer struct {
videoList []config.InputItem
currentVideoIndex int
cmd *exec.Cmd
logFile *os.File
ctx context.Context
cancel context.CancelFunc
mu sync.Mutex
logger logger.Logger
}
func NewStreamer(videoList []config.InputItem) *Streamer {
logDir := "logs"
if err := os.MkdirAll(logDir, 0755); err != nil {
log.Printf("Error creating log directory: %v\n", err)
}
logPath := filepath.Join(logDir, fmt.Sprintf("ffmpeg_%s.log", time.Now().Format("2006-01-02_15-04-05")))
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("Error opening log file: %v\n", err)
}
return &Streamer{
var GlobalStreamer *Streamer
func NewStreamer(videoList []config.InputItem, logger logger.Logger) *Streamer {
GlobalStreamer = &Streamer{
videoList: videoList,
currentVideoIndex: 0,
cmd: nil,
logFile: logFile,
ctx: nil,
logger: logger,
}
return GlobalStreamer
}
func (s *Streamer) Stream() {
for {
if len(s.videoList) == 0 {
time.Sleep(time.Second)
continue
}
s.start()
}
}
func (s *Streamer) start() {
s.Stop()
s.ctx, s.cancel = context.WithCancel(context.Background())
currentVideo := s.videoList[s.currentVideoIndex]
videoPath := currentVideo.Path
s.logger.Println("start stream: ", videoPath)
s.mu.Lock()
s.cmd = exec.CommandContext(s.ctx, "ffmpeg", s.buildFFmpegArgs(currentVideo)...)
s.mu.Unlock()
pipe, err := s.cmd.StderrPipe()
if err != nil {
s.logger.Printf("failed to get pipe: %v", err)
return
}
reader := bufio.NewReader(pipe)
if err := s.cmd.Start(); err != nil {
s.logger.Printf("starting ffmpeg error: %v\n", err)
return
}
go s.log(reader)
<-s.ctx.Done()
s.logger.Printf("stop stream: %s", videoPath)
// stream next video
s.currentVideoIndex++
if s.currentVideoIndex >= len(s.videoList) {
s.currentVideoIndex = 0
}
}
func (s *Streamer) Stop() {
if s.cancel != nil {
stopped := make(chan error)
go func() {
stopped <- s.cmd.Wait()
}()
s.cancel()
s.mu.Lock()
if s.cmd != nil && s.cmd.Process != nil {
select {
case <-stopped:
break
case <-time.After(3 * time.Second):
_ = s.cmd.Process.Kill()
break
}
s.cmd = nil
}
s.mu.Unlock()
}
}
@ -79,13 +142,29 @@ func (s *Streamer) Next() {
s.Stop()
}
func (s *Streamer) Stream() {
for {
if len(s.videoList) == 0 {
time.Sleep(time.Second)
continue
func (s *Streamer) log(reader *bufio.Reader) {
select {
case <-s.ctx.Done():
return
default:
if !config.GlobalConfig.Log.PlayState {
return
}
buf := make([]byte, 1024)
for {
n, err := reader.Read(buf)
if n > 0 {
videoPath, _ := s.GetCurrentVideoPath()
buf = append([]byte(videoPath), buf...)
s.logger.Print(string(buf[:n]))
}
if err != nil {
if err != io.EOF {
s.logger.Printf("reading ffmpeg error: %v\n", err)
}
break
}
}
s.start()
}
}
@ -125,75 +204,16 @@ func (s *Streamer) buildFFmpegArgs(videoItem config.InputItem) []string {
args = append(args, fmt.Sprintf("%s/%s", config.GlobalConfig.Output.RTMPServer, config.GlobalConfig.Output.StreamKey))
// log.Println("ffmpeg args: ", args)
// logger.GlobalLogger.Println("ffmpeg args: ", args)
return args
}
func (s *Streamer) start() {
s.Stop()
s.ctx, s.cancel = context.WithCancel(context.Background())
currentVideo := s.videoList[s.currentVideoIndex]
videoPath := currentVideo.Path
log.Println("start stream: ", videoPath)
s.mu.Lock()
s.cmd = exec.CommandContext(s.ctx, "ffmpeg", s.buildFFmpegArgs(currentVideo)...)
s.mu.Unlock()
pipe, err := s.cmd.StderrPipe()
if err != nil {
log.Printf("failed to get pipe: %v", err)
return
func (s *Streamer) GetCurrentVideoPath() (string, error) {
if len(s.videoList) == 0 {
return "", errors.New("no video streaming")
}
reader := bufio.NewReader(pipe)
writer := bufio.NewWriter(s.logFile)
if err := s.cmd.Start(); err != nil {
log.Printf("starting ffmpeg error: %v\n", err)
return
}
go s.log(reader, writer)
<-s.ctx.Done()
log.Printf("stop stream: %s", videoPath)
if currentVideo == s.videoList[s.currentVideoIndex] {
s.Next()
}
}
func (s *Streamer) Stop() {
if s.cancel != nil {
done := make(chan error)
go func() {
done <- s.cmd.Wait()
}()
s.cancel()
s.mu.Lock()
if s.cmd != nil && s.cmd.Process != nil {
select {
case <-done:
break
case <-time.After(3 * time.Second):
// log.Printf("ffmpeg process is still running, killing it...\n")
if !s.cmd.ProcessState.Exited() {
_ = s.cmd.Process.Kill()
}
break
}
s.cmd = nil
}
s.mu.Unlock()
}
}
func (s *Streamer) GetCurrentVideo() string {
return s.videoList[s.currentVideoIndex].Path
return s.videoList[s.currentVideoIndex].Path, nil
}
func (s *Streamer) GetVideoList() []config.InputItem {
@ -213,41 +233,5 @@ func (s *Streamer) GetCurrentIndex() int {
}
func (s *Streamer) Close() {
if s.logFile != nil {
s.logFile.Close()
s.logFile = nil
}
s.Stop()
}
func (s *Streamer) log(reader *bufio.Reader, writer *bufio.Writer) {
defer func() {
if s.logFile != nil {
if err := s.logFile.Sync(); err != nil {
log.Printf("syncing log file error: %v\n", err)
}
}
}()
buf := make([]byte, 1024)
for {
n, err := reader.Read(buf)
if err != nil {
if err != io.EOF {
log.Printf("reading ffmpeg error: %v\n", err)
}
break
}
if n > 0 {
timestamp := time.Now().Format("2006-01-02 15:04:05")
logLine := fmt.Sprintf("[%s] %s", timestamp, string(buf[:n]))
if s.logFile != nil {
if _, err := writer.WriteString(logLine); err != nil {
log.Printf("writing to log file error: %v\n", err)
}
if err := writer.Flush(); err != nil {
log.Printf("flushing writer error: %v\n", err)
}
}
}
}
}