mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 10:21:36 +03:00
45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
|
export default function extractVideoMetadata(file) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
let duration, width, height;
|
||
|
|
||
|
const video = document.createElement('video');
|
||
|
video.preload = 'metadata';
|
||
|
video.muted = true;
|
||
|
video.playsInline = true;
|
||
|
|
||
|
video.onerror = reject;
|
||
|
|
||
|
video.onloadedmetadata = function () {
|
||
|
duration = video.duration;
|
||
|
width = video.videoWidth;
|
||
|
height = video.videoHeight;
|
||
|
|
||
|
setTimeout(() => {
|
||
|
video.currentTime = 0.5;
|
||
|
}, 200);
|
||
|
};
|
||
|
|
||
|
video.onseeked = function () {
|
||
|
const canvas = document.createElement('canvas');
|
||
|
canvas.width = width;
|
||
|
canvas.height = height;
|
||
|
|
||
|
const ctx = canvas.getContext('2d');
|
||
|
ctx.drawImage(video, 0, 0, width, height);
|
||
|
|
||
|
window.URL.revokeObjectURL(video.src);
|
||
|
|
||
|
ctx.canvas.toBlob((thumbnailBlob) => {
|
||
|
resolve({
|
||
|
duration,
|
||
|
width,
|
||
|
height,
|
||
|
thumbnailBlob
|
||
|
});
|
||
|
}, 'image/jpeg', 0.75);
|
||
|
};
|
||
|
|
||
|
video.src = URL.createObjectURL(file);
|
||
|
});
|
||
|
}
|