Comments (2)
Nevermind, I found a way to just convert the webm output into mp4 (so, the MediaRecorder way), that works much better:
saveCanvasToVideoWithAudio() {
let mp4 = this.selectedVideoFormat === 'mp4'
const canvas = document.getElementById("canvas") as HTMLCanvasElement;
const stream = canvas.captureStream(30);
const audioElements = this.editorElements.filter(isEditorAudioElement)
const audioStreams: MediaStream[] = [];
audioElements.forEach((audio) => {
const audioElement = document.getElementById(audio.properties.elementId) as HTMLAudioElement;
let ctx = new AudioContext();
let sourceNode = ctx.createMediaElementSource(audioElement);
let dest = ctx.createMediaStreamDestination();
sourceNode.connect(dest);
sourceNode.connect(ctx.destination);
audioStreams.push(dest.stream);
});
audioStreams.forEach((audioStream) => {
stream.addTrack(audioStream.getAudioTracks()[0]);
});
const video = document.createElement("video");
video.srcObject = stream;
video.height = 540;
video.width = 960;
// video.controls = true;
// document.body.appendChild(video);
video.play().then(() => {
const mediaRecorder = new MediaRecorder(stream);
const chunks: Blob[] = [];
mediaRecorder.ondataavailable = function (e) {
chunks.push(e.data);
console.log("data available");
};
mediaRecorder.onstop = async function (e) {
const blob = new Blob(chunks, { type: "video/webm" });
if (mp4) {
// lets use ffmpeg to convert webm to mp4
const data = new Uint8Array(await (blob).arrayBuffer());
const ffmpeg = new FFmpeg();
const baseURL = "https://unpkg.com/@ffmpeg/[email protected]/dist/umd"
await ffmpeg.load({
coreURL: await toBlobURL(`${baseURL}/ffmpeg-core.js`, 'text/javascript'),
wasmURL: await toBlobURL(`${baseURL}/ffmpeg-core.wasm`, 'application/wasm'),
// workerURL: await toBlobURL(`${baseURL}/ffmpeg-core.worker.js`, 'text/javascript'),
});
await ffmpeg.writeFile('video.webm', data);
await ffmpeg.exec(["-y", "-i", "video.webm", "-c", "copy", "video.mp4"]);
const output = await ffmpeg.readFile('video.mp4');
const outputBlob = new Blob([output], { type: "video/mp4" });
const outputUrl = URL.createObjectURL(outputBlob);
const a = document.createElement("a");
a.download = "video.mp4";
a.href = outputUrl;
a.click();
} else {
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "video.webm";
a.click();
}
};
mediaRecorder.start();
setTimeout(() => {
mediaRecorder.stop();
}, this.maxTime);
video.remove();
})
}
The button ends up like this:
<button
className="bg-gray-500 hover:bg-gray-700 text-white font-bold py-1 px-1 rounded-lg m-4"
onClick={() => {
store.handleSeek(0);
store.setSelectedElement(null);
setTimeout(() => {
store.setPlaying(true);
store.saveCanvasToVideoWithAudio();
}, 1000);
}}
>
Export Video ({store.maxTime / 1000} secs) {store.selectedVideoFormat === "mp4" ? ("ALPHA") : ""}
</button>
from fabric-video-editor.
@Ivanca thank you! I will check your suggested approach.
from fabric-video-editor.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fabric-video-editor.