Check out the Demo
The VideoEditor is a responsive VanillaJS library that allows users to crop, trim, and save videos. It is a component-based library that can be used in any web application. The VideoEditor is designed to be simple and easy-to-use and customizable to fit your needs. Please note that it does not perform any video transformations itself, but it does provide an object detailing the transformations that can be used to transform the video with a backend service.
import VideoEditor from '../VideoEditor';
const options = {
src: 'https://storage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4',
};
const videoEditor = new VideoEditor({ options });
videoEditor.render(myHTMLContainer);
const options = {
src : 'https://storage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4';
crop: { width: 7, height: 4 },
maxHeight: 450,
transformations: {
crop: { h: 173, scale: '0.2', w: 343, x: '308', y: '153' },
time: { in: 5, out: 10 },
},
limit: { maxDuration: 10 },
menuBarButtons: {
inlineEndButtons: {
cancel: {
index: 3,``
label: 'Exit',
fontAwesomeIcon: 'fa fa-times',
},
},
},
onError: (error) => {
// do something with the error
},
onSave: (transform, videoSrc) => {
// do something with the transformed video
},
onTimelineClick: (timeIndex) => {
// get the current time index
},
onClickHelpButton: (event) => {
// custom help button
},
onRangeLimit: ({ marker, maxDuration, time }) => {
// do something when the range limit is reached
},
onRangeUpdate: (...args) => {
// do something when the range is updated
},
}
You can save the video by passing a callback to the onSave option or by calling the save method on the VideoEditor instance. The VideoEditor does not perform any transformations, however it does provide an object detailing the transformations that can be used to transform the video. For convenience, the video source is also provided.
const options = {
src : 'https://storage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4';
onSave: (transformations, videoSrc) => {
// do something with the transformations and video source
},
}
const videoEditor = new VideoEditor({ options });
videoEditor.render(myHTMLContainer);
// after the video is edited...
const [transformations, videoSrc] = videoEditor.save();
The transformations object contains the crop and time transformations.
{
crop: { h: 173, scale: '0.2', w: 343, x: '308', y: '153' },
time: { in: 5, out: 10 }, // time in seconds
}
@type string | Blob
The video source. This can be a Blob or a URL string.
Example: 'https://storage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4'
@type object
An object with the width and height of the crop.
Example: { width: 16, height: 9 }
@type object
An object with the crop and time transformations.
Example: { crop: { h: 173, scale: '0.2', w: 343, x: '308', y: '153' }, time: { in: 5, out: 10 } }
@type number
The max height of the video editor. The default is 300.
@type object
An object with the max duration the video can be trimmed to
Example: { maxDuration: 5 }
@type object
An object with custom menu bar buttons with option to disable default buttons.
Example: { inlineStartButtons: {}, inlineEndButtons: {}, disable: []}
Menu bar button properties:
- index: the position of the button
- label: the button label
- fontAwesomeIcon: the font awesome icon Example: { index: 3, label: 'Exit', fontAwesomeIcon: 'fa fa-times' }
Example: { inlineEndButtons: { cancel: { index: 3, label: 'Exit', fontAwesomeIcon: 'fa fa-times' } } }
@type object
An object with keys of button names to disable
The default button keys are: 'mute', 'crop', 'save', 'help'
Example: { mute: true, crop: true }
@type function
Error callback. If the error type is AxiosError, the error will contain status and statusText properties.
Example: (error) => { console.error(error) }
Example of AxiosError: (error) => { console.error(error.status, error.statusText) }
@type function
onReady callback
Example: () => { console.log('video editor is ready') }
@type function
Callback when the range is updated
Example: (currentIndex, { in : 0, out : 1 }) => { console.log(currentIndex) }
@type function
Callback when the range limit is reached
Example: ({ marker, maxDuration, time }) => { console.log('range limit reached') }
@type function
Callback when the marker drag is started
Example: (currentMarker) => { console.log('marker drag started', currentMarker) }
@type function
Callback when the marker drag is ended
Example: (currentMarker) => { console.log('marker drag ended', currentMarker) }
You can test the VideoEditor in a local environment. Make sure to install npm depencies, including dependencies.
npm install --dev
npm run start
I welcome contributions and suggestions.
If you would like to contribute to the VideoEditor, please read the CONTRIBUTING.md file for more information.