• Stars
    star
    260
  • Rank 156,621 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🌊 WFPlayer.js is an audio waveform generator

WFPlayer

version license size

WFPlayer.js is an audio waveform generator

Screenshot

Demo

Checkout the demo from Github Pages

Features

  • Create waveforms without loading the entire media file
  • Customize cursor, progress, grid, ruler display and color
  • Support for loading media urls and loading media dom elements (video tags and audio tags)
  • Support for real-time change options like color or width etc
  • Listen to the playback state of media elements for playback animation
  • Adaptive parent element size and audio data
  • And more...

Install

Install with npm

$ npm install wfplayer

Or install with yarn

$ yarn add wfplayer
import WFPlayer from 'wfplayer';

Or umd builds are also available

<script src="path/to/wfplayer.js"></script>

Will expose the global variable to window.WFPlayer.

Usage

HTML

<div id="waveform" style="width: 1000px; height: 300px"></div>
<video id="video" src="path/to/video.mp4"></video>

<!-- or -->
<audio id="audio" src="path/to/audio.mp3"></audio>

JS

var wf = new WFPlayer({
    container: document.querySelector('#waveform'),
    mediaElement: document.querySelector('#video'),
});

wf.load(document.querySelector('#video'));

// or
wf.load('path/to/audio.mp3');

API

Options

var wf = new WFPlayer({
    // Mount the audio waveform of the dom
    container: '#waveform',

    // Whether to use scroll mode
    scrollable: false,

    // Media element like: video tag or audio tag
    mediaElement: null,

    // Whether use web worker
    useWorker: true,

    // Thw refresh delay time
    refreshDelay: 50,

    // Whether to display wave
    wave: true,

    // Waveform color
    waveColor: 'rgba(255, 255, 255, 0.1)',

    // Background color
    backgroundColor: 'rgb(28, 32, 34)',

    // Whether to display cursor
    cursor: true,

    // Cursor color
    cursorColor: '#ff0000',

    // Whether to display progress
    progress: true,

    // progress color
    progressColor: 'rgba(255, 255, 255, 0.5)',

    // Whether to display grid
    grid: true,

    // Grid color
    gridColor: 'rgba(255, 255, 255, 0.05)',

    // Whether to display ruler
    ruler: true,

    // Ruler color
    rulerColor: 'rgba(255, 255, 255, 0.5)',

    // Whether to display scrollbar
    scrollbar: true,

    // Scrollbar color
    scrollbarColor: 'rgba(255, 255, 255, 0.25)',

    // Whether to display ruler at the top
    rulerAtTop: true,

    // Pixel ratio
    pixelRatio: window.devicePixelRatio,

    // Which audio channel to render
    channel: 0,

    // Duration of rendering
    duration: 10,

    // The ratio of spaces on both sides
    padding: 5,

    // Waveform height scale ratio
    waveScale: 0.8,

    // Waveform Size ratio
    waveSize: 1,
});

Instance methods and properties

Load target:

// The target can be the url address of media or a mediaElement or ArrayBuffer or Audiobuffer
wf.load(target);

Change Channel:

wf.changeChannel(1);

Jump to a certain time:

wf.seek(second);

Jump to a certain time with smooth:

wf.smoothSeek(second);

Export image:

wf.exportImage();

Modify option:

wf.setOptions({
    // Like change wave color
    waveColor: 'red',
});

Destroy instance:

wf.destroy();

Common Problem

When decoding a video to an audio waveform, it will cause insufficient browser memory.

If the video volume is too large, it will cause the front-end decoding difficult. Best practice is to use the server's FFMPEG, convert the video into audio format MP3.

-ac is the number of channels, -ar is a sample rate:

Back End

ffmpeg -i path/to/video.mp4 -ac 1 -ar 8000 path/to/audio.mp3

HTML

<div id="waveform" style="width: 1000px; height: 300px"></div>
<video id="video" src="path/to/video.mp4"></video>

JS

var wf = new WFPlayer({
    container: document.querySelector('#waveform'),
    mediaElement: document.querySelector('#video'),
});

