Clean up browser examples
This commit is contained in:
parent
5f48200442
commit
93cf474b9e
@ -1,20 +1,8 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<script src="/dist/ffmpeg.dev.js"></script>
|
<link rel="stylesheet" href="style.css">
|
||||||
<style>
|
<script src="../../packages/ffmpeg/dist/umd/ffmpeg.js"></script>
|
||||||
html,
|
<script src="../../packages/util/dist/umd/index.js"></script>
|
||||||
body {
|
|
||||||
margin: 0;
|
|
||||||
width: 100%;
|
|
||||||
height: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
align-items: center;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
@ -23,24 +11,35 @@
|
|||||||
<input type="file" id="uploader" multiple />
|
<input type="file" id="uploader" multiple />
|
||||||
<p id="message"></p>
|
<p id="message"></p>
|
||||||
<script>
|
<script>
|
||||||
const { createFFmpeg, fetchFile } = FFmpeg;
|
const { fetchFile } = FFmpegUtil;
|
||||||
const ffmpeg = createFFmpeg({ log: true });
|
const { FFmpeg } = FFmpegWASM;
|
||||||
|
let ffmpeg = null;
|
||||||
|
|
||||||
const transcode = async ({ target: { files } }) => {
|
const transcode = async ({ target: { files } }) => {
|
||||||
const message = document.getElementById("message");
|
const message = document.getElementById("message");
|
||||||
message.innerHTML = "Loading ffmpeg-core.js";
|
if (ffmpeg === null) {
|
||||||
await ffmpeg.load();
|
ffmpeg = new FFmpeg();
|
||||||
|
ffmpeg.on("log", ({ message }) => {
|
||||||
|
console.log(message);
|
||||||
|
})
|
||||||
|
ffmpeg.on("progress", ({ progress }) => {
|
||||||
|
message.innerHTML = `${progress * 100} %`;
|
||||||
|
});
|
||||||
|
await ffmpeg.load({
|
||||||
|
coreURL: "/packages/core/dist/umd/ffmpeg-core.js",
|
||||||
|
});
|
||||||
|
}
|
||||||
message.innerHTML = "Start Concating";
|
message.innerHTML = "Start Concating";
|
||||||
const inputPaths = [];
|
const inputPaths = [];
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const { name } = file;
|
const { name } = file;
|
||||||
ffmpeg.FS('writeFile', name, await fetchFile(file));
|
ffmpeg.writeFile(name, await fetchFile(file));
|
||||||
inputPaths.push(`file ${name}`);
|
inputPaths.push(`file ${name}`);
|
||||||
}
|
}
|
||||||
ffmpeg.FS('writeFile', 'concat_list.txt', inputPaths.join('\n'));
|
await ffmpeg.writeFile('concat_list.txt', inputPaths.join('\n'));
|
||||||
await ffmpeg.run('-f', 'concat', '-safe', '0', '-i', 'concat_list.txt', 'output.mp4');
|
await ffmpeg.exec(['-f', 'concat', '-safe', '0', '-i', 'concat_list.txt', 'output.mp4']);
|
||||||
message.innerHTML = "Complete Concating";
|
message.innerHTML = "Complete Concating";
|
||||||
const data = ffmpeg.FS('readFile', 'output.mp4');
|
const data = await ffmpeg.readFile('output.mp4');
|
||||||
const video = document.getElementById("output-video");
|
const video = document.getElementById("output-video");
|
||||||
video.src = URL.createObjectURL(
|
video.src = URL.createObjectURL(
|
||||||
new Blob([data.buffer], {
|
new Blob([data.buffer], {
|
||||||
|
@ -1,53 +0,0 @@
|
|||||||
<html>
|
|
||||||
<head>
|
|
||||||
<script src="/dist/ffmpeg.dev.js"></script>
|
|
||||||
<style>
|
|
||||||
html, body {
|
|
||||||
margin: 0;
|
|
||||||
width: 100%;
|
|
||||||
height: 100%
|
|
||||||
}
|
|
||||||
body {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
align-items: center;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h3>Click start to transcode images to mp4 (x264) and play!</h3>
|
|
||||||
<video id="output-video" controls></video><br/>
|
|
||||||
<button id="start-btn">Start</button>
|
|
||||||
<p id="message"></p>
|
|
||||||
<a href="https://github.com/ffmpegjs/ffmpeg.js/tree/master/examples/assets/triangle">Data Set</a>
|
|
||||||
<script>
|
|
||||||
const { createFFmpeg, fetchFile } = FFmpeg;
|
|
||||||
const ffmpeg = createFFmpeg({ log: true });
|
|
||||||
|
|
||||||
const image2video = async () => {
|
|
||||||
const message = document.getElementById('message');
|
|
||||||
message.innerHTML = 'Loading ffmpeg-core.js';
|
|
||||||
await ffmpeg.load();
|
|
||||||
message.innerHTML = 'Loading data';
|
|
||||||
ffmpeg.FS('writeFile', 'audio.ogg', await fetchFile('../assets/triangle/audio.ogg'));
|
|
||||||
for (let i = 0; i < 60; i += 1) {
|
|
||||||
const num = `00${i}`.slice(-3);
|
|
||||||
ffmpeg.FS('writeFile', `tmp.${num}.png`, await fetchFile(`../assets/triangle/tmp.${num}.png`));
|
|
||||||
}
|
|
||||||
message.innerHTML = 'Start transcoding';
|
|
||||||
await ffmpeg.run('-framerate', '30', '-pattern_type', 'glob', '-i', '*.png', '-i', 'audio.ogg', '-c:a', 'copy', '-shortest', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', 'out.mp4');
|
|
||||||
const data = ffmpeg.FS('readFile', 'out.mp4');
|
|
||||||
ffmpeg.FS('unlink', 'audio.ogg')
|
|
||||||
for (let i = 0; i < 60; i += 1) {
|
|
||||||
const num = `00${i}`.slice(-3);
|
|
||||||
ffmpeg.FS('unlink', `tmp.${num}.png`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const video = document.getElementById('output-video');
|
|
||||||
video.src = URL.createObjectURL(new Blob([data.buffer], { type: 'video/mp4' }));
|
|
||||||
}
|
|
||||||
const elm = document.getElementById('start-btn');
|
|
||||||
elm.addEventListener('click', image2video);
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
@ -11,21 +11,29 @@
|
|||||||
<p id="message"></p>
|
<p id="message"></p>
|
||||||
<script>
|
<script>
|
||||||
const { fetchFile } = FFmpegUtil;
|
const { fetchFile } = FFmpegUtil;
|
||||||
|
const { FFmpeg } = FFmpegWASM;
|
||||||
let ffmpeg = null;
|
let ffmpeg = null;
|
||||||
|
|
||||||
const trim = async ({ target: { files } }) => {
|
const trim = async ({ target: { files } }) => {
|
||||||
const message = document.getElementById('message');
|
const message = document.getElementById('message');
|
||||||
if (ffmpeg === null) {
|
if (ffmpeg === null) {
|
||||||
ffmpeg = await createFFmpeg();
|
ffmpeg = new FFmpeg();
|
||||||
ffmpeg.setProgress((progress) => { console.log(progress * 100) });
|
ffmpeg.on("log", ({ message }) => {
|
||||||
|
console.log(message);
|
||||||
|
})
|
||||||
|
ffmpeg.on("progress", ({ progress }) => {
|
||||||
|
message.innerHTML = `${progress * 100} %`;
|
||||||
|
});
|
||||||
|
await ffmpeg.load({
|
||||||
|
coreURL: "/packages/core/dist/umd/ffmpeg-core.js",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
const { name } = files[0];
|
const { name } = files[0];
|
||||||
message.innerHTML = 'Loading ffmpeg-core.js';
|
await ffmpeg.writeFile(name, await fetchFile(files[0]));
|
||||||
message.innerHTML = 'Start trimming';
|
message.innerHTML = 'Start trimming';
|
||||||
ffmpeg.FS.writeFile(name, await fetchFile(files[0]));
|
await ffmpeg.exec(['-i', name, '-ss', '0', '-to', '1', 'output.mp4']);
|
||||||
await ffmpeg.exec('-i', name, '-ss', '0', '-to', '1', 'output.mp4');
|
|
||||||
message.innerHTML = 'Complete trimming';
|
message.innerHTML = 'Complete trimming';
|
||||||
const data = ffmpeg.FS.readFile('output.mp4');
|
const data = await ffmpeg.readFile('output.mp4');
|
||||||
|
|
||||||
const video = document.getElementById('output-video');
|
const video = document.getElementById('output-video');
|
||||||
video.src = URL.createObjectURL(new Blob([data.buffer], { type: 'video/mp4' }));
|
video.src = URL.createObjectURL(new Blob([data.buffer], { type: 'video/mp4' }));
|
||||||
|
@ -1,72 +0,0 @@
|
|||||||
<html>
|
|
||||||
<head>
|
|
||||||
<script src="/dist/ffmpeg.dev.js"></script>
|
|
||||||
<style>
|
|
||||||
html, body {
|
|
||||||
margin: 0;
|
|
||||||
width: 100%;
|
|
||||||
height: 100%
|
|
||||||
}
|
|
||||||
body {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
align-items: center;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h3>Record video from webcam and transcode to mp4 (x264) and play!</h3>
|
|
||||||
<div>
|
|
||||||
<video id="webcam" width="320px" height="180px"></video>
|
|
||||||
<video id="output-video" width="320px" height="180px" controls></video>
|
|
||||||
</div>
|
|
||||||
<button id="record" disabled>Start Recording</button>
|
|
||||||
<p id="message"></p>
|
|
||||||
<script>
|
|
||||||
const { createFFmpeg, fetchFile } = FFmpeg;
|
|
||||||
const ffmpeg = createFFmpeg({ log: true });
|
|
||||||
|
|
||||||
const webcam = document.getElementById('webcam');
|
|
||||||
const recordBtn = document.getElementById('record');
|
|
||||||
const startRecording = () => {
|
|
||||||
const rec = new MediaRecorder(webcam.srcObject);
|
|
||||||
const chunks = [];
|
|
||||||
|
|
||||||
recordBtn.textContent = 'Stop Recording';
|
|
||||||
recordBtn.onclick = () => {
|
|
||||||
rec.stop();
|
|
||||||
recordBtn.textContent = 'Start Recording';
|
|
||||||
recordBtn.onclick = startRecording;
|
|
||||||
}
|
|
||||||
|
|
||||||
rec.ondataavailable = e => chunks.push(e.data);
|
|
||||||
rec.onstop = async () => {
|
|
||||||
transcode(new Uint8Array(await (new Blob(chunks)).arrayBuffer()));
|
|
||||||
};
|
|
||||||
rec.start();
|
|
||||||
};
|
|
||||||
|
|
||||||
(async () => {
|
|
||||||
webcam.srcObject = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
|
|
||||||
await webcam.play();
|
|
||||||
recordBtn.disabled = false;
|
|
||||||
recordBtn.onclick = startRecording;
|
|
||||||
})();
|
|
||||||
|
|
||||||
const transcode = async (webcamData) => {
|
|
||||||
const message = document.getElementById('message');
|
|
||||||
const name = 'record.webm';
|
|
||||||
message.innerHTML = 'Loading ffmpeg-core.js';
|
|
||||||
await ffmpeg.load();
|
|
||||||
message.innerHTML = 'Start transcoding';
|
|
||||||
ffmpeg.FS('writeFile', name, await fetchFile(webcamData));
|
|
||||||
await ffmpeg.run('-i', name, 'output.mp4');
|
|
||||||
message.innerHTML = 'Complete transcoding';
|
|
||||||
const data = ffmpeg.FS('readFile', 'output.mp4');
|
|
||||||
|
|
||||||
const video = document.getElementById('output-video');
|
|
||||||
video.src = URL.createObjectURL(new Blob([data.buffer], { type: 'video/mp4' }));
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
Loading…
Reference in New Issue
Block a user