1

I use data URI, base64 to add images and videos to my games, but I found if I want to make a game with multiple videos it will be really bad code with more than 500 lines for each video, so is there any advice to treat this type of file, what I really do that I make a Mixin file and I make multiple functions that return base64.

export default{
    stage1(){
        return "data:video/mp4;base64,AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAFkAdtZGF0AAADAAYF///........."
    },
    stage2(){
        return "data:video/mp4;base64,AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAFkAdtZGF0AAADAAYF///........."
    },
}
winner_joiner
  • 12,173
  • 4
  • 36
  • 61
a_7md.f
  • 33
  • 3
  • I updated my answer to include a short info to a webpack demo, I hope it helps. Just wanted to see how it works with webpack 5 and thought I could share my demo. – winner_joiner May 25 '23 at 19:28

1 Answers1

0

I would recommend using a bundler like (webpack, parcel, rollup, browserify,...) you can programm in several files and before deployment the bundler will combine all files into an single file (and minifiy it).

Using a bundler like webpack you can create a json file with all dataurl's like:

// filename data.json
{
    "video1": "data:video/mp4;base64,AAAA...", 
    "video2": "data:video/mp4;base64,AAAA...", 
    "video3": "data:video/mp4;base64,AAAA...",
    // ...
}  

in the app script:

 import videoData from './data.json';

 ...
     stage1(){
         return videoData.video1;
     }
     stage2(){
         return videoData.video2;
     }
 ...
   
 

and in the application file you can import teh json-file, and it will be integrated on the bundler build action. And create a single file with all files for the application.

For details on webpack and how to setup your project checkout the documentation. It is too long to describe it in an answer, and there are already good articles / video's out there. (but you can also use any other bundler)

I my answer points you in the right direction.

Extra Info: If you use webpack you would need to use the plugins "html-webpack-plugin" and "html-inline-script-webpack-plugin"

btw.: you can use webpack also to inline assets (images, video, ...) into a file, without the need for you to convert them to base64 yourself. (link to the documentation)

Update:

A small demo project (if you have node and npm installed):

Install with npm these packages

  • html-inline-script-webpack-plugin
  • html-webpack-plugin
  • webpack
  • webpack-cli

than create a basic config file for webpack:

// webpack.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlInlineScriptPlugin = require('html-inline-script-webpack-plugin');
const path = require('path');

module.exports = {
    entry: path.resolve(__dirname, "scripts/app.js"),
    output: {
        path: path.resolve(__dirname, './dist'),
    },
    module:{
        rules: [{
            test: /\.(mp4|png)/,
            type: 'asset/inline'
        }]
    },
    plugins: [
        new HtmlWebpackPlugin(),
        new HtmlInlineScriptPlugin(),
    ],
};

here a demo app code:

// app.js
import Phaser from './phaser.min.js';

import videoFile from '../media/video.mp4';
import imageFile from '../media/demo.png';

let base64ImageFile = imageFile;
let base64VideoFile = videoFile;

let config = {
    scene: { 
        preload: function(){
            this.textures.addBase64('demo', base64ImageFile);
            this.load.video('video', base64VideoFile);
        },
        create: function() { 
            this.add.text(10, 10, 'DEMO')
                .setOrigin(0); 
            this.add.image(10, 30, 'demo')
                .setScale(.25)
                .setOrigin(0);
            this.add.video(300, 30, 'video')
                .setScale(.5)
                .setOrigin(0);
        } 
    }
};

new Phaser.Game(config);

The project structure for the demo would look like this:

screenshot project folder structure

And it would create just one output html-file, all other files are inlined.

winner_joiner
  • 12,173
  • 4
  • 36
  • 61
  • Are you sure about the video thing? because I use URl-loader and it gives me this message when I try to import videos: client:159 ./assets/Stage 1.mp4 1:0 Module parse failed: Unexpected character ' – a_7md.f May 29 '23 at 10:55
  • I'm 100% sure, because the demo described above works _(I built it)_. can you share the import line in your question or in the comment? I would check if the file path is correct, the error message suggests one quote mark **'** seems to be wrong, missing or too much _(may be even in the line above)._ – winner_joiner May 29 '23 at 11:54
  • **btw.:** you don't need the url-loader, the code and plugins I posted is all you would need. – winner_joiner May 29 '23 at 12:24
  • I got ths error : Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema. - configuration.module.rules[0].type should be one of these: "javascript/auto" | "javascript/dynamic" | "javascript/esm" | "json" | "webassembly/experimental" -> Module type to use for the module – a_7md.f May 29 '23 at 12:40
  • it is hard to say without seeing the code where the problem is, that why I uploaded this small demo to a [github repo](https://github.com/akumagamo/html-quick-demo/tree/main), if you follow the guide it should work. _(the `package.json`, should contain all the packages needed in the correct versions)_ – winner_joiner May 29 '23 at 13:13
  • @a_7md.f did the demo work? do you has some issues with the code? – winner_joiner Jun 09 '23 at 16:58