Last active
May 16, 2022 09:48
-
-
Save KostaMalsev/99831b9e842ca4b9781d642213853309 to your computer and use it in GitHub Desktop.
Gif creator jscript part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Set up Image and video uploaders: | |
window.addEventListener('load', async function() { | |
//Image loader: | |
imageUploadButton.addEventListener('change', async function() { | |
if (videoDownloadLink) videoDownloadLink.remove(); | |
if (this.files && this.files[0]) { | |
imgTag.onload = () => { | |
URL.revokeObjectURL(imgTag.src); // no longer needed, free memory | |
} | |
imgTag.src = URL.createObjectURL(this.files[0]); // set src to blob url | |
//Generate blob from url: | |
let blob = await fetch(imgTag.src).then(r => r.blob()); | |
//Convert image to buffer Array: | |
retreiveImage(blob); | |
// show convert button | |
button.classList.remove('hidden', 'gray'); | |
uploadButtons.classList.add('hidden'); | |
//Set to convert gif to video: | |
button.removeEventListener('click', convertVideo2Gif); | |
button.removeEventListener('click', convertGifToMp4); | |
//Attach run command to convert button | |
button.addEventListener('click', convertGifToMp4); | |
} | |
}); | |
inputTag.addEventListener('change', readVideo); | |
//Read video from url | |
async function readVideo(event) { | |
if (videoDownloadLink) videoDownloadLink.remove(); | |
if (event.target.files && event.target.files[0]) { | |
fileName = event.target.files[0]; | |
var srcResult = URL.createObjectURL(event.target.files[0]); | |
videoTag.onload = () => { | |
URL.revokeObjectURL(srcResult); // no longer needed, free memory | |
} | |
videoTag.src = srcResult; | |
videoTag.play(); | |
videoTag.classList.remove('hidden'); | |
//Generate blob from url: | |
let blob = await fetch(srcResult).then(r => r.blob()); | |
//Convert image to buffer Array: | |
retreiveVideo(event.target.files[0]); | |
} | |
uploadButtons.classList.add('hidden'); | |
// show convert button | |
button.classList.remove('hidden', 'gray'); | |
//Attach run command to convert button | |
button.removeEventListener('click', convertVideo2Gif); | |
button.removeEventListener('click', convertGifToMp4); | |
button.addEventListener('click', convertVideo2Gif); | |
} | |
}); | |
//Push to arrayBuffer of image | |
function retreiveImage(blob) { | |
var fileReader = new FileReader(); | |
fileReader.onload = function(event) { | |
sampleImageData = new Uint8Array(event.target.result); | |
}; | |
fileReader.readAsArrayBuffer(blob); | |
} | |
//Push to arrayBuffer of video | |
function retreiveVideo(file) { | |
var fileReader = new FileReader(); | |
fileReader.onload = function(event) { | |
sampleVideoData = new Uint8Array(event.target.result); | |
}; | |
fileReader.readAsArrayBuffer(file); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment