Spaces:
Running
Running
nicehero
commited on
Commit
·
05d832a
1
Parent(s):
7698ac4
trim.html
Browse files
trim.html
CHANGED
|
@@ -5,41 +5,72 @@
|
|
| 5 |
<script src="/assets/util/package/dist/umd/index.js"></script>
|
| 6 |
</head>
|
| 7 |
<body>
|
| 8 |
-
<h3>Upload a mp4 (x264) video and trim
|
| 9 |
-
<
|
|
|
|
| 10 |
<input type="file" id="uploader">
|
|
|
|
|
|
|
|
|
|
| 11 |
<p id="message"></p>
|
| 12 |
<script>
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
const trim = async ({ target: { files } }) => {
|
| 18 |
-
const message = document.getElementById('message');
|
| 19 |
-
if (ffmpeg === null) {
|
| 20 |
-
ffmpeg = new FFmpeg();
|
| 21 |
-
ffmpeg.on("log", ({ message }) => {
|
| 22 |
-
console.log(message);
|
| 23 |
-
})
|
| 24 |
-
ffmpeg.on("progress", ({ progress }) => {
|
| 25 |
-
message.innerHTML = `${progress * 100} %`;
|
| 26 |
-
});
|
| 27 |
-
await ffmpeg.load({
|
| 28 |
-
coreURL: "/assets/core/package/dist/umd/ffmpeg-core.js",
|
| 29 |
-
});
|
| 30 |
}
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
await ffmpeg.exec(['-i', name, '-ss', '0', '-to', '1', 'output.mp4']);
|
| 35 |
-
message.innerHTML = 'Complete trimming';
|
| 36 |
-
const data = await ffmpeg.readFile('output.mp4');
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
</script>
|
| 44 |
</body>
|
| 45 |
</html>
|
|
|
|
| 5 |
<script src="/assets/util/package/dist/umd/index.js"></script>
|
| 6 |
</head>
|
| 7 |
<body>
|
| 8 |
+
<h3>Upload a mp4 (x264) video and trim it!</h3>
|
| 9 |
+
<input type="text" id="startTime" placeholder="Start Time (second)">Start Time
|
| 10 |
+
<input type="text" id="endTime" placeholder="End Time (second)">End Time
|
| 11 |
<input type="file" id="uploader">
|
| 12 |
+
<button id="downloadButton" style="display:none;" >download</button>
|
| 13 |
+
<a id="downloadLink" style="display:none;">download</a>
|
| 14 |
+
<video id="output-video" controls></video><br/>
|
| 15 |
<p id="message"></p>
|
| 16 |
<script>
|
| 17 |
+
function generateRandomFileName() {
|
| 18 |
+
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
| 19 |
+
var length = 10; // 文件名长度
|
| 20 |
+
var fileName = '';
|
| 21 |
+
for (var i = 0; i < length; i++) {
|
| 22 |
+
var randomIndex = Math.floor(Math.random() * characters.length);
|
| 23 |
+
fileName += characters.charAt(randomIndex);
|
| 24 |
+
}
|
| 25 |
+
return fileName;
|
| 26 |
+
}
|
| 27 |
+
const { fetchFile } = FFmpegUtil;
|
| 28 |
+
const { FFmpeg } = FFmpegWASM;
|
| 29 |
+
let ffmpeg = null;
|
| 30 |
+
const trim = async ({ target: { files } }) => {
|
| 31 |
+
const message1 = document.getElementById('message');
|
| 32 |
+
if (ffmpeg === null) {
|
| 33 |
+
ffmpeg = new FFmpeg();
|
| 34 |
+
ffmpeg.on("log", ({ message }) => {
|
| 35 |
+
console.log(message);
|
| 36 |
+
message1.innerHTML = message1.innerHTML + "<br>" + message;
|
| 37 |
+
})
|
| 38 |
+
ffmpeg.on("progress", ({ progress }) => {
|
| 39 |
+
//message1.innerHTML = `load ffmpeg.wasm ${progress * 100} %`;
|
| 40 |
+
});
|
| 41 |
+
await ffmpeg.load({
|
| 42 |
+
coreURL: "/assets/core/package/dist/umd/ffmpeg-core.js",
|
| 43 |
+
});
|
| 44 |
+
}
|
| 45 |
+
const start = document.getElementById('startTime').value;
|
| 46 |
+
const end = document.getElementById('endTime').value;
|
| 47 |
+
const { name } = files[0];
|
| 48 |
+
await ffmpeg.writeFile(name, await fetchFile(files[0]));
|
| 49 |
+
message1.innerHTML = 'Start trimming';
|
| 50 |
+
await ffmpeg.exec(['-i', name, '-ss', start, '-to', end, 'output.mp4']);
|
| 51 |
+
message1.innerHTML = message1.innerHTML + "<br>" + 'Complete trimming';
|
| 52 |
+
const data = await ffmpeg.readFile('output.mp4');
|
| 53 |
+
|
| 54 |
+
const video = document.getElementById('output-video');
|
| 55 |
+
video.src = URL.createObjectURL(new Blob([data.buffer], { type: 'video/mp4' }));
|
| 56 |
+
|
| 57 |
+
const downloadButton = document.getElementById('downloadButton');
|
| 58 |
+
downloadButton.style.display = 'inline'; // 显示下载链接
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
}
|
| 61 |
+
document.getElementById('downloadButton').addEventListener('click', function() {
|
| 62 |
+
const video = document.getElementById('output-video');
|
| 63 |
+
const downloadLink = document.getElementById('downloadLink');
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
// 设置下载链接
|
| 66 |
+
downloadLink.href = video.src;
|
| 67 |
+
downloadLink.innerText = '下载视频';
|
| 68 |
+
downloadLink.download = 'trim_' + generateRandomFileName() + '.mp4';
|
| 69 |
+
downloadLink.style.display = 'inline'; // 显示下载链接
|
| 70 |
+
downloadLink.click();
|
| 71 |
+
});
|
| 72 |
+
const elm = document.getElementById('uploader');
|
| 73 |
+
elm.addEventListener('change', trim);
|
| 74 |
</script>
|
| 75 |
</body>
|
| 76 |
</html>
|