2024-04-11 08:28:23 +02:00
|
|
|
// Copyright 2024 The Ebitengine Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2024-10-27 10:07:47 +01:00
|
|
|
"net/http"
|
2024-04-11 08:28:23 +02:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio"
|
2024-04-12 08:16:26 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
2024-04-11 08:28:23 +02:00
|
|
|
)
|
|
|
|
|
2024-10-27 10:07:47 +01:00
|
|
|
// 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"
|
2024-04-11 08:28:23 +02:00
|
|
|
|
|
|
|
type Game struct {
|
|
|
|
player *mpegPlayer
|
2024-04-11 17:32:01 +02:00
|
|
|
err error
|
2024-04-11 08:28:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Update() error {
|
2024-04-11 17:32:01 +02:00
|
|
|
if g.err != nil {
|
|
|
|
return g.err
|
|
|
|
}
|
2024-04-11 08:28:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
2024-04-11 17:32:01 +02:00
|
|
|
if g.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := g.player.Draw(screen); err != nil {
|
|
|
|
g.err = err
|
|
|
|
}
|
2024-04-12 08:16:26 +02:00
|
|
|
ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.ActualFPS()))
|
2024-04-11 08:28:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
|
|
|
return outsideWidth, outsideHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Initialize audio context.
|
|
|
|
_ = audio.NewContext(48000)
|
|
|
|
|
|
|
|
// If you want to play your own video, the video must be an MPEG-1 video with 48000 audio sample rate.
|
|
|
|
// You can convert the video to MPEG-1 with the below command:
|
|
|
|
//
|
|
|
|
// 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.
|
2024-10-27 10:07:47 +01:00
|
|
|
var in io.ReadCloser
|
2024-04-11 08:28:23 +02:00
|
|
|
if len(os.Args) > 1 {
|
|
|
|
f, err := os.Open(os.Args[1])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
in = f
|
|
|
|
} else {
|
2024-10-27 10:07:47 +01:00
|
|
|
res, err := http.Get(mpgURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
in = res.Body
|
2024-04-11 08:28:23 +02:00
|
|
|
fmt.Println("Play the default video. You can specify a video file as an argument.")
|
|
|
|
}
|
|
|
|
|
2024-10-27 10:07:47 +01:00
|
|
|
player, err := newMPEGPlayer(in)
|
2024-04-11 08:28:23 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
g := &Game{
|
|
|
|
player: player,
|
|
|
|
}
|
|
|
|
player.Play()
|
|
|
|
|
2024-04-11 17:54:20 +02:00
|
|
|
ebiten.SetWindowTitle("Video (Ebitengine Demo)")
|
2024-04-11 08:28:23 +02:00
|
|
|
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
|
|
|
|
if err := ebiten.RunGame(g); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|