mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 10:21:36 +03:00
d3bd7e8767
refs https://github.com/TryGhost/Team/issues/1229 - `mimeType` was specified in the payload but it had been missed when extracting/setting the video metadata
47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
export default function extractVideoMetadata(file) {
|
|
return new Promise((resolve, reject) => {
|
|
const mimeType = file.type;
|
|
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,
|
|
mimeType,
|
|
thumbnailBlob
|
|
});
|
|
}, 'image/jpeg', 0.75);
|
|
};
|
|
|
|
video.src = URL.createObjectURL(file);
|
|
});
|
|
}
|