如何限制视频分辨率

导言:

在许多场景下,我们需要确保用户上传的视频满足一定的分辨率要求,以保证在后续的处理中能够获得良好的视觉效果。例如,一些网站可能对用户上传的视频分辨率进行限制,以确保页面加载和播放的性能。


技术实现步骤:

1、创建视频元素和 Canvas:

ini 复制代码
const video = document.createElement('video');
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');

2、加载视频文件

ini 复制代码
video.preload = 'metadata';
video.src = URL.createObjectURL(videoFile);

3、获取视频元数据

ini 复制代码
video.onloadedmetadata = () => {
  const videoWidth = video.videoWidth;
  const videoHeight = video.videoHeight;

  // 进行分辨率校验...
};

4.分辨率校验逻辑:

ini 复制代码
// 校验最小分辨率
if (minResolution && (videoWidth < minResolution[0] || videoHeight < minResolution[1])) {
  reject(`分辨率太小,最小要求 ${minResolution[0]}x${minResolution[1]}`);
  return;
}

// 校验最大分辨率
if (maxResolution && (videoWidth > maxResolution[0] || videoHeight > maxResolution[1])) {
  reject(`分辨率太大,最大允许 ${maxResolution[0]}x${maxResolution[1]}`);
  return;
}

// 通过校验
resolve(true);

5、异常处理:

ini 复制代码
video.onerror = () => {
  reject('无法读取视频信息');
};

6、使用 Canvas 提取视频帧:

ini 复制代码
video.onloadeddata = () => {
  canvas.width = video.videoWidth;
  canvas.height = video.videoHeight;
  context.drawImage(video, 0, 0, canvas.width, canvas.height);
};

4. 示例用法:

typescript 复制代码
const videoFile: File = /* 从文件输入获取的 File 对象 */;
const minResolution: Resolution = [640, 480];
const maxResolution: Resolution = [1920, 1080];

validateResolutionInBrowser(videoFile, minResolution, maxResolution)
  .then((isValid: boolean) => {
    console.log(isValid ? '分辨率符合要求' : '分辨率不符合要求');
  })
  .catch((error: string) => {
    console.error(`校验失败: ${error}`);
  });

完整代码

ini 复制代码
type Resolution = [number, number];

function validateResolutionInBrowser(videoFile: File, minResolution?: Resolution, maxResolution?: Resolution): Promise<boolean> {
  return new Promise((resolve, reject) => {
    const video = document.createElement('video');
    const canvas = document.createElement('canvas');
    const context = canvas.getContext('2d');

    video.preload = 'metadata';
    video.src = URL.createObjectURL(videoFile);

    video.onloadedmetadata = () => {
      const videoWidth = video.videoWidth;
      const videoHeight = video.videoHeight;

      // 校验最小分辨率
      if (minResolution && (videoWidth < minResolution[0] || videoHeight < minResolution[1])) {
        reject(`分辨率太小,最小要求 ${minResolution[0]}x${minResolution[1]}`);
        return;
      }

      // 校验最大分辨率
      if (maxResolution && (videoWidth > maxResolution[0] || videoHeight > maxResolution[1])) {
        reject(`分辨率太大,最大允许 ${maxResolution[0]}x${maxResolution[1]}`);
        return;
      }

      // 通过校验
      resolve(true);
    };

    video.onerror = () => {
      reject('无法读取视频信息');
    };

    video.onloadeddata = () => {
      // 使用 Canvas 提取视频帧
      canvas.width = video.videoWidth;
      canvas.height = video.videoHeight;
      context.drawImage(video, 0, 0, canvas.width, canvas.height);
    };
  });
}

// 示例用法:
const videoFile: File = /* 从文件输入获取的 File 对象 */;
const minResolution: Resolution = [640, 480];
const maxResolution: Resolution = [1920, 1080];

validateResolutionInBrowser(videoFile, minResolution, maxResolution)
  .then((isValid: boolean) => {
    console.log(isValid ? '分辨率符合要求' : '分辨率不符合要求');
  })
  .catch((error: string) => {
    console.error(`校验失败: ${error}`);
  });

结论:

通过结合 TypeScript 和 HTML5 中的视频元素,我们可以更好地在 Web 应用中处理用户上传的视频文件,提高用户体验和整体性能。


参考文献:

相关推荐
江城开朗的豌豆6 分钟前
forEach遇上await:你的异步代码真的在按顺序执行吗?
前端·javascript·面试
万少15 分钟前
HarmonyOS Next 弹窗系列教程(3)
前端·harmonyos·客户端
七灵微1 小时前
【后端】单点登录
服务器·前端
持久的棒棒君5 小时前
npm安装electron下载太慢,导致报错
前端·electron·npm
crary,记忆7 小时前
Angular微前端架构:Module Federation + ngx-build-plus (Webpack)
前端·webpack·angular·angular.js
漂流瓶jz8 小时前
让数据"流动"起来!Node.js实现流式渲染/流式传输与背后的HTTP原理
前端·javascript·node.js
SamHou08 小时前
手把手 CSS 盒子模型——从零开始的奶奶级 Web 开发教程2
前端·css·web
我不吃饼干8 小时前
从 Vue3 源码中了解你所不知道的 never
前端·typescript
开航母的李大8 小时前
【中间件】Web服务、消息队列、缓存与微服务治理:Nginx、Kafka、Redis、Nacos 详解
前端·redis·nginx·缓存·微服务·kafka
Bruk.Liu8 小时前
《Minio 分片上传实现(基于Spring Boot)》
前端·spring boot·minio