diff --git a/examples/video/license.md b/examples/video/license.md deleted file mode 100644 index f9adf5599535..000000000000 --- a/examples/video/license.md +++ /dev/null @@ -1,9 +0,0 @@ -# License - -## `shibuya.mpg` - -https://commons.wikimedia.org/wiki/File:Shibuya_Crossing,_Tokyo,_Japan_(video).webm - -"Shibuya Crossing, Tokyo, Japan (video).webm" by Basile Morin - -The Creative Commons Attribution-Share Alike 4.0 International license diff --git a/examples/video/main.go b/examples/video/main.go index 445a6fb503d7..cc889c461815 100644 --- a/examples/video/main.go +++ b/examples/video/main.go @@ -15,12 +15,11 @@ package main import ( - "bufio" - "bytes" _ "embed" "fmt" "io" "log" + "net/http" "os" "github.com/hajimehoshi/ebiten/v2" @@ -28,8 +27,12 @@ import ( "github.com/hajimehoshi/ebiten/v2/ebitenutil" ) -//go:embed shibuya.mpg -var shibuya_mpg []byte +// mpgURL is a URL of an example MPEG-1 video. The license is the following: +// +// https://commons.wikimedia.org/wiki/File:Shibuya_Crossing,_Tokyo,_Japan_(video).webm +// "Shibuya Crossing, Tokyo, Japan (video).webm" by Basile Morin +// The Creative Commons Attribution-Share Alike 4.0 International license +const mpgURL = "https://example-resources.ebitengine.org/shibuya.mpg" type Game struct { player *mpegPlayer @@ -67,22 +70,23 @@ func main() { // ffmpeg -i YOUR_VIDEO -c:v mpeg1video -q:v 8 -c:a mp2 -format mpeg -ar 48000 output.mpg // // You can adjust quality by changing -q:v value. A lower value indicates better quality. - var in io.ReadSeeker + var in io.ReadCloser if len(os.Args) > 1 { f, err := os.Open(os.Args[1]) if err != nil { log.Fatal(err) } - defer func() { - _ = f.Close() - }() in = f } else { - in = bytes.NewReader(shibuya_mpg) + res, err := http.Get(mpgURL) + if err != nil { + log.Fatal(err) + } + in = res.Body fmt.Println("Play the default video. You can specify a video file as an argument.") } - player, err := newMPEGPlayer(bufio.NewReader(in)) + player, err := newMPEGPlayer(in) if err != nil { log.Fatal(err) } diff --git a/examples/video/mpegplayer.go b/examples/video/mpegplayer.go index 0ed15251c3c8..741c1de71c8b 100644 --- a/examples/video/mpegplayer.go +++ b/examples/video/mpegplayer.go @@ -50,10 +50,14 @@ type mpegPlayer struct { // These members are used when the video doesn't have an audio stream. refTime time.Time + src io.ReadCloser + + closeOnce sync.Once + m sync.Mutex } -func newMPEGPlayer(src io.Reader) (*mpegPlayer, error) { +func newMPEGPlayer(src io.ReadCloser) (*mpegPlayer, error) { mpg, err := mpeg.New(src) if err != nil { return nil, err @@ -70,6 +74,7 @@ func newMPEGPlayer(src io.Reader) (*mpegPlayer, error) { yCbCrImage: ebiten.NewImage(mpg.Width(), mpg.Height()), yCbCrBytes: make([]byte, 4*mpg.Width()*mpg.Height()), frameImage: ebiten.NewImage(mpg.Width(), mpg.Height()), + src: src, } s, err := ebiten.NewShader([]byte(`package main @@ -140,7 +145,14 @@ func (p *mpegPlayer) updateFrame() error { video := p.mpg.Video() if video.HasEnded() { p.frameImage.Clear() - return nil + var err error + p.closeOnce.Do(func() { + fmt.Println("The video has ended.") + if err1 := p.src.Close(); err1 != nil { + err = err1 + } + }) + return err } d := 1 / p.mpg.Framerate() diff --git a/examples/video/shibuya.mpg b/examples/video/shibuya.mpg deleted file mode 100644 index 873bec604396..000000000000 Binary files a/examples/video/shibuya.mpg and /dev/null differ