wf.load('path/to/audio.mp3');

// or
fetch('path/to/audio.mp3')
    .then((res) => res.arrayBuffer())
    .then((arrayBuffer) => {
        const uint8 = new Uint8Array(arrayBuffer);
        wf.load(uint8);
    });

If you really don't want to use the server to transfer, I recommend to use @ffmpeg/ffmpeg

npm i -S @ffmpeg/ffmpeg

HTML

<div id="waveform" style="width: 1000px; height: 300px"></div>
<video id="video" src="path/to/video.mp4"></video>
<input type="file" id="file" />

JS

import FFmpeg from '@ffmpeg/ffmpeg';

const wf = new WFPlayer({
    container: document.querySelector('#waveform'),
    mediaElement: document.querySelector('#video'),
});

document.getElementById('file').addEventListener('change', async (event) => {
    const file = event.target.files[0];
    const { createFFmpeg, fetchFile } = FFmpeg;
    const ffmpeg = createFFmpeg({ log: true });
    await ffmpeg.load();
    ffmpeg.FS('writeFile', file.name, await fetchFile(file));
    await ffmpeg.run('-i', file.name, '-ac', '1', '-ar', '8000', 'audio.mp3');
    const uint8 = ffmpeg.FS('readFile', 'audio.mp3');

    await wf.load(uint8);
})

How to add a custom event

var wf = new WFPlayer({
    container: document.querySelector('#waveform'),
    mediaElement: document.querySelector('#video'),
});

wf.load('path/to/audio.mp3');

// click event
wf.on('click', (currentTime) => {
     wf.seek(currentTime)
});

// grab event
wf.on('grabbing', (currentTime) => {
    wf.seek(currentTime)
});

// scroll event
wf.on('scroll', (deltaY) => {
    wf.seek(wf.currentTime + deltaY / 10)
});

How to get currentTime from an event

var waveform = document.querySelector('#waveform')
var wf = new WFPlayer({
    container: waveform,
    mediaElement: document.querySelector('#video'),
});

wf.load('path/to/audio.mp3');

waveform.addEventListener('mousemove', event => {
    const currentTime = this.getCurrentTimeFromEvent(event);
    console.log(currentTime);
})

waveform.addEventListener('click', event => {
    const currentTime = this.getCurrentTimeFromEvent(event);
    console.log(currentTime);
})

Donations

We accept donations through these channels:

QQ Group

QQ Group

License

MIT © Harvey Zack

More Repositories

1

ArtPlayer

🎨 ArtPlayer.js is a modern and full featured HTML5 video player
JavaScript
2,453
star
2

SubPlayer

📝 SubPlayer is an online subtitle editor
JavaScript
932
star
3

live-video-study-notes

📺 整理前端视频直播相关技术的笔记,适合想入门前端流媒体技术的人阅读
591
star
4

FlvPlayer

🍭 FlvPlayer.js is a JavaScript player for decode flv to the canvas
JavaScript
300
star
5

bilibili-live-hime

🍓 Chrome 扩展 - Bilibili 直播姬
JavaScript
222
star
6

Teahouse-Wordpress-Theme

🍵 Wordpress blog theme Teahouse
PHP
105
star
7

100plugins

💯 Write 100 javascript plugins
JavaScript
40
star
8

blank

Generate blank video online
JavaScript
17
star
9

term-web

📟 A simple Terminal UI that run on the web
JavaScript
17
star
10

v2ex-card

Chrome 扩展 - v2ex 悬停名片
JavaScript
16
star
11

snippets-box

📒 Code snippet manager based on GitHub Gist, Github Pages
JavaScript
16
star
12

chrome-audio-capture

Chrome Audio Capture
JavaScript
15
star
13

pluto

Wordpress 博客主题 Pluto 源码
PHP
14
star
14

mse-player

基于MSE实现的视频分段加载播放器
TypeScript
14
star
15

traitor

有内鬼,终止交易!
JavaScript
13
star
16

