Skip to content

Commit

Permalink
examples/video: put a video file in a separate server
Browse files Browse the repository at this point in the history
This change also fixes an issue where a file could be closed before
the video play ends.

Closes #3116
  • Loading branch information
hajimehoshi committed Oct 27, 2024
1 parent c5e47c7 commit 332c682
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
9 changes: 0 additions & 9 deletions examples/video/license.md

This file was deleted.

24 changes: 14 additions & 10 deletions examples/video/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@
package main

import (
"bufio"
"bytes"
_ "embed"
"fmt"
"io"
"log"
"net/http"
"os"

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio"
"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
Expand Down Expand Up @@ -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)
}
Expand Down
16 changes: 14 additions & 2 deletions examples/video/mpegplayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Binary file removed examples/video/shibuya.mpg
Binary file not shown.

0 comments on commit 332c682

Please sign in to comment.