(file, max, seekTo = 0.0, isURL)
| 3847 | } |
| 3848 | |
| 3849 | function createVideoThumbnail(file, max, seekTo = 0.0, isURL) { |
| 3850 | return new Promise((resolve, reject) => { |
| 3851 | const videoPlayer = document.createElement('video'); |
| 3852 | if (isURL) { |
| 3853 | videoPlayer.setAttribute('src', file); |
| 3854 | } else { |
| 3855 | videoPlayer.setAttribute('src', URL.createObjectURL(file)); |
| 3856 | } |
| 3857 | videoPlayer.setAttribute('crossorigin', 'anonymous'); |
| 3858 | videoPlayer.load(); |
| 3859 | videoPlayer.addEventListener('error', (ex) => { |
| 3860 | reject('error when loading video file', ex); |
| 3861 | }); |
| 3862 | videoPlayer.addEventListener('loadedmetadata', () => { |
| 3863 | if (videoPlayer.duration < seekTo) { |
| 3864 | reject('video is too short.'); |
| 3865 | return; |
| 3866 | } |
| 3867 | setTimeout(() => { |
| 3868 | videoPlayer.currentTime = seekTo; |
| 3869 | }, 200); |
| 3870 | videoPlayer.addEventListener('seeked', () => { |
| 3871 | var oc = document.createElement('canvas'); |
| 3872 | var octx = oc.getContext('2d'); |
| 3873 | oc.width = videoPlayer.videoWidth; |
| 3874 | oc.height = videoPlayer.videoheight; |
| 3875 | octx.drawImage(videoPlayer, 0, 0); |
| 3876 | if (videoPlayer.videoWidth > videoPlayer.videoHeight) { |
| 3877 | oc.height = |
| 3878 | (videoPlayer.videoHeight / videoPlayer.videoWidth) * max; |
| 3879 | oc.width = max; |
| 3880 | } else { |
| 3881 | oc.width = |
| 3882 | (videoPlayer.videoWidth / videoPlayer.videoHeight) * max; |
| 3883 | oc.height = max; |
| 3884 | } |
| 3885 | octx.drawImage(oc, 0, 0, oc.width, oc.height); |
| 3886 | octx.drawImage(videoPlayer, 0, 0, oc.width, oc.height); |
| 3887 | resolve(oc.toDataURL()); |
| 3888 | }); |
| 3889 | }); |
| 3890 | }); |
| 3891 | } |
| 3892 | |
| 3893 | function createThumbnail(file, max) { |
| 3894 | return new Promise(function (resolve, reject) { |
no test coverage detected