1.1.0 • Published 3 years ago

nmp-player v1.1.0

Weekly downloads
54
License
MIT
Repository
github
Last release
3 years ago

nmp-player

Node Minecraft Protocol Video Player

Description

This library allows you to display videos using the 'map' item in minecraft. Currently supports:

Contributing

Feel free to open an issue or a pull request! :D

Install

Available on npm npm install nmp-player

API

NMP-Player

  • VideoPlayer
  • Video
  • Converter

Class: VideoPlayer(server, options) extends EventEmitter

A video player. Extends EventEmitter. Args:

  • Server: minecraft-protocol server.
  • Options: Object
  • options.ID: number
  • options.frame: function, if given will call with buffer data to display frames

Example

const { VideoPlayer } = require('nmp-player')

<...>

videoplayer = new VideoPlayer(server)

videoplayer.play('./video.json')

videoplayer.on('play', (video) => console.log() )

Properties

videoplayer.loaded

The loaded video. Is Video or null.

videoplayer.ID

A number. Represents the ID of the video player. Used for sending map packets. Default: 0

Functions

videoplayer.giveMaps(options)

Gives a map with the video player's ID to all the players in the server.

  • options: Object
  • options.slot: The slot to give the item to. Defaults to 36.

videoplayer.load(src)

Loads a video.

videoplayer.play(resource)

Plays the loaded video, loads the resource if given first.

  • resource: A source Returns: Promise (resolves after video stops playing)

Events

"finished" (video)

Fires when a video is finished.

"play" (video)

Fires when a video starts playing.

"frame" (video, buffer, frameNo)

Fires every frame in a video.

  • video: Video
  • buffer: Buffer data of the frame.
  • frameNo: Number of the frame

Class: SongPlayer(server)

A song player. Plays .nbs files, using nbs.js. Server is optional, but you should write your own _note method. Extends EventEmitter.

Example

const { SongPlayer } = require('nmp-player')

<...>

songplayer = new SongPlayer()

songplayer._note = function(packet){
	packet.x = 0
	packet.y = 0
	packet.z = 0
	client.write('sound_effect', packet)
}

songplayer.play('./song.nbs')

Properties

songplayer.song

The loaded Song

songplayer.tick

The current tick

songplayer.interval

The interval of the song player

songplayer.playing

Boolean representing if a song is playing

Functions

songplayer.load(src)

Try to load src, can be:

  • Song
  • String (filename)

songplayer.play(src)

Plays song. src is same as load()

songplayer._note(packet)

Handles notes, you must set this to your own function.

  • The packet name is 'sound_effect'
  • You must give x, y, z to the packet, calculate this by multiplying the players coordinate by 8.

songplayer.stop()

Stops playing song

Events

"play" (song)

Fires when a song starts playing.

"stop"

Fires when the songplayer is manually stopped.

"end"

Fires when song ends.


Class: Video

Used internally. You should not generally initialize this class yourself. Either way, constructor:

  • videoplayer: VideoPlayer
  • data: Video Data (An array of hex-encoded buffer strings)
  • meta: Object containing metadata of the video.

Properties

video.FPS

Number. The FPS of the video.

video.playing

Boolean representing if the video is currently playing or not.

video.data

An array of buffers that represent the frames.

video.meta

An object for video metadata. If the video is loaded from a file, it can contain 'filename'


Class: Converter

Static Methods

Converter.extractFrames(options)

Extracts frames using ffmpeg.

  • options: Object
  • options.filename: Path to the video or name of the video. Default: "./video.mp4"
  • options.ffmpegPath: Path to ffmpeg. Default: "%FFMPEG_PATH%"
  • options.path: Name of the folder that you want to extract the frames to. Default: "frames" Returns: Promise (resolves when ffmpeg finishes)

Converter.convertFrames(path, delete)

Converts frames from a folder. Use this after Converter.extractFrames() Note: This method does not save the converted frames. Look for the output.

  • path: Folder of the frames. Default: "frames"
  • delete: Deletes the converted frame or not. Default: true Returns: Promise<Array>

Converter.convertToMap(buffer)

Converts buffer to map data (resized to 128x128). I dont really know how to handle the output, check code? This method just does resizeForMap and toMap.

  • buffer: image Returns: Promise

Converter.toMap(buffer)

Converts buffer to map data. Warn: Does not resize.

  • buffer: image (pixels) Returns: Promise

Converter.resizeForMap(buffer, width)

Resizes the image. (128x128)

  • buffer: image (pixels)
  • width: number (Default 128)
  • height: number (Default 128) Returns: Promise

Function: downloadYoutube(URL, filename)

Downloads a video.

  • URL: Video URL
  • filename: Name of the file to save to. Default: "./video.mp4" Returns: EventEmitter - Events: - "progress" (percentage) - "finish" (filename)

Function: convertYoutube(URL)

Downloads, extracts and converts the frames, then saves the video as "video.json"

  • URL: Video URL Returns: Promise<"video.json">

Source

Either

  • File path of a video json
  • A video json
  • An array of hex-encoded buffer strings

Update Logs

1.0.2

  • Added SongPlayer
  • Server is now optional for VideoPlayer
  • Added the ability to add custom frame display functions to VideoPlayer

1.0.1

  • Changed Converter class function names to be more understandable