52 lines
1.9 KiB
HTML
52 lines
1.9 KiB
HTML
<!DOCTYPE html>
|
||
<html>
|
||
<head>
|
||
<meta charset="UTF-8" />
|
||
<title>H.265 硬解码 (WebCodecs)</title>
|
||
</head>
|
||
<body>
|
||
<script src="https://unpkg.com/@ffmpeg/ffmpeg@0.10.0/dist/ffmpeg.min.js"></script>
|
||
|
||
<canvas id="canvas" width="1280" height="720"></canvas>
|
||
<script>
|
||
const { createFFmpeg, fetchFile } = FFmpeg;
|
||
const ffmpeg = createFFmpeg({ log: true });
|
||
|
||
async function transcodeToH265(inputFile) {
|
||
// 确保FFmpeg加载
|
||
if (!ffmpeg.isLoaded()) {
|
||
await ffmpeg.load();
|
||
}
|
||
|
||
// 将输入文件写入FFmpeg的文件系统
|
||
ffmpeg.FS("writeFile", "input.mp4", await fetchFile(inputFile));
|
||
|
||
// 执行转码命令:将输入文件转码为H.265编码,输出为output.mp4
|
||
await ffmpeg.run("-i", "input.mp4", "-c:v", "libx265", "output.mp4");
|
||
|
||
// 从FFmpeg文件系统中读取输出文件
|
||
const data = ffmpeg.FS("readFile", "output.mp4");
|
||
|
||
// 创建Blob和URL以供下载
|
||
const outputUrl = URL.createObjectURL(new Blob([data.buffer], { type: "video/mp4" }));
|
||
return outputUrl;
|
||
}
|
||
|
||
// 使用示例:当用户选择文件后
|
||
document.getElementById("fileInput").addEventListener("change", async (e) => {
|
||
const file = e.target.files[0];
|
||
if (!file) return;
|
||
|
||
const outputUrl = await transcodeToH265(file);
|
||
// 创建一个下载链接
|
||
const a = document.createElement("a");
|
||
a.href = outputUrl;
|
||
a.download = "output_h265.mp4";
|
||
document.body.appendChild(a);
|
||
a.click();
|
||
document.body.removeChild(a);
|
||
});
|
||
</script>
|
||
</body>
|
||
</html>
|