bilibili-live-recorder

Bilibili 直播间录播姬
JavaScript
12
star
17

chrome-extension-vuexTree

一个Chrome扩展,为 Vuex 实现类似 redux-devtools-extension 的状态树展示效果
JavaScript
10
star
18

RNHearthstone

React Native 《炉石传说》查卡App
JavaScript
7
star
19

duration-time-conversion

Duration and time format conversion
JavaScript
6
star
20

console.log

console.log
JavaScript
5
star
21

island

Wordpress 博客主题 Island 源码
PHP
5
star
22

record-hime

🎈 Chrome 扩展 - 录播姬
JavaScript
5
star
23

owl

Wordpress 博客主题 Owl 源码
PHP
4
star
24

flv-reader

🍬 [WIP] Parse flv to fmp4 for Media Source Extension
JavaScript
3
star
25

csp-generator

Content Security Policy Generator
JavaScript
3
star
26

dida.js

An audio stream blender for the browser.
JavaScript
3
star
27

http-flv-streaming-demo

http-flv 直播流例子
Dockerfile
3
star
28

puppeteer-crx

Use puppeteer to package chrome extensions(.crx)
JavaScript
3
star
29

rollup-plugin-worker-inline

A simple rollup plugin to create inline worker
JavaScript
3
star
30

gloom

Wordpress 博客主题 Gloom 源码
PHP
3
star
31

website-builder

多页面静态网站生成器
JavaScript
3
star
32

option-validator

A simple option validator
JavaScript
3
star
33

Webpack-MultiplePage

Webpack多页面打包分析
JavaScript
2
star
34

gitting

💬 Gitting is a modern comment component based on Github Issue.
JavaScript
2
star
35

bilibili-danmuku

获取B站弹幕
JavaScript
2
star
36

nodejs-quick-view

NodeJS文档快速预览
JavaScript
2
star
37

obj-to-string

对象转字符串(非JSON)
JavaScript
2
star
38

detect-connection-speed

Detect connection speed with JavaScript
JavaScript
2
star
39

simple-observer

A simple observer with ES6 Proxy
JavaScript
2
star
40

assets-cdn

CDN资源中转站
2
star
41

Express-mongoose-Passport

使用Express搭建一个简易的全栈管理系统
JavaScript
2
star
42

table-export-zip

打包下载分割的表格
JavaScript
2
star
43

hitokoto-cli

A Hitokoto Cli Tool (一言命令行工具)
JavaScript
2
star
44

html-layout-webpack-plugin

webpack html 布局插件
JavaScript
1
star
45

srt.js

📺 Let the html5 video player support .srt format subtitles.
JavaScript
1
star
46

simple-i18n-webpack-plugin

webpack i18n plugin (webpack i18n 多语言插件)
JavaScript
1
star
47

island-cli

Island blog system command line tool
JavaScript
1
star
48

bilibili-cinema

🎥 Chrome 扩展 - Bilibili 影院点播系统
1
star
49

island-cli-theme-default

Island blog default theme
CSS
1
star
50

js-quick-view

JavaScript 标准库快速浏览
JavaScript
1
star
51

zhw2590582

1
star
52

blur-image-url

JavaScript
1
star
53

online-code-editor

HTML
1
star
54

threejs-template

Threejs Template
JavaScript
1
star
55

aimu-statistics

JavaScript
1
star
56

island-loader

A island loader for webpack
JavaScript
1
star
57

weibo-backup

Chrome 扩展 - 微博备份工具
1
star
58

auto-update-from-github

Auto update from github
JavaScript
1
star
59

island-cli-init

Island blog initialization framework
JavaScript
1
star
60

brusher-html

Wordpress 博客主题 Brusher 静态文件
JavaScript
1
star
61

mini-speed-chart

Detect connection speed with mini chart
JavaScript
1
star
62

artplayer-ie-tester

Test Artplayer.js in Internet Explorer
JavaScript
1
star
63

alioss-watcher

Chrome 扩展 - 阿里云OSS监听器
1
star